Components
Modal
A free-form window over the page for a confirmation, a form or a preview, with an optional ring around it.
Introduction
Modal is a window in the middle of the page that asks for the user's attention. It dims what is behind and waits: confirm this delete, fill in this form, look at this preview. Its panel is already padded and spaced, so you lay out what is inside freely: a title, some text, a warning, a field and buttons, in any order.
isRingis a boolean. It draws a frosted halo around the panel, with the backdrop blurred behind it, so the modal looks framed against the page, in the light and the dark theme.false, the default, is a plain panel.- The page behind cannot be used or scrolled, and Tab stays inside. Escape closes it, and focus returns to the button that opened it.
- Five sizes, in the middle or near the top, and long content scrolls in 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, such as confirming a delete. It is announced more urgently, and clicking outside does not dismiss it.
Modal and Dialog do the same job. Dialog gives you a fixed header, body and footer with their own spacing; Modal gives you a padded panel and gets out of the way. Pick the one whose layout suits you.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Modal 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/modal-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Modal. `dialog-overlay` and `dialog-modal` give the backdrop and
// the panel their entrance and exit animations (see globals.css, they are the
// same as a Dialog's). Unlike Dialog, the panel itself is padded and spaced,
// so its parts are free to be anything and in any order. `modal` is react-aria's
// Modal, which sizes and animates; `panel` is the dialog inside it, which is
// what you see.
export const modalStyles = tv({
slots: {
overlay:
"dialog-overlay fixed inset-0 z-50 flex justify-center bg-black/50 p-4",
modal: "dialog-modal w-full",
panel:
"relative flex max-h-[inherit] flex-col gap-4 rounded-2xl border bg-card p-6 text-card-fg shadow-xl outline-none",
header: "flex flex-col gap-1.5 pr-8",
title: "m-0 text-lg leading-snug font-semibold",
description: "m-0 text-sm text-muted-fg",
// The small negative margin and padding keep a focus ring on a field
// inside from being cut off by the scroll area.
body: "-m-1 min-h-0 flex-1 p-1 text-sm",
footer: "flex flex-col-reverse gap-2 pt-2 sm:flex-row sm:justify-end",
close: "absolute top-4 right-4",
},
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)]",
panel: "h-full",
},
},
placement: {
center: { overlay: "items-center" },
top: { overlay: "items-start pt-[10vh]" },
},
// Where a long modal 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" },
},
// A ring like a frame of frosted glass around the panel, measured from a
// reference (at 2x, so halve the pixels):
// - a band 6px wide, white at 72% at the top down to 34% at the bottom;
// - a hairline 1px outside it, black at 4%;
// - the panel's own border 1px, a cool gray (#c4c7cb), not the usual pale
// border, so the edge of the panel stays sharp against the band;
// - a shadow that falls mostly below (0 20px 40px -10px, black at 43%), cast
// by the modal so it also shows through the band.
// The backdrop is lighter (11% black) so the ring reads in the light theme,
// and the dark theme has its own values.
// The ring is drawn by a pseudo-element 7px wider than the modal on every
// side. It sits on the modal, not the panel, so the panel paints over it.
isRing: {
true: {
overlay: "bg-black/[0.11] dark:bg-black/60",
modal: [
"relative rounded-2xl shadow-[0_20px_40px_-10px_rgb(0_0_0/0.43)] dark:shadow-[0_20px_40px_-10px_rgb(0_0_0/0.6)]",
"before:pointer-events-none before:absolute before:-inset-[7px] before:rounded-[23px] before:content-['']",
"before:border before:border-black/[0.04] before:backdrop-blur-sm",
"before:bg-[linear-gradient(to_bottom,rgb(255_255_255/0.72),rgb(255_255_255/0.34))]",
"dark:before:border-white/10 dark:before:bg-[linear-gradient(to_bottom,rgb(255_255_255/0.2),rgb(255_255_255/0.07))]",
],
panel:
"border-[oklch(0.82_0.008_255)] shadow-none dark:border-white/15",
},
false: {},
},
},
defaultVariants: {
size: "md",
placement: "center",
scrollBehavior: "inside",
isRing: false,
},
});2. Add the Modal file
src/components/ui/modal/modal.tsxShow code
"use client";
import type { ReactNode } from "react";
import {
Dialog as AriaDialog,
Modal as AriaModal,
DialogTrigger,
type DialogTriggerProps,
Heading,
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 { modalStyles } from "@/types/modal-styles";
export type ModalProps = DialogTriggerProps;
/**
* A window in the middle of the page that takes the user's attention: a
* confirmation, a form, a preview. The page behind is dimmed and cannot be
* used, focus stays inside until it closes and then goes back to what opened
* it, and Escape closes it. It is a wrapper with no look of its own: put the
* button that opens it and a `ModalContent` inside.
*
* Modal is the free-form one. Its panel is already padded and spaced, so put
* a header, text, a field, a notice and a footer in any order. For a window
* with a fixed header, body and footer, see `Dialog`.
*
* It is uncontrolled: it opens when the trigger is pressed. Open it from
* elsewhere with `isOpen` and `onOpenChange`, or start open with `defaultOpen`.
*
* @example
* <Modal>
* <Button>Open</Button>
* <ModalContent isRing>
* <ModalHeader><ModalTitle>Are you sure?</ModalTitle></ModalHeader>
* <ModalFooter><ModalClose>Cancel</ModalClose></ModalFooter>
* </ModalContent>
* </Modal>
*/
export function Modal(props: ModalProps) {
return <DialogTrigger {...props} />;
}
export interface ModalContentProps {
/** The content, or a function that receives `close`. */
children: ReactNode | ((options: { close: () => void }) => ReactNode);
/**
* A frame of frosted glass around the panel: a translucent band 6px wide,
* lighter at the top, with a hairline outside it, a sharper panel border and
* a shadow that falls below. It also lightens the backdrop so the ring shows,
* and it has its own values in the dark theme. `false` is a plain panel.
*/
isRing?: boolean;
size?: "sm" | "md" | "lg" | "xl" | "full";
/** Middle of the screen, or near the top. */
placement?: "center" | "top";
/** Where a long modal 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 modal when it has no `ModalTitle`. */
"aria-label"?: string;
/** Classes for the panel. */
className?: string;
}
/**
* The window: a dimmed backdrop and a padded panel in the middle. It animates
* in and out, and not for people who prefer reduced motion.
*/
export function ModalContent({
children,
isRing,
size,
placement,
scrollBehavior,
role = "dialog",
isDismissable,
isKeyboardDismissDisabled,
showCloseButton,
className,
...props
}: ModalContentProps) {
const isAlert = role === "alertdialog";
const styles = modalStyles({ size, placement, scrollBehavior, isRing });
return (
<ModalOverlay
isDismissable={isDismissable ?? !isAlert}
isKeyboardDismissDisabled={isKeyboardDismissDisabled}
className={styles.overlay()}
>
<AriaModal className={styles.modal()}>
<AriaDialog
role={role}
aria-label={props["aria-label"]}
className={cn(styles.panel(), className)}
>
{(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>
</AriaModal>
</ModalOverlay>
);
}
const styles = modalStyles();
interface PartProps {
children: ReactNode;
className?: string;
}
/** A group for the title and the line under it. */
export function ModalHeader({ children, className }: PartProps) {
return <div className={cn(styles.header(), className)}>{children}</div>;
}
/** The heading of the modal. It also names the modal for screen readers. */
export function ModalTitle({ children, className }: PartProps) {
return (
<Heading slot="title" className={cn(styles.title(), className)}>
{children}
</Heading>
);
}
/** A line under the title that says what the modal is for. */
export function ModalDescription({ children, className }: PartProps) {
return <p className={cn(styles.description(), className)}>{children}</p>;
}
/**
* A group for the main content. It scrolls when the modal is taller than the
* screen. You do not have to use it: anything can sit directly in the modal.
*/
export function ModalBody({ children, className }: PartProps) {
return <div className={cn(styles.body(), className)}>{children}</div>;
}
/** The buttons, on one line ending at the right. */
export function ModalFooter({ children, className }: PartProps) {
return <div className={cn(styles.footer(), className)}>{children}</div>;
}
/**
* A button that closes the modal it is in. It is a `Button`, so it takes the
* same variants and sizes, and `onPress` runs before the modal closes.
*/
export function ModalClose({
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. They are the same as Dialog's. Without them the modal still works, it just appears and disappears at once.
4. Use it
import {
Modal,
ModalBody,
ModalClose,
ModalContent,
ModalDescription,
ModalFooter,
ModalHeader,
ModalTitle,
} from "@/components/ui/modal/modal";Anatomy
A trigger, and a padded panel with whatever you put in it.
<Modal>
<Button>Open</Button>
<ModalContent isRing>
<ModalHeader>
<ModalTitle>Invite a teammate</ModalTitle>
<ModalDescription>They get an email with a link.</ModalDescription>
</ModalHeader>
<ModalBody>{/* anything */}</ModalBody>
<ModalFooter>
<ModalClose>Cancel</ModalClose>
<Button>Send invite</Button>
</ModalFooter>
</ModalContent>
</Modal>| Part | Rendered as | Notes |
|---|---|---|
| Modal | no element | Holds whether it is open. It has no look: put the button that opens it, and a ModalContent, inside. |
| Trigger | any Button | The first press-able child. Focus goes back to it when the modal closes. |
| ModalContent | backdrop and panel | The dimmed backdrop and the padded panel in the middle. It sets the size, and with isRing adds the halo around the panel. |
| ModalHeader / ModalTitle / ModalDescription | div, heading, p | The title, which also names the modal for screen readers, and a line under it. |
| ModalBody / ModalFooter | div | Optional groups. The panel is already padded and spaced, so anything can sit directly in it. |
| ModalClose | Button | A button that closes the modal. |
| X button | Button | In the corner, unless showCloseButton is false. |
Examples
Press a button to open its modal.
Usage
Ring
isRing draws a frame of frosted glass around the panel: a translucent band 6px wide that is whiter at the top, a hairline outside it, a sharper border on the panel, and a shadow that falls below. It also lightens the backdrop so the ring shows, and it has its own colors in the dark theme. It is a boolean: true for the ring, false (the default) for a plain panel.
Type to confirm
An alert modal for something that cannot be undone. The button that does it stays disabled until the user types the text, here in a modal with a ring.
No answer yet.
Sizes
Placement
In the middle of the screen, or near the top, which keeps it in place when its content changes height.
Scrolling
A modal 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 modal 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
Component API
Modal is made of the parts below.
Modal
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | - (the trigger and a ModalContent) |
| isOpen | boolean | - |
| defaultOpen | boolean | false |
| onOpenChange | (isOpen: boolean) => void | - |
ModalContent
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | ({ close }) => ReactNode | - (required) |
| isRing | boolean | false |
| 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 ModalTitle) |
| className | string | - |
The other parts
| Part | Props |
|---|---|
| ModalHeader, ModalTitle, ModalDescription | children, className |
| ModalBody, ModalFooter | children, className |
| ModalClose | any Button prop except slot and href, such as variant and onPress |