This documentation is under construction. Pages can be missing or change at any time. Follow it on GitHub.

Skip to content

Drawer

A panel that slides in from the edge of the screen.

Examples#

State#

By default, Drawer is an uncontrolled component: the browser keeps its state. The defaultOpen prop is the state that the server renders: a render with a new value opens or closes the drawer, and the controller keeps the value of the browser in data-phem-dialog-open-value.

Position#

The swipeDirection prop places the drawer, and gives the direction of the swipe that closes it. It defaults to "down" for bottom sheets. Use "up", "left", or "right" for other drawer positions.

Nested drawers#

Use the [data-nested-drawer-open] selector and the --nested-drawers CSS variable to style drawers when a nested drawer is open.

This demo stacks nested drawers using a constant peek so the frontmost drawer stays anchored to the bottom while the ones behind it are scaled down and lifted. It also uses the --drawer-height and --drawer-frontmost-height CSS variables to handle varying drawer heights.

Snap points#

Use snapPoints to snap a bottom sheet drawer to preset heights. Numbers between 0 and 1 represent fractions of the viewport height, and numbers greater than 1 are treated as pixel values. String values support px and rem units (for example, '148px' or '30rem'). Apply the snap point offset in your styles: translateY(calc(var(--drawer-snap-point-offset) + var(--drawer-swipe-movement-y))).

By default, the drawer can skip snap points when swiping quickly. Specify the snapToSequentialPoints prop to disable velocity-based skipping so the snap target is determined by drag distance (you can still drag past multiple points).

Virtual keyboard aware#

Set virtualKeyboard on a bottom sheet to make it react to software keyboards when it contains form controls. When the keyboard opens, the field with the focus scrolls into view, and the popup gets the height of the keyboard in --drawer-keyboard-inset.

Keep the popup frame stable: place header and footer content outside a plain scrollable body. If a footer contains its own input, reserve a footer slot below the scroll area and offset it by var(--drawer-keyboard-inset, 0px). Always include the 0px fallback: the variable is only set while the keyboard is open.

Indent effect#

Scale the background down when any drawer opens by wrapping your app in DrawerProvider and using DrawerIndentBackground and DrawerIndent at the top of your tree. Any Drawer within the provider activates the indent parts while it is open: they get the [data-active] attribute, and --drawer-swipe-progress during a swipe.

Non-modal#

Set modal={{ false }} to opt out of focus trapping and disablePointerDismissal to keep the drawer open on outside clicks.

Mobile navigation#

You can build a full-screen mobile navigation sheet using Drawer parts, including a flick-to-dismiss from the top gesture.

Swipe to open#

Place DrawerSwipeArea along the edge of the viewport to enable swipe-to-open gestures.

Close confirmation#

This example shows a nested confirmation dialog that opens if the text entered in the drawer is going to be discarded.

To implement this, give the confirmClose prop of the Drawer the handle of an AlertDialog in its popup. When the user changed a field, a request to close opens the alert dialog: the confirmation is automatically shown when the user clicks the backdrop, presses the Esc key, clicks a close button, or dismisses the drawer with a swipe gesture. Its DialogDiscard button closes the two and resets the text.

Action sheet#

This demo builds an action sheet with a grouped list of actions plus a separate destructive action button.

Detached triggers#

A drawer can be controlled by a trigger located either inside or outside the Drawer component. For simple, one-off interactions, place the DrawerTrigger inside Drawer.

However, if defining the drawer's content next to its trigger is not practical, you can use a detached trigger: give the same handle to the DrawerTrigger and to the Drawer. The drawer can render different content depending on which trigger opened it: give each trigger a payload, and the DrawerPayload part shows it.

API reference#

Import the components of the family:

use Phem\Ui\Drawer;
use Phem\Ui\DrawerViewport;
use function Phem\Ui\{DrawerTrigger, DrawerPopup, DrawerContent, DrawerTitle, DrawerDescription, DrawerClose, DrawerPayload, DrawerBackdrop, DrawerSwipeArea, DrawerProvider, DrawerIndentBackground, DrawerIndent};

