Skip to content

Dialog

Dialog draws a panel and a scrim. The behaviour comes from @hozo/patterns. Open it, then press Escape or one of its buttons:

Delete the project?

Every build and report goes with it. This cannot be undone.

React Native: no class in this look is Web-only.

Screenshots on Android and iOS
DialogDemo.tsx
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 cancel event
  • 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.

  • open drives it, and onClose is called when the browser asks to close it (Escape). Keep open in your state and set it to false there.
  • accessibilityLabel is the dialog’s accessible name. A screen reader announces it when the dialog opens.
  • accessibilityLabelledBy names it from an element inside, usually its heading, so the name is not written twice. Give the heading that nativeID. On React Native only Android reads it, so keep accessibilityLabel too if the dialog is used on iOS.
  • closeOnBackdropPress closes 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 open turns false the dialog gets data-state="closed" and stays shown until the transition or animation that starts has ended, then closes and gives focus back. Style the exit with data-[state=closed]: classes. With none, it closes at once. On React Native the Modal fades 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. Pass restoreFocusTo with a ref to the opener so TalkBack and VoiceOver land back on it, since React Native cannot report what had focus.

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:

ts
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.