Skip to main content

Starwind UI v3.7 is here with stable Vue and Svelte, the new Chart component, and full RTL support. Read the release notes

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.

Edit profile

Update your public profile.

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>

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

Prop Type Default Toggle
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen

Events

Event Callback Value Toggle
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
  1. Check internal eligibility and intent.
  2. Create one details object for the proposal.
  3. Call the Runtime callback with the details object when the controller exposes one.
  4. Dispatch the cancelable DOM event with the same details object, including when the callback canceled it.
  5. Read details.isCanceled, including cancellation caused by preventDefault().
  6. Apply the accepted state.
  7. 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

State Type Props Toggle
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.

  • open
  • defaultOpen
  • onOpenChange
Astro

Use open or defaultOpen for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • open
  • defaultOpen
  • starwind:open-change
  • setOpen
Runtime / HTML

Use data-default-open for initial state, listen for starwind:open-change, and call setOpen for later updates.

  • data-default-open
  • starwind:open-change
  • setOpen

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-dialog - Marks the Root part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Root part.
Metadata
Attribute Value Description
data-default-open - Reflects the default open prop on the Root part.
data-close-on-escape - Reflects the close on escape prop on the Root part.
data-close-on-outside-interact - Reflects the close on outside interact prop on the Root part.
data-modal - Reflects the modal prop on the Root part.

Trigger

The control that opens, closes, or targets the Dialog content.

Default element
button
Discovery hook
data-sw-dialog-trigger
Role
-

Props

Prop Type Default Toggle
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
Attribute Value Description
data-sw-dialog-trigger - Marks the Trigger part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Trigger part.
Metadata
Attribute Value Description
data-sw-dialog-target-id - Reflects the sw dialog target id prop on the Trigger part.

Backdrop

The backdrop shown behind the Dialog overlay.

Default element
div
Discovery hook
data-sw-dialog-overlay
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-dialog-overlay - Marks the Backdrop part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Backdrop part.

The floating content container for Dialog.

Default element
dialog
Discovery hook
data-sw-dialog-content
Role
dialog

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-dialog-content - Marks the Popup part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Popup part.

Title

The accessible title for Dialog.

Default element
h2
Discovery hook
data-sw-dialog-title
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-dialog-title - Marks the Title part so Starwind Runtime can find it.

Description

Supporting description text for Dialog.

Default element
p
Discovery hook
data-sw-dialog-description
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-dialog-description - Marks the Description part so Starwind Runtime can find it.

Close

A control that closes Dialog.

Default element
button
Discovery hook
data-sw-dialog-close
Role
-

Props

Prop Type Default Toggle
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
Attribute Value Description
data-sw-dialog-close - Marks the Close part so Starwind Runtime can find it.

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

OptionLifecycle
closeOnEscapeconstructor-only
closeOnOutsideInteractconstructor-only
defaultOpenconstructor-only
modalconstructor-only
onCloseCompleteconstructor-only
onOpenChangeconstructor-only
opensetter-backed

Runtime Setters

Method Target Description
setOpen state: open Opens or closes Dialog from Runtime code.
Component Relationship
Dialog Direct primitive

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.