drawer

onChange called 0 time(s)

Delivery day

Thursday

import { state } from '@markless/core';import { drawer, lucide } from '@markless/ui';export default function DrawerBasic() @{	let day = state('Thursday');	<drawer.root class="drawer">		<div class="card">			<p class="card-label">Delivery day</p>			<p class="card-day">{day}</p>			<drawer.trigger class="button open">				<lucide.truck class="icon" aria-hidden="true" />				Change day			</drawer.trigger>		</div>		<drawer.backdrop class="backdrop">			<drawer.content class="sheet">				<drawer.title class="title">Pick a delivery day</drawer.title>				<drawer.description class="description">					Drag the sheet away, press Escape, or pick a day.				</drawer.description>				<div class="days">					<drawer.close						class="button"						data-current={day === 'Wednesday'}						onClick={() => {							day = 'Wednesday';						}}					>						Wednesday					</drawer.close>					<drawer.close						class="button"						data-current={day === 'Thursday'}						onClick={() => {							day = 'Thursday';						}}					>						Thursday					</drawer.close>					<drawer.close						class="button"						data-current={day === 'Friday'}						onClick={() => {							day = 'Friday';						}}					>						Friday					</drawer.close>				</div>			</drawer.content>		</drawer.backdrop>	</drawer.root>;}

Press Change day to open the sheet. Pick a day, press Escape, or drag the sheet down. Change orientation and start to bring it in from another edge.

A drawer is a dialog that slides in from one edge of the screen. A person can swipe it back out. Use it for a bottom sheet on a phone, or a side panel of filters or settings.

While the drawer is open, the page behind it is out of reach. A click, Tab or a screen reader cannot get to it, and it does not scroll. Escape, a press on the backdrop, a swipe or drawer.close puts the drawer away. Then focus goes back to the trigger.

Anatomy

drawer anatomy
Anatomy perspectiveFlat
Layers
Open detailsProject detailsA little more context.×
drawer.root

A dialog that slides in from an edge and can be swiped back out.

drawer.backdrop is the layer that covers the page, and it wraps drawer.content. The content is the sheet itself, with role="dialog". It is also the part a swipe moves. drawer.title names the dialog, and drawer.description describes it.

The family ships a little CSS in the markless layer. It pins the backdrop over the whole screen and pins the sheet to its edge. One plain rule of yours beats it, as Styling explains.

A side panel

A drawer comes up from the bottom by default. Set orientation to horizontal for a side panel. Add start to bring it from the other edge. Both follow the page's direction, so a right-to-left page gets the mirrored side with no extra prop.

<drawer.root orientation="horizontal" start>

Rest positions

snapPoints lists the places the drawer can stop. Each one is a fraction of the drawer's own size. A number above 1 counts as pixels. A slow swipe settles on the nearest rest position. A quick flick moves exactly one position.

<drawer.root snapPoints={[0.4, 1]} defaultSnapPoint={0.4}>

Swipe past the lowest position and let go, and the drawer closes. How far past is closeThreshold, a fraction of that lowest position. The default is 0.25.

Leave the page live

Set modal={false} and the page behind stays usable. The drawer still opens, swipes and closes.

<drawer.root modal={false}>

Keyboard

Opening the drawer moves focus onto the sheet. The arrow keys work only while the sheet itself has focus. Inside your own controls, the arrow keys still belong to those controls.

Key What it does
Escape Close the drawer and put focus back on the trigger.
Up or Down arrow On a bottom or top sheet, move one rest position more open or more closed.
Left or Right arrow The same, on a side panel.
Enter or Space Press the focused trigger or close button. Both are native buttons.

The arrows stop at the first and last rest position. With one rest position they do nothing.

API

Every prop is optional.

drawer.root

Prop Type What it does
open boolean Whether the drawer starts open. The default is false.
modal boolean The page behind is out of reach while the drawer is open. The default is true.
orientation 'horizontal' | 'vertical' vertical is a bottom sheet and the default. horizontal is a side panel.
start boolean Anchor the drawer at the start edge of its axis instead of the end edge.
snapPoints readonly number[] The rest positions. The default is [1], fully open. Order does not matter.
snapPoint number The rest position now. Passing it makes you the owner of the position.
defaultSnapPoint number Where the drawer starts when you do not pass snapPoint. The default is the largest.
closeThreshold number How far past the lowest rest position a swipe must go to close. The default is 0.25.
onChange (open: boolean) => void Called with the new value when the drawer opens or closes, by any route.
onSnapPointChange (snapPoint: number) => void Called with the rest position a swipe or a key settled on.

When you pass snapPoint, a swipe calls onSnapPointChange. The drawer does not move until you pass the new value back in.

Attribute When
ui-open The drawer is open.
ui-closed The drawer is closed.
ui-orientation Always. It holds vertical or horizontal.
ui-start start is set.
ui-dragging A swipe is moving the sheet. Turn your transition off while it is set.

Every other prop goes on the root <div>.

drawer.trigger

A <button> with aria-haspopup="dialog", aria-expanded and aria-controls. It writes ui-open and ui-closed. Your own onClick runs after the drawer has opened.

drawer.backdrop

A <div> that wraps drawer.content. It gets hidden while the drawer is closed. Your own onDismiss runs after the family has closed the drawer.

Attribute When
ui-backdrop Always. Style the dimmed layer with it.
ui-open, ui-closed The drawer is open or closed.
ui-orientation, ui-start The same as on the root.
Selector Default in the markless layer
[ui-backdrop] position: fixed; inset: 0

A press on the backdrop closes the drawer only if the press also ends there. A swipe that starts on the sheet and ends on the backdrop does not close it.

drawer.content

A <div role="dialog" tabindex="-1">, so it can hold focus without being a Tab stop. aria-modal="true" is set while modal is on. aria-labelledby points at drawer.title, and aria-describedby at drawer.description.

Render drawer.title and it names the drawer: the content points aria-labelledby at it, with no prop. VoiceOver and NVDA both read that name, on focus and on a walk, wherever the surface sits on the page. Use aria-label on drawer.content only when there is no title. With a title rendered, aria-labelledby outranks aria-label in the accessible name, so an aria-label beside a title changes nothing.

The family owns this element's style attribute. It writes --offset there: how far the sheet is pushed out, as a fraction of its size. Style the sheet with a class and a stylesheet, as the demo does with sheet.

Attribute When
ui-content Always.
ui-open, ui-closed The drawer is open or closed.
ui-orientation, ui-start The same as on the root.
ui-dragging A swipe is moving the sheet.
Selector Default in the markless layer
[ui-content] position: fixed, pinned to its edge, moved by translate with --offset
[ui-content] touch-action: none; overscroll-behavior: contain
[ui-content][ui-start], [ui-content][ui-orientation=horizontal] The same, pinned to the other edge or side

A press on any element inside the sheet never starts a swipe. Only a press on the sheet itself does.

drawer.title

An <h2>. Rendering it names the dialog.

drawer.description

A <p>. The dialog uses it as its description.

drawer.close

A <button>. Your own onClick runs after the drawer has closed.