Drawer#

A panel that slides in from an edge of the viewport, and closes with a swipe: Base UI Drawer.Root, on the phem-dialog controller with a swipe-direction.

The props of Dialog work the same: handle links detached triggers (a DrawerTrigger with the same handle), triggerId is the id of the trigger that opened it, confirmClose is the handle of an AlertDialog that asks before the close, and finalFocus is the selector of the element that gets the focus after the close. With modal={{ false }}, the page stays active and the focus is not trapped. With disablePointerDismissal, a press outside does not close it. With virtualKeyboard (Base UI Drawer.VirtualKeyboardProvider), the popup gets --drawer-keyboard-inset while the keyboard of the device is open.

Animate the open, the close, the swipes and the stack with CSS: the popup gets data-starting-style when it starts to open and data-ending-style when it starts to close. --nested-drawers is the depth in the stack (0 for the frontmost drawer), and --drawer-height and --drawer-frontmost-height align the drawers behind it when the heights are not the same. A drawer behind the frontmost one gets data-nested-drawer-open, and data-nested-drawer-swiping while the frontmost one moves. --drawer-swipe-movement-x, --drawer-swipe-movement-y and --drawer-snap-point-offset give the drag and snap offsets, data-swipe-direction with data-ending-style gives the direction of the close, --drawer-swipe-progress fades the backdrop, and --drawer-swipe-strength scales the duration of the release.

PropTypeDefault
swipeDirection'up' | 'down' | 'left' | 'right'SwipeDirection::Down
handle?stringnull
defaultOpenboolfalse
triggerId?stringnull
dismissiblebooltrue
disablePointerDismissalboolfalse
modalbooltrue
confirmClose?stringnull
finalFocus?stringnull
virtualKeyboardboolfalse
snapPointsarray[]
snapToSequentialPointsboolfalse
children?Htmlnull
...attrsarray[]

DrawerPopup#

The panel. Its ::backdrop is the Backdrop of Base UI: it fades with the swipe. In a DrawerViewport, it is a div, and the viewport has the ::backdrop. A drawer that is not modal is not in the top layer: z-50 puts it above the page, as the Portal of Base UI.

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerViewport#

The container that places the popup, as the Viewport of Base UI: a <dialog> that fills the screen, with the popup on the edge of the swipeDirection. Its ::backdrop is the Backdrop: it fades with the swipe. A tap on it outside the popup closes the drawer. With z-50, a drawer that is not modal (not in the top layer) is above the page.

PropTypeDefault
children?Htmlnull
...attrsarray[]

DrawerTrigger#

The button that opens the drawer. With a handle, it is a detached trigger of the Drawer with the same handle, and its payload is the text of the DrawerPayload.

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerContent#

The content of the panel: a centered column. A mouse press in it selects its text, and does not start a swipe.

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerTitle#

The heading of the drawer. It names the drawer (aria-labelledby).

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerDescription#

The text that describes the drawer (aria-describedby).

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerClose#

A button that closes the drawer.

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerPayload#

The text of the payload of the trigger that opened the drawer, or its children.

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerBackdrop#

The Backdrop of Base UI, as an element: for a drawer that is not modal (it has no ::backdrop), or in a container of the page. It fades with the swipe.

PropTypeDefault
...restmixed—

DrawerSwipeArea#

An area at the edge of the screen: a swipe away from the edge opens the drawer.

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerProvider#

The part of the page that the drawers indent, as the Provider of Base UI: put the DrawerIndentBackground and the DrawerIndent in it. While a drawer in it is open, the two parts get data-active, and --drawer-swipe-progress during its swipe.

PropTypeDefault
children?Htmlnull
...restmixed—

DrawerIndentBackground#

The background behind the DrawerIndent: it shows while the indent is smaller.

PropTypeDefault
...restmixed—

DrawerIndent#

The page that gets smaller while a drawer is open: style it with data-active.

PropTypeDefault
children?Htmlnull
...restmixed—

The source of the examples: examples/ui/Drawer.phem.

Phem
Esc
↑↓ to navigate↵ to openEsc to close