Drawer
A panel that slides in from an edge of the screen, such as a side menu or a filter panel.
Usage
import { Drawer, Portal } from '@leverui/components';
<Drawer.Root>
<Drawer.Trigger />
<Portal>
<Drawer.Backdrop />
<Drawer.Positioner>
<Drawer.Content>
<Drawer.CloseTrigger />
<Drawer.Header>
<Drawer.Title />
</Drawer.Header>
<Drawer.Body />
<Drawer.Footer />
</Drawer.Content>
</Drawer.Positioner>
</Portal>
</Drawer.Root>
Examples
Controlled
Use the open and onOpenChange props to control the drawer component.
Sizes
Use the size prop to change the size of the drawer component.
Context
Use the Drawer.Context component to access the drawer state and methods from outside the drawer.
Offset
Use the padding CSS property on Drawer.Positioner to adjust the offset of the drawer component.
Placement
Use the placement prop to change the placement of the drawer component.
Initial Focus
Use the initialFocusEl prop to set the initial focus of the drawer component.
Without initialFocusEl, focus moves to the first element marked data-autofocus, falling back to the first tabbable element that is not marked data-no-autofocus.
<Input data-autofocus />
Custom Container
Here's an example of how to render the drawer component in a custom container.
Consider setting closeOnInteractOutside to false to prevent the drawer from closing when interacting outside the drawer.
Header Actions
Here's an example of rendering actions in the header of the drawer component.
Drawer with conditional variants
Here is an example of how to change variants based on the different breakpoints.
Open the drawer, then make the window as narrow as a phone.
This example uses the mdDown breakpoint to change the drawer's placement on smaller screens. This approach is recommended because both conditions are translated into CSS media queries, which helps avoid base style merging issues.
If you really want to use the base condition instead, you'll also need to define corresponding sizes. For example:
<Drawer.Root placement={{ base: 'bottom', md: 'end' }} size={{ base: 'xs', md: 'md' }}>
Non-Modal Drawer
We don't recommend using a non-modal drawer due to the accessibility concerns they present. In event you need it, here's what you can do:
- set the
modalprop tofalse - set
pointer-eventstononeon theDrawer.Positionercomponent - (optional) set the
closeOnInteractOutsideprop tofalse
Guides
Accessibility and focus
Works like a dialog: focus stays inside while open, and it closes the same
ways. Keep Title and Description inside the content for screen readers.
Content in a Portal renders elsewhere on the page. Set any
scoped theme variables there.
Action triggers
Drawer.ActionTrigger runs your click handler, then closes the drawer.
Shared defaults
Drawer.RootPropsProvider sets default props for the drawers inside it. A prop
on a drawer wins.
Contained drawers
contained leaves a gap around the drawer and rounds its corners. Responsive,
with the same breakpoint arrays and objects as size and placement.
true applies across its whole range, and a later false does not cancel a
true that still matches. To stop the gap at a breakpoint, use a range that
ends there.
<Drawer.Root contained={{ mdDown: true }}>{/* Drawer parts */}</Drawer.Root>
Props
Root
| Prop | Default | Type | Description |
|---|---|---|---|
closeOnEscape | true | boolean | Close on Escape. |
closeOnInteractOutside | true | boolean | Close on outside click. |
lazyMount | true | boolean | Render the content on first open. |
modal | true | boolean | Block outside clicks and hide all content below. |
preventScroll | true | boolean | Stop the page behind from scrolling while open. |
role | 'dialog' | 'dialog' | 'alertdialog' | ARIA role. |
trapFocus | true | boolean | Keep keyboard focus inside while open. |
unmountOnExit | true | boolean | Remove the content from the page on close. |
size | 'xs' | 'xs' | 'sm' | 'md' | 'lg' | 'xl' | 'full' | Drawer size. |
placement | 'end' | 'start' | 'end' | 'top' | 'bottom' | Screen edge the drawer slides in from. |
unstyled | — | boolean | Turn off the Lever UI styles. |
aria-label | — | string | Screen reader label. Use when there is no title. |
defaultOpen | false | boolean | Open on first render. Uncontrolled. |
defaultTriggerValue | — | string | Active trigger on first render. Uncontrolled. |
finalFocusEl | — | () => MaybeElement | Element focused on close. |
id | — | string | Unique id of the drawer's state machine. |
ids | — | Element ID map | Ids of the drawer's elements. For use with other components. |
immediate | — | boolean | Apply present changes right away, not on the next frame. |
initialFocusEl | — | () => MaybeElement | Element focused on open. |
onEscapeKeyDown | — | (event: KeyboardEvent) => void | Called on Escape. |
onExitComplete | — | VoidFunction | Called when the closing animation ends. |
onFocusOutside | — | (event: FocusOutsideEvent) => void | Called when focus moves outside. |
onInteractOutside | — | (event: InteractOutsideEvent) => void | Called on an interaction outside. |
onOpenChange | — | (details: OpenChangeDetails) => void | Called when the drawer opens or closes. |
onPointerDownOutside | — | (event: PointerDownOutsideEvent) => void | Called on pointer down outside. |
onRequestDismiss | — | (event: LayerDismissEvent) => void | Called when a parent layer closing closes the drawer. |
onTriggerValueChange | — | (details: TriggerValueChangeDetails) => void | Called when the active trigger changes. |
open | — | boolean | Open state. Controlled. |
persistentElements | — | (() => Element | null)[] | Elements that stay clickable and do not close the drawer. |
present | — | boolean | Whether the drawer is present. Controlled. |
restoreFocus | — | boolean | Return focus to the element that had it before opening. |
skipAnimationOnMount | false | boolean | Skip the opening animation when starting open. |
triggerValue | — | string | Active trigger. Controlled. |
contained | — | boolean | Leave a gap around the drawer and round its corners. |
Customize with CSS
Load the package stylesheet first. The root takes only
the listed size, placement, and contained values, plus unstyled. It
does not take a recipe object.
Every part has a fixed .leverui-drawer__* class. To style one drawer, use
class or Solid's style prop.
<Drawer.Content class="settings-drawer">Settings</Drawer.Content>
.settings-drawer {
--leverui-drawer-background: #f7f9ff;
}