# Drawer

> A panel that slides in from a screen edge and dismisses with a swipe.

```tsx title="hero.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Drawer,
  DrawerBackdrop,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerPopup,
  DrawerPortal,
  DrawerTitle,
  DrawerTrigger,
  DrawerViewport,
} from '@langgenius/dify-ui/drawer'
import { IconButton } from '@langgenius/dify-ui/icon-button'

const details = [
  { label: 'App mode', value: 'Chatflow' },
  { label: 'Model', value: 'gpt-4o' },
  { label: 'Knowledge bases', value: 'Product docs, Refund policy' },
  { label: 'Service endpoint', value: '/v1/chat-messages', mono: true },
  { label: 'Rate limit', value: '60 requests / min', tabular: true },
  { label: 'Memory window', value: '12 messages', tabular: true },
]

export function DrawerHeroDemo() {
  return (
    <Drawer swipeDirection="right">
      <DrawerTrigger render={<Button />}>App details</DrawerTrigger>
      <DrawerPortal>
        <DrawerBackdrop />
        <DrawerViewport>
          <DrawerPopup>
            <div className="flex shrink-0 items-start gap-4 px-6 pt-6 pb-4">
              <div className="flex min-w-0 flex-1 flex-col gap-1">
                <DrawerTitle className="title-xl-semi-bold text-text-primary">
                  Customer support bot
                </DrawerTitle>
                <DrawerDescription className="system-sm-regular text-text-tertiary">
                  Published configuration for the active production release.
                </DrawerDescription>
              </div>
              <DrawerClose
                render={
                  <IconButton aria-label="Close drawer" size="lg">
                    <span aria-hidden="true" className="i-ri-close-line size-4" />
                  </IconButton>
                }
              />
            </div>
            <DrawerContent className="px-6 pt-2 pb-6">
              <dl className="divide-y divide-divider-subtle">
                {details.map((item) => (
                  <div
                    key={item.label}
                    className="flex flex-wrap items-baseline justify-between gap-x-4 gap-y-1 py-3 first:pt-0"
                  >
                    <dt className="system-xs-medium text-text-tertiary">{item.label}</dt>
                    <dd
                      className={`system-sm-medium text-text-primary ${item.mono ? 'font-mono' : ''} ${item.tabular ? 'tabular-nums' : ''}`}
                    >
                      {item.value}
                    </dd>
                  </div>
                ))}
              </dl>
            </DrawerContent>
            <div className="flex shrink-0 justify-end border-t-[0.5px] border-divider-subtle px-6 py-4">
              <DrawerClose render={<Button />}>Close</DrawerClose>
            </div>
          </DrawerPopup>
        </DrawerViewport>
      </DrawerPortal>
    </Drawer>
  )
}
```

## Usage guidelines

- Use `Drawer` for a side panel or bottom sheet that follows the drawer model: it slides in from an edge and the user can swipe it away. A centered modal task uses `Dialog`.
- Set `swipeDirection` to the edge the drawer dismisses toward. It defaults to `down` for a bottom sheet; `right` and `left` make full-height side panels, and `DrawerPopup` styles each direction from `data-swipe-direction`.
- Compose `DrawerPortal`, `DrawerBackdrop`, `DrawerViewport` and `DrawerPopup` explicitly. Put the panel's content in `DrawerContent`, which scrolls and lets mouse users select text without starting a swipe.
- Every drawer has a `DrawerTitle`, which names it. Add a `DrawerDescription` when a sentence of context helps.
- `DrawerClose` is unstyled. Compose it with `IconButton` and an `aria-label` for the corner close icon of a side panel, or with `Button` for a footer action. A bottom sheet with a handle needs no visible close icon; keep a visually hidden `DrawerClose` so screen reader users, who cannot swipe, still have a way out.
- Use `DrawerTrigger` under the root for a dedicated button. Items of a list open one drawer at the list by passing `payload`; a trigger outside the root connects through `createDrawerHandle`, and controlled `open` is for open state another owner must observe.
- Set `modal={false}` for a panel the user works beside, such as a detail view next to the page. Pair it with `disablePointerDismissal` when clicks on the page should not close it.

