Drawer

A panel that slides in from an edge of the screen, such as a side menu or a filter panel.

Usage

Code
import { Drawer, Portal } from '@leverui/components';
Code
<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.

Code
<Input data-autofocus />

Custom Container

Here's an example of how to render the drawer component in a custom container.

Render drawer here

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 modal prop to false
  • set pointer-events to none on the Drawer.Positioner component
  • (optional) set the closeOnInteractOutside prop to false

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.

Code
<Drawer.Root contained={{ mdDown: true }}>{/* Drawer parts */}</Drawer.Root>

Props

Root

PropDefaultTypeDescription
closeOnEscapetruebooleanClose on Escape.
closeOnInteractOutsidetruebooleanClose on outside click.
lazyMounttruebooleanRender the content on first open.
modaltruebooleanBlock outside clicks and hide all content below.
preventScrolltruebooleanStop the page behind from scrolling while open.
role'dialog''dialog' | 'alertdialog'ARIA role.
trapFocustruebooleanKeep keyboard focus inside while open.
unmountOnExittruebooleanRemove 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—booleanTurn off the Lever UI styles.
aria-label—stringScreen reader label. Use when there is no title.
defaultOpenfalsebooleanOpen on first render. Uncontrolled.
defaultTriggerValue—stringActive trigger on first render. Uncontrolled.
finalFocusEl—() => MaybeElementElement focused on close.
id—stringUnique id of the drawer's state machine.
ids—Element ID mapIds of the drawer's elements. For use with other components.
immediate—booleanApply present changes right away, not on the next frame.
initialFocusEl—() => MaybeElementElement focused on open.
onEscapeKeyDown—(event: KeyboardEvent) => voidCalled on Escape.
onExitComplete—VoidFunctionCalled when the closing animation ends.
onFocusOutside—(event: FocusOutsideEvent) => voidCalled when focus moves outside.
onInteractOutside—(event: InteractOutsideEvent) => voidCalled on an interaction outside.
onOpenChange—(details: OpenChangeDetails) => voidCalled when the drawer opens or closes.
onPointerDownOutside—(event: PointerDownOutsideEvent) => voidCalled on pointer down outside.
onRequestDismiss—(event: LayerDismissEvent) => voidCalled when a parent layer closing closes the drawer.
onTriggerValueChange—(details: TriggerValueChangeDetails) => voidCalled when the active trigger changes.
open—booleanOpen state. Controlled.
persistentElements—(() => Element | null)[]Elements that stay clickable and do not close the drawer.
present—booleanWhether the drawer is present. Controlled.
restoreFocus—booleanReturn focus to the element that had it before opening.
skipAnimationOnMountfalsebooleanSkip the opening animation when starting open.
triggerValue—stringActive trigger. Controlled.
contained—booleanLeave 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.

Code
<Drawer.Content class="settings-drawer">Settings</Drawer.Content>
Code
.settings-drawer {
  --leverui-drawer-background: #f7f9ff;
}