From 620fd458503d5ef89e54a4c71926ff7bbbb24d43 Mon Sep 17 00:00:00 2001 From: Ben Pate Date: Mon, 26 Oct 2020 13:06:17 -0600 Subject: [PATCH] Website: demo modal dialogs using UIKit This is my first try at doing this, so feedback is welcome. I can't get the SASS plugin working with eleventy, so I'm guessing about the global page CSS. It looks like the UIKit stylesheet may mess up the global look of the page. If this is a dealbreaker (or can't be fixed) then we may not be able to use this commit :( --- www/examples.md | 3 +- www/examples/modal-uikit.md | 136 ++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 www/examples/modal-uikit.md diff --git a/www/examples.md b/www/examples.md index 640993d4..350ef2c6 100644 --- a/www/examples.md +++ b/www/examples.md @@ -21,5 +21,6 @@ You can copy and paste them and then adjust them for your needs. | [Active Search](/examples/active-search) | Demonstrates the active search box pattern | [Progress Bar](/examples/progress-bar) | Demonstrates a job-runner like progress bar | [Value Select](/examples/value-select) | Demonstrates making the values of a select dependent on another select -| [Dialogs](/examples/dialogs) | Demonstrates the prompt and confirm dialogs | [Animations](/examples/animations) | Demonstrates various animation techniques +| [Dialogs - Browser](/examples/dialogs) | Demonstrates the prompt and confirm dialogs +| [Dialogs - UIKIt](/examples/modal-uikit) | Demonstrates modal dialogs using UIKit diff --git a/www/examples/modal-uikit.md b/www/examples/modal-uikit.md new file mode 100644 index 00000000..825046c4 --- /dev/null +++ b/www/examples/modal-uikit.md @@ -0,0 +1,136 @@ +--- +layout: demo_layout.njk +--- + +## Modal Dialogs + +Many CSS toolkits include styles (and Javascript) for creating modal dialog boxes. +This example shows how to use HTMX to display dynamic dialog using UIKit, and how to +trigger its animation styles with little or no Javascript. + +We start with a button that triggers the dialog, along with a DIV at the bottom of your +markup where the dialog will be loaded: + +This is an example of using HTMX to remotely load modal dialogs using UIKit. + +```html + + +
+``` + +This button uses a `GET` request to `/uikit-modal.html` when this button is clicked. The +contents of this file will be added to the DOM underneath the `#show-modals-here` DIV. + +We're replacing the standard UIKit Javascript library with a little bit of Hyperscript, +which triggers UIKit's smooth animations. It is delayed by 10ms so that UIKit's animations +will run correctly. + +Finally, the server responds with a slightly modified version of UIKit's standard modal + +```html + +``` + +Hyperscript on the button and the form trigger animations when this dialog is completed +or canceled. If you don't want to use Hyperscript, the modals will still work but UIKit's +fade in animations will not be triggered. + +Alternatively, you can add and remove these CSS classes without Hyperscript, by using in straight Javascript. +It's just a lot more code: + +```javascript + +// This triggers the fade-in animation when a modal dialog is loaded and displayed +window.document.getElementById("showButton").addEventListener("htmx:afterOnLoad", function() { + setTimeout(function(){ + window.document.getElementById("modal").classList.add("uk-open") + }, 10) +}) + + +// This triggers the fade-out animation when the modal is closed. +window.document.getElementById("cancelButton").addEventListener() +on click take .uk-open from #modal wait 200ms then remove #modal +``` + +The input issues a `POST` to `/search` on the `keyup` event and sets the body of the table to be the resulting content. + +We add the `delay:500ms` modifier to the trigger to delay sending the query until the user stops typing. Additionally, we add the `changed` modifier to the trigger to ensure we don't send new queries when the user doesn't change the value of the input (e.g. they hit an arrow key). + +Finally, we show an indicator when the search is in flight with the `hx-indicator` attribute. + +
+ +{% include demo_ui.html.liquid %} + + + + +