Modal
A dialog on the native dialog element. Backdrop click and Esc close it.
Modal
Open with data-modal-open="#id". Close with data-modal-close, a form with method="dialog", Esc, or a click on the backdrop.
<button class="btn btn-primary" data-modal-open="#demo-modal">Open modal</button>
<dialog class="modal" id="demo-modal" aria-labelledby="demo-modal-title">
<form method="dialog">
<div class="modal-head">
<h3 class="modal-title" id="demo-modal-title">New project</h3>
<button class="btn btn-ghost btn-icon btn-sm" aria-label="Close" data-modal-close><svg class="i"><use href="#i-x"/></svg></button>
</div>
<div class="modal-body">
<div class="field"><label class="label" for="mp-name">Name</label><input class="input" id="mp-name" placeholder="my-app"></div>
<div class="field"><label class="label" for="mp-desc">Description</label><textarea class="input textarea" id="mp-desc" placeholder="What is it for?"></textarea></div>
</div>
<div class="modal-foot">
<button class="btn btn-ghost" value="cancel">Cancel</button>
<button class="btn btn-primary" value="ok" data-toast='{"type":"ok","title":"Project created"}'>Create</button>
</div>
</form>
</dialog>Load a modal with htmx
The dialog opens by itself when htmx swaps content into it. The server can close it with HX-Trigger: closeModal.
<button class="btn" hx-get="/users/new" hx-target="#modal-body">New user</button>
<dialog class="modal" id="modal">
<div id="modal-body"></div>
</dialog>
<!-- server returns .modal-head / .modal-body / .modal-foot markup;
the form can hx-post and the response can send HX-Trigger: closeModal -->Classes
| Selector | Purpose |
|---|---|
dialog.modal | The dialog |
.modal-head / .modal-body / .modal-foot | Sections |
.modal-title | Heading |
[data-modal-open="#id"] | Opens a dialog |
[data-modal-close] | Closes the enclosing dialog |
Coverup.modal(sel).open() / .close() | From JavaScript |