Component · top layer

Modal

A centred dialog for a decision or a short task. The page behind it neither scrolls nor takes focus.

Delete project?

This removes the project and its history. You can not undo it.

Delete project?

Deleting takes two seconds. Until it is done, nothing closes this, and Cancel is off.

Terms

Term 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 13. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 14. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 15. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 16. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 17. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 18. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 19. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Term 20. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Customised

Narrower, square and with a blue backdrop, from three token overrides.

Astro

---
import Modal from '@subfloor/astro/Modal.astro'
---
<button command="show-modal" commandfor="delete-project">Delete project</button>

<Modal id="delete-project" title="Delete project?">
  <p>This removes the project and its history.</p>
  <form method="dialog" slot="footer">
    <button value="cancel">Cancel</button>
    <button value="delete">Delete</button>
  </form>
</Modal>

The trigger is a plain button with command and commandfor; it needs no script in browsers with Invoker Commands, and the core runs it in the others. Any <form method="dialog"> inside closes the dialog and sets its returnValue.

While it works

A confirm step whose action takes a moment must not close halfway. With dismissible={false} (data-dismiss="off"), Escape, the backdrop, the close button, command="close" buttons and swiping a sheet do nothing, and nothing is reported. Close it yourself when the work is done. closeButton={false} leaves out the close button when the footer has its own Cancel.

<Modal id="delete" title="Delete project?" closeButton={false}>
  <p>This removes the project and its history.</p>
  <Fragment slot="footer">
    <button id="delete-confirm" class="danger" data-autofocus>Delete</button>
    <button command="close" commandfor="delete">Cancel</button>
  </Fragment>
</Modal>

<script>
  import { getDialog } from '@subfloor/core'
  const dialog = document.getElementById('delete')
  document.getElementById('delete-confirm').addEventListener('click', async () => {
    dialog.dataset.dismiss = 'off' // nothing the user does closes it now
    await deleteProject()
    delete dialog.dataset.dismiss
    getDialog(dialog)?.close()
  })
</script>

React, with open kept by the parent:

const [busy, setBusy] = useState(false)

<Modal
  title="Delete project?"
  open={open}
  onOpenChange={setOpen}
  dismissible={!busy}
  closeButton={false}
  footer={
    <>
      {/* Not autoFocus: on content added as it opens, React focuses too early. */}
      <button data-autofocus aria-busy={busy} onClick={confirm}>Delete</button>
      <button disabled={busy} onClick={() => setOpen(false)}>Cancel</button>
    </>
  }
>
  …
</Modal>

onOpenChange calls: none

Delete project?

This removes the project and its history.

React autoFocus

React autoFocus, added on open

After closing

Focus goes back to the button that opened the dialog. When the page wants it elsewhere, for example because that button is gone, it says so once the dialog has closed: the page cannot take focus while the dialog is still open.

Orders

Cancel the order?

The customer is told by email.

Any dialog sends sf:closed with the reason and the opener. Cancel it to keep focus from going back, and focus something that can take it (a heading needs tabindex="-1"). Like sf:open-change, it does not bubble: listen on the dialog.

<script>
  const dialog = document.getElementById('cancel-order')
  dialog.addEventListener('sf:closed', (event) => {
    // { reason, opener }: once the page can take focus again, before focus goes back.
    if (dialog.returnValue !== 'cancel') return // the default: back to the opener
    event.detail.opener?.closest('li')?.remove()
    event.preventDefault()
    document.getElementById('orders-title').focus()
  })
</script>

In React, returnFocus takes an element, or a function called once the dialog has closed. If it cannot take focus, focus goes back to the opener; it is scrolled into view if need be:

<Modal
  title="Cancel the order?"
  open={open}
  onOpenChange={setOpen}
  // Called once it has closed; null keeps the default, the button that opened it.
  returnFocus={() => (cancelled.current ? heading.current : null)}
  …
/>

React orders

  • Order 101
  • Order 102
  • Order 103

Cancel the order?

