> Leaf UI 0.17.3 · Package: @ifhover/leaf-ui · Source: c3221834f84f2f48481a2054026b713456f8e5dc
> Browser documentation: https://ifhover.github.io/leaf-ui/en/components/drawer.html

> For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/en/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/en/llms-full.txt.

# 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`.


View details

basic.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/drawer/basic.tsx"
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>
    </>
  );
}
```

Expand code


## Examples

### Placement

Use `width` for left and right drawers, and `height` for top and bottom drawers.


LeftRightTopBottom

placement.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/drawer/placement.tsx"
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>
    </>
  );
}
```

Expand code

### Footer submission

Associate the footer submit button with the form using its `form` attribute to retain native validation. Submission and closing are handled by the application.


Edit name

sections.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/drawer/sections.tsx"
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>
    </>
  );
}
```

Expand code

### 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.


Resize the drawer

resize.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/drawer/resize.tsx"
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>
    </>
  );
}
```

Expand code

### 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.


Open first drawer

nested.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/drawer/nested.tsx"
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>
    </>
  );
}
```

Expand code

### 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`.


Inspect details

tabs.tsx

Copy code

```tsx lineNumbers file="../../../src/examples/en/drawer/tabs.tsx"
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>
    </>
  );
}
```

Expand code

For content preservation and focus configuration, see [Modal advanced usage](https://ifhover.github.io/leaf-ui/en/components/modal.md#advanced-usage).


## API

### Drawer API

Configuration for controlled visibility, placement, content regions and dismissal.

| Property         | Type                                                                       | Default   | Description                                                                                                    |
| ---------------- | -------------------------------------------------------------------------- | --------- | -------------------------------------------------------------------------------------------------------------- |
| `open`           | `boolean`                                                                  | Required  | Controlled open state, updated by the application.                                                             |
| `title`          | `ReactNode`                                                                | —         | Drawer title.                                                                                                  |
| `description`    | `ReactNode`                                                                | —         | Additional description.                                                                                        |
| `tabs`           | [`DrawerTabsProps`](#drawertabs-api)                                       | —         | Embedded tabs; replace the heading when untitled or sit beneath it, retaining the tab bar bottom divider.      |
| `onClose`        | `() => void`                                                               | —         | Close request; update open in application state                                                                |
| `placement`      | `'left' \| 'right' \| 'top' \| 'bottom'`                                   | `'right'` | Opening edge                                                                                                   |
| `width`          | `number \| string`                                                         | `380`     | Width for left and right drawers                                                                               |
| `height`         | `number \| string`                                                         | `300`     | Height for top and bottom drawers                                                                              |
| `footer`         | `ReactNode`                                                                | —         | Footer content                                                                                                 |
| `extra`          | `ReactNode`                                                                | —         | Actions beside the title                                                                                       |
| `icon`           | `ReactNode`                                                                | —         | Decorative icon to the left of the heading.                                                                    |
| `divided`        | `boolean`                                                                  | `false`   | Separate the heading, content and footer with subtle borders.                                                  |
| `closable`       | `boolean`                                                                  | `true`    | Show the close button.                                                                                         |
| `maskClosable`   | `boolean`                                                                  | `true`    | Request closing when the mask is clicked.                                                                      |
| `keyboard`       | `boolean`                                                                  | `true`    | Allow Escape to request closing.                                                                               |
| `initialFocus`   | `RefObject<HTMLElement \| null> \| (() => HTMLElement \| null)`            | —         | Specify initial focus after opening.                                                                           |
| `returnFocus`    | `boolean \| RefObject<HTMLElement \| null> \| (() => HTMLElement \| null)` | —         | Restore focus on close; specify a destination or use false.                                                    |
| `resizable`      | `boolean`                                                                  | `false`   | Allow resizing by dragging or arrow keys.                                                                      |
| `minSize`        | `number`                                                                   | `240`     | Minimum size in pixels.                                                                                        |
| `maxSize`        | `number`                                                                   | `900`     | Maximum size in pixels.                                                                                        |
| `onResize`       | `(size: number) => void`                                                   | —         | Return the new size on pointer or keyboard resizing; changing width or height resets the manual size.          |
| `push`           | `boolean \| number`                                                        | `false`   | Push the nearest parent overlay; true uses 40px and a number specifies the distance.                           |
| `afterClose`     | `() => void`                                                               | —         | Called after the closing transition                                                                            |
| `destroyOnClose` | `boolean`                                                                  | `false`   | Unmount contents after closing; preserve takes precedence and forceRender keeps contents mounted.              |
| `preserve`       | `boolean`                                                                  | —         | Control content preservation, taking precedence over destroyOnClose; does not reset external controlled state. |
| `forceRender`    | `boolean`                                                                  | `false`   | Mount before opening and keep hidden while closed.                                                             |
| `container`      | `Element \| DocumentFragment \| (() => Element \| DocumentFragment)`       | —         | Portal mount target; may also return a ShadowRoot.                                                             |

### DrawerTabs API

See the [Tabs API
](tabs.html#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.
```ts
type DrawerTabsProps = Omit<TabsProps, 'type' | 'placement' | 'children'>;
```