## Anatomy

```tsx
import {
  Drawer,
  DrawerBackdrop,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerPopup,
  DrawerPortal,
  DrawerTitle,
  DrawerTrigger,
  DrawerViewport,
} from '@langgenius/dify-ui/drawer'
```

```tsx
<Drawer>
  <DrawerTrigger />
  <DrawerPortal>
    <DrawerBackdrop />
    <DrawerViewport>
      <DrawerPopup>
        <DrawerContent>
          <DrawerTitle />
          <DrawerDescription />
          <DrawerClose />
        </DrawerContent>
      </DrawerPopup>
    </DrawerViewport>
  </DrawerPortal>
</Drawer>
```

`DrawerSwipeArea`, `DrawerProvider`, `DrawerIndent`, `DrawerIndentBackground` and `DrawerVirtualKeyboardProvider` add swipe-to-open, the indent effect and software keyboard handling from Base UI.

## Examples

### Bottom sheet

Without `swipeDirection`, the drawer is a bottom sheet that the user swipes down to dismiss. A decorative handle at the top hints at the gesture, so the visible close icon is dropped and a visually hidden `DrawerClose` remains for screen readers.

```tsx title="bottom-sheet.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Drawer,
  DrawerBackdrop,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerPopup,
  DrawerPortal,
  DrawerTitle,
  DrawerTrigger,
  DrawerViewport,
} from '@langgenius/dify-ui/drawer'

const channels = [
  { icon: 'i-ri-global-line', label: 'Public web app' },
  { icon: 'i-ri-code-s-slash-line', label: 'Embed on a website' },
  { icon: 'i-ri-terminal-box-line', label: 'Backend service API' },
]

export function DrawerBottomSheetDemo() {
  return (
    <Drawer>
      <DrawerTrigger render={<Button />}>Publish app</DrawerTrigger>
      <DrawerPortal>
        <DrawerBackdrop />
        <DrawerViewport>
          <DrawerPopup>
            <div
              aria-hidden="true"
              className="mx-auto mt-3 h-1 w-10 shrink-0 rounded-full bg-state-base-handle"
            />
            <div className="mx-auto flex w-full max-w-lg shrink-0 flex-col gap-1 px-6 pt-4 pb-3">
              <DrawerTitle className="title-xl-semi-bold text-text-primary">
                Publish Customer support bot
              </DrawerTitle>
              <DrawerDescription className="system-sm-regular text-text-tertiary">
                Choose where people reach the published version.
              </DrawerDescription>
              <DrawerClose className="sr-only">Close</DrawerClose>
            </div>
            <DrawerContent className="mx-auto w-full max-w-lg px-6 pt-1 pb-6">
              <div className="flex flex-col gap-2">
                {channels.map((channel) => (
                  <DrawerClose key={channel.label} render={<Button className="justify-start" />}>
                    <span aria-hidden="true" className={`${channel.icon} size-4`} />
                    {channel.label}
                  </DrawerClose>
                ))}
              </div>
            </DrawerContent>
          </DrawerPopup>
        </DrawerViewport>
      </DrawerPortal>
    </Drawer>
  )
}
```

### One drawer for a list of items

A list whose items open the same drawer renders one `Drawer` around the list. Each `DrawerTrigger` carries its item as `payload`, and the drawer reads the payload of the trigger that opened it.

