Components
Dialog
A window over the page that asks for the user's attention: a form to fill, a confirmation, a detail.
Introduction
Dialog interrupts on purpose. It puts a panel over the page, dims what is behind, and waits for an answer: fill in this form, confirm this delete, read this detail. Use it for things that need the user's full attention, and not for what could sit in the page.
- The page behind cannot be used or scrolled, and Tab stays inside the panel. Escape closes it, and focus returns to the button that opened it.
- The
DialogTitlenames the dialog for screen readers. Without a title, give the content anaria-label. - Five sizes, in the middle or near the top, and long content scrolls inside the panel or behind it. It animates in and out, and not for people who prefer reduced motion.
role="alertdialog"is for a question that needs an answer. It is announced more urgently, and clicking outside does not dismiss it.- Uncontrolled: it opens when its button is pressed. Or control it with
isOpen.
For something small next to its trigger, use Popover. For a message that goes away on its own, use Toast.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Dialog uses Button for its close buttons.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxAdd Button first (see its Manual installation), then follow the steps below.
Manual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/dialog-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Dialog. `dialog-overlay` and `dialog-modal` are the classes that
// give the backdrop and the panel their entrance and exit animations (see
// globals.css).
export const dialogStyles = tv({
slots: {
overlay:
"dialog-overlay fixed inset-0 z-50 flex justify-center bg-black/50 p-4",
modal:
"dialog-modal relative flex w-full flex-col rounded-2xl border bg-card text-card-fg shadow-xl outline-none",
dialog: "flex min-h-0 flex-1 flex-col outline-none",
header: "flex flex-col gap-1.5 px-6 pt-6 pr-12 pb-4",
title: "m-0 text-lg leading-snug font-semibold",
description: "m-0 text-sm text-muted-fg",
body: "min-h-0 flex-1 px-6 py-2 text-sm",
footer:
"flex flex-col-reverse gap-2 px-6 pt-4 pb-6 sm:flex-row sm:justify-end",
close: "absolute top-3 right-3",
},
variants: {
size: {
sm: { modal: "max-w-sm" },
md: { modal: "max-w-md" },
lg: { modal: "max-w-lg" },
xl: { modal: "max-w-2xl" },
// Nearly the whole screen, for editors and previews.
full: { modal: "h-[calc(100dvh-2rem)] max-w-[calc(100vw-2rem)]" },
},
placement: {
center: { overlay: "items-center" },
top: { overlay: "items-start pt-[10vh]" },
},
// Where a long dialog scrolls: its body, or the whole page behind it.
scrollBehavior: {
inside: { modal: "max-h-[calc(100dvh-2rem)]", body: "overflow-y-auto" },
outside: { overlay: "items-start overflow-y-auto", modal: "my-auto" },
},
},
defaultVariants: {
size: "md",
placement: "center",
scrollBehavior: "inside",
},
});2. Add the Dialog file
src/components/ui/dialog/dialog.tsxShow code
"use client";
import type { ReactNode } from "react";
import {
Dialog as AriaDialog,
DialogTrigger,
type DialogTriggerProps,
Heading,
Modal,
ModalOverlay,
} from "react-aria-components";
import { XIcon } from "@/components/icons";
import { Button, type ButtonProps } from "@/components/ui/button/button";
import { cn } from "@/lib/cn";
import { dialogStyles } from "@/types/dialog-styles";
export type DialogProps = DialogTriggerProps;
/**
* A window over the page that asks for the user's attention: a form to fill, a
* confirmation, a detail. The page behind it is dimmed and cannot be used, and
* focus stays inside the window until it closes and then returns to what
* opened it. It is a wrapper with no look of its own: put the button that
* opens it and a `DialogContent` inside.
*
* It is uncontrolled: it opens when the trigger is pressed. Open it from
* elsewhere with `isOpen` and `onOpenChange`, or start open with `defaultOpen`.
*
* @example
* <Dialog>
* <Button>Delete</Button>
* <DialogContent>
* <DialogHeader>
* <DialogTitle>Delete the project?</DialogTitle>
* </DialogHeader>
* <DialogFooter>
* <DialogClose>Cancel</DialogClose>
* </DialogFooter>
* </DialogContent>
* </Dialog>
*/
export function Dialog(props: DialogProps) {
return <DialogTrigger {...props} />;
}
export interface DialogContentProps {
/** The content, or a function that receives `close`. */
children: ReactNode | ((options: { close: () => void }) => ReactNode);
size?: "sm" | "md" | "lg" | "xl" | "full";
/** Middle of the screen, or near the top. */
placement?: "center" | "top";
/** Where a long dialog scrolls: its body, or the page behind it. */
scrollBehavior?: "inside" | "outside";
/**
* `alertdialog` is for a message that needs an answer, such as confirming a
* delete. Screen readers announce it more urgently, and it cannot be
* dismissed by clicking outside.
*/
role?: "dialog" | "alertdialog";
/** Close when the user clicks outside. Defaults to `true`, and to `false` for an `alertdialog`. */
isDismissable?: boolean;
/** Do not close on Escape. */
isKeyboardDismissDisabled?: boolean;
/** The X button in the corner. Defaults to `true`, and to `false` for an `alertdialog`. */
showCloseButton?: boolean;
/** Names the dialog when it has no `DialogTitle`. */
"aria-label"?: string;
/** Classes for the panel. */
className?: string;
}
/**
* The window: a dimmed backdrop and a panel in the middle. Put a
* `DialogHeader` with a `DialogTitle`, a `DialogBody` and a `DialogFooter`
* inside. It animates in and out, and not for people who prefer reduced
* motion.
*/
export function DialogContent({
children,
size,
placement,
scrollBehavior,
role = "dialog",
isDismissable,
isKeyboardDismissDisabled,
showCloseButton,
className,
...props
}: DialogContentProps) {
const isAlert = role === "alertdialog";
const styles = dialogStyles({ size, placement, scrollBehavior });
return (
<ModalOverlay
isDismissable={isDismissable ?? !isAlert}
isKeyboardDismissDisabled={isKeyboardDismissDisabled}
className={styles.overlay()}
>
<Modal className={cn(styles.modal(), className)}>
<AriaDialog
role={role}
aria-label={props["aria-label"]}
className={styles.dialog()}
>
{(options) => (
<>
{typeof children === "function" ? children(options) : children}
{(showCloseButton ?? !isAlert) ? (
<Button
slot="close"
isIcon
variant="ghost"
size="xs"
aria-label="Close"
className={styles.close()}
>
<XIcon />
</Button>
) : null}
</>
)}
</AriaDialog>
</Modal>
</ModalOverlay>
);
}
const styles = dialogStyles();
/** The top of the dialog: the title and, under it, a line that says more. */
export function DialogHeader({
children,
className,
}: {
children: ReactNode;
className?: string;
}) {
return <div className={cn(styles.header(), className)}>{children}</div>;
}
/** The heading of the dialog. It also names the dialog for screen readers. */
export function DialogTitle({
children,
className,
}: {
children: ReactNode;
className?: string;
}) {
return (
<Heading slot="title" className={cn(styles.title(), className)}>
{children}
</Heading>
);
}
/** A line under the title that says what the dialog is for. */
export function DialogDescription({
children,
className,
}: {
children: ReactNode;
className?: string;
}) {
return <p className={cn(styles.description(), className)}>{children}</p>;
}
/** The middle of the dialog. It scrolls when the dialog is taller than the screen. */
export function DialogBody({
children,
className,
}: {
children: ReactNode;
className?: string;
}) {
return <div className={cn(styles.body(), className)}>{children}</div>;
}
/** The bottom of the dialog: its buttons, on one line, ending at the right. */
export function DialogFooter({
children,
className,
}: {
children: ReactNode;
className?: string;
}) {
return <div className={cn(styles.footer(), className)}>{children}</div>;
}
/**
* A button that closes the dialog it is in. It is a `Button`, so it takes the
* same variants and sizes, and `onPress` runs before the dialog closes.
*/
export function DialogClose({
variant = "outline",
...props
}: Omit<ButtonProps, "slot" | "href">) {
return <Button slot="close" variant={variant} {...props} />;
}It imports XIcon from @/components/icons. Add the icon, or swap it for your own.
3. Add the animations
In src/app/globals.css copy the four dialog-* keyframes, the four .dialog-overlay and .dialog-modal rules for [data-entering] and [data-exiting], and the reduced-motion rule that turns them off. Without them the dialog still works, it just appears and disappears at once.
4. Use it
import {
Dialog,
DialogBody,
DialogClose,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog/dialog";Anatomy
A trigger, and a panel with a header, a body and a footer.
<Dialog>
<Button>Open</Button>
<DialogContent>
<DialogHeader>
<DialogTitle>Invite a teammate</DialogTitle>
<DialogDescription>They get an email with a link.</DialogDescription>
</DialogHeader>
<DialogBody>{/* anything */}</DialogBody>
<DialogFooter>
<DialogClose>Cancel</DialogClose>
<Button>Send invite</Button>
</DialogFooter>
</DialogContent>
</Dialog>| Part | Rendered as | Notes |
|---|---|---|
| Dialog | no element | Holds whether it is open. It has no look: put the button that opens it, and a DialogContent, inside. |
| Trigger | any Button | The first press-able child. Focus goes back to it when the dialog closes. |
| DialogContent | backdrop and panel | The dimmed backdrop, and the panel in the middle. The page behind cannot be used, and Tab stays inside the panel. |
| DialogHeader / DialogTitle / DialogDescription | div, heading, p | The top: a title that also names the dialog for screen readers, and a line under it. |
| DialogBody | div | The middle. It scrolls when the dialog is taller than the screen. |
| DialogFooter | div | The buttons, at the bottom right. |
| DialogClose | Button | A button that closes the dialog. |
| X button | Button | In the corner, unless showCloseButton is false. |
Examples
Press a button to open its dialog.
Usage
Sizes
Placement
In the middle of the screen, or near the top, which keeps it in place when its content changes height.
Scrolling
A dialog taller than the screen scrolls its body and keeps the header and the footer in view. Or the panel can grow and the whole page behind it scroll.
Dismissing
A dialog closes on Escape and on a click outside, plus its buttons. isDismissable turns off the click outside, and isKeyboardDismissDisabled turns off Escape. Then only its buttons close it, so leave one.
Without the X
showCloseButton hides the X in the corner. Keep a button in the footer.
Controlled
Keep whether it is open in your own state, to open it from anywhere: a menu item, a keyboard shortcut, the result of a request.
isOpen: false
With a form
Children can be a function that receives close. Here the form closes the dialog when it is submitted.
Nothing saved yet.
Alert dialog
role="alertdialog" is for a question the user has to answer, such as confirming a delete. It has no X button, and clicking outside does not close it.
No answer yet.
Component API
Dialog is made of the parts below.
Dialog
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | - (the trigger and a DialogContent) |
| isOpen | boolean | - |
| defaultOpen | boolean | false |
| onOpenChange | (isOpen: boolean) => void | - |
DialogContent
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | ({ close }) => ReactNode | - (required) |
| size | "sm" | "md" | "lg" | "xl" | "full" | "md" |
| placement | "center" | "top" | "center" |
| scrollBehavior | "inside" | "outside" | "inside" |
| role | "dialog" | "alertdialog" | "dialog" |
| isDismissable | boolean | true (false for alertdialog) |
| isKeyboardDismissDisabled | boolean | false |
| showCloseButton | boolean | true (false for alertdialog) |
| aria-label | string | - (when there is no DialogTitle) |
| className | string | - |
The other parts
| Part | Props |
|---|---|
| DialogHeader, DialogTitle, DialogDescription | children, className |
| DialogBody, DialogFooter | children, className |
| DialogClose | any Button prop except slot and href, such as variant and onPress |