@radix-ui/react-dialog
@radix-ui/react-dialog is the Dialog primitive from Radix: unstyled parts, Root, Trigger, Portal, Overlay, Content, Title, Description, and Close, that the app assembles and styles. In modal mode, focus stays inside the dialog, the content underneath becomes inert, and Escape closes it. Setting modal to false switches to non-modal mode. Title and Description give screen readers the dialog's name and summary.
- Downloads / mo
- 365.3M
- Stars
- 19.5K
- Last release
- 1.3.1
- License
- MIT
Strengths
- Modal and non-modal. Setting
modalto false keeps the page behind the dialog interactive. - Controlled or uncontrolled. The dialog tracks its own open state or follows
openandonOpenChange.
Limitations
- Styles come from the app. Layout and appearance come from the app's CSS.
- Custom triggers forward refs. A custom component used as a trigger accepts Radix's props, event handlers, and ref.
Quickstart
import * as Dialog from "@radix-ui/react-dialog"; export default function Example() { return ( <Dialog.Root> <Dialog.Trigger>Open dialog</Dialog.Trigger> <Dialog.Portal> <Dialog.Overlay /> <Dialog.Content> <Dialog.Title>Notice</Dialog.Title> <Dialog.Description>Review this notice.</Dialog.Description> <Dialog.Close>Close</Dialog.Close> </Dialog.Content> </Dialog.Portal> </Dialog.Root> ); }
@radix-ui/react-dialog examples
function DialogContent({ className, children, showCloseButton = true, ...props }: React.ComponentProps<typeof DialogPrimitive.Content> & { showCloseButton?: boolean }) { return ( <DialogPortal data-slot="dialog-portal"> <DialogOverlay /> <DialogPrimitive.Content data-slot="dialog-content" className={cn( "fixed top-[50%] left-[50%] z-50 grid w-full max-w-[calc(100%-2rem)] translate-x-[-50%] translate-y-[-50%] gap-4 rounded-lg border bg-background p-6 shadow-lg duration-200 outline-none data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95 sm:max-w-lg", className )} {...props} > {children} {showCloseButton && ( <DialogPrimitive.Close data-slot="dialog-close" className="absolute top-4 right-4 rounded-xs opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:ring-2 focus:ring-ring focus:ring-offset-2 focus:outline-hidden disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4" > <XIcon /> <span className="sr-only">Close</span> </DialogPrimitive.Close> )} </DialogPrimitive.Content> </DialogPortal> ) }