```tsx title="payload.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Drawer,
  DrawerBackdrop,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerPopup,
  DrawerPortal,
  DrawerTitle,
  DrawerTrigger,
  DrawerViewport,
} from '@langgenius/dify-ui/drawer'
import { IconButton } from '@langgenius/dify-ui/icon-button'
import { StatusDot } from '@langgenius/dify-ui/status-dot'
import * as React from 'react'

type Run = {
  id: string
  startedAt: string
  status: 'Succeeded' | 'Failed'
  duration: string
  tokens: string
}

const runs: Run[] = [
  { id: '8f2c', startedAt: 'Today, 09:41', status: 'Succeeded', duration: '2.4s', tokens: '1,284' },
  { id: '7a19', startedAt: 'Today, 09:12', status: 'Failed', duration: '0.8s', tokens: '312' },
  {
    id: '6d03',
    startedAt: 'Yesterday, 18:05',
    status: 'Succeeded',
    duration: '3.1s',
    tokens: '2,047',
  },
]

function RunStatus({ status }: { status: Run['status'] }) {
  return (
    <span className="flex shrink-0 items-center gap-1.5 system-xs-medium text-text-secondary">
      <StatusDot status={status === 'Succeeded' ? 'success' : 'error'} size="small" />
      {status}
    </span>
  )
}

export function DrawerPayloadDemo() {
  return (
    <Drawer<Run> swipeDirection="right">
      {({ payload }) => (
        <React.Fragment>
          <ul className="flex w-full max-w-md flex-col divide-y divide-divider-subtle">
            {runs.map((run) => (
              <li key={run.id} className="flex items-center gap-3 py-2.5">
                <div className="flex min-w-0 flex-1 flex-col gap-0.5">
                  <span className="truncate system-sm-medium text-text-primary">Run {run.id}</span>
                  <span className="truncate system-xs-regular text-text-tertiary">
                    {run.startedAt}
                  </span>
                </div>
                <RunStatus status={run.status} />
                <DrawerTrigger<Run>
                  payload={run}
                  aria-label={`View run ${run.id}`}
                  render={<Button size="small" className="shrink-0" />}
                >
                  View
                </DrawerTrigger>
              </li>
            ))}
          </ul>
          <DrawerPortal>
            <DrawerBackdrop />
            <DrawerViewport>
              <DrawerPopup>
                <div className="flex shrink-0 items-start gap-4 px-6 pt-6 pb-4">
                  <div className="flex min-w-0 flex-1 flex-col gap-1">
                    <DrawerTitle className="title-xl-semi-bold text-text-primary">
                      Run {payload?.id}
                    </DrawerTitle>
                    <DrawerDescription className="system-sm-regular text-text-tertiary">
                      Started {payload?.startedAt}
                    </DrawerDescription>
                  </div>
                  <DrawerClose
                    render={
                      <IconButton aria-label="Close" size="lg">
                        <span aria-hidden="true" className="i-ri-close-line size-4" />
                      </IconButton>
                    }
                  />
                </div>
                <DrawerContent className="px-6 pb-6">
                  {payload && (
                    <dl className="grid grid-cols-2 gap-3 sm:grid-cols-3">
                      <div className="col-span-2 flex min-w-0 flex-col gap-1 rounded-xl bg-background-section-burn px-3 py-2.5 sm:col-span-1">
                        <dt className="system-xs-medium text-text-tertiary">Status</dt>
                        <dd>
                          <RunStatus status={payload.status} />
                        </dd>
                      </div>
                      <div className="flex min-w-0 flex-col gap-1 rounded-xl bg-background-section-burn px-3 py-2.5">
                        <dt className="system-xs-medium text-text-tertiary">Duration</dt>
                        <dd className="system-sm-medium text-text-primary tabular-nums">
                          {payload.duration}
                        </dd>
                      </div>
                      <div className="flex min-w-0 flex-col gap-1 rounded-xl bg-background-section-burn px-3 py-2.5">
                        <dt className="system-xs-medium text-text-tertiary">Tokens</dt>
                        <dd className="system-sm-medium text-text-primary tabular-nums">
                          {payload.tokens}
                        </dd>
                      </div>
                    </dl>
                  )}
                </DrawerContent>
              </DrawerPopup>
            </DrawerViewport>
          </DrawerPortal>
        </React.Fragment>
      )}
    </Drawer>
  )
}
```

### Non-modal panel

With `modal={false}` and `disablePointerDismissal`, the page behind stays interactive and clicks on it leave the panel open. Without a backdrop, the viewport passes pointer events through to the page.

