Skip to Content
Zuri
Components

Dialog

A modal dialog with focus trapping, scroll lock and a glass option, built on Radix.

Live preview
Theme
Preview width

Use Dialog when the user must finish or dismiss something before going back to the page: confirming a payment, editing a profile. For a small panel that should not block the page, use Popover. For a short hint, use Tooltip.

Installation

pnpm dlx shadcn@latest add https://components.abdulqadirmsingi.com/r/dialog.json

This adds one dependency, @radix-ui/react-dialog, which the command installs for you. It provides the hard parts: focus handling, scroll lock, and hiding the rest of the page from screen readers.

Usage

import {
  Dialog, DialogClose, DialogContent, DialogDescription,
  DialogFooter, DialogHeader, DialogTitle, DialogTrigger,
} from "@/components/ui/dialog";

<Dialog>
  <DialogTrigger>Edit profile</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Edit profile</DialogTitle>
      <DialogDescription>Change how your name appears on receipts.</DialogDescription>
    </DialogHeader>
    …
    <DialogFooter>
      <DialogClose>Cancel</DialogClose>
    </DialogFooter>
  </DialogContent>
</Dialog>

To control it yourself, pass open and onOpenChange to Dialog. To style the trigger like a button, pass className (for example buttonVariants() from Button).

Glass

Live preview
Theme
Preview width

Props

DialogContent takes the Radix content props and native <div> props, and Dialog takes open, defaultOpen and onOpenChange.

Props
PropTypeDefaultDescription
variant"default" | "glass""default"On DialogContent. Visual style of the panel.
closeLabelstring"Close"On DialogContent. Accessible name of the corner close button. Translate it for other languages.
open / defaultOpen / onOpenChangeboolean / boolean / (open: boolean) => void–On Dialog. Controlled and uncontrolled use.
classNamestring–Merged with the built-in classes; yours win on conflicts.

Accessibility

  • After a mouse click, focus returns to the button that opened it without drawing a focus ring (no stray border); after a key press the ring shows as normal. This comes from the small focus-modality helper, which installs with this component.
  • It has role="dialog" and is named by DialogTitle and described by DialogDescription. Always include a title; without it the dialog has no name. If you really don't want one visible, hide it with sr-only.
  • Focus moves into the dialog when it opens, is trapped there, and returns to the trigger when it closes.
  • Escape closes it, and so does clicking the backdrop. The rest of the page is hidden from screen readers and can't be scrolled while it is open.
  • The corner close button is 32 pixels square and always present, so keyboard and touch users have an obvious way out.
  • It fades and scales in using the --duration-* tokens, which are almost zero under prefers-reduced-motion. It closes instantly.

Tokens used

--background, --foreground, --muted, --muted-foreground, --border, --ring, --duration-base, --ease-out, --z-overlay, --z-modal, and the glass variables.

Known limitations

  • It does not animate out; it unmounts when closed.
  • For a destructive confirmation (such as deleting something), consider an alert dialog that does not close on an outside click. That is a separate component and not built yet.
  • Glass text contrast has not been measured against worst-case backdrops.
  • It renders into a portal at the end of <body>, so it is not inside your app's landmarks.

Source

"use client";import * as DialogPrimitive from "@radix-ui/react-dialog";import { cva, type VariantProps } from "class-variance-authority";import type { ComponentProps } from "react";import { useQuietFocusRestore } from "@/lib/focus-modality";import { cn } from "@/lib/utils";/** Compose with DialogTrigger, DialogContent, DialogTitle and DialogDescription. */export const Dialog = DialogPrimitive.Root;export const DialogTrigger = DialogPrimitive.Trigger;export const DialogClose = DialogPrimitive.Close;const contentVariants = cva(  // Enter animation only (`starting:`): Radix unmounts immediately on close, which is fine  // and keeps this free of keyframes. Durations come from tokens, so reduced motion is honoured.  "fixed start-1/2 top-1/2 z-[var(--z-modal)] flex max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-lg -translate-x-1/2 -translate-y-1/2 flex-col gap-4 overflow-y-auto rounded-xl p-6 text-foreground transition-[opacity,transform,translate,scale,rotate] duration-[var(--duration-base)] ease-[var(--ease-out)] starting:scale-95 starting:opacity-0 rtl:translate-x-1/2 focus-visible:outline-none",  {    variants: {      variant: {        default: "border border-border bg-background",        glass: "zuri-glass",      },    },    defaultVariants: { variant: "default" },  },);export interface DialogContentProps  extends ComponentProps<typeof DialogPrimitive.Content>,    VariantProps<typeof contentVariants> {  /** Accessible name for the close button. Defaults to "Close". */  closeLabel?: string;}/** * The modal itself, with its backdrop. Radix traps focus inside, returns it to the trigger on * close, locks page scroll, closes on Escape and on an outside click, and hides the rest of * the page from screen readers. Always include a DialogTitle: it is the dialog's name. */export function DialogContent({  className,  variant,  closeLabel = "Close",  children,  onCloseAutoFocus,  ...props}: DialogContentProps) {  const quiet = useQuietFocusRestore(onCloseAutoFocus);  return (    <DialogPrimitive.Portal>      <DialogPrimitive.Overlay className="fixed inset-0 z-[var(--z-overlay)] bg-foreground/40 transition-opacity duration-[var(--duration-base)] starting:opacity-0" />      <DialogPrimitive.Content        className={cn(contentVariants({ variant }), className)}        onCloseAutoFocus={quiet.onCloseAutoFocus}        {...props}      >        <span hidden ref={quiet.probe} />        {children}        <DialogPrimitive.Close          aria-label={closeLabel}          className="absolute end-3 top-3 inline-flex size-8 cursor-pointer items-center justify-center rounded-md text-muted-foreground hover:bg-muted hover:text-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring"        >          <svg            aria-hidden="true"            viewBox="0 0 16 16"            fill="none"            stroke="currentColor"            strokeWidth="2"            strokeLinecap="round"            className="size-4"          >            <path d="M3.5 3.5l9 9M12.5 3.5l-9 9" />          </svg>        </DialogPrimitive.Close>      </DialogPrimitive.Content>    </DialogPrimitive.Portal>  );}export function DialogHeader({ className, ...props }: ComponentProps<"div">) {  return <div className={cn("flex flex-col gap-1.5 pe-8", className)} {...props} />;}export function DialogFooter({ className, ...props }: ComponentProps<"div">) {  return (    <div      className={cn("flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)}      {...props}    />  );}export function DialogTitle({ className, ...props }: ComponentProps<typeof DialogPrimitive.Title>) {  return (    <DialogPrimitive.Title      className={cn("text-lg font-semibold leading-tight", className)}      {...props}    />  );}export function DialogDescription({  className,  ...props}: ComponentProps<typeof DialogPrimitive.Description>) {  return (    <DialogPrimitive.Description      className={cn("text-sm text-muted-foreground", className)}      {...props}    />  );}export { contentVariants as dialogContentVariants };

On this page