Skip to content
Thrum

Dialog

A window over the page for something that needs an answer before work goes on: a confirmation, a short form, details. It holds the focus until it is closed. For a hint or a small set of options next to a control, use a popover or a menu instead.

Install

npx shadcn@latest add https://thrumui.dev/r/dialog.json

Adds components/ui/dialog.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.

import { Dialog } from "@/components/ui/dialog";

Automatically installs icon x from the icon set.

Props

PropTypeDefaultDescription
Dialog: openboolean–Whether it is open, when the parent holds the state. Use it with onOpenChange.
Dialog: defaultOpenbooleanDefault falseWhether it starts open, when the dialog holds its own state.
Dialog: onOpenChange(open: boolean) => void–Called when the dialog opens or closes, whatever caused it.
Dialog: soundbooleanDefault truePlay a soft sound on opening and on closing, if the visitor has sound on.
DialogTriggerbutton props–The button that opens the dialog. Style it with buttonClass().
DialogContent: showClosebooleanDefault trueShow the close button in the top right corner. Escape and a press outside close the dialog either way.
DialogContent: classNamestring–Extra classes for the window. max-w-lg makes it wider; the default is 448 pixels.
DialogHeader, DialogFooterComponentProps<"div">–Layout for the title block and for the actions. The footer stacks its buttons on a phone.
DialogTitleheading props–The dialog's name, announced when it opens. Every dialog needs one.
DialogDescriptionparagraph props–The explanation under the title, read after the name.
DialogClosebutton props–A button that closes the dialog. Its own onClick runs first.

Motion and sound

Motion

  • The window fades in and grows from 96% to full size over 150 ms. The veil fades with it.
  • Closing plays the same change backwards. Closing during the opening reverses it from where it is.
  • Reduced motion: the window and the veil appear and leave at once.

Sound

  • A soft rising tone when the dialog opens.
  • A soft falling tone when it closes, whichever way it was closed.

Sound is off until the visitor turns it on. Pass sound={false} to keep this component silent. How sound works

Accessibility

  • It follows the WAI-ARIA dialog pattern: role dialog, named by its title and described by its description.
  • While it is open the rest of the page cannot be reached by pointer, keyboard or screen reader, and the page behind does not scroll.
  • Focus moves into the dialog when it opens and goes back to the trigger when it closes.
  • The close button is 28 pixels square and named Close.
  • A long dialog scrolls inside itself and never grows taller than the screen.
  • Under a glass material the window is translucent. Its tint keeps the text at 4.5 to 1 over any page.
KeysAction
Enter / SpaceOn the trigger: open the dialog. Focus moves to the first control inside it.
Tab / Shift TabMove through the controls inside the dialog. Focus cannot leave it.
EscClose the dialog. Focus returns to the trigger.

Source

components/ui/dialog.tsx
"use client";

import type { ComponentProps } from "react";
import { Dialog as DialogPrimitive } from "@base-ui/react/dialog";

import { cn } from "@/lib/utils";
import { play } from "@/lib/thrum-sound";
import { XIcon } from "@/components/icons/x";

export type DialogProps = DialogPrimitive.Root.Props & {
  /** Play a soft sound when the dialog opens and when it closes, if the visitor has sound on. */
  sound?: boolean;
};

/**
 * A window over the page that holds the focus until it is closed: a form, a confirmation, details.
 * `Dialog` holds the state; `DialogTrigger` opens it; `DialogContent` is the window.
 */
export function Dialog({ sound = true, onOpenChange, ...props }: DialogProps) {
  return (
    <DialogPrimitive.Root
      onOpenChange={(open, details) => {
        if (sound) play(open ? "open" : "close");
        onOpenChange?.(open, details);
      }}
      {...props}
    />
  );
}

/** The button that opens the dialog. Style it with `buttonClass()` or pass a `Button` through `render`. */
export function DialogTrigger(props: DialogPrimitive.Trigger.Props) {
  return <DialogPrimitive.Trigger data-slot="dialog-trigger" {...props} />;
}

/** A button that closes the dialog. */
export function DialogClose(props: DialogPrimitive.Close.Props) {
  return <DialogPrimitive.Close data-slot="dialog-close" {...props} />;
}

export type DialogContentProps = DialogPrimitive.Popup.Props & {
  /** Show the close button in the top right corner. Escape and a press outside close the dialog either way. */
  showClose?: boolean;
};

/**
 * The window itself, with the veil behind it. It is a floating surface, so it turns to glass when the page's material asks for it.
 * It opens and closes with a CSS transition, which a second press can reverse halfway.
 */
export function DialogContent({ showClose = true, className, children, ...props }: DialogContentProps) {
  return (
    <DialogPrimitive.Portal>
      <DialogPrimitive.Backdrop
        data-slot="dialog-backdrop"
        className="fixed inset-0 z-50 bg-background/70 transition-opacity duration-150 ease-out data-ending-style:opacity-0 data-starting-style:opacity-0 motion-reduce:transition-none"
      />
      <DialogPrimitive.Popup
        data-slot="dialog"
        data-surface="floating"
        className={cn(
          "fixed top-1/2 left-1/2 z-50 grid max-h-[calc(100dvh-2rem)] w-[calc(100%-2rem)] max-w-md -translate-x-1/2 -translate-y-1/2 gap-4 overflow-y-auto overscroll-contain rounded-lg border bg-popover p-5 text-popover-foreground shadow-lg outline-none",
          "transition-[opacity,scale] duration-150 ease-out data-ending-style:scale-[0.96] data-ending-style:opacity-0 data-starting-style:scale-[0.96] data-starting-style:opacity-0 motion-reduce:transition-none",
          className,
        )}
        {...props}
      >
        {children}
        {showClose && (
          <DialogPrimitive.Close
            aria-label="Close"
            className="absolute top-3 right-3 grid size-7 cursor-pointer place-items-center rounded-md text-muted-foreground transition-colors duration-150 ease-out hover:bg-accent hover:text-accent-foreground focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring motion-reduce:transition-none"
          >
            <XIcon size={16} />
          </DialogPrimitive.Close>
        )}
      </DialogPrimitive.Popup>
    </DialogPrimitive.Portal>
  );
}

/** Title and description, kept clear of the close button. */
export function DialogHeader({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="dialog-header" className={cn("grid gap-1.5 pr-8", className)} {...props} />;
}

/** The dialog's name: every dialog needs one. Screen readers announce it when the dialog opens. */
export function DialogTitle({ className, ...props }: DialogPrimitive.Title.Props) {
  return <DialogPrimitive.Title data-slot="dialog-title" className={cn("text-base leading-5 font-semibold", className)} {...props} />;
}

export function DialogDescription({ className, ...props }: DialogPrimitive.Description.Props) {
  return <DialogPrimitive.Description data-slot="dialog-description" className={cn("text-sm text-muted-foreground", className)} {...props} />;
}

/** The actions, right-aligned; stacked with the main action on top on a phone. */
export function DialogFooter({ className, ...props }: ComponentProps<"div">) {
  return <div data-slot="dialog-footer" className={cn("flex flex-col-reverse gap-2 sm:flex-row sm:justify-end", className)} {...props} />;
}

Files and dependencies

Adds
components/ui/dialog.tsx
Packages
@base-ui/react@1.9.0
Brings along
Sound engine
Icons
x

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor