Skip to the content
human-kit design
32 — Overlays

Drawer

A panel that floats in from an edge, over the page, and a swipe sends back.

Installation

pnpm dlx @human-kit/design add drawer
npx @human-kit/design add drawer
yarn dlx @human-kit/design add drawer
bunx @human-kit/design add drawer

The command copies the files to src/lib/components/ui/drawer. It also installs the packages that the files import.

Usage

<script lang="ts">
	import { buttonVariants } from '$lib/components/ui/button';
	import * as Drawer from '$lib/components/ui/drawer';
</script>

<Drawer.Root side="right">
	<Drawer.Trigger class={buttonVariants({ variant: 'outline' })}>Open patient</Drawer.Trigger>
	<Drawer.Content side="right">
		<Drawer.Header>
			<Drawer.Title>Laura Gómez</Drawer.Title>
		</Drawer.Header>
		<Drawer.Body>…</Drawer.Body>
		<Drawer.Footer>
			<Drawer.Close class={buttonVariants({ variant: 'outline' })}>Close</Drawer.Close>
		</Drawer.Footer>
	</Drawer.Content>
</Drawer.Root>

Examples

Snap points

snapPoints gives the panel places to rest between closed and fully open: a fraction of the screen, pixels, or any CSS length. This one opens at 40% and a drag up takes it to its full height.

Guidelines

  • Use a drawer for a record or a task that sits beside the page: a patient, an order, a filter. For a short question that must be answered before going on, use a dialog.
  • Hang it on the right for detail on a wide screen, and on the bottom on a phone, where the thumb reaches it and a swipe down puts it away.
  • Give the root and the content the same side. The root owns the drag and defaults to bottom; the content owns the look and defaults to right.
  • Use sm for a filter or a short form, md for a record, and lg for one with a table or two columns.
  • Put what scrolls in Drawer.Body, and leave the grab bar and the header outside it: a drag there moves the panel, a drag in the body selects text.
  • Always keep a way to close it with the keyboard, the close button or Drawer.Close: a swipe has no keyboard equivalent.

Accessibility

  • The content is a role="dialog" with aria-modal while open, named by Drawer.Title and described by Drawer.Description. Always give it a title.
  • While open the focus stays inside it, the page behind is hidden from a screen reader and does not scroll, on iOS Safari too.
  • The focus goes to the first control when it opens, and back to the trigger when it closes. The close button comes last in the code for that reason, though it sits in the corner.
  • Escape and a press outside close the topmost layer only: a popover open inside the drawer closes first.
  • The close button is named by closeLabel, "Close" unless you give another word. The grab bar is hidden from assistive technology.

API reference

Drawer.Root

PropTypeDefault
openboolean

Whether the drawer shows. Bindable.

—
defaultOpenboolean

Whether it starts open, when no open is bound.

false
onOpenChange(open: boolean) => void

Runs when it opens or closes.

—
controlledOpenboolean

Stops it writing back to open: it only reports through onOpenChange, so your code can refuse a change.

false
side'top' | 'right' | 'bottom' | 'left'

The edge it hangs on, and so the way a swipe sends it off. Give the content the same.

'bottom'
modalboolean | 'trap-focus'

How much of the page it takes: true holds the focus and stops the scroll, 'trap-focus' only holds the focus, false neither.

true
dismissibleboolean

Lets a swipe toward its edge close it.

true
shouldCloseOnEscapeboolean

Closes it with Escape.

true
shouldCloseOnInteractOutsideboolean

Closes it on a press outside the panel.

true
snapPointsreadonly (number | string)[]

Places to rest between closed and open: a fraction of the screen up to 1, pixels above it, or a CSS length.

—
snapPointnumber | string | null

The snap point it rests on. Bindable.

—
defaultSnapPointnumber | string | null

The snap point it opens on, when no snapPoint is bound.

—
onSnapPointChange(snapPoint: number | string | null, event?: Event) => void

Runs when it moves to another snap point.

—
snapToSequentialPointsboolean

Stops a fast flick at the next snap point instead of skipping one.

false
handleDrawerHandle

Opens it from an object made by createDrawerHandle(), for a trigger anywhere else in the page.

—
triggerRefHTMLElement | null

The element the focus goes back to on close. Drawer.Trigger sets it. Bindable.

—

It draws nothing of its own.

Drawer.Trigger

PropTypeDefault
handleDrawerHandle

Opens the drawer of that handle, from outside its root.

—
payloadunknown

A value handed to the root's children snippet when this trigger opens it.

—
elementHTMLButtonElement | null

The rendered button. Bindable.

—

Plus every attribute of a native <button> but type. It has no look: give it the classes of a button, buttonVariants({ variant: 'outline' }).

Drawer.Content

PropTypeDefault
side'top' | 'right' | 'bottom' | 'left'

The edge it is drawn on. At the bottom it gets a grab bar.

'right'
size'sm' | 'md' | 'lg'

How wide it gets. A side panel is 320, 384 or 512px wide. A sheet at the top or the bottom spans a phone and, from the sm width up, stands centred at most 448, 576 or 768px wide.

'md'
showCloseButtonboolean

Shows a close button in the top corner.

true
closeLabelstring

The name of the close button, read aloud.

'Close'
initialFocusHTMLElement | string | (() => HTMLElement | null | undefined)

What takes the focus when it opens, instead of the first control.

—
touchActionstring | null

The touch-action of the panel. A side panel defaults to pan-y, so its content still scrolls.

—

Plus every attribute of a <div>. It brings its own portal and the curtain over the page.

Drawer.Body

PropTypeDefault
classstring

Classes merged onto the body.

—

Plus every attribute of a <div>. The only part that scrolls, and the panel is not dragged from it.

Drawer.Header

PropTypeDefault
classstring

Classes merged onto the header.

—

Plus every attribute of a <div>. It holds the title and the description, and keeps room for the close button.

PropTypeDefault
classstring

Classes merged onto the footer.

—

Plus every attribute of a <div>. It sits at the foot of the panel; an empty footer is not drawn.

Drawer.Title

PropTypeDefault
classstring

Classes merged onto the title.

—

Plus every attribute of the heading it renders. It names the drawer.

Drawer.Description

PropTypeDefault
classstring

Classes merged onto the text.

—

Plus every attribute of the paragraph it renders. It describes the drawer.

Drawer.Close

PropTypeDefault
elementHTMLButtonElement | null

The rendered button. Bindable.

—

Plus every attribute of a native <button> but type. It closes the drawer it is in. It has no look: give it the classes of a button.