```tsx title="non-modal.tsx"
import { Button } from '@langgenius/dify-ui/button'
import {
  Drawer,
  DrawerClose,
  DrawerContent,
  DrawerDescription,
  DrawerPopup,
  DrawerPortal,
  DrawerTitle,
  DrawerTrigger,
  DrawerViewport,
} from '@langgenius/dify-ui/drawer'
import { IconButton } from '@langgenius/dify-ui/icon-button'

const tools = [
  { name: 'search_web', description: 'Searches the web and returns the top results.' },
  { name: 'fetch_page', description: 'Reads the text content of a public URL.' },
  { name: 'summarize', description: 'Condenses a long passage into key points.' },
]

export function DrawerNonModalDemo() {
  return (
    <Drawer swipeDirection="right" modal={false} disablePointerDismissal>
      <DrawerTrigger render={<Button />}>Tool details</DrawerTrigger>
      <DrawerPortal>
        <DrawerViewport className="pointer-events-none">
          <DrawerPopup className="pointer-events-auto data-ending-style:shadow-none data-starting-style:shadow-none data-[swipe-direction=right]:top-2 data-[swipe-direction=right]:right-2 data-[swipe-direction=right]:bottom-2 data-[swipe-direction=right]:h-auto data-[swipe-direction=right]:w-100 data-[swipe-direction=right]:rounded-2xl data-ending-style:data-[swipe-direction=right]:transform-[translateX(calc(100%+1rem))] data-starting-style:data-[swipe-direction=right]:transform-[translateX(calc(100%+1rem))]">
            <div className="flex shrink-0 items-start gap-4 px-6 pt-6 pb-4">
              <div className="flex min-w-0 flex-1 flex-col gap-1">
                <DrawerTitle className="title-xl-semi-bold text-text-primary">
                  Web Research
                </DrawerTitle>
                <DrawerDescription className="system-sm-regular text-text-tertiary">
                  3 tools available to agents in this workspace.
                </DrawerDescription>
              </div>
              <DrawerClose
                render={
                  <IconButton aria-label="Close" size="lg">
                    <span aria-hidden="true" className="i-ri-close-line size-4" />
                  </IconButton>
                }
              />
            </div>
            <DrawerContent className="px-6 pb-6">
              <ul className="flex flex-col gap-2">
                {tools.map((tool) => (
                  <li key={tool.name} className="rounded-xl bg-background-section-burn px-3 py-2.5">
                    <p className="font-mono system-sm-medium text-text-primary">{tool.name}</p>
                    <p className="mt-0.5 system-xs-regular text-text-tertiary">
                      {tool.description}
                    </p>
                  </li>
                ))}
              </ul>
            </DrawerContent>
          </DrawerPopup>
        </DrawerViewport>
      </DrawerPortal>
    </Drawer>
  )
}
```

## Keyboard

| Key                    | Action                                                                                         |
| ---------------------- | ---------------------------------------------------------------------------------------------- |
| `Enter`, `Space`       | Opens the drawer from a focused trigger.                                                       |
| `Tab`, `Shift` + `Tab` | Moves focus between the drawer's controls. Focus stays inside a modal drawer while it is open. |
| `Escape`               | Closes the drawer and returns focus to the trigger.                                            |

## API reference

### Drawer

Groups all parts of a swipeable drawer or bottom sheet. Doesn't render its own HTML element.

