Dialog
A modal on the native dialog element, with an alert variant.
Demo
Usage
import { Button, Dialog } from '@tuttn/slant'
// It keeps its own open state: the trigger opens it, and Escape, the backdrop
// or a Dialog.Close closes it.
<Dialog trigger={<Button>Rename…</Button>}>
<Dialog.Title>Rename branch</Dialog.Title>
<Dialog.Body><form id="rename" onSubmit={save}>…</form></Dialog.Body>
<Dialog.Footer>
<Dialog.Close>Cancel</Dialog.Close>
<Button type="submit" form="rename" variant="primary">Rename</Button>
</Dialog.Footer>
</Dialog>
// An alert: the backdrop doesn't dismiss it, the Body is its description,
// and the answers sit side by side. Focus starts on the safe one.
const cancel = useRef<HTMLButtonElement>(null)
<Dialog role="alertdialog" initialFocus={cancel} trigger={<Button tone="danger">Delete branch…</Button>}>
<Dialog.Title>Delete branch?</Dialog.Title>
<Dialog.Body>feature/overlays and its 12 commits will be gone.</Dialog.Body>
<Dialog.Footer>
<Dialog.Close ref={cancel}>Cancel</Dialog.Close>
<Dialog.Close tone="danger" variant="primary" onClick={remove}>Delete</Dialog.Close>
</Dialog.Footer>
</Dialog>
// An answer that waits for an acknowledgement: Dialog.Confirm stays unavailable
// until the reader ticks the box the Footer shows, and the box is unticked
// again each time the dialog opens.
<Dialog role="alertdialog" acknowledge="I understand this deletes 14 files for good"
trigger={<Button tone="danger">Delete folder…</Button>}>
<Dialog.Title>Delete “Drafts”?</Dialog.Title>
<Dialog.Body>The folder and everything in it are removed. This can’t be undone.</Dialog.Body>
<Dialog.Footer>
<Dialog.Close>Cancel</Dialog.Close>
<Dialog.Confirm tone="danger" onClick={remove}>Delete</Dialog.Confirm>
</Dialog.Footer>
</Dialog>
// The tick controlled: it keeps whatever you pass, across openings.
<Dialog acknowledge="I have a backup" acknowledged={backedUp} onAcknowledgedChange={setBackedUp}>…</Dialog>
// Controlled, with no trigger: open it from anywhere, name it with label.
<Dialog open={open} onOpenChange={setOpen} label="Edit details">…</Dialog>Props
| Prop | Type | Default | Description |
|---|---|---|---|
open | boolean | — | Whether it is open. Pass it with onOpenChange to control it; leave it out and it keeps its own state. |
defaultOpen | boolean | false | Whether it starts open, when uncontrolled. |
onOpenChange | (open: boolean) => void | — | Called with the new state whenever it opens or closes: the trigger, Escape, the backdrop, a Close part. |
role | 'dialog' | 'alertdialog' | 'dialog' | `alertdialog` for a question that must be answered: a press on the backdrop leaves it open, its Body becomes its description, and the Footer's answers sit side by side in equal columns. |
label | string | — | The accessible name, when no Title names it. |
initialFocus | RefObject<HTMLElement | null> | — | The element to focus on opening. By default the first focusable element inside, else the panel itself. |
trigger | ReactElement | — | An element that opens it when clicked (a Button, say), rendered just before it with `aria-haspopup="dialog"`. Its own handlers run first. Without it, open it through `open`. |
children | ReactNode | — | The content: Title, Body and Footer parts, or anything else. |
acknowledge | ReactNode | — | What the reader must tick before `Dialog.Confirm` answers, such as “I understand this deletes 14 files for good”. The Footer shows it as a checkbox in its own row above the answers, and Confirm stays unavailable until it is ticked. Meant for `role="alertdialog"`; it works on a plain dialog too. |
acknowledged | boolean | — | Whether the acknowledgement is ticked. Pass it with onAcknowledgedChange to control it; leave it out and it keeps its own state, which goes back to `defaultAcknowledged` each time the dialog opens. |
defaultAcknowledged | boolean | false | Whether the acknowledgement is ticked on opening, when uncontrolled. |
onAcknowledgedChange | (acknowledged: boolean) => void | — | Called with the new state when the acknowledgement is ticked or unticked, and when opening resets it. |
Dialog.Title | HTMLAttributes<HTMLHeadingElement> | — | An h2 that names the dialog (aria-labelledby), unless label is given. |
Dialog.Body | HTMLAttributes<HTMLDivElement> | — | The content. It scrolls when the dialog runs out of height, and it describes an alertdialog (aria-describedby). |
Dialog.Footer | HTMLAttributes<HTMLDivElement> | — | The actions, at the inline end. In an alertdialog, one row of equal columns across the panel. With acknowledge, the checkbox takes a row of its own (.sl-dialog-acknowledge) above them, and the actions move into .sl-dialog-answers, which keeps this layout. |
Dialog.Close | ButtonProps | — | A Slant Button that closes the dialog. Its onClick runs first and can keep it open with preventDefault. |
Dialog.Confirm | ButtonProps | — | The answer that goes ahead: a Slant Button, primary unless you set variant, that runs its onClick and then closes the dialog unless that called preventDefault. While an acknowledge is unticked it is aria-disabled and described by the acknowledgement, and a press does nothing (not even submit a form). Drawer has it too, as Drawer.Confirm. |
…rest | HTMLAttributes<HTMLDialogElement> | — | className, style, other attributes and the ref go on the <dialog>. Each part forwards its ref and attributes to its own element. |
Keyboard
| Tab / Shift+Tab | moves through the dialog’s controls and wraps round; focus never reaches the page behind |
| Escape | closes it, unless a menu or popover open inside takes it first |
| Space | on the acknowledgement, ticks or unticks it |
| Enter / Space | on Dialog.Close or Dialog.Confirm, answers and closes it; on a Confirm that waits for its acknowledgement, does nothing |
Accessibility
- It is a native
<dialog>opened withshowModal(), so the browser treats it as modal: the rest of the page is inert and out of the accessibility tree while it is open. - The Title names it (
aria-labelledby), orlabeldoes. An alertdialog is also described by its Body (aria-describedby). - Opening moves focus to
initialFocus, else the first focusable element, else the panel; closing puts it back where it was, usually on the trigger, which saysaria-haspopup="dialog". - The acknowledgement is a native checkbox named by
acknowledge. By default it takes focus on opening, as the first control, so the reader meets the warning before the answer; don’t pointinitialFocusat Confirm. - While it is unticked, Confirm is
aria-disabledrather thandisabled, so it stays in the tab order and is read as unavailable, and the acknowledgement’s words describe it (aria-describedby), so a screen reader says why. Ticked, the description goes.
It is the native <dialog> element opened with showModal(): the browser lifts it into the top layer, above every z-index, overflow box and transform, centres it, and makes the rest of the page inert. The element is always rendered, so content inside keeps its state between openings. Opening it moves focus to initialFocus, else the first focusable element inside, else the panel; Tab and Shift+Tab cycle inside it; closing it returns focus to the element that had it, usually the trigger. The page behind doesn’t scroll while it is open.
Escape closes it, as does a press on the backdrop that starts and ends there, and any Dialog.Close. A menu, popover or tooltip open inside answers Escape first, and renders inside the dialog, so it shows above the backdrop and takes clicks. Controlled, it stays as open says: Escape only reports onOpenChange(false). Without open it keeps its own state, starting from defaultOpen.
role="alertdialog" is for a question that must be answered. The backdrop leaves it open, its Body is read as its description, and the footer lays the answers out side by side in equal columns, like a system alert. Give the safe answer initialFocus.
Confirming with an acknowledgement
For an answer that can’t be taken back (deleting a project, revoking keys, an irreversible migration), pass acknowledge, the statement the reader must agree to, and answer with Dialog.Confirm. The Footer shows the statement as a checkbox in its own row above the answers, and Confirm waits until it is ticked: it looks dimmed and a press does nothing, but it keeps its place in the tab order. Ticked, Confirm runs its onClick and closes the dialog, as Dialog.Close does; preventDefault keeps it open. Without acknowledge, Confirm is simply a primary Close.
Uncontrolled, the box goes back to defaultAcknowledged (unticked) each time the dialog opens, so every opening asks again, and the reset is reported through onAcknowledgedChange. Controlled with acknowledged, it keeps what you pass. It is meant for role="alertdialog", where the backdrop can’t dismiss it, but works on a plain dialog and on a Drawer, which has Drawer.Confirm. Leave initialFocus alone: focus then opens on the checkbox, so Space ticks it and Tab moves on to the answers.