Dialogs provide important prompts in a user flow. Use a dialog to make sure that people act on information, such as to confirm a high-risk action like deleting progress.
A dialog is a modal window that appears in front of the app's content to give critical information or to ask for a decision. While it is open, the rest of the app cannot be used, and it stays on screen until it is confirmed, dismissed, or a required action has been taken. Dedicate each dialog to a single task.
For a brief message that needs no response, use a snackbar instead.
A dialog is composed of these parts:
| Part | Use |
|---|---|
DialogTrigger | The trigger. Wraps the button that opens the dialog and the Dialog, with the button first. The button can be a Button, an IconButton or any other React Aria button. |
Dialog | The scrim and the dialog's container: a basic dialog (the default) or, with isFullScreen, a full-screen dialog |
Heading with slot="title" | The headline, which names the dialog |
Text with slot="description" | The supporting text, which describes the dialog |
A button with slot="close" | An action, which closes the dialog |
import { Dialog, DialogTrigger } from '@material-web/react'
import { Heading, Text } from 'react-aria-components'Basic dialog
Wrap the button that opens the dialog and the Dialog in a DialogTrigger, and write the dialog's parts as its children, each with its slot.
import { Button, Dialog, DialogTrigger } from '@material-web/react'
import { Heading, Text } from 'react-aria-components'
export default function Basic() {
return (
<DialogTrigger>
<Button variant="tonal">Use location</Button>
<Dialog>
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Use location service?
</Heading>
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
Let this service help other apps determine your location. This means sharing anonymous
location data, even when no apps are running.
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Disagree
</Button>
<Button variant="text" slot="close">
Agree
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}Style the headline, the supporting text and the actions with your own classes, as listed under Headline, content and actions. For where Heading and Text come from, see Imports.
Slots
- title
- description
- close
| Slot | Child | Use |
|---|---|---|
title | A Heading | The headline, which names the dialog |
description | A Text | The supporting text, which describes the dialog |
close | Any button | An action that closes the dialog |
Headline, content and actions
A basic dialog has 24dp of padding above and below its content and none at the sides, so a Divider in it spans the dialog. Lay it out in these parts:
| Part | Use |
|---|---|
| Headline | Optional |
| Content | Add min-h-0 grow overflow-y-auto to scroll between the headline and the actions, as in Scrolling long content. The dialog sets the type style and color of the supporting text. |
| Actions | Write the buttons in reading order, with the dismissing action first. |
| Dividers | Optional. Put a Divider between the parts. |
Style the parts with the theme's classes:
<Dialog>
<Heading slot="title" className="px-space-300 pb-space-200 text-headline-sm text-on-surface">
…
</Heading>
<div className="px-space-300 pb-space-100">…</div>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
…
</div>
</Dialog>Buttons that do not fit side by side stack with the confirming action on top. In a right-to-left page, the actions are at the left edge.
import { Button, Dialog, DialogTrigger } from '@material-web/react'
import { Heading, Text } from 'react-aria-components'
export default function StackedActions() {
return (
<DialogTrigger>
<Button variant="tonal">Speed boost</Button>
<Dialog className="max-w-70">
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Turn on speed boost?
</Heading>
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
Speed boost compresses data sent to and from your phone to load maps and directions
faster.
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
No thanks
</Button>
<Button variant="text" slot="close">
Turn on speed boost
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}Alerts
A basic dialog has the alertdialog role: it interrupts the flow to give a message and to get a response. A full-screen dialog has the dialog role. The role follows the variant and cannot be set.
The headline is optional, so an alert can ask its question in the supporting text alone. Without a title, the dialog is named by its trigger's text. Set an aria-label to give it a name of its own, or when it has no trigger.
import { Button, Dialog, DialogTrigger } from '@material-web/react'
import { Text } from 'react-aria-components'
export default function Alert() {
return (
<DialogTrigger>
<Button variant="tonal">Discard draft</Button>
<Dialog>
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
Discard draft?
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" slot="close">
Discard
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}Opening and closing
The DialogTrigger opens the dialog when its button is pressed. It keeps the open state itself unless you control it with isOpen and onOpenChange. Set defaultOpen to start with the dialog open. While the dialog is open, the button stays pressed, so a Button with hasShapeMorph keeps its pressed shape until the dialog closes.
A button in the dialog with slot="close" closes it, and the button's own onPress is still called. To close the dialog from your own code, pass a function as the Dialog's children and call state.close(): {({ state }) => …}.
To open a dialog from your own code, write a Dialog without a trigger and control it with isOpen and onOpenChange. Without a title, such a dialog needs an aria-label. The Dialog takes these props, and defaultOpen, as React Aria's Modal does.
import { Button, Dialog } from '@material-web/react'
import { useState } from 'react'
import { Heading, Text } from 'react-aria-components'
export default function Controlled() {
const [isOpen, setOpen] = useState(false)
const [isDeleted, setDeleted] = useState(false)
return (
<>
<Button variant="tonal" isDisabled={isDeleted} onPress={() => setOpen(true)}>
{isDeleted ? 'Photos deleted' : 'Delete photos'}
</Button>
<Dialog isOpen={isOpen} onOpenChange={setOpen}>
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Permanently delete?
</Heading>
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
Deleting the selected photos will also remove them from all synced devices.
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" slot="close" onPress={() => setDeleted(true)}>
Delete
</Button>
</div>
</Dialog>
</>
)
}Dismissal
Escape and a click on the scrim close a dialog, and onOpenChange is called with false. Set isDismissable={false} to keep a click on the scrim from closing it, and isKeyboardDismissDisabled to keep Escape from closing it. In this example, only the actions close the dialog.
import { Button, Dialog, DialogTrigger } from '@material-web/react'
import { Heading, Text } from 'react-aria-components'
export default function Dismissal() {
return (
<DialogTrigger>
<Button variant="tonal">Reset settings</Button>
<Dialog isDismissable={false} isKeyboardDismissDisabled>
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Reset settings?
</Heading>
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
This will reset your app preferences back to their default settings.
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" slot="close">
Reset
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}To decide for each press outside the dialog whether it closes the dialog, pass a function as shouldCloseOnInteractOutside. It receives the pressed element, and the dialog stays open when it returns false.
Forms
To put a form in a dialog, wrap all of the dialog's content in a <form>. The parts are laid out in the form as they are in the dialog. The form is submitted by its confirming action, a button with type="submit", and by Enter in a field. Close the dialog from the form's onSubmit with state.close(). In this example, the content has pt-space-100 so that the label of the outlined field is not clipped.
import { Button, Dialog, DialogTrigger, TextField } from '@material-web/react'
import { Heading } from 'react-aria-components'
export default function Form() {
return (
<DialogTrigger>
<Button variant="tonal">Rename</Button>
<Dialog>
{({ state }) => (
<form
onSubmit={(event) => {
event.preventDefault()
state.close()
}}
>
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Rename file
</Heading>
<div className="min-h-0 grow overflow-y-auto px-space-300 pt-space-100 pb-space-100">
<TextField
label="File name"
name="name"
variant="outlined"
defaultValue="Budget 2026"
className="w-full"
/>
</div>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" type="submit">
Rename
</Button>
</div>
</form>
)}
</Dialog>
</DialogTrigger>
)
}For the fields of a form and their validation, see Forms.
Size and scrolling
A basic dialog is centered in the window. It is as wide as its text, between 280dp and 560dp, and as tall as its content, up to the height the window allows.
The dialog is a flex item in a box that covers the window. Use the Dialog's className to move it from the center (ms-auto self-end) or to narrow it (max-w-80).
import { Button, Dialog, DialogTrigger } from '@material-web/react'
import { Heading, Text } from 'react-aria-components'
export default function Position() {
return (
<DialogTrigger>
<Button variant="tonal">Turn on backup</Button>
<Dialog className="ms-auto max-w-80 self-end">
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Back up photos?
</Heading>
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
Photos you take on this device are saved to your account.
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Not now
</Button>
<Button variant="text" slot="close">
Turn on
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}Scrolling long content
The content of a basic dialog is laid out in a column no taller than the dialog. Give the part that should scroll min-h-0 grow overflow-y-auto, and it scrolls between the parts above and below it. A box with a set height and no content shrinks in the column unless you give it shrink-0.
import { Button, Dialog, DialogTrigger } from '@material-web/react'
import { Heading } from 'react-aria-components'
const terms = [
'These terms cover your use of the service. By creating an account, you agree to them.',
'You keep the rights to what you upload. You let the service store it and show it to others.',
'You are responsible for your account. Keep your password to yourself.',
'We may change the service. When a change affects what you have stored, we tell you first.',
'You can close your account at any time. What you have stored is deleted 30 days later.',
'When these terms change, we ask you to accept the new terms before you continue.',
]
export default function LongContent() {
return (
<DialogTrigger>
<Button variant="tonal">Terms of service</Button>
<Dialog className="max-h-100">
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Terms of service
</Heading>
<div className="min-h-0 grow overflow-y-auto px-space-300 pb-space-100">
{terms.map((term) => (
<p key={term} className="mt-0 mb-space-200">
{term}
</p>
))}
</div>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Decline
</Button>
<Button variant="text" slot="close">
Accept
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}Note: A scrolling part clips what extends past its top edge. Give it pt-space-100 when it starts with an outlined TextField or with a control that shows a focus ring.
A full-screen dialog scrolls as a whole, under its app bar.
Full-screen
Set isFullScreen for a full-screen dialog. It fills the window, up to 560dp wide, and scrolls as a whole.
A full-screen dialog holds a series of tasks, such as creating a calendar entry with its title, date, location and time. Use one when the dialog has fields that take keyboard input, when changes are not saved instantly, or when its parts open other dialogs.
Write its header as a small app bar, first in the Dialog: the close button in the leading slot, the headline in the headline slot, and the confirming text button in the trailing slot. These buttons cannot take slot="close": close the dialog from each button's onPress. The app bar stays at the top as the dialog scrolls.
Write the content after the header, with px-space-300 pt-space-300. It does not need to scroll on its own. Put a long headline in the content, as a Heading in the title slot at its start, and keep the header's headline short or leave it out.
import {
AppBar,
Button,
Dialog,
DialogTrigger,
Icon,
IconButton,
TextField,
} from '@material-web/react'
import { Heading } from 'react-aria-components'
export default function FullScreen() {
return (
<DialogTrigger>
<Button variant="tonal">New event</Button>
<Dialog isFullScreen>
{({ state }) => (
<>
<AppBar>
<IconButton slot="leading" aria-label="Close" onPress={() => state.close()}>
<Icon name="close" />
</IconButton>
<Heading slot="headline">New event</Heading>
<Button slot="trailing" variant="text" onPress={() => state.close()}>
Save
</Button>
</AppBar>
<div className="flex flex-col gap-space-200 px-space-300 pt-space-300">
{['Event name', 'Location', 'Guests', 'Description'].map((label) => (
<TextField key={label} label={label} variant="outlined" className="w-full" />
))}
</div>
</>
)}
</Dialog>
</DialogTrigger>
)
}Responsive
Use a full-screen dialog in compact windows only. To change it into a basic dialog in wider windows, set isFullScreen from your own breakpoint, and write the parts of each variant: the app bar of a full-screen dialog, or the headline and the actions of a basic one.
In this example, the dialog is full-screen in a window narrower than 600dp, the theme's medium breakpoint.
import {
AppBar,
Button,
Dialog,
DialogTrigger,
Icon,
IconButton,
TextField,
} from '@material-web/react'
import { useSyncExternalStore } from 'react'
import { Heading } from 'react-aria-components'
// A compact window: one narrower than the theme's medium breakpoint.
const compact = '(width < 600px)'
function subscribe(onChange: () => void) {
const query = window.matchMedia(compact)
query.addEventListener('change', onChange)
return () => query.removeEventListener('change', onChange)
}
export default function Responsive() {
const isCompact = useSyncExternalStore(
subscribe,
() => window.matchMedia(compact).matches,
() => false,
)
const field = <TextField label="Album name" variant="outlined" className="w-full" />
return (
<DialogTrigger>
<Button variant="tonal">New album</Button>
<Dialog isFullScreen={isCompact}>
{({ state }) =>
isCompact ? (
<>
<AppBar>
<IconButton slot="leading" aria-label="Close" onPress={() => state.close()}>
<Icon name="close" />
</IconButton>
<Heading slot="headline">New album</Heading>
<Button slot="trailing" variant="text" onPress={() => state.close()}>
Create
</Button>
</AppBar>
<div className="px-space-300 pt-space-300">{field}</div>
</>
) : (
<>
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
New album
</Heading>
<div className="min-h-0 grow overflow-y-auto px-space-300 pt-space-100 pb-space-100">
{field}
</div>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" slot="close">
Create
</Button>
</div>
</>
)
}
</Dialog>
</DialogTrigger>
)
}Confirmation dialogs
For a choice that people confirm with an action, write the choices as the dialog's content. In this example, a list of choices scrolls between two dividers, while the headline and the actions stay in place. OK is disabled until a choice is made.
import { Button, Dialog, DialogTrigger, Divider, Radio, RadioGroup } from '@material-web/react'
import { useState } from 'react'
import { Heading } from 'react-aria-components'
const ringtones = ['None', 'Callisto', 'Ganymede', 'Luna', 'Europa', 'Titan', 'Io', 'Oberon']
export default function Scrolling() {
const [ringtone, setRingtone] = useState<string | null>(null)
return (
<DialogTrigger>
<Button variant="tonal">Phone ringtone</Button>
<Dialog className="max-h-100">
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Phone ringtone
</Heading>
<Divider />
<RadioGroup
aria-label="Phone ringtone"
value={ringtone}
onChange={setRingtone}
className="flex min-h-0 grow flex-col overflow-y-auto px-space-300"
>
{ringtones.map((name) => (
<Radio
key={name}
value={name}
className="flex h-12 items-center text-body-lg text-on-surface"
>
{name}
</Radio>
))}
</RadioGroup>
<Divider />
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" slot="close" isDisabled={ringtone === null}>
OK
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}Nested dialogs
A dialog opened from another shows in front of it, and Escape closes the one in front. Write the second dialog beside the DialogTrigger, or give it its own DialogTrigger. Do the same for a modal sheet opened from a dialog. A full-screen dialog is the only dialog that another should appear over.
A modal bottom sheet or side sheet stacks the same way: one opened from a dialog, or a dialog opened from one, shows in front of the other, and Escape closes the one in front. The page cannot be used or scrolled until both have closed. A menu in a dialog opens in front of it.
In this example, the full-screen dialog is controlled with the DialogTrigger's isOpen. When it is closed with changes made, by its close button, Escape or the scrim, a basic dialog opens in front of it and asks whether to discard them.
import {
AppBar,
Button,
Dialog,
DialogTrigger,
Icon,
IconButton,
TextField,
} from '@material-web/react'
import { useState } from 'react'
import { Heading, Text } from 'react-aria-components'
export default function Nested() {
const [isEditing, setEditing] = useState(false)
const [isChanged, setChanged] = useState(false)
const [isConfirming, setConfirming] = useState(false)
const close = () => {
setEditing(false)
setConfirming(false)
setChanged(false)
}
const leave = () => (isChanged ? setConfirming(true) : close())
return (
<>
<DialogTrigger
isOpen={isEditing}
onOpenChange={(isOpen) => (isOpen ? setEditing(true) : leave())}
>
<Button variant="tonal">Write a note</Button>
<Dialog isFullScreen>
<AppBar>
<IconButton slot="leading" aria-label="Close" onPress={leave}>
<Icon name="close" />
</IconButton>
<Heading slot="headline">New note</Heading>
<Button slot="trailing" variant="text" onPress={close}>
Save
</Button>
</AppBar>
<div className="px-space-300 pt-space-300">
<TextField
label="Note"
variant="outlined"
className="w-full"
onChange={() => setChanged(true)}
/>
</div>
</Dialog>
</DialogTrigger>
<Dialog isOpen={isConfirming} onOpenChange={setConfirming} aria-label="Discard draft">
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
Discard draft?
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" onPress={close}>
Discard
</Button>
</div>
</Dialog>
</>
)
}Customization
Pass classes in className to restyle one dialog. className, style and ref apply to the dialog's container, inside the scrim, and what the dialog holds is yours to lay out. See Styling for the classes, the states and the theme.
In this example, the dialog is no wider than 400dp, its headline shares a row with a close icon button, and dividers separate its content.
import { Button, Dialog, DialogTrigger, Divider, Icon, IconButton } from '@material-web/react'
import { Heading, Text } from 'react-aria-components'
export default function Custom() {
return (
<DialogTrigger>
<Button variant="tonal">What's new</Button>
<Dialog className="max-w-100">
<div className="flex shrink-0 items-center gap-space-200 ps-space-300 pe-space-200 pb-space-200">
<Heading slot="title" className="m-0 grow text-headline-sm text-on-surface">
What's new
</Heading>
<IconButton slot="close" aria-label="Close">
<Icon name="close" />
</IconButton>
</div>
<Divider />
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 py-space-200"
>
Shared albums now show who added each photo, and you can sort an album by the date a photo
was taken or added.
</Text>
<Divider />
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Got it
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}- States: Style the dialog as it opens and closes with the
entering:andexiting:variants. See Transitions. - Functions:
className,styleandchildrencan be functions of the dialog's state:isEntering,isExitingandstate. See Children as a function. - Ancestors: A dialog is rendered at the end of the page, not inside the element it is written in. A class or a
data-themeon an element around it does not reach it: set it on theDialogitself, or on the root element. See Theme.
Transitions
While a dialog opens and closes, its container has data-entering and data-exiting. Target them in your own classes with the entering: and exiting: variants: entering: styles what the dialog opens from, and exiting: what it closes to, as with React Aria's CSS transitions. In this example, the dialog comes up from below as it opens, and goes back down as it closes.
import { Button, Dialog, DialogTrigger } from '@material-web/react'
import { Heading, Text } from 'react-aria-components'
export default function Transition() {
return (
<DialogTrigger>
<Button variant="tonal">Sign out</Button>
<Dialog className="entering:translate-y-12 exiting:translate-y-12">
<Heading
slot="title"
className="m-0 px-space-300 pb-space-200 text-headline-sm text-on-surface"
>
Sign out?
</Heading>
<Text
slot="description"
elementType="p"
className="m-0 min-h-0 grow overflow-y-auto px-space-300 pb-space-100"
>
You will need your password to sign in again on this device.
</Text>
<div className="flex shrink-0 flex-wrap-reverse justify-end gap-space-100 px-space-300 pt-space-200">
<Button variant="text" slot="close">
Cancel
</Button>
<Button variant="text" slot="close">
Sign out
</Button>
</div>
</Dialog>
</DialogTrigger>
)
}To animate a dialog from your own code, such as with an animation library, set isEntering while the entry animation runs and isExiting while the exit animation runs. isExiting keeps a closed dialog on the page until you clear it. See React Aria's Animation guide.
Limitations
Close buttons in an app bar
The buttons in the app bar of a full-screen dialog take the app bar's slots, so they cannot also take slot="close". Close the dialog from each button's onPress, with the state.close() of the Dialog's children function or with your own open state.
Non-modal dialogs
A Dialog is always modal. For a surface that leaves the page in use while it is open, use a standard side sheet or bottom sheet.
Use cases
Material lists what people should be able to do with assistive technology:
- Open and close a dialog
- Provide and submit other inputs if the dialog is interactive, such as a text field or selectable list
- Scroll the dialog to access all of its contents if that content extends beyond the container of the dialog
Interaction and style
- Color contrast: The supporting text has 4.5:1 contrast or more with a basic dialog's container, in light and dark schemes at every contrast level. Keep that contrast when you change its colors.
- Text size: A basic dialog grows with its content up to the height the window allows, and breaks a word too long for it. Give the part that should scroll
min-h-0 grow overflow-y-auto, so that it scrolls when enlarged text makes the dialog taller than the window, as in Scrolling long content. Material advises concise text, which wraps less at 200% text size. - Motion: With reduced motion, a dialog opens and closes at once.
- Forced colors: A dialog keeps an edge in forced colors.
- Use sparingly: A dialog interrupts the page, and the flow of people who navigate it with a screen reader. Material advises using one sparingly, for critical information only, and showing less critical information in the flow of the page.
Focus
- When a dialog opens, the focus moves to the dialog itself, and Tab then moves it to the first focusable element.
- Material advises that the focus land on the first interactive element as the dialog appears. To focus an element as the dialog opens, give it
autoFocus. - While a dialog is open, the page behind it cannot be used or scrolled, and the focus stays in the dialog. When the dialog closes, the focus returns to where it was.
- In a full-screen dialog, the element that the keyboard focuses is scrolled into view below the app bar.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus to the next element in the dialog, or from the last to the first |
| Shift+Tab | Move the focus to the previous element in the dialog, or from the first to the last |
| Space or Enter | Activate the focused element |
| Escape | Close the dialog |
- Escape closes the dialog, or the one in front when one is over another, unless
isKeyboardDismissDisabledis set. - In a form, Enter in a field submits the form.
Labeling elements
- A basic dialog has the
alertdialogrole, as Material advises for the web, and a full-screen dialog thedialogrole. - Name the dialog with a
Headingin thetitleslot. It is an<h2>unless you set itslevel. Describe a basic dialog with aTextin thedescriptionslot. - Without a title, a dialog is named by its trigger's text unless you set an
aria-label. A dialog with no title and no trigger needs anaria-label. - The trigger reports whether the dialog is open with
aria-expanded. - While a dialog is open, the page behind it is hidden from assistive technology.
- Label each component in the dialog as its own page describes, such as buttons and text fields, as Material advises.
Dialog
Note: isDismissable is true by default, not false as the table shows.
isFullScreenbooleanDefaultfalseTwo variants: basic and full-screen
Full-screen dialogs fill the entire screen, containing actions that require a series of tasks to complete. One example is creating a calendar entry with the event title, date, location, and time.
Full-screen dialogs may be used for content or tasks that meet any of these criteria:
- Dialogs that include components which require keyboard input, such as form fields
- When changes aren’t saved instantly
- When components within the dialog open additional dialogs
Full-screen dialogs are for compact breakpoints only, like mobile devices. For medium and expanded breakpoints, use a basic dialog.
Dialogs can swap variants as the breakpoint changes. For example, a full-screen dialog can change into a basic dialog at larger breakpoints.
Because they take up the entire screen, full-screen dialogs are the only dialogs over which other dialogs can appear.
Props of Dialog (20)
classNameClassNameOrFunction<ModalRenderProps>Default'react-aria-ModalOverlay'The CSS className for the element. A function may be provided to compute the class based on component state.
isEnteringbooleanWhether the modal is currently performing an entry animation.
isExitingbooleanWhether the modal is currently performing an exit animation.
onEnter(element: HTMLElement) => void | Promise<void>Callback when the modal starts entering. Return a promise to indicate when the entry animation has completed.
onExit(element: HTMLElement) => void | Promise<void>Callback when the modal starts exiting. Return a promise to indicate when the exit animation has completed.
UNSTABLE_portalContainerdeprecatedElementDefaultdocument.bodyThe container element in which the overlay portal will be placed. This may have unknown behavior depending on where it is portalled to.
UNSTABLE_deferUntilEnteredbooleanWhether to wait until the entry animation completes before hiding content outside the modal and moving focus into it.
isDismissablebooleanDefaultfalseWhether to close the modal when the user interacts outside it.
isKeyboardDismissDisabledbooleanDefaultfalseWhether pressing the escape key to close the modal should be disabled.
UNSTABLE_overrideFocusbooleanWhether to override the
focus()method so it does not scroll the page. This is temporary and will be removed after fully testing all overlays.shouldCloseOnInteractOutside(element: Element) => booleanWhen user interacts with the argument element outside of the overlay ref, return true if onClose should be called. This gives you a chance to filter out interaction with elements that should not dismiss the overlay. By default, onClose will always be called on interaction outside the overlay ref.
isOpenbooleanWhether the overlay is open by default (controlled).
defaultOpenbooleanWhether the overlay is open by default (uncontrolled).
onOpenChange(isOpen: boolean) => voidHandler that is called when the overlay's open state changes.
childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: ModalRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', ModalRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.aria-labelstringDefines a string value that labels the current element.
refRef<HTMLDivElement>
Dialog also takes the 70 attributes and events of the element it renders.
DialogTrigger
Props of DialogTrigger (4)
childrenrequiredReactNodeisOpenbooleanWhether the overlay is open by default (controlled).
defaultOpenbooleanWhether the overlay is open by default (uncontrolled).
onOpenChange(isOpen: boolean) => voidHandler that is called when the overlay's open state changes.