Drawer Primitive
Drawer is a Starwind Runtime primitive in the dialog-native-overlay contract family. Astro and React share one semantic component API. React coordinates reactive state through controlled and default props plus callbacks. Astro renders initial state and coordinates later changes through DOM events and Runtime methods. Raw HTML uses Runtime attributes, DOM events, and imperative methods.
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>Use the React primitive adapter when Drawer state participates in React rendering.
import { Drawer } from "@starwind-ui/react/drawer";
export function Example() { return ( <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> );}Render the Drawer data-sw-* contract yourself, then initialize createDrawer.
<div data-sw-drawer> <button data-sw-drawer-trigger type="button" aria-haspopup="dialog">Open drawer</button> <div data-sw-drawer-backdrop hidden></div> <div data-sw-drawer-viewport> <dialog data-sw-drawer-popup role="dialog"> <h2 data-sw-drawer-title>Drawer title</h2> <p data-sw-drawer-description>Drawer description</p> <button data-sw-drawer-close type="button">Close</button> </dialog> </div></div>
<script type="module"> import { createDrawer } from "@starwind-ui/runtime/drawer";
const root = document.querySelector("[data-sw-drawer]"); if (root) { createDrawer(root); }</script>API Reference
Root
The main element that owns the Drawer Runtime instance.
- Default element
- div
- Discovery hook
- data-sw-drawer
- Role
- -
Props
open boolean -
- Description
- Controls whether Drawer 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 Drawer 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 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
- 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 Drawer opens or closes.
- DOM event
- starwind:open-change
- Details type
- DrawerOpenChangeDetails
- 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 Drawer has finished closing.
- DOM event
- starwind:close-complete
- Details type
- DrawerCloseCompleteDetails
- Timing
- after-state-commit
- Cancelable
- No
State
open boolean open / defaultOpen
- Description
- Tracks whether Drawer 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 Drawer content.
- Default element
- button
- Discovery hook
- data-sw-drawer-trigger
- Role
- -
Props
targetId string -
- Description
- Targets a specific root element by id.
- Kind
- attribute
- Targets
- trigger
- Full type
- string
Data Attributes
Runtime hooks
State
Metadata
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
Backdrop
The backdrop shown behind the Drawer overlay.
- Default element
- div
- Discovery hook
- data-sw-drawer-backdrop
- Role
- -
Data Attributes
Runtime hooks
State
Viewport
The visible viewport for Drawer content.
- Default element
- div
- Discovery hook
- data-sw-drawer-viewport
- Role
- -
Data Attributes
Runtime hooks
Popup
The floating content container for Drawer.
- Default element
- dialog
- Discovery hook
- data-sw-drawer-popup
- Role
- dialog
Props
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
State
Metadata
Title
The accessible title for Drawer.
- Default element
- h2
- Discovery hook
- data-sw-drawer-title
- Role
- -
Data Attributes
Runtime hooks
Description
Supporting description text for Drawer.
- Default element
- p
- Discovery hook
- data-sw-drawer-description
- Role
- -
Data Attributes
Runtime hooks
Close
A control that closes Drawer.
- Default element
- button
- Discovery hook
- data-sw-drawer-close
- Role
- -
Data Attributes
Runtime hooks
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
| 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.0.0 3 releases
v1.0.0
- Promoted this Primitive's vendoring version to the stable 1.0.0 baseline. Its existing API and Runtime behavior carry forward from the previous release.
v0.1.1
- Marked generated React Primitive files as client modules so vendored installs preserve client boundaries in React server frameworks.
- Kept generated React state-change callbacks and DOM events cancelable until the Runtime accepts the proposed state, then synchronized rendered state.
v0.1.0
- Introduced sliding dialog behavior with modal state, focus management, dismissal, and panel presence.