Drawer
A panel that slides in from the edge of the screen and can be swiped away, for details beside a list or a sheet on a phone. Built on ui-sdk’s Drawer.
Usage
import {
Drawer,
DrawerBody,
DrawerCloseButton,
DrawerContent,
DrawerHeader,
DrawerTitle,
DrawerTrigger,
} from 'nyctoui/drawer'
<Drawer side="right">
<DrawerTrigger render={<Button />}>Open issue</DrawerTrigger>
<DrawerContent>
<DrawerHeader>
<DrawerTitle>ENG-142 Offline mode for drafts</DrawerTitle>
<DrawerCloseButton />
</DrawerHeader>
<DrawerBody>…</DrawerBody>
</DrawerContent>
</Drawer>The panel floats 8px from the screen edges with the same surface, border and radius as a dialog. It shares the dialog’s layout parts, so a header, a body and a footer look alike in both.
Drag or swipe the panel back toward its edge to close it; a quick flick closes it faster. Esc and a tap on the backdrop work too. With reduced motion the panel appears without sliding.
Good for
- An issue’s details sliding in beside the list in a project tool, while the list stays visible.
- Filters on a phone in a shopping app, as a bottom sheet.
- A shopping cart that slides in from the side of a store.
- A row inspector in an admin table, with the record’s fields and history.
Anatomy
<Drawer>
<DrawerTrigger />
<DrawerContent>
<DrawerHeader>
<DrawerTitle />
<DrawerCloseButton />
<DrawerDescription />
</DrawerHeader>
<DrawerBody />
<DrawerFooter>
<DrawerClose />
</DrawerFooter>
</DrawerContent>
</Drawer>Examples
Left
side="left" suits navigation on a small screen. Close the drawer when a link is followed.
Bottom sheet
side="bottom" is a sheet with a grab handle, for a short list of choices on a phone, where the thumb can reach them. Swipe it down to dismiss.
With actions
A DrawerFooter stays pinned to the bottom of the panel while the body scrolls.
Sizes
size sets the width of a side drawer. A bottom drawer spans the screen, up to 640px.
Best practices
- Use it for detail that belongs to a list, so people keep their place in the list behind it.
- Use
side="bottom"for a sheet on phones, where a side drawer leaves little room. - Give every drawer a visible close, such as
DrawerCloseButton, especially when it isn’tmodaland there’s no backdrop to click. - Keep the last item’s content in place while the drawer slides out, so it doesn’t go blank on the way.
- Set
disablePointerDismissalwhen the drawer holds a form.
- Use a drawer for a short confirmation or a two-field form. That’s a dialog.
- Open a drawer from inside another drawer.
- Use a drawer for content that should push the page aside. That’s a resizable panel or a sidebar layout.
API reference
Drawer
Holds the open state and the swipe gesture. Renders no element.
| Prop | Type | Default |
|---|---|---|
sideThe edge the panel slides in from and is swiped back to. | 'left' | 'right' | 'bottom' | 'right' |
defaultOpenStarts open. For uncontrolled use. | boolean | false |
openWhether the drawer is open. For controlled use. | boolean | — |
onOpenChangeCalled when the drawer opens or closes. | (open: boolean, details) => void | — |
modalTraps focus, locks page scroll and blocks the page. false leaves the page usable. | boolean | 'trap-focus' | true |
disablePointerDismissalKeeps the drawer open on a tap outside. | boolean | false |
DrawerTrigger
The button that opens the drawer. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
DrawerContent
The panel, with the backdrop behind it. Renders a <div> in the top layer.
| Prop | Type | Default |
|---|---|---|
sizeWidth of a side drawer: 320, 400 or 560px. | 'sm' | 'md' | 'lg' | 'md' |
containerElement to render into instead of the page body, such as a Phone screen. | HTMLElement | RefObject | null | — |
initialFocusThe element to focus on open. Defaults to the first focusable one. | boolean | RefObject | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
DrawerHeader
Lays out the title, description and close button. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
DrawerTitle
Labels the drawer. Renders an <h2>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
DrawerDescription
Describes the drawer to assistive tech. Renders a <p>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
DrawerBody
Fills the panel and scrolls. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
DrawerFooter
Actions pinned to the bottom, aligned to the end. Renders a <div>.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
DrawerClose
Closes the drawer. Renders a <button>.
| Prop | Type | Default |
|---|---|---|
renderReplaces the rendered element, e.g. render={<Button />}. | ReactElement | function | — |
DrawerCloseButton
A ghost icon Button with an X that closes the drawer.
| Prop | Type | Default |
|---|---|---|
aria-labelThe accessible name of the button. | string | 'Close' |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |