Drawer
Open content from a page edge to view details or complete an action.
Basic usage
Handle onClose and update open in the application. Provide aria-label when omitting title.
import { Button, Drawer, Paragraph } from '@ifhover/leaf-ui';
import { useState } from 'react';
export function DrawerBasic() {
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>View details</Button>
<Drawer open={open} title="Document details" onClose={() => setOpen(false)}>
<Paragraph>
This document is maintained by the design team and was last updated this week.
</Paragraph>
</Drawer>
</>
);
}
Examples
Placement
Use width for left and right drawers, and height for top and bottom drawers.
import { Button, Drawer, type DrawerProps, Paragraph, Space } from '@ifhover/leaf-ui';
import { useState } from 'react';
const placements = [
{ value: 'left', label: 'Left' },
{ value: 'right', label: 'Right' },
{ value: 'top', label: 'Top' },
{ value: 'bottom', label: 'Bottom' },
] as const;
export function DrawerPlacement() {
const [open, setOpen] = useState(false);
const [placement, setPlacement] = useState<DrawerProps['placement']>('right');
return (
<>
<Space wrap>
{placements.map((item) => (
<Button
key={item.value}
onClick={() => {
setPlacement(item.value);
setOpen(true);
}}
>
{item.label}
</Button>
))}
</Space>
<Drawer
open={open}
placement={placement}
title="Document details"
onClose={() => setOpen(false)}
>
<Paragraph>View details from the selected edge of the page.</Paragraph>
</Drawer>
</>
);
}
Associate the footer submit button with the form using its form attribute to retain native validation. Submission and closing are handled by the application.
import { Button, Drawer, Form, FormField, Input, Space } from '@ifhover/leaf-ui';
import { useId, useState } from 'react';
export function DrawerSections() {
const [open, setOpen] = useState(false);
const formId = useId();
return (
<>
<Button onClick={() => setOpen(true)}>Edit name</Button>
<Drawer
open={open}
title="Edit name"
onClose={() => setOpen(false)}
footer={
<Space wrap>
<Button onClick={() => setOpen(false)}>Cancel</Button>
<Button variant="solid" type="submit" form={formId}>
Save
</Button>
</Space>
}
>
<Form
id={formId}
layout="vertical"
onSubmit={(event) => {
event.preventDefault();
setOpen(false);
}}
>
<FormField label="Name" required>
<Input name="name" defaultValue="Leaf UI" />
</FormField>
</Form>
</Drawer>
</>
);
}
Resizing
Enable resizable to drag the edge or focus the separator and use arrow keys. minSize and maxSize limit the resize value reported by onResize; the visible size remains constrained by the viewport. Changing width or height resets the manual adjustment.
import { Button, Drawer, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';
export function DrawerResize() {
const [open, setOpen] = useState(false);
const [size, setSize] = useState<number>();
return (
<>
<Button onClick={() => setOpen(true)}>Resize the drawer</Button>
<Drawer
open={open}
title="Resize"
onClose={() => setOpen(false)}
resizable
minSize={260}
maxSize={640}
onResize={setSize}
>
<Text tone="muted" role="status">
{size == null ? 'Not resized yet' : `Resized to: ${size}px`}
</Text>
</Drawer>
</>
);
}
Nested push
Set push on the child drawer to shift its nearest parent overlay. true means 40px, or pass a distance directly. Escape closes only the topmost drawer, then focus returns to its opener. Reset the child's open when closing the parent to avoid reopening both layers.
import { Button, Drawer, Paragraph } from '@ifhover/leaf-ui';
import { useState } from 'react';
export function DrawerNested() {
const [open, setOpen] = useState(false);
const [childOpen, setChildOpen] = useState(false);
const closeParent = () => {
setChildOpen(false);
setOpen(false);
};
return (
<>
<Button onClick={() => setOpen(true)}>Open first drawer</Button>
<Drawer open={open} title="First drawer" onClose={closeParent}>
<Button onClick={() => setChildOpen(true)}>Open second drawer</Button>
<Drawer
open={childOpen}
title="Second drawer"
width={300}
push
onClose={() => setChildOpen(false)}
>
<Paragraph>This is the content of the second drawer.</Paragraph>
</Drawer>
</Drawer>
</>
);
}
Tabbed drawer
Use tabs to embed line tabs beneath the heading or in place of it when untitled. The tab bar stays fixed above the scrolling body and always keeps its bottom divider even when divided={false} (the default). Omit item children when using the tabs solely to switch state for Drawer's children.
import { Button, Drawer, Text } from '@ifhover/leaf-ui';
import { useState } from 'react';
export function DrawerTabs() {
const [open, setOpen] = useState(false);
const [view, setView] = useState('overview');
return (
<>
<Button onClick={() => setOpen(true)}>Inspect details</Button>
<Drawer
open={open}
title="Task details"
onClose={() => setOpen(false)}
tabs={{
activeKey: view,
onChange: setView,
items: [
{ key: 'overview', label: 'Overview' },
{ key: 'logs', label: 'Logs' },
],
}}
>
<Text>
{view === 'overview'
? 'Omit item children to use the tab bar as a view switcher for the drawer body.'
: 'Showing the logs view while keeping the tab bar divider visible.'}
</Text>
</Drawer>
</>
);
}
For content preservation and focus configuration, see Modal advanced usage.
API
Drawer API
Configuration for controlled visibility, placement, content regions and dismissal.
DrawerTabs API
See the Tabs API (opens in a new tab) for embedded tab props. Drawer fixes type="line" and placement="top", keeps the tab bar fixed above the scrollable body with its bottom divider always visible regardless of divided={false}, and restores an ordinary drawer when items is empty.
type DrawerTabsProps = Omit<TabsProps, 'type' | 'placement' | 'children'>;