Drawer for Astro
Astro panel that slides into view from the screen edge, with swipe gestures, snapping, and nested interfaces.
Installation
bunx --bun shadcn@latest add @gnostic/drawernpx shadcn@latest add @gnostic/drawerpnpm dlx shadcn@latest add @gnostic/drawerInstall component packages
bun add @data-slot/drawernpm install @data-slot/drawerpnpm add @data-slot/drawerCopy the files
src/components/ui/drawer/drawer-backdrop.astrosrc/components/ui/drawer/drawer-close.astrosrc/components/ui/drawer/drawer-description.astrosrc/components/ui/drawer/drawer-footer.astrosrc/components/ui/drawer/drawer-header.astrosrc/components/ui/drawer/drawer-menu-group-label.astrosrc/components/ui/drawer/drawer-menu-group.astrosrc/components/ui/drawer/drawer-menu-item.astrosrc/components/ui/drawer/drawer-menu-trigger.astrosrc/components/ui/drawer/drawer-menu.astrosrc/components/ui/drawer/drawer-panel.astrosrc/components/ui/drawer/drawer-popup.astrosrc/components/ui/drawer/drawer-title.astrosrc/components/ui/drawer/drawer-trigger.astrosrc/components/ui/drawer/drawer.astrosrc/components/ui/drawer/index.tssrc/components/ui/drawer/types.ts
Usage
---
import { buttonVariants } from "@/components/ui/button"
import {
Drawer, DrawerTrigger, DrawerPopup, DrawerHeader, DrawerTitle,
DrawerDescription, DrawerPanel, DrawerFooter, DrawerClose,
DrawerMenu, DrawerMenuItem,
} from "@/components/ui/drawer"
---
<Drawer position="right">
<DrawerTrigger>Open menu</DrawerTrigger>
<DrawerPopup variant="inset" showCloseButton>
<DrawerHeader>
<DrawerTitle>Menu</DrawerTitle>
</DrawerHeader>
<DrawerPanel>
<DrawerMenu aria-label="Site navigation">
<DrawerMenuItem as="a" href="/about" closeOnSelect>About</DrawerMenuItem>
<DrawerMenuItem as="a" href="/contact" closeOnSelect>Contact</DrawerMenuItem>
</DrawerMenu>
</DrawerPanel>
</DrawerPopup>
</Drawer>
Always include DrawerTitle. Use class="sr-only" to hide the title visually.
Use Astro’s as prop for link controls. DrawerClose is a plain element.
Pass class={buttonVariants()} when it should look like a button.
DrawerMenuItem keeps the drawer open unless closeOnSelect is set.
Use closeOnSelect="all" to close the current drawer and its parent drawers.
Links keep their normal navigation behavior.
Positions and styles
The root sets both the screen edge and the swipe direction. DrawerPopup
includes the portal, backdrop, and viewport.
Scrollable content
The panel scrolls while the title and footer stay in place. Native scrolling keeps long menus usable on small screens.
Snap point
Set snapPoint={0.6} to open a bottom drawer at 60% of the viewport height.
The popup’s CSS height limits the open size, so give it a larger height such as
h-[80dvh]. For a side drawer, use a length such as snapPoint="20rem".
The popup keeps a direct DrawerPanel within the visible area. A drawer has
one open snap point. A drag returns it to that point or closes it.
Nested drawers
Place a complete drawer inside the parent panel. Escape and swipe dismiss the front drawer first. Focus returns to its trigger. Rear drawers move back by one step per open child. Rounded drawers also become smaller. The front drawer’s height sets the visible edge, so drawers with different content heights keep the same spacing. Nested drawers share the outer drawer’s backdrop.
Nesting does not need a DrawerProvider or an extra wrapper around Drawer.
The data-slot controller tracks nested roots and supplies --nested-drawers,
--drawer-front-height, and --drawer-swipe-progress to the popup styles.
The optional data-slot drawer-provider slot controls a separate effect: it
moves the page surface inside drawer-indent when a drawer opens.
Drawer menus
Place DrawerMenuTrigger inside a nested Drawer to open a submenu panel.
It uses the drawer-trigger slot and adds a trailing chevron. Each submenu
uses the same drawer behavior: swipe, Escape, or Back closes the front panel
and returns focus to its trigger.
Use DrawerMenuGroup to group related items. Its optional label appears
above the items. Use DrawerMenuGroupLabel when the label needs its own
attributes. Keep a DrawerTitle and a close control in each panel.
No backdrop
Use modal={false} and showBackdrop={false} to keep the page visible and
usable. Add disablePointerDismissal when users need to use the page while
the drawer stays open. Keep a close button available.
<Drawer position="right" modal={false} disablePointerDismissal>
<DrawerTrigger>Design</DrawerTrigger>
<DrawerPopup showBackdrop={false} showCloseButton>
<DrawerHeader>
<DrawerTitle>Design</DrawerTitle>
</DrawerHeader>
<DrawerPanel>Design controls</DrawerPanel>
</DrawerPopup>
</Drawer>
Props
DrawerProps
defaultOpen?boolean
Initial open state when `open` is omitted (default: `data-default-open`, then `false`).
booleantriggerId?string | null
Initial associated trigger ID, taking precedence over `defaultTriggerId` when non-null.
string | nulldefaultTriggerId?string | null
Fallback initial trigger ID, before root trigger-ID attributes. Defaults to no associated trigger.
string | nullmodal?boolean | 'trap-focus'
`true` traps focus, makes outside content inert, and locks scrolling (default). `"trap-focus"` traps focus without inertness or scroll locking; `false` is non-modal. Also configurable with root `data-modal`.
boolean | 'trap-focus'closeOnEscape?boolean
Allow Escape to dismiss the drawer (default: `true`).
booleandisablePointerDismissal?boolean
Ignore outside pointer presses and non-modal focus-out dismissal (default: `false`).
booleansnapPoint?DrawerSnapPoint | null
Initial single open position. Use a viewport fraction, pixel number, or CSS length in `px` or `rem`. The popup size limits this value. Omit both snap props to use the popup's full size.
DrawerSnapPoint | nulldefaultSnapPoint?DrawerSnapPoint | null
Initial open position when `snapPoint` is not set.
DrawerSnapPoint | nullposition?'bottom' | 'top' | 'left' | 'right'
The position of the drawer.
'bottom' | 'top' | 'left' | 'right'"bottom"closeOnMedia?string
A media query string that will be used to close the drawer when the media query matches.
stringDrawerTriggerProps
target?string
The id of the drawer to target if using it from outside the drawer root
stringpayload?Record<string, unknown>
JSON serializable payload that willl be forwarded to the event listeners `drawer:beforeChange` and `drawer:change`
Record<string, unknown>DrawerPopupProps
keepMounted?boolean
Keep content at its portal destination while closed and hidden (default: `false`; portal `data-keep-mounted`).
booleancontainer?string
CSS selector for the portal destination.
string"body"initialFocus?boolean | string
CSS selector for focus when the drawer opens. Use `true` for the autofocus target or first focusable element, or `false` to skip focus. Omit this prop to focus the popup.
boolean | stringfinalFocus?boolean | string
CSS selector for focus when the drawer closes. Use `true` for the trigger or previous focus, or `false` to skip focus restoration. When omitted, the controller can preserve focus outside the drawer.
boolean | stringvariant?'default' | 'straight' | 'inset'
The variant of the drawer - `straight` - standard drawer attached to the edge of the viewport - `inset` - drawer inset from the top/side edges of the viewport, aka `floating`.
'default' | 'straight' | 'inset''default'showCloseButton?boolean
Show a close button in the top right corner of the drawer
booleanfalseshowBar?boolean
Show a drawer handle bar at the drawer edge, used for swipe gestures
booleanfalseshowBackdrop?boolean
Show a dark backdrop behind the drawer, overlaying the content
booleantrueDrawerMenuGroupProps
label?string
Text displayed above the controls and used to name the group.
stringDrawerMenuItemProps
closeOnSelect?boolean | 'all'
Close the current drawer on selection. Use `"all"` to close it and all its parent drawers. Link navigation keeps its normal behavior.
boolean | 'all'falsevariant?'default' | 'destructive'
Style for a standard or destructive action.
'default' | 'destructive''default'Events
Dispatch these events on the drawer root. The root emits the outbound events.
Outbound events
| Event | Detail |
|---|---|
drawer:beforechange |
{ open: boolean, reason: DrawerChangeReason, trigger: HTMLElement | null, payload: unknown, originalEvent?: Event, cancel(): void, preventUnmountOnClose(): void } |
drawer:change |
{ open: boolean, reason: DrawerChangeReason, trigger: HTMLElement | null, payload: unknown, originalEvent?: Event, cancel(): void, preventUnmountOnClose(): void } |
drawer:change-complete |
{ open: boolean } |
drawer:beforesnapchange |
{ snapPoint: number | string | null, reason: DrawerChangeReason, originalEvent?: Event, cancel(): void } |
drawer:snapchange |
{ snapPoint: number | string | null, reason: DrawerChangeReason, originalEvent?: Event, cancel(): void } |
drawer:beforechange and drawer:beforesnapchange are cancelable. Call event.preventDefault() or detail.cancel() to stop the change. DrawerChangeReason is trigger-press, close-press, outside-press, escape-key, focus-out, imperative-action, swipe, or none.
Inbound events
| Event | Detail |
|---|---|
drawer:set |
{ open?: boolean, snapPoint?: number | string | null, triggerId?: string } |
drawer:open |
none |
drawer:close |
none |
drawer:toggle |
none |
drawer:unmount |
none |
For a complete navigation block, see Mobile Menu.
When to use Drawer
Use Drawer for a short task or detail view that enters from a screen edge. Keep a title and explicit close control. Choose the direction that fits the page; a bottom drawer can work well for short mobile actions.
Keyboard and behavior
The current controller handles dragging, Escape, and modal focus. Keep a close button available so dragging is never the only way out. Each drawer has one open snap point.
Theme and layout
The drawer uses shared dialog colors and direction-specific motion. Test long content while dragging and scrolling on touch screens. A larger drawer needs a constrained body so controls remain reachable.