Skip to main content

Starwind UI v3.0 beta is now available! Read the migration guide

Drawer Primitive

Drawer is a Starwind Runtime primitive in the dialog-native-overlay contract family.

Anatomy

Use the Astro primitive adapter to render Drawer anatomy with the Runtime wiring included.

---
import { Drawer } from "@starwind-ui/astro/drawer";
---
<Drawer.Root>
<Drawer.Trigger>Open drawer</Drawer.Trigger>
<Drawer.Backdrop />
<Drawer.Viewport>
<Drawer.Popup>
<Drawer.Title>Drawer title</Drawer.Title>
<Drawer.Description>Drawer description</Drawer.Description>
<Drawer.Close>Close</Drawer.Close>
</Drawer.Popup>
</Drawer.Viewport>
</Drawer.Root>

API Reference

Root

The main element that owns the Drawer Runtime instance.

Default element
div
Discovery hook
data-sw-drawer
Role
-

Props

Prop Type Default Toggle
open boolean -
Description
Controls whether Drawer is open.
Kind
control
Targets
root
Full type
boolean
defaultOpen boolean false
Description
Sets whether Drawer starts open.
Kind
control
Targets
root
Full type
boolean
closeOnEscape boolean true
Description
Closes Drawer when Escape is pressed.
Kind
option
Targets
-
Full type
boolean
closeOnOutsideInteract boolean true
Description
Closes Drawer when the user interacts outside it.
Kind
option
Targets
-
Full type
boolean
modal boolean true
Description
Makes Drawer behave as a modal overlay.
Kind
option
Targets
-
Full type
boolean
onCloseComplete (open: boolean, details: DrawerCloseCompleteDetails) => void -
Description
Runs after Drawer has finished closing.
Kind
callback
Targets
-
Full type
(open: boolean, details: DrawerCloseCompleteDetails) => void
onOpenChange (open: boolean, details: DrawerOpenChangeDetails) => void -
Description
Runs when Drawer opens or closes.
Kind
callback
Targets
-
Full type
(open: boolean, details: DrawerOpenChangeDetails) => void

Events

Event Callback Value Toggle
openChange onOpenChange open: boolean
Description
Fires when Drawer opens or closes.
DOM event
starwind:open-change
Details type
DrawerOpenChangeDetails
Timing
before-state-commit
Cancelable
Yes
closeComplete onCloseComplete open: boolean
Description
Fires after Drawer has finished closing.
DOM event
starwind:close-complete
Details type
DrawerCloseCompleteDetails
Timing
after-state-commit
Cancelable
No

State

State Type Props Toggle
open boolean open / defaultOpen
Description
Tracks whether Drawer is open.
Initial attribute
data-default-open
Runtime getter
getOpen
Runtime setter
setOpen
State control support
React supports controlled and default state with open and defaultOpen props. Runtime/HTML reads initial state from data-default-open, emits starwind:open-change and starwind:close-complete, and updates with setOpen. Astro adapters render initial/default state, but do not expose reactive controlled-state props for this state.

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-drawer - 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 Drawer content.

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

Props

Prop Type Default Toggle
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-drawer-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-drawer-target-id - Reflects the sw drawer target id prop on the Trigger part.

Portal

Moves Drawer overlay content to the document body when needed.

Default element
div
Discovery hook
data-sw-drawer-portal
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-drawer-portal - Marks the Portal part so Starwind Runtime can find it.

Backdrop

The backdrop shown behind the Drawer overlay.

Default element
div
Discovery hook
data-sw-drawer-backdrop
Role
-

Data Attributes

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

Viewport

The visible viewport for Drawer content.

Default element
div
Discovery hook
data-sw-drawer-viewport
Role
-

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-drawer-viewport - Marks the Viewport part so Starwind Runtime can find it.

The floating content container for Drawer.

Default element
dialog
Discovery hook
data-sw-drawer-popup
Role
dialog

Props

Prop Type Default Toggle
side "top" | "right" | "bottom" | "left" "right"
Description
Sets the preferred side for Drawer content.
Kind
option
Targets
popup
Full type
"top" | "right" | "bottom" | "left"

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-drawer-popup - Marks the Popup part so Starwind Runtime can find it.
State
Attribute Value Description
data-state - Reflects the current state on the Popup part.
Metadata
Attribute Value Description
data-side - Reflects the side prop on the Popup part.

Title

The accessible title for Drawer.

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

Data Attributes

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

Description

Supporting description text for Drawer.

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

Data Attributes

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

Close

A control that closes Drawer.

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

Data Attributes

Runtime hooks
Attribute Value Description
data-sw-drawer-close - Marks the Close part so Starwind Runtime can find it.

Runtime API

Factory
createDrawer
Import
@starwind-ui/runtime/drawer
Root hook
root data-sw-drawer
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 Drawer from Runtime code.
Component Relationship
Sheet Renamed primitive