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
<script setup lang="ts">
import { shallowRef } from "vue"
import { Button } from "@/components/ui/button"
import {
Dialog, DialogClose, DialogContent, DialogDescription, DialogFooter,
DialogHeader, DialogTitle, DialogTrigger,
} from "@/components/ui/dialog"
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Input } from "@/components/ui/input"
// React's state setter argument: the next value, or a function of the last.
type SetStateAction<T> = T | ((value: T) => T)
const open = shallowRef(false)
const setOpen = (next: SetStateAction<typeof open.value>) => {
open.value = typeof next === "function" ? next(open.value) : next
}
const handleOpenChange = (next: boolean) => {
setOpen(next);
}
const handleSubmit = (event: Event) => {
event.preventDefault();
setOpen(false);
}
</script>
<template>
<Dialog @open-change="handleOpenChange" :open="open">
<DialogTrigger>
<template #render="{ props }">
<Button v-bind="props" variant="outline">Open Dialog</Button>
</template>
</DialogTrigger>
<DialogContent class="sm:max-w-sm">
<form class="contents" @submit="handleSubmit">
<DialogHeader>
<DialogTitle>Edit profile</DialogTitle>
<DialogDescription>
Make changes to your profile here. Click save when you're done.
</DialogDescription>
</DialogHeader>
<FieldGroup>
<Field>
<FieldLabel>Name</FieldLabel>
<Input defaultValue="Pedro Duarte" name="name" type="text" />
</Field>
<Field>
<FieldLabel>Username</FieldLabel>
<Input defaultValue="@peduarte" name="username" type="text" />
</Field>
</FieldGroup>
<DialogFooter>
<DialogClose>
<template #render="{ props }">
<Button v-bind="props" variant="outline">Cancel</Button>
</template>
</DialogClose>
<Button type="submit">Save changes</Button>
</DialogFooter>
</form>
</DialogContent>
</Dialog>
</template>
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, or on the dialog itself when it was opened by touch, so a phone keyboard doesn't pop up. 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.
- 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.
- EscEscape
- 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.
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. The dialog renders in a portal at the end of the page, and the component carries the provider's direction and language there, so the title, labels and footer buttons line up from the right and the corner X moves to the left.
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 Vue?
Hold the open state in a ref and bind it with v-model:open on Dialog, or pass open and listen to @open-change, which receives the new boolean first.
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. A DialogClose with an action that returns the request's Promise works too: UI SDK keeps the dialog open until it resolves, and a rejection leaves it open.
How do I stop a Dialog from closing on an outside click or Escape?
Set closeOnOutsidePress to false on Dialog to ignore clicks outside. For Escape, check details.reason in @open-change and call details.cancel() when it is "escape-key", or set dismissible to false to refuse both, along with Android's back gesture.
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, UI SDK 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. 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.
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
Whether the dialog is open. Pass it to control the dialog. Two-way with v-model or v-model:open.
defaultOpen?boolean
Whether the dialog starts open, when uncontrolled. Focus moves into it once it's on the page.
- Default
false
closeOnOutsidePress?boolean
Whether pressing outside the dialog, like on its backdrop, closes it. The press counts once it's released outside too, so dragging a text selection out of a field doesn't close it.
- Default
true, or false when `modal` is false
container?DocumentFragment | Element | null
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
- Type
DocumentFragment | Element | null
controller?PopupController<any, DialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
Opens, closes and inspects the dialog from anywhere, and connects triggers outside it. Create one with createDialogController().
- Type
PopupController<any, DialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
debug?boolean
Logs every state change with its reason to the console, in development builds.
dismissible?boolean
Whether Escape, an outside press and Android's back gesture can close the dialog. Close parts, triggers and the controller always can. Pass false while a form has unsaved changes, and ask in onDismissAttempt.
- Default
true
modal?"trap-focus" | boolean
true: the rest of the page is inert and doesn't scroll, focus is trapped, and an outside press only closes the dialog. "trap-focus": traps focus, and leaves the page scrollable and clickable. false: a dialog beside the page, which Tab can leave and outside presses don't close.
- Type
"trap-focus" | boolean- Default
true
pendingAnnouncement?string
What screen readers hear once a Close part's action has been running for a second, so a slow action doesn't pass in silence. Set it in the page's language. An empty string says nothing.
- Default
"Loading"
persistentElements?(string | Element)[] | (() → Element[])
Elements that stay usable while the dialog makes the page inert, like a chat launcher or a password manager's button. Elements, CSS selectors, or a function that returns elements, read when the dialog opens. Presses on them never close it.
- Type
(string | Element)[] | (() → Element[])- Parameters
string | Element)[] | ((- Returns
Element[])
presentation?PresentationSetting<"dialog">
How the dialog presents. "sheet" slides it up from the bottom of the screen, where it can be swiped away, and "auto" does that on small touch screens. Decided each time it opens; the server renders "dialog" for "auto".
- Type
PresentationSetting<"dialog">- Default
"dialog"
@open-change(open: boolean, details: DialogOpenChangeDetails) → void
Runs when the dialog wants to open or close. details.reason says why, details.cancel() stops it, and details.returnValue carries what closed it.
- Type
(open: boolean, details: DialogOpenChangeDetails) → void- Parameters
open– booleandetails– DialogOpenChangeDetails- Returns
void
@dismiss-attempt(details: DialogDismissAttemptDetails) → void
Runs when a dismissal was refused, because dismissible is false, an outside press doesn't close it, or a Close part's action is running. Ask "Discard changes?" here.
- Type
(details: DialogDismissAttemptDetails) → void- Parameters
details– DialogDismissAttemptDetails- Returns
void
@open-change-complete(open: boolean) → void
Runs once the dialog has finished opening or closing, after any animation.
- Parameters
open– boolean- Returns
void
@payload-change(payload: unknown, details: ChangeDetails<DialogPayloadChangeReason>) → void
Runs when the payload the content shows changes: a trigger with another payload opens the dialog, or takes a non-modal dialog over while it's open, which fires no onOpenChange, and so does a controller's open({ payload }) on the open dialog. details.cancel() keeps the previous payload.
- Type
(payload: unknown, details: ChangeDetails<DialogPayloadChangeReason>) → void- Parameters
payload– unknowndetails– ChangeDetails<DialogPayloadChangeReason>- Returns
void
DialogTrigger
<button> data-slot="dialog-trigger"
controller?PopupController<any, DialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
Connects a trigger outside the dialog, like one per table row, to the dialog with this controller.
- Type
PopupController<any, DialogOpenChangeReason, {}, any, DialogPayloadChangeReason>
disabled?boolean
Stops the trigger from opening the dialog. A native button can't be focused or pressed, unless focusableWhenDisabled is set.
- Default
false
focusableWhenDisabled?boolean
Keeps a disabled trigger in the tab order with aria-disabled instead of the native attribute, so it can host a tooltip that says why. Presses still do nothing.
- Default
false
payload?unknown
Data for the content to render when this trigger opens the dialog, like the row it belongs to.
shortcut?string
A keyboard shortcut that presses the trigger, like "mod+k". mod is Command on Apple platforms and Control elsewhere. Sets aria-keyshortcuts, and a Shortcut part inside shows it. A modal dialog pauses the page's shortcuts while it's open.
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
data-closed?string
Present while the dialog is closed, including while it animates out.
data-disabled?string
Present when the trigger is disabled.
data-open?string
Present while the dialog is open. On a trigger, while it's open from that trigger.
DialogPortal
DialogClose
<button> data-slot="dialog-close"
action?(event: Event, details: ActionDetails) → unknown
Runs when the part is pressed, before the dialog closes. A returned Promise keeps the dialog open while it runs: the part gets data-pending, aria-busy and aria-disabled, screen readers hear the dialog's pendingAnnouncement after a second, and nothing but the controller closes the dialog. It closes with reason "action" once the Promise resolves. A rejection keeps it open and puts the reason in the part's error state until the next press, or until the dialog opens again. details.source says how the part was pressed, for changes made after an await.
- Type
(event: Event, details: ActionDetails) → unknown- Parameters
event– Eventdetails– ActionDetails- Returns
unknown
value?string
Reported as returnValue in onOpenChange and by the controller's open(). createConfirm() resolves true for "confirm".
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
data-disabled?stringdata-pending?string
Present on a Close part while its action runs.
DialogOverlay
<div> data-slot="dialog-backdrop"
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
data-closed?string
Present while the dialog is closed, including while it animates out.
data-ending-style?string
Present while the dialog animates out. Style the state you animate to.
data-open?string
Present while the dialog is open. On a trigger, while it's open from that trigger.
data-starting-style?string
Present for the first frame. Style the state you animate from.
DialogContent
<div> data-slot="dialog-content"
bottomStickOnMobile?boolean
Stick the dialog to the bottom of the screen on mobile.
- Default
true
container?DocumentFragment | Element | null
Renders the popup into this element instead of in place, like a container inside a shadow root or an iframe. On web components, a CSS selector. Nested popups stay inside their parent.
- Type
DocumentFragment | Element | null
finalFocus?PopupFocusTarget
Where focus goes when the dialog closes while focus is inside it.
- Default
"trigger"
initialFocus?DialogInitialFocus
Where focus goes when the dialog opens. Opening with touch focuses the dialog itself, so no on-screen keyboard appears, unless this is set to something other than "auto".
- Default
"auto"
inline?boolean
Renders the dialog and its backdrop where they are in the page instead of above it, so it can stay inside a frame, like an app preview. Pair it with modal="trap-focus" to keep the rest of the page usable. A container on the dialog still moves its backdrop, so leave it off.
- Default
false
keepMounted?boolean
Whether the dialog stays in the page, hidden, while closed. Its content keeps its state between opens.
- Default
false
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"
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
data-closed?string
Present while the dialog is closed, including while it animates out.
data-ending-style?string
Present while the dialog animates out. Style the state you animate to.
data-keyboard?string
Present while the on-screen keyboard covers part of the page.
data-open?string
Present while the dialog is open. On a trigger, while it's open from that trigger.
data-presentation"dialog" | "sheet"
How the dialog presents this time it's open.
- Type
"dialog" | "sheet"
data-starting-style?string
Present for the first frame. Style the state you animate from.
--dialog-stackstring
How many dialogs are open above this one, nested inside it. 0 for the topmost.
--drawer-dimstring
While it presents as a sheet: how dim the page behind is, from 0 to 1. The backdrop's opacity follows it.
--drawer-expandstring
While it presents as a sheet: 0 at the second-largest snap point and below, 1 at the largest. With one snap point, the whole travel.
--drawer-progressstring
While it presents as a sheet: how far open the sheet is, from 0 closed to 1 at its largest snap point. Never past 1.
--drawer-stackstring
While it presents as a sheet: how many sheets opened from inside this one are open above it, rising smoothly as each one opens. Push it back with it.
--keyboard-insetstring
Unless it presents as a sheet: how many pixels of the page the on-screen keyboard covers, 0 without one.
--sheet-backdropstring
Set it yourself: the color the page dims to while it presents as a sheet. Black at 0.2 by default.
--visual-viewport-heightstring
Unless it presents as a sheet: the height of the part of the page you can see, above the keyboard.
--visual-viewport-topstring
Unless it presents as a sheet: how far the part of the page you can see is scrolled from the top of the layout, as when the keyboard pushes it up.
--visual-viewport-widthstring
Unless it presents as a sheet: the width of the part of the page you can see.
DialogHeader
<div> data-slot="dialog-header"
description?string
The description of the dialog.
title?string
The title of the dialog.
class?string
Merged with the part's classes.
DialogTitle
<h2> data-slot="dialog-title"
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
class?string
Merged with the part's classes.
DialogDescription
<p> data-slot="dialog-description"
#render?{ props, state, children }
A slot that renders your own element: bind its props with v-bind="props". It also gets the part's state, and children, its content.
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
class?string
Merged with the part's classes.