The customer is told by email.

Actions in the header

Every dialog's header can hold actions between the title and the close button, such as previous and next for the item it shows. Give an icon button the class sf-dialog__action for the close button's size and look, and an aria-label. Text such as "2 of 5" can go there too, or in the title, which then names the dialog with it. The title takes the room left and wraps. On a sheet, a touch that starts on the actions never swipes it: swipe by the title. On opening, the browser focuses the first control, so the first action if there is one; set data-autofocus to choose another. See it on the order panel beside a list.

<!-- Astro: the header-actions slot (Modal, Sidepanel, Mobile nav) -->
<Sidepanel id="order" title="Order" placement="bottom">
  <Fragment slot="header-actions">
    <span>2 of 5</span>
    <button type="button" class="sf-dialog__action" aria-label="Previous order">‹</button>
    <button type="button" class="sf-dialog__action" aria-label="Next order">›</button>
  </Fragment>
  …
</Sidepanel>

// React: headerActions
<Sidepanel title="Order" headerActions={<>…</>}>…</Sidepanel>

<!-- Plain HTML: between the title and the close button's form -->
<header class="sf-dialog__header">
  <h2 id="order-title" class="sf-dialog__title">Order</h2>
  <div class="sf-dialog__actions">…</div>
  <form method="dialog" class="sf-dialog__close-form">…</form>
</header>

Markup

What the adapters render, for any other framework or plain HTML.

<button command="show-modal" commandfor="delete-project">Delete project</button>

<dialog id="delete-project" class="sf-dialog" data-sf-dialog data-placement="center"
        aria-labelledby="delete-project-title" closedby="any">
  <header class="sf-dialog__header">
    <h2 id="delete-project-title" class="sf-dialog__title">Delete project?</h2>
    <form method="dialog" class="sf-dialog__close-form">
      <button class="sf-dialog__close" value="close" aria-label="Close">…</button>
    </form>
  </header>
  <div class="sf-dialog__body">…</div>
  <footer class="sf-dialog__footer">…</footer>
</dialog>

<script type="module">
  import { initDialogs } from '@subfloor/core'
  initDialogs()
</script>

Behaviour

Events

const dialog = document.getElementById('delete-project')
dialog.addEventListener('sf:open-change', (event) => {
  // { open: false, reason: 'escape' | 'backdrop' | 'button' | 'api' }
  console.log(event.detail)
  if (!event.detail.open) console.log(dialog.returnValue) // 'delete' or 'cancel'
})

Customisation

/* Your stylesheet, unlayered or in a layer after subfloor */
.compact-modal {
  --sf-dialog-width: 20rem;
  --sf-radius-overlay: 0;
  --sf-color-backdrop: rgb(37 99 235 / 35%);
}

/* A full-screen modal: it only fades; a slide would show the page at its edge. */
.full-screen-modal {
  --sf-dialog-enter-from: none;
}

Variants are data attributes (data-placement), state is data-state, and the parts have sf-dialog__* classes you can style directly.

TokenDefault
--sf-color-backdroprgb(9 9 11 / 50%)
--sf-radius-overlay12px
--sf-shadow-overlay0 20px 50px rgb(0 0 0 / 20%)
--sf-title-size1.125rem
--sf-title-weight600
--sf-title-line-height1.4
--sf-overlay-guttervar(--sf-space-4)
--sf-dialog-width32rem
--sf-button-spin-duration800ms
--sf-duration-enter200ms
--sf-duration-exit150ms
--sf-ease-entercubic-bezier(0.2, 0.8, 0.2, 1)
--sf-ease-exitcubic-bezier(0.4, 0, 1, 1)
--sf-dialog-enter-fromtranslate(0, 0.5rem)

Background

Content 1. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 2. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 3. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 4. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 5. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 6. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 7. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 8. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 9. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 10. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 11. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.

Content 12. Background text that only exists so there is something to scroll. When an overlay is open, this must stay exactly where it is, whatever the wheel, the keyboard or a finger does.