-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Docs: HTML Dialog Example #3604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
klnusbaum
wants to merge
3
commits into
bigskysoftware:master
Choose a base branch
from
klnusbaum:html-modal
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+92
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| +++ | ||
| title = "Modal Dialogs with HTML Dialogs" | ||
| template = "demo.html" | ||
| +++ | ||
|
|
||
| Since 2022 the HTML spec has included a `<dialog>` tag and it works well with | ||
| htmx. Consider the following html: | ||
|
|
||
| ```html | ||
| <button | ||
| class="btn primary" | ||
| hx-get="/modal" | ||
| hx-target="#modal-placeholder" | ||
| hx-swap="outerHTML" | ||
| >Open Modal</button> | ||
|
|
||
| <div id="modal-placeholder"></div> | ||
| ``` | ||
|
|
||
| This button sends a `GET` request to `/modal` when the button is clicked. The | ||
| server then responds with with a dialog that get swapped into an empty `<div>`. | ||
| The `<dialog>` looks like this: | ||
|
|
||
| ```html | ||
| <dialog id="modal-dialog" closedby="none" hx-on::after-settle="this.showModal()"> | ||
| <h1>Modal Dialog</h1> | ||
| This is the modal content. You can put anything here, like text, | ||
| or a form, or an image. | ||
| <br/> | ||
| <br/> | ||
| <button class="btn danger" hx-get="/close" hx-target="#modal-dialog" hx-swap="outerHTML">Close</button> | ||
| </dialog>` | ||
| ``` | ||
|
|
||
| The close button fetches an empty `div` via the `/close` route to replace the dialog when it's closed: | ||
|
|
||
| ```html | ||
| <div id="modal-placeholder"></div> | ||
| ``` | ||
|
|
||
| This essentially "resets" the page, and clicking the button again will open a new dialog. | ||
|
|
||
| Note that in the dialog: | ||
| 1. We use `hx-on::after-settle`. This is needed because in order for the dialog to actually be shown, | ||
| the `showModal` function must be called. | ||
| 2. The `closedby` attribute is set to `none`. If we didn't have this, the user might close the dialog, | ||
| via some mechanism that doesn't trigger the fetch of the `/close` endpoint. | ||
|
|
||
| {{ demoenv() }} | ||
|
|
||
| <div id="modal-placeholder"></div> | ||
|
|
||
| <style> | ||
| dialog { | ||
| margin: auto; | ||
| padding: 1em; | ||
| border: 0; | ||
| } | ||
| </style> | ||
|
|
||
| <script> | ||
|
|
||
| //========================================================================= | ||
| // Fake Server Side Code | ||
| //========================================================================= | ||
|
|
||
| // routes | ||
| init("/demo", function(request, params) { | ||
| return `<button | ||
| class="btn primary" | ||
| hx-get="/modal" | ||
| hx-target="#modal-placeholder" | ||
| hx-swap="outerHTML" | ||
| >Open Modal</button> | ||
| `}) | ||
|
|
||
| onGet("/modal", function(request, params){ | ||
| return `<dialog id="modal-dialog" closedby="none" hx-on::after-settle="this.showModal()"> | ||
| <h1>Modal Dialog</h1> | ||
| This is the modal content. You can put anything here, like text, | ||
| or a form, or an image. | ||
| <br/> | ||
| <br/> | ||
| <button class="btn danger" hx-get="/close" hx-target="#modal-dialog" hx-swap="outerHTML">Close</button> | ||
| </dialog>` | ||
| }) | ||
|
|
||
| onGet("/close", function(request, params){ | ||
| return `<div id="modal-placeholder"></div>` | ||
| }) | ||
| </script> | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My intuition tells me there should be an easier way to do this, without needing this place holder div. But without, there's nothing to do an "outerHTML" swap with. And without that "outerHTML" swap, the
hx-on::after-settledoesn't get called on the dialog. For example, if we have the button target the<body>and do ahx-swap="beforeend", then its the body that gets thehx-on::after-settleevent.I think for Locality-of-behavior reasons, it's best to have the call to
showModalon the dialog itself. So that's why I went with this. Happy to entertain other ideas.