Modal

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 nameTypeDescription
modalComponentApply to a <dialog>; animates in and out
modal-titlePartHeading inside the dialog
modal-actionsPartRight-aligned row of buttons at the bottom
openStateSet by the browser while the dialog is shown

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>