Skip to content

    Dialog

    A modal window above a scrim, in five widths. Focus moves into it when it opens and goes back to the trigger when it closes. It uses shadow-high, the shadow kept for floating layers.

    React

    Preview

    Props

    • open
    • defaultOpen
    • size
    • showCloseButton
    • dismissible
    • title
    import { Button } from "@/components/ui/button"
    import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
    
    <Dialog>
      <DialogTrigger asChild>
        <Button>Rename project</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>Pick a name your team will recognize.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>

    Usage

    When to use

    • A short task that should block the page until it is done or cancelled, such as renaming a project.
    • A confirmation that is not destructive.
    • A task that needs room, such as a table or a preview, with a larger size.

    When not to use

    • Confirming a destructive action. Use AlertDialog.
    • Long forms or reading. Give them their own page.
    • Opening a dialog from a dialog. Close the first one before the next one opens, or give the flow its own page.

    Platform support

    What each platform has, from the spec. A selected platform is highlighted.

    NameWebiOSAndroidNote
    openYesYesYes
    defaultOpenYesNoNoOnly on web. Not generated for iOS and Android.
    onOpenChangeYesYesYes
    titleNoYesYesOnly on iOS, Android.
    descriptionNoYesYesOnly on iOS, Android.
    modalYesNoNoOnly on web. Not generated for iOS and Android.
    sizeYesDeferredDeferredNot generated for iOS and Android.
    showCloseButtonYesNoNoOnly on web. Not generated for iOS and Android.
    dismissibleYesDeferredDeferredNot generated for iOS and Android.

    Variants

    size

    Set on DialogContent. Widest the panel gets: sm 448px (short confirmations), md 512px (forms of a few fields), lg 672px, xl 896px (tables, previews), full the whole viewport minus spacing-lg on each side, in height too. On a narrow screen every size fills the width minus spacing-lg.

    • sm

    • mddefault

    • lg

    • xl

    • full

    import { Button } from "@/components/ui/button"
    import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
    
    <Dialog>
      <DialogTrigger asChild>
        <Button>Rename project</Button>
      </DialogTrigger>
      <DialogContent size="sm">
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>Pick a name your team will recognize.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
    <Dialog>
      <DialogTrigger asChild>
        <Button>Rename project</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>Pick a name your team will recognize.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
    <Dialog>
      <DialogTrigger asChild>
        <Button>Rename project</Button>
      </DialogTrigger>
      <DialogContent size="lg">
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>Pick a name your team will recognize.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
    <Dialog>
      <DialogTrigger asChild>
        <Button>Rename project</Button>
      </DialogTrigger>
      <DialogContent size="xl">
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>Pick a name your team will recognize.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>
    <Dialog>
      <DialogTrigger asChild>
        <Button>Rename project</Button>
      </DialogTrigger>
      <DialogContent size="full">
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>Pick a name your team will recognize.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>

    Examples

    Default

    import { Button } from "@/components/ui/button"
    import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
    
    <Dialog>
      <DialogTrigger asChild>
        <Button>Rename project</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Rename project</DialogTitle>
          <DialogDescription>Pick a name your team will recognize.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>

    Scrolling body

    import { Button } from "@/components/ui/button"
    import { Dialog, DialogBody, DialogClose, DialogContent, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
    
    <Dialog>
      <DialogTrigger asChild>
        <Button variant="secondary">Read terms</Button>
      </DialogTrigger>
      <DialogContent>
        <DialogHeader>
          <DialogTitle>Terms of service</DialogTitle>
        </DialogHeader>
        <DialogBody tabIndex={0} aria-label="Terms" className="max-h-64">
          <p className="text-sm text-text-secondary">Long terms text goes here. The header and the footer stay put while it scrolls.</p>
        </DialogBody>
        <DialogFooter>
          <DialogClose asChild>
            <Button>Accept</Button>
          </DialogClose>
        </DialogFooter>
      </DialogContent>
    </Dialog>

    Not dismissible

    For a form with unsaved input.

    import { Button } from "@/components/ui/button"
    import { Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog"
    
    <Dialog>
      <DialogTrigger asChild>
        <Button>Edit profile</Button>
      </DialogTrigger>
      <DialogContent dismissible={false}>
        <DialogHeader>
          <DialogTitle>Edit profile</DialogTitle>
          <DialogDescription>A click outside does not close it, so typed input is safe.</DialogDescription>
        </DialogHeader>
        <DialogFooter>
          <DialogClose asChild>
            <Button variant="secondary">Cancel</Button>
          </DialogClose>
          <Button>Save</Button>
        </DialogFooter>
      </DialogContent>
    </Dialog>

    Do and don't

    Do

    • Use a dialog for a short task or a confirmation that should block the page until done or cancelled.
    • Pick the smallest size that fits the content: sm for a sentence and two buttons, md for a short form, lg or xl for tables and previews. Long content scrolls inside the panel, which stays spacing-lg from the viewport edges.
    • Let Escape, the close button and a scrim click close it.
    • Let it fade and scale in (duration-slow, ease-enter) and out, faster (duration-fast, ease-exit). Under reduced motion it only fades. A command palette opened from the keyboard many times a day opens and closes at once instead.
    • Return focus to the trigger on close.

    Don't

    • Don't use Dialog to confirm a destructive action. Use AlertDialog, which focuses Cancel first and doesn't close on a scrim click.
    • Don't open a dialog from a dialog.
    • Don't use a dialog for long forms or reading; use a page or sheet. A bigger size does not make a long form short.
    • Don't use size full for a short task; the user loses sight of the page they came from.
    • Don't leave out DialogTitle. If it shouldn't show, make it sr-only.
    • Don't remove shadow-high from the panel. A dialog floats above the page, so a border alone isn't enough.

    Props

    NameTypeValuesDefaultDescription
    openboolean--Set on Dialog. Whether the dialog is open, when you control it. Pair it with onOpenChange. On iOS it is the isPresented binding.
    defaultOpenboolean-falseSet on Dialog. Opens the dialog on first render, when you don't control it. Only on web. Not generated for iOS and Android.
    onOpenChangefunction--Set on Dialog. Called with true or false when the dialog opens or closes (trigger, close button, Escape, scrim).
    title requiredstring--The dialog's accessible name and its visible title. Only on iOS, Android.
    descriptionstring-Optional supporting text under the title. Only on iOS, Android.
    modalboolean-trueSet on Dialog. true traps focus and hides the rest of the page from assistive tech. Keep it true; false only for a panel the user works next to, which is better as a sheet. Only on web. Not generated for iOS and Android.
    sizeenumsm | md | lg | xl | fullmdSet on DialogContent. Widest the panel gets: sm 448px (short confirmations), md 512px (forms of a few fields), lg 672px, xl 896px (tables, previews), full the whole viewport minus spacing-lg on each side, in height too. On a narrow screen every size fills the width minus spacing-lg. Not generated for iOS and Android.
    showCloseButtonboolean-trueDialogContent: show the corner close button. DialogFooter takes the same prop (default false) to add a secondary Close button. Only on web. Not generated for iOS and Android.
    dismissibleboolean-trueSet on DialogContent. false stops a click outside the panel from closing it, for a form with unsaved input. Escape, the corner close and Cancel still close it. Not generated for iOS and Android.

    Not generated: DialogBody, DialogClose, DialogTrigger.

    Slots

    NameRequiredDescription
    triggernoElement that opens the dialog.
    titleyesDialog title (required for a11y).
    descriptionnoOptional supporting text.
    contentnoMain body.
    footernoActions.

    Anatomy

    PartDescription
    rootRoot. Controls open state.
    overlayScrim behind the content. color-overlay-scrim.
    contentThe floating panel: raised surface, radius-page, shadow-high, border.
    headerTop block for title and description.
    titleAccessible name of the dialog.
    descriptionOptional supporting text.
    footerActions row at the bottom.
    closeIcon button that dismisses the dialog.
    iconTabler XIcon inside the close control, 16px.
    hitAreaTransparent ::before on the close control, never smaller than size-target-min.
    bodyOptional DialogBody between header and footer. It scrolls on its own, so the header and the footer stay put.

    Accessibility

    Role: dialog

    KeysAction
    EscapeCloses the dialog and returns focus to the trigger.
    TabCycles focus inside the dialog (focus trap).
    • Traps focus and closes on Escape. Hides the rest of the page from assistive tech (aria-hidden on siblings); it does not set aria-modal.
    • Always provide a DialogTitle (visible or sr-only) so the dialog has an accessible name.
    • The corner close is a DialogClose with an sr-only "Close" label and a Tabler XIcon. If showCloseButton is false on DialogContent, the footer must have a Cancel or Close.
    • Do not nest dialogs.
    • A DialogBody that scrolls is reached with Tab through its controls. Give a body of plain text tabIndex={0} and an aria-label so keyboard users can scroll it.

    Install

    npx werewolf-ui add dialog

    Run npx werewolf-ui init once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See Installation for requirements and the alternative registry setup.