Dialog Primitive
Use Dialog to give a task its own space, such as editing settings. It moves focus into the dialog and returns focus when it closes.
Anatomy
Select Open settings, then press Escape or select Close. Focus returns to the trigger.
---import { Dialog } from "@starwind-ui/astro/dialog";---
<Dialog.Root> <Dialog.Trigger>Open settings</Dialog.Trigger> <Dialog.Backdrop /> <Dialog.Popup> <Dialog.Title>Settings</Dialog.Title> <Dialog.Description>Review your settings.</Dialog.Description> <Dialog.Close>Close</Dialog.Close> </Dialog.Popup></Dialog.Root>Select Open settings, then press Escape or select Close. Focus returns to the trigger.
import { Dialog } from "@starwind-ui/react/dialog";
export function Example() { return ( <Dialog.Root> <Dialog.Trigger>Open settings</Dialog.Trigger> <Dialog.Backdrop /> <Dialog.Popup> <Dialog.Title>Settings</Dialog.Title> <Dialog.Description>Review your settings.</Dialog.Description> <Dialog.Close>Close</Dialog.Close> </Dialog.Popup> </Dialog.Root> );}Select Open settings, then press Escape or select Close. Focus returns to the trigger.
<script setup lang="ts">import Dialog from "@starwind-ui/vue/dialog";</script>
<template> <Dialog.Root> <Dialog.Trigger>Open settings</Dialog.Trigger> <Dialog.Backdrop /> <Dialog.Popup> <Dialog.Title>Settings</Dialog.Title> <Dialog.Description>Review your settings.</Dialog.Description> <Dialog.Close>Close</Dialog.Close> </Dialog.Popup> </Dialog.Root></template>Select Open settings, then press Escape or select Close. Focus returns to the trigger.
<script lang="ts"> import { Dialog } from "@starwind-ui/svelte/dialog";</script>
<Dialog.Root> <Dialog.Trigger>Open settings</Dialog.Trigger> <Dialog.Backdrop /> <Dialog.Popup> <Dialog.Title>Settings</Dialog.Title> <Dialog.Description>Review your settings.</Dialog.Description> <Dialog.Close>Close</Dialog.Close> </Dialog.Popup></Dialog.Root>Select Open settings, then press Escape or select Close. Focus returns to the trigger.
<div data-sw-dialog> <button data-sw-dialog-trigger type="button" aria-haspopup="dialog">Open settings</button> <div data-sw-dialog-overlay hidden></div> <dialog data-sw-dialog-content role="dialog"> <h2 data-sw-dialog-title>Settings</h2> <p data-sw-dialog-description>Review your settings.</p> <button data-sw-dialog-close type="button">Close</button> </dialog></div>
<script type="module"> import { createDialog } from "@starwind-ui/runtime/dialog";
const root = document.querySelector("[data-sw-dialog]"); if (root) { createDialog(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed packageโs types for the available props and events. Plain HTML uses DOM events and Runtime methods.
API Reference
Root
The main element that owns the Dialog Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-dialog
- Role
- -
Props
open boolean -
- Description
- Controls whether Dialog is open.
- Kind
- control
- Targets
- root
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
defaultOpen boolean false
- Description
- Sets whether Dialog starts open.
- Kind
- control
- Targets
- root
- Full type
- boolean
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
closeOnEscape boolean true
- Description
- Closes Dialog when Escape is pressed.
- Kind
- option
- Targets
- -
- Full type
- boolean
closeOnOutsideInteract boolean true
- Description
- Closes Dialog when the user interacts outside it.
- Kind
- option
- Targets
- -
- Full type
- boolean
modal boolean true
- Description
- Makes Dialog behave as a modal overlay.
- Kind
- option
- Targets
- -
- Full type
- boolean
onCloseComplete (open: boolean, details: DialogCloseCompleteDetails) => void -
- Description
- Runs after Dialog has finished closing.
- Kind
- callback
- Targets
- -
- Full type
- (open: boolean, details: DialogCloseCompleteDetails) => void
onOpenChange (open: boolean, details: DialogOpenChangeDetails) => void -
- Description
- Runs when Dialog opens or closes.
- Kind
- callback
- Targets
- -
- Full type
- (open: boolean, details: DialogOpenChangeDetails) => void
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Events
openChange onOpenChange open: boolean
- Description
- Fires when Dialog opens or closes.
- DOM event
- starwind:open-change
- Details type
- DialogOpenChangeDetails
- Timing
- before-state-commit
- Cancelable
- Yes
- Cancellation sequence
- Check internal eligibility and intent.
- Create one details object for the proposal.
- Call the Runtime callback with the details object when the controller exposes one.
- Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
- Read details.isCanceled, including cancellation caused by preventDefault().
- Apply the accepted state.
- Notify Runtime subscribers and other accepted-only observers.
closeComplete onCloseComplete open: boolean
- Description
- Fires after Dialog has finished closing.
- DOM event
- starwind:close-complete
- Details type
- DialogCloseCompleteDetails
- Timing
- after-state-commit
- Cancelable
- No
State
open boolean open / defaultOpen
- Description
- Tracks whether Dialog is open.
- Initial attribute
- data-default-open
- Runtime getter
- getOpen
- Runtime setter
- setOpen
- React
Use open for controlled state and defaultOpen for default state, and onOpenChange for change proposals.
opendefaultOpenonOpenChange
- Astro
Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.
opendefaultOpenstarwind:open-changesetOpen
- Runtime / HTML
Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.
data-default-openstarwind:open-changesetOpen
Data Attributes
Runtime hooks
State
Metadata
Trigger
The control that opens, closes, or targets the Dialog content.
- Default element
- button
- Discovery hook
- data-sw-dialog-trigger
- Role
- -
Props
asChild boolean false
- Description
- Merges behavior onto your child element instead of rendering the default Trigger element.
- Kind
- rendering
- Targets
- trigger, close
- Full type
- boolean
targetId string -
- Description
- Targets a specific root element by id.
- Kind
- attribute
- Targets
- trigger
- Full type
- string
Data Attributes
Runtime hooks
State
Metadata
Backdrop
The backdrop shown behind the Dialog overlay.
- Default element
- div
- Discovery hook
- data-sw-dialog-overlay
- Role
- -
Data Attributes
Runtime hooks
State
Popup
The floating content container for Dialog.
- Default element
- dialog
- Discovery hook
- data-sw-dialog-content
- Role
- dialog
Data Attributes
Runtime hooks
State
Title
The accessible title for Dialog.
- Default element
- h2
- Discovery hook
- data-sw-dialog-title
- Role
- -
Data Attributes
Runtime hooks
Description
Supporting description text for Dialog.
- Default element
- p
- Discovery hook
- data-sw-dialog-description
- Role
- -
Data Attributes
Runtime hooks
Close
A control that closes Dialog.
- Default element
- button
- Discovery hook
- data-sw-dialog-close
- Role
- -
Props
asChild boolean false
- Description
- Merges behavior onto your child element instead of rendering the default Close element.
- Kind
- rendering
- Targets
- trigger, close
- Full type
- boolean
Data Attributes
Runtime hooks
Runtime API
- Factory
createDialog- Import
@starwind-ui/runtime/dialog- Root hook
- root
data-sw-dialog - Option props
- closeOnEscape, closeOnOutsideInteract, defaultOpen, modal, onCloseComplete, onOpenChange, open
Option Lifecycles
| Option | Lifecycle |
|---|---|
| closeOnEscape | constructor-only |
| closeOnOutsideInteract | constructor-only |
| defaultOpen | constructor-only |
| modal | constructor-only |
| onCloseComplete | constructor-only |
| onOpenChange | constructor-only |
| open | setter-backed |
Runtime Setters
Related Styled Components
Changelog
View version history v1.3.0 7 releases
v1.3.0
- Dialog content keeps the direction of the section that contains the dialog when it moves into a portal, including a section with the opposite `dir`.
- In Vue, `Dialog.Trigger` and `Dialog.Close` with `asChild` now accept a child wrapped in one fragment or next to an empty `v-if`.
- In Vue, `DialogContext` is no longer exported. The parts get it from their Root.
- In Vue, this component now needs Vue 3.5.21 or newer.
v1.2.0
- In Astro, `Dialog.Trigger` and `Dialog.Close` now accept `asChild`. Before, a button child gave a control that did nothing.
- In Astro, an attribute that you set on a Dialog part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
- In Svelte, a defined `open` that you pass when Dialog mounts now stays under your control, as in React and Vue. To change it, use `bind:` or update your state in the change callback. A form reset keeps it. If you pass `undefined` at mount, Dialog keeps its own state.
- In Svelte, the `children` snippet on the Dialog root no longer receives `open`. Use `bind:open`.
- A non-modal Dialog no longer moves focus back to its trigger when you move to another control while it closes.
v1.1.0
- Trigger and close buttons added or replaced after the dialog mounts now work without closing and reopening it.
- Vue Dialog keeps its previous open state when an open or close action is canceled.
- Added `asChild` support to React and Vue controls so you can use your own elements without an extra wrapper.
- Updated the shared setup file included with the Astro Primitive. Its public API and behavior are unchanged.
v1.0.1
- Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.1.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
- In React, you can cancel a change from its change callback or DOM event before it applies. React then shows only the changes that apply.
v0.1.0
- First release. Dialog can be modal or non-modal. It manages focus, closing, and its open and close animations, and it works with dialogs inside dialogs.