Modal
A dialog that traps focus and gives it back.
Examples
Playground
Size
The four widths, each for a kind of content. Open them one after another to compare.
Focus the action
`initialFocus` points focus at the one obvious action, so a confirmation needs no tabbing.
No backdrop dismiss
`dismissOnBackdrop={false}` for anything destructive or holding unsaved input.
Install
npx shadcn add @hipuku/modalUsage
import { Modal } from '@/components/haus/components/Modal'
<Modal
open={open}
onClose={() => setOpen(false)}
title="Invite people"
initialFocus={emailRef}
footer={<Button variant="primary" onClick={invite}>Send invites</Button>}
>
<Input ref={emailRef} label="Email" />
</Modal>API
Modal also takes its element's own attributes and forwards its ref. className lands on the
backdrop and dialogClassName on the dialog.
| Prop | Type | Description |
|---|---|---|
openrequired | boolean | |
onCloserequired | () => void | |
size | "sm" | "md" | "lg" | "xl" | |
footer | React.ReactNode | |
initialFocus | React.RefObject<HTMLElement | null> | Where focus goes when the dialog opens. Defaults to the dialog itself, which is safe and says nothing: a screen reader announces the dialog and the user tabs from the top. Point it at the primary action when there is an obvious one, or at the first field on a form. |
dismissOnBackdrop | boolean | Whether clicking the backdrop closes the dialog. On by default, and worth turning off for anything destructive or anything holding unsaved input: a misplaced click should not discard work, and there was no way to say so. |
dialogClassName | string | Lands on the dialog rather than the backdrop, where className used to.
Ruling B5 put className on the root, here the backdrop, and this is the
named second target it asks for. |
placement | "center" | "start" | "end" | Where the dialog sits. center is a modal; start and end anchor it
to that edge, full height, which is what Drawer is. Logical, so end
is the right edge in a left-to-right page and the left in a right-to-left
one. |
title | string | |
aria-label | string |
Size and placement
size sets the dialog's width. placement is center by default; start and end anchor it to
an edge at full height, which is what Drawer does for you.
Dismissing
Escape, the close button and a click on the backdrop all call onClose. Turn
dismissOnBackdrop off for anything destructive or anything holding unsaved input, so a stray
click does not lose work.
A Select or Popover inside a Modal is placed inside the dialog automatically, so it stays inside the focus trap and is not clipped by the dialog's scrolling body.
Accessibility
- A name is required:
title, oraria-labelwhen there is no visible title. - Focus moves into the dialog on open, to
initialFocusif you set it, and returns to what had it on close. - Tab stays inside the dialog, and the page behind does not scroll.