Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Dialog for React Aria

A modal window for React on React Aria that keeps focus inside until people save or cancel, in ten widths, and anchored to the bottom on phones.

On this page9 sections

A dialog opens a window over the page for a short task with a clear end, such as editing a profile or copying a share link. Until the person saves or cancels, focus stays inside it and the page behind can't be used.

To confirm something destructive, reach for an Alert Dialog, which a click outside won't dismiss. A long form or a set of filters fits better in a Sheet that slides in from the edge of the screen.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Dialog anatomy: content, title, description, close, footer

Usage

import { type FormEvent, useState } from "react";
import { Button } from "@/components/ui/button";
import {
  Dialog, DialogClose, DialogDescription, DialogFooter, DialogHeader,
  DialogTitle, DialogTrigger,
} from "@/components/ui/dialog";
const DialogDemo = () => {
  const [open, setOpen] = useState(false);

  const handleSubmit = (event: FormEvent<HTMLFormElement>) => {
    event.preventDefault();
    setOpen(false);
  };

  return (
    <DialogTrigger isOpen={open} onOpenChange={setOpen}>
      <Button variant="outline">Open Dialog</Button>
      <Dialog className="sm:max-w-sm">
        {/* Inside the dialog: it renders in a portal, so a form around the
            whole dialog would hold neither the fields nor the Save button. */}
        <form className="contents" onSubmit={handleSubmit}>
          <DialogHeader>
            <DialogTitle>Edit profile</DialogTitle>
            <DialogDescription>
              Make changes to your profile here. Click save when you&apos;re
              done.
            </DialogDescription>
          </DialogHeader>
          <div className="flex flex-col gap-4">
            <label className="flex flex-col gap-2 text-sm">
              Name
              <input
                className="h-9 rounded-lg border border-input bg-transparent px-3"
                defaultValue="Pedro Duarte"
                name="name"
                type="text"
              />
            </label>
            <label className="flex flex-col gap-2 text-sm">
              Username
              <input
                className="h-9 rounded-lg border border-input bg-transparent px-3"
                defaultValue="@peduarte"
                name="username"
                type="text"
              />
            </label>
          </div>
          <DialogFooter>
            <DialogClose variant="outline">Cancel</DialogClose>
            <Button type="submit">Save changes</Button>
          </DialogFooter>
        </form>
      </Dialog>
    </DialogTrigger>
  );
};

Accessibility

The window has role="dialog" and takes its name from DialogTitle, an h2. While it is open, Tab stays inside it, the page behind is hidden from screen readers, and Escape closes it and returns focus to the trigger. Focus starts on the dialog itself rather than its first field, unless a field inside has autoFocus. React Aria doesn't tie DialogDescription to the dialog, so a screen reader reads it in order rather than on open. Give every dialog a title, hidden with sr-only when the design shows none.

Enter
When focus is on the trigger, opens the dialog.
Space
When focus is on the trigger, opens the dialog.
Tab
Moves focus to the next focusable element in the dialog, and from the last one back to the first.
Shift+Tab
Moves focus to the previous focusable element in the dialog, and from the first one to the last.
Escape
Closes the dialog and moves focus back to the trigger.

Demos

Close button

A Share button opens a dialog with a read-only link and a Close button at the start of the footer, built with DialogClose. The corner X stays as well. Use a labeled button when the dialog has nothing to submit and people expect to finish at the bottom, as they do after copying a link.

Preview width

No close button

Setting showCloseButton to false on Dialog removes the corner X. This one holds only a title and a sentence, so Escape and a click outside are the only ways out. In a real dialog, hide the X when the footer already has a Cancel or Done button.

Preview width

Controlled

The name shown above the Edit name button is page state. The dialog's open state lives in useState too, and the form's submit handler saves the new name and then closes the dialog, while Cancel still goes through DialogClose. Hold the state yourself whenever closing depends on your code, such as a save that has to succeed first.

Preview width

Ten paragraphs scroll inside a region capped at half the screen's height, and the header and the footer's Close button stay in place because they sit outside it. Use this for terms of service or a long form, where the button people need would otherwise scroll out of view.

Preview width

Scrollable content

The same capped scroll region with no footer: the title and description stay put while the text moves, and the corner X or Escape closes it. It suits something people read and dismiss, like release notes. Once there is an action to take, move it into a footer as the sticky footer demo does.

Preview width

RTL

The edit profile form in Arabic, with dir set to rtl on Dialog. The title, labels and footer buttons line up from the right, and the corner X moves to the left, because the close button is placed with logical properties. Set dir on the dialog itself when its language differs from the page's.

Preview width

FAQ

When should I use a Dialog instead of an Alert Dialog or a Sheet?

Use a Dialog for a short task with fields or choices in it: editing a profile, sharing a link, entering a code. A click outside closes it, so people can back out of something they opened by mistake.

When the only question is yes or no about something that can't be undone, such as deleting a project, use an Alert Dialog, which ignores a click outside. For a long form or a filter panel that wants the full height of the screen, use a Sheet.

How do I open and close a Dialog from code in React?

Hold the open state yourself: pass isOpen to DialogTrigger and update it in onOpenChange, which receives the new boolean. A Dialog with no trigger around it takes the same two props.

Then anything can open it, such as a menu item or a keyboard shortcut, and your code can close it after a save, as the Controlled demo does.

How do I submit a form in a Dialog and close it after saving?

Put the <form> inside Dialog, around the fields and the footer. The content renders in a portal at the end of the page, so a form wrapped around the whole dialog doesn't contain its inputs, and the submit button does nothing.

Hold the open state yourself, disable the submit button while the request runs, and close the dialog only once it succeeds, so a failed save leaves the form open with the error in it. The Controlled demo saves and closes this way. Keep the save off DialogClose, which closes before the request has finished.

How do I stop a Dialog from closing on an outside click or Escape?

Set isDismissable to false on Dialog to ignore clicks outside, since this component turns it on, and add isKeyboardDismissDisabled to ignore Escape.

Keep a visible way out, such as a Cancel button, or people are stuck. If the dialog only asks people to confirm, an Alert Dialog already ignores clicks outside.

How do I hide the close button in the corner of a Dialog?

Set showCloseButton to false on Dialog, as in the No close button demo. It removes only the button: Escape and a click outside still close the dialog.

Put a Cancel or Done button in the footer instead, so touch and screen reader users have something to press. DialogFooter takes showCloseButton too, which adds an outlined Close button at its end.

Does a Dialog need a title for accessibility?

Yes. DialogTitle gives the dialog its name, which a screen reader announces when focus moves in. Without it, React Aria falls back to the label of the button that opened it, which is rarely a good name for what is inside.

When the design has no visible heading, keep DialogTitle and add the sr-only class to it. React Aria doesn't tie DialogDescription to the dialog, so anything people must hear the moment it opens belongs in the title.

How do I make a long Dialog scroll with a sticky footer?

Keep DialogHeader and DialogFooter outside the part that scrolls, and give that part a maximum height and overflow-y-auto, as the Sticky footer demo does with half the screen's height. The header and footer stay in place while the middle moves.

When that part holds only text, give it a tab index of 0, the region role and an aria-label, as the demo does, and leave its scrollbar showing. Without the tab index Safari's keyboard can't reach it, and the label tells a screen reader what it holds.

How do I change the width of a Dialog, or its layout on mobile?

Set size on Dialog to one of sm, md, lg, xl, 2xl through 6xl, or fullscreen, where md is the default. Sizes apply from the sm breakpoint up, so for a width of your own add a sm:max-w-* class, which replaces the size. A plain max-w-* loses to the size there, and on phones to the full-width layout described next.

Below that breakpoint the dialog spans the screen's width and sits at the bottom, with rounded top corners. Set bottomStickOnMobile to false to keep it centered there too.

API Reference

Dialog

data-slot="dialog-content"

PropType
defaultOpen?boolean

Whether the overlay is open by default (uncontrolled).

Default
false
onOpenChange?(isOpen: boolean) → void

Handler that is called when the overlay's open state changes.

Parameters

isOpen – boolean

Returns
void
bottomStickOnMobile?boolean

Stick the dialog to the bottom of the screen on mobile.

Default
true
isDismissable?boolean

Whether to close the modal when the user interacts outside it. Ark default: false.

Default
true
isEntering?boolean

Whether the modal is currently performing an entry animation.

isExiting?boolean

Whether the modal is currently performing an exit animation.

isKeyboardDismissDisabled?boolean

Whether pressing the escape key to close the modal should be disabled.

Default
false
isOpen?boolean

Whether the overlay is open by default (controlled).

onEnter?(element: HTMLElement) → void | Promise<void>

Callback when the modal starts entering. Return a promise to indicate when the entry animation has completed.

Type
(element: HTMLElement) → void | Promise<void>
Parameters

element – HTMLElement

Returns
void | Promise<void>
onExit?(element: HTMLElement) → void | Promise<void>

Callback when the modal starts exiting. Return a promise to indicate when the exit animation has completed.

Type
(element: HTMLElement) → void | Promise<void>
Parameters

element – HTMLElement

Returns
void | Promise<void>
role?"dialog" | "alertdialog"

The accessibility role for the dialog.

Type
"dialog" | "alertdialog"
Default
"dialog"
shouldCloseOnInteractOutside?(element: Element) → boolean

When user interacts with the argument element outside of the overlay ref, return true if onClose should be called. This gives you a chance to filter out interaction with elements that should not dismiss the overlay. By default, onClose will always be called on interaction outside the overlay ref.

Parameters

element – Element

Returns
boolean
showCloseButton?boolean

Show close button at the top right corner.

