Dialog
A modal window over the page, with a title, description and close button.
Installation
npx shadcn@latest add @oakoss/dialogThis also installs Button, which the close buttons use.
Usage
import {
Dialog,
DialogClose,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from '#/components/ui/overlays/dialog';
<DialogTrigger>
<Button>Open</Button>
<Dialog>
<DialogHeader>
<DialogTitle>Title</DialogTitle>
<DialogDescription>Description</DialogDescription>
</DialogHeader>
<DialogFooter>
<DialogClose>Cancel</DialogClose>
</DialogFooter>
</Dialog>
</DialogTrigger>;Examples
Sizes
size (sm, md, lg) caps the width from the sm breakpoint up, and on
narrower screens the dialog sits on the bottom edge and slides up. full
fills the screen at every width:
Scrolling content
DialogBody scrolls between the header and footer, so the title and actions
stay in view. When the visible viewport is too short to leave it room (below
31.25rem, including while a phone's on-screen keyboard is open), the whole
dialog scrolls instead:
Controlled
isOpen and onOpenChange open the dialog from state instead of a trigger.
showCloseButton on DialogFooter adds a Close button to the footer:
Required choice
isDismissable={false} keeps a click outside from closing the dialog, and
showCloseButton={false} drops the close button, so the footer's buttons are
the only way out. Escape still closes it unless isKeyboardDismissDisabled is
set:
Accessibility
React Aria renders the dialog in a portal and handles:
- Name:
DialogTitlenames the dialog (aria-labelledby). A dialog without one needs anaria-label. - Description: with
role="alertdialog",DialogDescriptiondescribes the dialog (aria-describedby). A plain dialog leaves it out, so screen readers don't read a long dialog in full when it opens. - Focus: focus moves into the dialog on open, stays inside it, and returns to the trigger on close. The page behind it is hidden from screen readers.
DialogClose and the close button close the dialog through React Aria's
close slot. The close button's name comes from closeLabel, and the dialog
keeps a visible edge in Windows High Contrast. DialogBody is focusable, so
keyboard users can scroll it, and the dialog's height follows React Aria's
visible-viewport height, which leaves out a phone's on-screen keyboard.
| Key | Action |
|---|---|
Escape | Closes the dialog, unless isKeyboardDismissDisabled is set |
Tab | Moves to the next focusable element, wrapping inside the dialog |
Shift + Tab | Moves to the previous one, wrapping inside the dialog |
Follows the WAI-ARIA dialog pattern.
API
DialogTrigger
React Aria's DialogTrigger: opens
the Dialog from its first child, a pressable element. It renders no element
of its own.
Dialog
Renders React Aria's ModalOverlay,
Modal and Dialog. className applies to the panel.
Props
Prop
Type
Also accepts all React Aria ModalOverlay props.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always: dialog-overlay, dialog-content (the panel) and dialog; a data-slot prop replaces dialog in each |
data-size | Always, on the overlay and panel; the size |
data-entering | The overlay and panel are animating in |
data-exiting | The overlay and panel are animating out |
DialogHeader, DialogTitle, DialogDescription
DialogHeader is a div that stacks the title and description.
DialogTitle is React Aria's Heading
in the title slot, and DialogDescription is its Text in the
description slot. Their data-slots are dialog-header, dialog-title and
dialog-description.
DialogBody
A div that scrolls between the header and footer, and is focusable so the
keyboard can scroll it. Accepts all div props. data-slot is dialog-body.
A <form> wrapped around the body and footer needs className="contents",
so the body still scrolls between them; a plain form box stops the body
shrinking and the whole dialog scrolls instead.
DialogFooter
A div for the dialog's actions, stacked on small screens and in a row from
sm up.
Props
Also accepts all div props. data-slot is dialog-footer.
DialogClose
A Button in React Aria's close slot, so
pressing it closes the dialog. It defaults to variant="outline" and
intent="neutral", and accepts all Button props. data-slot is
dialog-close.
DialogContent
The panel Dialog renders inside DialogOverlay, for building a custom modal,
as Sheet does: React Aria's
Modal with a Dialog and the close
button inside, and a DialogBody that scrolls between header and footer.
className applies to the panel.
Props
Prop
Type
Also accepts React Aria Modal props
except the ones React Aria reads from the overlay: the open state props,
isEntering, isExiting, the dismissal props and UNSTABLE_portalContainer,
which go on DialogOverlay.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always: the data-slot prop (dialog by default) on the dialog, with -content on the panel and -close on the close button |
data-entering | The panel is animating in |
data-exiting | The panel is animating out |
DialogOverlay
The ModalOverlay that Dialog renders, for building a custom modal. Accepts
all React Aria ModalOverlay props.
data-slot is dialog-overlay, or the data-slot passed in, as Sheet passes
sheet-overlay.