shadcn.io is an independent project built on shadcn/ui
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.
Installation
Anatomy

Usage
import { Button } from "@/components/ui/button";
import {
Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter,
DialogHeader, DialogTitle, DialogTrigger,
} from "@/components/ui/dialog";
import { Field, FieldLabel } from "@/components/ui/field";
import { Input } from "@/components/ui/input";The content renders in a portal at the end of the page, so the <form> goes inside DialogContent, where it holds the fields and the submit button.
<Dialog>
<DialogTrigger
asChild={(props) => (
<Button {...props()} variant="outline">Edit profile</Button>
)}
/>
<DialogContent class="sm:max-w-sm">
<form class="contents">
<DialogHeader>
<DialogTitle>Edit profile</DialogTitle>
<DialogDescription>
Make changes to your profile here. Click save when you're done.
</DialogDescription>
</DialogHeader>
<Field>
<FieldLabel>Name</FieldLabel>
<Input defaultValue="Pedro Duarte" name="name" type="text" />
</Field>
<DialogFooter>
<DialogClose
asChild={(props) => (
<Button {...props()} variant="outline">Cancel</Button>
)}
/>
<Button type="submit">Save changes</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>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 first focusable element inside, and initialFocusEl on Dialog picks another. DialogDescription is announced as the dialog's description. Give every dialog a title, hidden with sr-only when the design shows none.
- Enter
- When focus is on the trigger, opens the dialog.
- Tab
- Moves focus to the next focusable element within the content. Focus is trapped within the dialog.
- Shift+Tab
- Moves focus to the previous focusable element. Focus is trapped within the dialog.
- EscEscape
- Closes the dialog and moves focus to trigger or the defined final focus element.
- Space
- When focus is on the trigger, opens the dialog, as Enter does.
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.
No close button
Setting showCloseButton to false on DialogContent 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.
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.
Sticky footer
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.
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.
RTL
The edit profile form in Arabic, inside a Direction provider set to right to left. Ark UI reads the direction from that provider and writes it on every part, so the title, labels and footer buttons line up from the right and the corner X moves to the left. A dir on DialogContent alone would leave DialogTitle and DialogDescription left to right.
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 Solid?
Hold the open state yourself: pass open to Dialog and update it in onOpenChange, which receives an object, so read details.open.
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 DialogContent, 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 closeOnInteractOutside to false on Dialog to ignore clicks outside, and closeOnEscape to false 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 DialogContent, 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 the dialog has no name, and people hear only that a dialog opened.
When the design has no visible heading, keep DialogTitle and add the sr-only class to it. A description is optional, but DialogDescription is announced right after the title, so it is the place for one line of context.
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.
DialogBody wraps that middle part in a styled scroll area, and scrollFade on it fades the text out at its edges. It doesn't cap the height itself, so keep a maximum height on DialogContent when you use it.
How do I change the width of a Dialog, or its layout on mobile?
Set size on DialogContent 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
open?boolean
The controlled open state of the dialog.
defaultOpen?boolean
The initial open state of the dialog when rendered. Use when you don't need to control the open state of the dialog.
- Default
false
onOpenChange?(details: OpenChangeDetails) → void
Function to call when the dialog's open state changes.
- Type
(details: OpenChangeDetails) → void- Parameters
details– { open: boolean }- Returns
void
triggerValue?string | null
The controlled active trigger value.
- Type
string | null
defaultTriggerValue?string | null
The initial active trigger value when rendered. Use when you don't need to control the active trigger value.
- Type
string | null
onTriggerValueChange?(details: TriggerValueChangeDetails) → void
Function to call when the active trigger changes.
- Type
(details: TriggerValueChangeDetails) → void- Parameters
details– { value: string | null; triggerElement: HTMLElement | null }- Returns
void
closeOnEscape?boolean
Whether to close the dialog when the escape key is pressed.
- Default
true
closeOnInteractOutside?boolean
Whether to close the dialog when the outside is clicked.
- Default
true
dir?"ltr" | "rtl"
The document's text/writing direction.
- Type
"ltr" | "rtl"- Default
"ltr"
finalFocusEl?() → MaybeElement
Element to receive focus when the dialog is closed.
- Returns
MaybeElement
hideMode?"display-none" | "activity"
How to hide content when mounted but not present.
- display-none: HTML hidden attribute. Effects stay alive.
- activity: React 19 <Activity mode="hidden">. Effects pause. Requires React 19+.
- Type
"display-none" | "activity"- Default
"display-none"
id?string
The unique identifier of the machine.
ids?object
The ids of the elements in the dialog. Useful for composition.
- Type
{ trigger?: string | ((value?: string) → string); positioner?: string; backdrop?: string; content?: string; closeTrigger?: string; title?: string; description?: string }
immediate?boolean
Whether to synchronize the present change immediately or defer it to the next frame.
initialFocusEl?() → MaybeElement | false
Element to receive focus when the dialog is opened.
Return false to skip moving focus, or null to use the default.
- Type
() → MaybeElement | false- Returns
MaybeElement | false
lazyMount?boolean
Whether to enable lazy mounting. Ark default: false.
- Default
true
modal?boolean
Whether to prevent pointer interaction outside the element and hide all content below it.
- Default
true
onEnterComplete?VoidFunction
Function called when the animation ends in the open state.
onEscapeKeyDown?(event: KeyboardEvent) → void
Function called when the escape key is pressed.
- Parameters
event– { altKey: boolean; charCode: number; ctrlKey: boolean; code: string; getModifierState: (key: string) → boolean; key: string; keyCode: number; locale: string; location: number; metaKey: boolean; repeat: boolean; shiftKey: boolean; which: number }- Returns
void
onExitComplete?VoidFunction
Function called when the animation ends in the closed state.
onFocusOutside?(event: FocusOutsideEvent) → void
Function called when the focus is moved outside the component.
- Type
(event: FocusOutsideEvent) → void- Parameters
event– FocusOutsideEvent- Returns
void
onInteractOutside?(event: InteractOutsideEvent) → void
Function called when an interaction happens outside the component.
- Type
(event: InteractOutsideEvent) → void- Parameters
event– InteractOutsideEvent- Returns
void
onPointerDownOutside?(event: PointerDownOutsideEvent) → void
Function called when the pointer is pressed down outside the component.
- Type
(event: PointerDownOutsideEvent) → void- Parameters
event– PointerDownOutsideEvent- Returns
void
onRequestDismiss?(event: LayerDismissEvent) → void
Function called when this layer is closed due to a parent layer being closed.
- Type
(event: LayerDismissEvent) → void- Parameters
event– LayerDismissEvent- Returns
void
persistentElements?Array<() → Element | null>
Returns the persistent elements that: - should not have pointer-events disabled - should not trigger the dismiss event
- Type
Array<() → Element | null>
present?boolean
Whether the node is present (controlled by the user).
preventScroll?boolean
Whether to prevent scrolling behind the dialog when it's opened. Ark default: true.
- Default
(inPreview ? false : modal)
restoreFocus?boolean
Whether to restore focus to the element that had focus before the dialog was opened.
role?"dialog" | "alertdialog"
The dialog's role.
- Type
"dialog" | "alertdialog"- Default
"dialog"
skipAnimationOnMount?boolean
Whether to allow the initial presence animation.
- Default
false
trapFocus?boolean
Whether to trap focus inside the dialog when it's opened.
- Default
true
unmountOnExit?boolean
Whether to unmount on exit. Ark default: false.
- Default
true
DialogTrigger
<button> data-slot="dialog-trigger"
value?string
The value that identifies this specific trigger.
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
data-current?string
Present when current.
data-state"open" | "closed"
"open" or "closed".
- Type
"open" | "closed"
data-value?string
The value of the item.
DialogOverlay
<div> data-slot="dialog-overlay"
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
data-has-nested?string
Present when the backdrop has nested dialogs.
data-nested?string
Present when the backdrop is nested within another dialog.
data-state"open" | "closed"
"open" or "closed".
- Type
"open" | "closed"
--layer-indexstring
The index of the dismissable in the layer stack.
DialogPositioner
<div> data-slot="dialog-positioner"
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
data-has-nested?string
Present when the positioner has nested dialogs.
data-nested?string
Present when the positioner is nested within another dialog.
DialogContent
<div> data-slot="dialog-content"
bottomStickOnMobile?boolean
Stick the dialog to the bottom of the screen on mobile.
- Default
true
positionerClassName?string
Class names on the positioner (the viewport-fixed wrapper).
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"
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
data-has-nested?string
Present when another dialog is nested inside it.
data-nested?string
Present when it's nested inside another dialog.
data-state"open" | "closed"
"open" or "closed".
- Type
"open" | "closed"
--layer-indexstring
The index of the dismissable in the layer stack.
--nested-layer-countstring
The number of nested dialogs.
DialogTitle
<h2> data-slot="dialog-title"
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
DialogDescription
<div> data-slot="dialog-description"
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
DialogClose
<button> data-slot="dialog-close"
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
DialogBody
<div> data-slot="dialog-body"
scrollFade?boolean
Add a fade effect to the scroll area.
- Default
false
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
--spacestringDialogHeader
<div> data-slot="dialog-header"
description?string
The description of the dialog.
title?string
The title of the dialog.
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
DialogFooter
<div> data-slot="dialog-footer"
showCloseButton?boolean
Show a close button in the footer.
- Default
false
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
class?string
Merged with the part's classes.
DialogRootProvider
valueUseDialogReturn
What useDialog() returns: the state and API, made outside the component to control it.
hideMode?"display-none" | "activity"
How to hide content when mounted but not present.
- display-none: HTML hidden attribute. Effects stay alive.
- activity: React 19 <Activity mode="hidden">. Effects pause. Requires React 19+.
- Type
"display-none" | "activity"- Default
"display-none"
immediate?boolean
Whether to synchronize the present change immediately or defer it to the next frame.
lazyMount?boolean
Whether to enable lazy mounting. Ark default: false.
- Default
true
onEnterComplete?VoidFunction
Function called when the animation ends in the open state.
onExitComplete?VoidFunction
Function called when the animation ends in the closed state.
present?boolean
Whether the node is present (controlled by the user).
skipAnimationOnMount?boolean
Whether to allow the initial presence animation.
- Default
false
unmountOnExit?boolean
Whether to unmount on exit. Ark default: false.
- Default
true
asChild?(props: ParentProps) => JSX.Element
Render your own element: a function that gets the part's props to spread on it.
- Type
(props: ParentProps) => JSX.Element
useDialog
open?boolean
The controlled open state of the dialog.
defaultOpen?boolean
The initial open state of the dialog when rendered. Use when you don't need to control the open state of the dialog.
- Default
false
onOpenChange?(details: OpenChangeDetails) → void
Function to call when the dialog's open state changes.
- Type
(details: OpenChangeDetails) → void- Parameters
details– { open: boolean }- Returns
void
triggerValue?string | null
The controlled active trigger value.
- Type
string | null
defaultTriggerValue?string | null
The initial active trigger value when rendered. Use when you don't need to control the active trigger value.
- Type
string | null
onTriggerValueChange?(details: TriggerValueChangeDetails) → void
Function to call when the active trigger changes.
- Type
(details: TriggerValueChangeDetails) → void- Parameters
details– { value: string | null; triggerElement: HTMLElement | null }- Returns
void
closeOnEscape?boolean
Whether to close the dialog when the escape key is pressed.
- Default
true
closeOnInteractOutside?boolean
Whether to close the dialog when the outside is clicked.
- Default
true
finalFocusEl?() → MaybeElement
Element to receive focus when the dialog is closed.
- Returns
MaybeElement
id?string
The unique identifier of the machine.
ids?object
The ids of the elements in the dialog. Useful for composition.
- Type
{ trigger?: string | ((value?: string) → string); positioner?: string; backdrop?: string; content?: string; closeTrigger?: string; title?: string; description?: string }
initialFocusEl?() → MaybeElement | false
Element to receive focus when the dialog is opened.
Return false to skip moving focus, or null to use the default.
- Type
() → MaybeElement | false- Returns
MaybeElement | false
modal?boolean
Whether to prevent pointer interaction outside the element and hide all content below it.
- Default
true
onEscapeKeyDown?(event: KeyboardEvent) → void
Function called when the escape key is pressed.
- Parameters
event– { altKey: boolean; charCode: number; ctrlKey: boolean; code: string; getModifierState: (key: string) → boolean; key: string; keyCode: number; locale: string; location: number; metaKey: boolean; repeat: boolean; shiftKey: boolean; which: number }- Returns
void
onFocusOutside?(event: FocusOutsideEvent) → void
Function called when the focus is moved outside the component.
- Type
(event: FocusOutsideEvent) → void- Parameters
event– FocusOutsideEvent- Returns
void
onInteractOutside?(event: InteractOutsideEvent) → void
Function called when an interaction happens outside the component.
- Type
(event: InteractOutsideEvent) → void- Parameters
event– InteractOutsideEvent- Returns
void
onPointerDownOutside?(event: PointerDownOutsideEvent) → void
Function called when the pointer is pressed down outside the component.
- Type
(event: PointerDownOutsideEvent) → void- Parameters
event– PointerDownOutsideEvent- Returns
void
onRequestDismiss?(event: LayerDismissEvent) → void
Function called when this layer is closed due to a parent layer being closed.
- Type
(event: LayerDismissEvent) → void- Parameters
event– LayerDismissEvent- Returns
void
persistentElements?Array<() → Element | null>
Returns the persistent elements that: - should not have pointer-events disabled - should not trigger the dismiss event
- Type
Array<() → Element | null>
preventScroll?boolean
Whether to prevent scrolling behind the dialog when it's opened.
- Default
true
restoreFocus?boolean
Whether to restore focus to the element that had focus before the dialog was opened.
role?"dialog" | "alertdialog"
The dialog's role.
- Type
"dialog" | "alertdialog"- Default
"dialog"
trapFocus?boolean
Whether to trap focus inside the dialog when it's opened.
- Default
true
DialogContext
openboolean
Whether the dialog is open.
setOpen(open: boolean) → void
Function to open or close the dialog.
- Parameters
open– boolean- Returns
void
setTriggerValue(value: string | null) → void
Function to set the active trigger value.
- Type
(value: string | null) → void- Parameters
value– string | null- Returns
void
triggerValuestring | null
The active trigger value.
- Type
string | null











