Drawer
A modal panel at the right edge for content that accompanies the page: filters for a list, the detail of a selected row.
- Kyoto · 12 to 19 Oct, 3 travellers
- Lisbon · 3 to 6 Mar, 2 travellers
- Hanoi · 20 to 27 Apr, 4 travellers
- Da Nang · 1 to 8 Aug, 2 travellers
Installation
npx shadcn@latest add @flyingsalmon/drawerUsage
import { Button, ButtonVariant } from '@/components/ui/button'
import {
Drawer,
DrawerBody,
DrawerClose,
DrawerContent,
DrawerDescription,
DrawerFooter,
DrawerTitle,
DrawerTrigger,
} from '@/components/ui/drawer'
export function DrawerUsage() {
return (
<Drawer>
<DrawerTrigger>
<Button variant={ButtonVariant.Outline}>Trip details</Button>
</DrawerTrigger>
<DrawerContent>
<DrawerTitle>Kyoto in autumn</DrawerTitle>
<DrawerDescription>12 to 19 October, 3 travellers.</DrawerDescription>
<DrawerBody>
Three nights at Ryokan Aoi, then on to Kanazawa.
</DrawerBody>
<DrawerFooter>
<DrawerClose>
<Button variant={ButtonVariant.Outline}>Close</Button>
</DrawerClose>
</DrawerFooter>
</DrawerContent>
</Drawer>
)
}Examples
The detail of a selected row
One drawer serves every row. The app holds the selected row and drives open, so there is no DrawerTrigger: the buttons in the table are plain buttons that happen to open it.
| Stop | Nights | |
|---|---|---|
| Kyoto | 3 | |
| Kanazawa | 2 | |
| Tokyo | 4 |
Closing on submit
Save closes the panel and the trip card shows the new name and dates. The card is where the traveller was already looking, so it carries the result.
Kyoto in autumn
Fit to content
fitContent grows the panel to the width of a table. Remove it and the same table scrolls inside the standard panel.
A form that survives a stray click
dismissible={false} ignores a click on the page beside the panel. Escape and the close button still work.
Waiting inside the drawer
pending holds every exit while the address is looked up, because the answer decides what happens next. A wrong address keeps the panel open with the error on the field.
Guidelines
When to use
- For filters on a list, so the list stays in view and changes while the traveller adjusts them.
- For the detail of one selected row, with the table still beside it.
- For a short edit form where the item being edited should stay visible.
When not to use
- Use Dialog to confirm an action or stop the page for a decision. A dialog sits in the centre and the page behind it stops mattering.
- Use Sidebar to move between sections of the app. Navigation belongs to the sidebar at every width, including the strip it collapses to on a narrow screen.
- Use Notice to show the result of an action. A result needs only to be seen, not answered.
Do and don’t
- DoClose a form drawer on submit and show the result on the item that changed.The item is still on screen beside the panel, so the updated trip is the clearest confirmation.
- DoShow one selected thing at a time. Choosing another row swaps the drawer’s content.The drawer is the detail of the current selection. A second panel would cover the list it belongs to.
- Don’tTurn a detail drawer into an editor. Its action opens a dialog for a change that reshapes the trip.Detail is for reading beside the list. A change with consequences needs the full stop of a dialog and its own confirm.
- Don’tPut the destructive confirmation of a trip in a drawer.A confirmation demands a decision, and a drawer invites the traveller to keep working on the page behind it.
Accessibility
| Key | Description |
|---|---|
| EnterSpace | On the trigger, opens the drawer and moves focus to its first control. |
| TabShift+Tab | Cycles through the controls inside the drawer. The close button comes last. |
| Escape | Closes the drawer and returns focus to the trigger. Does nothing while the drawer is pending. |
The drawer is a modal dialog. It is labelled by its required title and described by DrawerDescription when there is one, and it sets aria-busy while pending. Focus lands on the first body or footer control, never the close button, and returns to the trigger on close. Everything outside an open drawer is hidden from assistive technology.
A drawer opened from your own state has no DrawerTrigger, so the button that opens it is a plain button without aria-expanded.
API
Drawer
The root. It takes the dialog root's props except size, so every prop means what it means on a dialog.
| Prop | Type | Default | Description |
|---|---|---|---|
| open | boolean | – | The controlled open state. |
| defaultOpen | boolean | false | The initial open state when uncontrolled. |
| onOpenChange | (open: boolean) => void | – | Called when the drawer opens or closes. |
| dismissible | boolean | true | false ignores a click outside. Escape and the close button still work. |
| pending | boolean | false | Blocks Escape, a click outside, and the close button, disables the close button, and sets aria-busy. Keep it for an answer the next step depends on, such as looking up an invite; a plain save closes the drawer at once. |
DrawerContent
The panel. Renders the scrim and the close button for you.
| Prop | Type | Default | Description |
|---|---|---|---|
| fitContent | boolean | false | Grows the panel to the width of its body, between the standard width and the viewport. Loose prose has no widest layout but one long line and pushes the panel to its cap, so give a body like that its own width. |
| onOpenAutoFocus | (event: Event) => void | – | Runs as focus moves in. Prevent the default to place focus yourself. |
| onCloseAutoFocus | (event: Event) => void | – | Runs as focus returns. Prevent the default to send it elsewhere. |
DrawerTrigger and DrawerClose take one element, usually a Button. DrawerTitle is required. DrawerDescription, DrawerBody, and DrawerFooter are optional and take the props of the element they render, typed as DrawerTitleProps, DrawerDescriptionProps, DrawerBodyProps, and DrawerFooterProps. DrawerBody is the only part that scrolls.