Drawer
A modal panel on the start, end or bottom edge, or floating.
Demo
Usage
import { Button, Drawer } from '@tuttn/slant'
// The same parts and behaviour as Dialog, on the inline end by default. It
// keeps its own open state; pass open and onOpenChange to control it.
<Drawer trigger={<Button>View options…</Button>}>
<Drawer.Title>View options</Drawer.Title>
<Drawer.Body>…</Drawer.Body>
<Drawer.Footer><Drawer.Close variant="primary">Done</Drawer.Close></Drawer.Footer>
</Drawer>
// A bottom sheet: full width, top corners rounded, a grab bar.
<Drawer side="bottom" open={open} onOpenChange={setOpen}>…</Drawer>
// Floating: clear of its edge with a close button, and a touch drag
// towards the edge dismisses it.
<Drawer side="bottom" variant="floating" trigger={<Button>Quick actions…</Button>}>…</Drawer>
// Size it with a custom property: a side drawer's width, a bottom sheet's height cap.
<Drawer style={{ '--sl-drawer-size': '480px' } as CSSProperties}>…</Drawer>Props
| Prop | Type | Default | Description |
|---|---|---|---|
side | DrawerSide | 'end' | The screen edge it slides in from. `start` and `end` are inline edges, so they swap on a right-to-left page; `bottom` makes it a bottom sheet with a grab bar. |
variant | 'attached' | 'floating' | 'attached' | `attached` fills its edge. `floating` stands clear of it, rounded on every corner, with a close button in its top corner, and a touch drag towards its edge dismisses it. |
dismissLabel | string | messages.dismiss ('Dismiss') | The accessible name of the floating variant's close button. |
open | boolean | — | Whether it is open. Pass it with onOpenChange to control it; leave it out and it keeps its own state. |
defaultOpen | boolean | false | Whether it starts open, when uncontrolled. |
onOpenChange | (open: boolean) => void | — | Called with the new state whenever it opens or closes: the trigger, Escape, the backdrop, a Close part. |
role | 'dialog' | 'alertdialog' | 'dialog' | `alertdialog` for a question that must be answered: a press on the backdrop leaves it open, its Body becomes its description, and the Footer's answers sit side by side in equal columns. |
label | string | — | The accessible name, when no Title names it. |
initialFocus | RefObject<HTMLElement | null> | — | The element to focus on opening. By default the first focusable element inside, else the panel itself. |
trigger | ReactElement | — | An element that opens it when clicked (a Button, say), rendered just before it with `aria-haspopup="dialog"`. Its own handlers run first. Without it, open it through `open`. |
children | ReactNode | — | The content: Title, Body and Footer parts, or anything else. |
acknowledge | ReactNode | — | What the reader must tick before `Dialog.Confirm` answers, such as “I understand this deletes 14 files for good”. The Footer shows it as a checkbox in its own row above the answers, and Confirm stays unavailable until it is ticked. Meant for `role="alertdialog"`; it works on a plain dialog too. |
acknowledged | boolean | — | Whether the acknowledgement is ticked. Pass it with onAcknowledgedChange to control it; leave it out and it keeps its own state, which goes back to `defaultAcknowledged` each time the dialog opens. |
defaultAcknowledged | boolean | false | Whether the acknowledgement is ticked on opening, when uncontrolled. |
onAcknowledgedChange | (acknowledged: boolean) => void | — | Called with the new state when the acknowledgement is ticked or unticked, and when opening resets it. |
Drawer.Title | HTMLAttributes<HTMLHeadingElement> | — | Dialog.Title: an h2 that names the drawer (aria-labelledby), unless label is given. |
Drawer.Body | HTMLAttributes<HTMLDivElement> | — | Dialog.Body: the content, which fills the drawer’s height and scrolls. |
Drawer.Footer | HTMLAttributes<HTMLDivElement> | — | Dialog.Footer: the actions, at the inline end, pinned to the bottom. |
Drawer.Close | ButtonProps | — | Dialog.Close: a Slant Button that closes it. Its onClick runs first and can keep it open with preventDefault. |
…rest | HTMLAttributes<HTMLDialogElement> | — | className, style, other attributes and the ref go on the <dialog>. Each part forwards its ref and attributes to its own element. |
A drawer is a Dialog placed on an edge: the same native modal in the top layer, the same parts (Drawer.Title is Dialog.Title), focus moving in on opening, Tab staying inside, focus returning on closing, the page locked behind it, and Escape or a press on the backdrop closing it. A menu or popover inside renders inside it.
side picks the edge. start and end are inline edges: a full-height panel that swaps sides on a right-to-left page, slides in from that side, and is --sl-drawer-size wide (360px). bottom is a bottom sheet across the full width, no taller than --sl-drawer-size (85% of the viewport), with a grab bar. Under reduced motion it appears without sliding.
variant="floating" stands clear of its edge by 12px (16px on wider screens), rounded on every corner. It adds a close button in its top inline-end corner, named by dismissLabel, and on touch screens a drag from anywhere outside its Body carries it towards its edge: let go past about a third of its size, or flick it, and it closes as Escape would; otherwise it springs back. A drag that starts in the Body scrolls the Body instead.