Modal
Modal is a native <dialog>. Open it with commandfor and command="show-modal"; close it with command="close", Esc, or closedby="any". No JavaScript needed.
Class names
| Class name | Type | Description |
|---|---|---|
modal | Component | Apply to a <dialog>; animates in and out |
modal-title | Part | Heading inside the dialog |
modal-actions | Part | Right-aligned row of buttons at the bottom |
open | State | Set by the browser while the dialog is shown |
Modal
<button class="btn btn-primary" commandfor="modal-ex1" command="show-modal">Open modal</button>
<dialog class="modal" id="modal-ex1" aria-labelledby="modal-ex1-title" closedby="any">
<h2 class="modal-title" id="modal-ex1-title">Hello</h2>
<p>Press Esc or click outside to close.</p>
<div class="modal-actions">
<button class="btn" commandfor="modal-ex1" command="close">Close</button>
</div>
</dialog>Confirm action
<button class="btn btn-danger" commandfor="modal-ex2" command="show-modal">Delete project</button>
<dialog class="modal" id="modal-ex2" aria-labelledby="modal-ex2-title" aria-describedby="modal-ex2-desc">
<h2 class="modal-title" id="modal-ex2-title">Delete project?</h2>
<p id="modal-ex2-desc">This cannot be undone.</p>
<div class="modal-actions">
<button class="btn btn-ghost" commandfor="modal-ex2" command="close">Cancel</button>
<button class="btn btn-danger" commandfor="modal-ex2" command="close">Delete</button>
</div>
</dialog>Form in a modal
method="dialog" closes the dialog when the form is submitted.
<button class="btn btn-primary" commandfor="modal-ex3" command="show-modal">Invite teammate</button>
<dialog class="modal" id="modal-ex3" aria-labelledby="modal-ex3-title" closedby="any">
<form method="dialog">
<h2 class="modal-title" id="modal-ex3-title">Invite teammate</h2>
<div class="field">
<label class="field-label" for="modal-ex3-email">Email</label>
<input class="input" id="modal-ex3-email" type="email" required>
</div>
<div class="modal-actions">
<button class="btn btn-ghost" type="button" commandfor="modal-ex3" command="close">Cancel</button>
<button class="btn btn-primary" type="submit">Send invite</button>
</div>
</form>
</dialog>