Dialog
Dialog draws a panel and a scrim. The behaviour comes from @hozo/patterns.
Open it, then press Escape or one of its buttons:
React Native: no class in this look is Web-only.
Screenshots on Android and iOS

cded685)
cded685)import { Heading, Paragraph } from '@hozo/core'import { Button, Dialog, Stack } from '@hozo/ui'import { useState } from 'react'export function DialogDemo() { const [open, setOpen] = useState(false) return ( <> <Button tone="danger" onPress={() => setOpen(true)}> Delete project </Button> <Dialog open={open} onClose={() => setOpen(false)} accessibilityLabel="Delete the project"> <Stack gap="normal"> <Heading level={3} className="text-lg font-bold text-hozo-text"> Delete the project? </Heading> <Paragraph className="text-sm text-hozo-text-body"> Every build and report goes with it. This cannot be undone. </Paragraph> <Stack direction="row" gap="tight"> <Button tone="danger" onPress={() => setOpen(false)}> Delete </Button> <Button tone="quiet" onPress={() => setOpen(false)}> Keep it </Button> </Stack> </Stack> </Dialog> </> )}On the Web it is a real <dialog> opened with showModal(), so the browser
supplies what a modal needs:
- the rest of the page becomes inert
- focus stays inside
- Escape closes it, through the
cancelevent - focus goes back to the button that opened it
The scrim is the dialog’s ::backdrop, so there is no overlay element and no
z-index to manage.
opendrives it, andonCloseis called when the browser asks to close it (Escape). Keepopenin your state and set it tofalsethere.accessibilityLabelis the dialog’s accessible name. A screen reader announces it when the dialog opens.accessibilityLabelledBynames it from an element inside, usually its heading, so the name is not written twice. Give the heading thatnativeID. On React Native only Android reads it, so keepaccessibilityLabeltoo if the dialog is used on iOS.closeOnBackdropPresscloses it when the scrim is pressed. It is off by default, as the browser’s<dialog>is: a press beside a form should not throw the form away unless you decide it should.- It animates out on the Web. When
openturns false the dialog getsdata-state="closed"and stays shown until the transition or animation that starts has ended, then closes and gives focus back. Style the exit withdata-[state=closed]:classes. With none, it closes at once. On React Native theModalfades out. - There is no close button. A dismiss control needs a name and a place in the reading order, so it is yours to put in the children, as the example does. Escape works without one.
- On React Native it is a
Modal. PassrestoreFocusTowith a ref to the opener so TalkBack and VoiceOver land back on it, since React Native cannot report what had focus.
Testing under jsdom
Section titled “Testing under jsdom”jsdom does not implement HTMLDialogElement.showModal(), so a unit test that
opens a Dialog throws. Stub it, and close() with it, before the test
renders one:
HTMLDialogElement.prototype.showModal ??= function () { this.setAttribute('open', '')}HTMLDialogElement.prototype.close ??= function () { this.removeAttribute('open')}The stub does not make the page inert, move focus or fire transitionend, so
what a test can assert is the markup: open, data-state and the names.