Inherits `open`, `defaultOpen`, `modal`, `onOpenChange`, `onOpenChangeComplete`, `disablePointerDismissal`, `actionsRef`, `handle`, `triggerId`, `defaultTriggerId`, `children`, `swipeDirection`, `snapPoints`, `snapToSequentialPoints`, `snapPoint`, `defaultSnapPoint`, `onSnapPointChange` from [Base UI Drawer.Root](https://base-ui.com/react/components/drawer#root).

### DrawerBackdrop

An overlay displayed beneath the popup. Renders a `<div>` element.

Inherits `forceRender`, `className`, `render`, `style` from [Base UI Drawer.Backdrop](https://base-ui.com/react/components/drawer#backdrop).

**Data attributes**

| Attribute             | Description                                  |
| --------------------- | -------------------------------------------- |
| `data-open`           | Present when the drawer is open.             |
| `data-closed`         | Present when the drawer is closed.           |
| `data-starting-style` | Present when the drawer begins animating in. |
| `data-ending-style`   | Present when the drawer is animating out.    |

**CSS variables**

| Variable                  | Type     | Description                               |
| ------------------------- | -------- | ----------------------------------------- |
| `--drawer-swipe-progress` | `number` | The swipe progress of the drawer gesture. |

### DrawerClose

Closes the drawer when activated; unstyled so it can be composed via `render` with `Button` or `IconButton`. Renders a `<button>` element.

Inherits `className`, `render`, `style`, `nativeButton` from [Base UI Drawer.Close](https://base-ui.com/react/components/drawer#close).

**Data attributes**

| Attribute       | Description                               |
| --------------- | ----------------------------------------- |
| `data-disabled` | Whether the button is currently disabled. |

### DrawerContent

A container for the drawer contents. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Drawer.Content](https://base-ui.com/react/components/drawer#content).

### DrawerDescription

A paragraph with additional information about the drawer. Renders a `<p>` element.

Inherits `className`, `render`, `style` from [Base UI Drawer.Description](https://base-ui.com/react/components/drawer#description).

### DrawerIndent

A wrapper element intended to contain your app's main UI. Applies `data-active` when any drawer within the nearest `<Drawer.Provider>` is open. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Drawer.Indent](https://base-ui.com/react/components/drawer#indent).

**Data attributes**

| Attribute     | Description                                                        |
| ------------- | ------------------------------------------------------------------ |
| `data-active` | Whether any drawer within the nearest `<Drawer.Provider>` is open. |

### DrawerIndentBackground

An element placed before `<Drawer.Indent>` to render a background layer that can be styled based on whether any drawer is open. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Drawer.IndentBackground](https://base-ui.com/react/components/drawer#indentbackground).

**Data attributes**

| Attribute     | Description                                                        |
| ------------- | ------------------------------------------------------------------ |
| `data-active` | Whether any drawer within the nearest `<Drawer.Provider>` is open. |

### DrawerPopup

A container for the drawer contents. Renders a `<div>` element.

Inherits `className`, `render`, `initialFocus`, `finalFocus`, `style` from [Base UI Drawer.Popup](https://base-ui.com/react/components/drawer#popup).

**Data attributes**

| Attribute                    | Description                                                          |
| ---------------------------- | -------------------------------------------------------------------- |
| `data-open`                  | Present when the drawer is open.                                     |
| `data-closed`                | Present when the drawer is closed.                                   |
| `data-starting-style`        | Present when the drawer begins animating in.                         |
| `data-ending-style`          | Present when the drawer is animating out.                            |
| `data-expanded`              | Present when the drawer is at the expanded (full-height) snap point. |
| `data-nested-drawer-open`    | Present when a nested drawer is open.                                |
| `data-nested-drawer-swiping` | Present when a nested drawer is being swiped.                        |
| `data-swipe-dismiss`         | Present when the drawer is dismissed by swiping.                     |
| `data-swipe-direction`       | Indicates the swipe direction.                                       |
| `data-swiping`               | Present when the drawer is being swiped.                             |

**CSS variables**

| Variable                     | Type         | Description                                                                  |
| ---------------------------- | ------------ | ---------------------------------------------------------------------------- |
| `--nested-drawers`           | `number`     | The number of nested drawers that are currently open.                        |
| `--drawer-height`            | `CSS length` | The height of the drawer popup.                                              |
| `--drawer-frontmost-height`  | `CSS length` | The height of the frontmost open drawer in the current nested drawer stack.  |
| `--drawer-swipe-movement-x`  | `CSS length` | The swipe movement on the X axis.                                            |
| `--drawer-swipe-movement-y`  | `CSS length` | The swipe movement on the Y axis.                                            |
| `--drawer-snap-point-offset` | `CSS length` | The snap point offset used for translating the drawer.                       |
| `--drawer-swipe-strength`    | `number`     | A scalar (0.1-1) used to scale the swipe release transition duration in CSS. |

### DrawerPortal

A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to `<body>`. Renders a `<div>` element.

Inherits `keepMounted`, `container`, `className`, `render`, `style` from [Base UI Drawer.Portal](https://base-ui.com/react/components/drawer#portal).

### DrawerProvider

Provides a shared context for coordinating global Drawer UI, such as indent/background effects based on whether any Drawer is open. Doesn't render its own HTML element.

Inherits `children` from [Base UI Drawer.Provider](https://base-ui.com/react/components/drawer#provider).

### DrawerSwipeArea

An invisible area that listens for swipe gestures to open the drawer. Renders a `<div>` element.

Inherits `disabled`, `swipeDirection`, `className`, `render`, `style` from [Base UI Drawer.SwipeArea](https://base-ui.com/react/components/drawer#swipearea).

**Data attributes**

| Attribute              | Description                              |
| ---------------------- | ---------------------------------------- |
| `data-open`            | Present when the drawer is open.         |
| `data-closed`          | Present when the drawer is closed.       |
| `data-disabled`        | Present when the swipe area is disabled. |
| `data-swipe-direction` | Indicates the swipe direction.           |
| `data-swiping`         | Present when the drawer is being swiped. |

### DrawerTitle

A heading that labels the drawer. Renders an `<h2>` element.

Inherits `className`, `render`, `style` from [Base UI Drawer.Title](https://base-ui.com/react/components/drawer#title).

### DrawerTrigger

A button that opens the drawer. Renders a `<button>` element.

Inherits `handle`, `payload`, `id`, `className`, `render`, `style`, `nativeButton` from [Base UI Drawer.Trigger](https://base-ui.com/react/components/drawer#trigger).

**Data attributes**

| Attribute       | Description                                                          |
| --------------- | -------------------------------------------------------------------- |
| `data-disabled` | Whether the trigger is currently disabled.                           |
| `data-open`     | Whether the drawer is currently open and was opened by this trigger. |

### DrawerViewport

A positioning container for the drawer popup that can be made scrollable. Renders a `<div>` element.

Inherits `className`, `render`, `style` from [Base UI Drawer.Viewport](https://base-ui.com/react/components/drawer#viewport).

**Data attributes**

| Attribute             | Description                                              |
| --------------------- | -------------------------------------------------------- |
| `data-open`           | Present when the drawer is open.                         |
| `data-closed`         | Present when the drawer is closed.                       |
| `data-starting-style` | Present when the drawer begins animating in.             |
| `data-ending-style`   | Present when the drawer is animating out.                |
| `data-nested`         | Present when the drawer is nested within another drawer. |

**CSS variables**

| Variable                  | Type         | Description                                                                                                                                                     |
| ------------------------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `--drawer-keyboard-inset` | `CSS length` | The software keyboard inset, measured from the bottom edge of the layout viewport. Present only when the drawer is wrapped in `Drawer.VirtualKeyboardProvider`. |

### DrawerVirtualKeyboardProvider

Provides keyboard-aware focus and scroll handling for bottom-sheet drawers with form fields.

Inherits `children` from [Base UI Drawer.VirtualKeyboardProvider](https://base-ui.com/react/components/drawer#virtualkeyboardprovider).

### createDrawerHandle

Creates a new handle to connect a Drawer.Root with detached Drawer.Trigger components.

```ts
createDrawerHandle<Payload>(): BaseDrawer.Handle<Payload>
```

**Returns:** `BaseDrawer.Handle<Payload>`

**Return properties**

| Member            | Type                                  | Description                                                                                                                                                                  |
| ----------------- | ------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `open`            | `(triggerId: string \| null) => void` | Opens the dialog, optionally associating it with a trigger. This method should only be called in an event handler or an effect (not during rendering).                       |
| `openWithPayload` | `(payload: Payload) => void`          | Opens the dialog with the given payload, without associating it with any trigger. This method should only be called in an event handler or an effect (not during rendering). |
| `close`           | `() => void`                          | Closes the dialog. This method should only be called in an event handler or an effect (not during rendering).                                                                |
| `isOpen`          | `boolean`                             | Whether the dialog is currently open. Returns `false` while no root is attached to the handle.                                                                               |
