Drawer
A panel that floats in from an edge, over the page, and a swipe sends back.
Installation
pnpm dlx @human-kit/design add drawernpx @human-kit/design add draweryarn dlx @human-kit/design add drawerbunx @human-kit/design add drawerThe 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
rightfor detail on a wide screen, and on thebottomon 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 tobottom; the content owns the look and defaults toright. - Use
smfor a filter or a short form,mdfor a record, andlgfor 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"witharia-modalwhile open, named byDrawer.Titleand described byDrawer.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
| Prop | Type | Default |
|---|---|---|
open | boolean Whether the drawer shows. Bindable. | — |
defaultOpen | boolean Whether it starts open, when no | false |
onOpenChange | (open: boolean) => void Runs when it opens or closes. | — |
controlledOpen | boolean Stops it writing back to | 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' |
modal | boolean | 'trap-focus' How much of the page it takes: | true |
dismissible | boolean Lets a swipe toward its edge close it. | true |
shouldCloseOnEscape | boolean Closes it with Escape. | true |
shouldCloseOnInteractOutside | boolean Closes it on a press outside the panel. | true |
snapPoints | readonly (number | string)[] Places to rest between closed and open: a fraction of the screen up to | — |
snapPoint | number | string | null The snap point it rests on. Bindable. | — |
defaultSnapPoint | number | string | null The snap point it opens on, when no | — |
onSnapPointChange | (snapPoint: number | string | null, event?: Event) => void Runs when it moves to another snap point. | — |
snapToSequentialPoints | boolean Stops a fast flick at the next snap point instead of skipping one. | false |
handle | DrawerHandle Opens it from an object made by | — |
triggerRef | HTMLElement | null The element the focus goes back to on close. | — |
It draws nothing of its own.
Drawer.Trigger
| Prop | Type | Default |
|---|---|---|
handle | DrawerHandle Opens the drawer of that handle, from outside its root. | — |
payload | unknown A value handed to the root's | — |
element | HTMLButtonElement | 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
| Prop | Type | Default |
|---|---|---|
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 | 'md' |
showCloseButton | boolean Shows a close button in the top corner. | true |
closeLabel | string The name of the close button, read aloud. | 'Close' |
initialFocus | HTMLElement | string | (() => HTMLElement | null | undefined) What takes the focus when it opens, instead of the first control. | — |
touchAction | string | null The | — |
Plus every attribute of a <div>. It brings its own portal and the curtain over the page.
Drawer.Body
| Prop | Type | Default |
|---|---|---|
class | string 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
| Prop | Type | Default |
|---|---|---|
class | string 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.
Drawer.Footer
| Prop | Type | Default |
|---|---|---|
class | string 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
| Prop | Type | Default |
|---|---|---|
class | string Classes merged onto the title. | — |
Plus every attribute of the heading it renders. It names the drawer.
Drawer.Description
| Prop | Type | Default |
|---|---|---|
class | string Classes merged onto the text. | — |
Plus every attribute of the paragraph it renders. It describes the drawer.
Drawer.Close
| Prop | Type | Default |
|---|---|---|
element | HTMLButtonElement | 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.