Default
true
size?"2xl" | "3xl" | "4xl" | "5xl" | "6xl" | "fullscreen" | "lg" | "md" | "sm" | "xl"

The size.

Type
"2xl" | "3xl" | "4xl" | "5xl" | "6xl" | "fullscreen" | "lg" | "md" | "sm" | "xl"
Default
"md"
slot?string | null

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Type
string | null
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
UNSTABLE_deferUntilEntered?boolean

Whether to wait until the entry animation completes before hiding content outside the modal and moving focus into it.

UNSTABLE_overrideFocus?boolean

Whether to override the focus() method so it does not scroll the page. This is temporary and will be removed after fully testing all overlays.

UNSTABLE_portalContainer?Element

The container element in which the overlay portal will be placed. This may have unknown behavior depending on where it is portalled to. Deprecated. - Use a parent UNSAFE_PortalProvider to set your portal container instead.

Default
document.body
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

DialogTrigger

data-slot="dialog-trigger"

PropType
defaultOpen?boolean

Whether the overlay is open by default (uncontrolled).

onOpenChange?(isOpen: boolean) → void

Handler that is called when the overlay's open state changes.

Parameters

isOpen – boolean

Returns
void
isOpen?boolean

Whether the overlay is open by default (controlled).

DialogClose

data-slot="dialog-close"

PropType
children?ReactNode

The content to display in the button.

clickEffect?boolean

Apply a click effect to the button.

Default
true
download?LinkPrimitiveProps["download"]

With href, downloads the linked file instead of opening it.

href?string

When set, the button renders as a link.

isDisabled?boolean

Whether the button is disabled.

isLoading?boolean

Show a loading indicator.

Default
false
rel?string

With href, the link's relationship to the page (noreferrer, …).

target?string

With href, where the linked page opens (_blank, …).

className?string

Merged with the part's classes.

DialogOverlay

data-slot="dialog-overlay"

PropType
defaultOpen?boolean

Whether the overlay is open by default (uncontrolled).

onOpenChange?(isOpen: boolean) → void

Handler that is called when the overlay's open state changes.

Parameters

isOpen – boolean

Returns
void
children?ChildrenOrFunction<T>

The children of the component. A function may be provided to alter the children based on component state.

Type
ChildrenOrFunction<T>
isDismissable?boolean

Whether to close the modal when the user interacts outside it.

Default
false
isEntering?boolean

Whether the modal is currently performing an entry animation.

isExiting?boolean

Whether the modal is currently performing an exit animation.

isKeyboardDismissDisabled?boolean

Whether pressing the escape key to close the modal should be disabled.

Default
false
isOpen?boolean

Whether the overlay is open by default (controlled).

onEnter?(element: HTMLElement) → void | Promise<void>

Callback when the modal starts entering. Return a promise to indicate when the entry animation has completed.

Type
(element: HTMLElement) → void | Promise<void>
Parameters

element – HTMLElement

Returns
void | Promise<void>
onExit?(element: HTMLElement) → void | Promise<void>

Callback when the modal starts exiting. Return a promise to indicate when the exit animation has completed.

Type
(element: HTMLElement) → void | Promise<void>
Parameters

element – HTMLElement

Returns
void | Promise<void>
shouldCloseOnInteractOutside?(element: Element) → boolean

When user interacts with the argument element outside of the overlay ref, return true if onClose should be called. This gives you a chance to filter out interaction with elements that should not dismiss the overlay. By default, onClose will always be called on interaction outside the overlay ref.

Parameters

element – Element

Returns
boolean
slot?string | null

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Type
string | null
style?StyleOrFunction<T>

The inline [style](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/style) for the element. A function may be provided to compute the style based on component state.

Type
StyleOrFunction<T>
UNSTABLE_deferUntilEntered?boolean

Whether to wait until the entry animation completes before hiding content outside the modal and moving focus into it.

UNSTABLE_overrideFocus?boolean

Whether to override the focus() method so it does not scroll the page. This is temporary and will be removed after fully testing all overlays.

UNSTABLE_portalContainer?Element

The container element in which the overlay portal will be placed. This may have unknown behavior depending on where it is portalled to. Deprecated. - Use a parent UNSAFE_PortalProvider to set your portal container instead.

Default
document.body
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

DialogHeader

<div> data-slot="dialog-header"

PropType
description?string

The description of the dialog.

title?string

The title of the dialog.

className?string

Merged with the part's classes.

DialogTitle

PropType
level?number

The heading level.

Default
3
render?DOMRenderFunction<E, T>

Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements: - You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>). - Only a single root DOM element can be rendered (no fragments). - You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

Type
DOMRenderFunction<E, T>
className?string

Merged with the part's classes.

DialogDescription

<div> data-slot="dialog-description"

PropType
className?string

Merged with the part's classes.

DialogFooter

<div> data-slot="dialog-footer"

PropType
showCloseButton?boolean

Show a close button in the footer.

Default
false
className?string

Merged with the part's classes.