# Drawer

A panel that slides in from the side, or a bottom sheet on phones, for detail and edit views beside the page.

- Family: overlay
- Import: `import { Drawer } from "@senvenli/react/overlay";`
- Page: https://design.senvenli.com/docs/components/drawer
- JSON: https://design.senvenli.com/docs/components/drawer.json

## Example: A record beside the page

A title, a subtitle and the record's fields, from the inline end.

```tsx
import { Button } from "@senvenli/react/action";
import { DescriptionList } from "@senvenli/react/display";
import { DialogTrigger, Drawer } from "@senvenli/react/overlay";

export default function Example() {
  return (
    <DialogTrigger>
      <Button>View order</Button>
      <Drawer title="Order ORD-1040" description="Ada Lovelace · placed March 18">
        <DescriptionList
          items={[
            { label: "Status", value: "Shipped" },
            { label: "Total", value: "$248.00" },
            { label: "Tracking", value: "1Z 999 AA1 01 2345 6784", copyValue: "1Z999AA10123456784" },
            { label: "Note" },
          ]}
        />
      </Drawer>
    </DialogTrigger>
  );
}
```

## Examples

### Header actions and a footer

A status beside the close button, and sticky actions that close the drawer.

```tsx
import { Button } from "@senvenli/react/action";
import { Badge, Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { DialogTrigger, Drawer, toast, useDrawerClose } from "@senvenli/react/overlay";

// Rendered inside the drawer, so `useDrawerClose` reaches it.
function InvoiceActions() {
  const close = useDrawerClose();
  return (
    <Stack direction="horizontal" space="space-100" justify="end">
      <Button variant="subtle" onPress={() => close()}>
        Close
      </Button>
      <Button
        variant="primary"
        iconBefore="send"
        onPress={() => {
          toast.success("Reminder sent", { description: "INV-2038 · Northwind Traders" });
          close();
        }}
      >
        Send reminder
      </Button>
    </Stack>
  );
}

export default function Example() {
  return (
    <DialogTrigger>
      <Button>View invoice</Button>
      <Drawer
        size="large"
        title="Invoice INV-2038"
        description="Northwind Traders · due March 4"
        headerActions={<Badge tone="danger">Overdue</Badge>}
        footer={<InvoiceActions />}
      >
        <Text elementType="p">$1,920.00 has been outstanding for 14 days. Two reminders were sent, the last one on March 11.</Text>
      </Drawer>
    </DialogTrigger>
  );
}
```

### Unsaved changes

Closing asks first while there are edits; saving closes without the question.

```tsx
import { Button } from "@senvenli/react/action";
import { TextArea } from "@senvenli/react/form";
import { Stack } from "@senvenli/react/layout";
import { DialogTrigger, Drawer, useDrawerClose } from "@senvenli/react/overlay";
import { useState } from "react";

function SaveActions({ onSave }: { onSave: () => void }) {
  const close = useDrawerClose();
  return (
    <Stack direction="horizontal" space="space-100" justify="end">
      <Button variant="subtle" onPress={() => close()}>
        Cancel
      </Button>
      <Button
        variant="primary"
        onPress={() => {
          onSave();
          // The parent's `hasUnsavedChanges` has not re-rendered yet: skip the question.
          close({ force: true });
        }}
      >
        Save note
      </Button>
    </Stack>
  );
}

export default function Example() {
  const [saved, setSaved] = useState("Prefers delivery before noon.");
  const [draft, setDraft] = useState(saved);
  return (
    <DialogTrigger onOpenChange={(isOpen) => isOpen && setDraft(saved)}>
      <Button>Edit note</Button>
      <Drawer
        title="Customer note"
        description="Ada Lovelace"
        hasUnsavedChanges={draft !== saved}
        footer={<SaveActions onSave={() => setSaved(draft)} />}
      >
        <TextArea label="Note" description="Change the text, then try to close." value={draft} onChange={setDraft} />
      </Drawer>
    </DialogTrigger>
  );
}
```

### From any edge

`position`: start, end (default), top or bottom; the app decides, at every width.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { SegmentedControl } from "@senvenli/react/navigation";
import { DialogTrigger, Drawer, type DrawerPosition } from "@senvenli/react/overlay";
import { useState } from "react";

// The edge is the app's choice, at every width: `start` and `end` mirror in right-to-left.
export default function Example() {
  const [position, setPosition] = useState<DrawerPosition>("end");
  return (
    <Stack space="space-200" align="center">
      <SegmentedControl
        label="Position"
        items={[
          { id: "start", label: "Start" },
          { id: "end", label: "End" },
          { id: "top", label: "Top" },
          { id: "bottom", label: "Bottom" },
        ]}
        value={position}
        onChange={(value) => setPosition(value as DrawerPosition)}
      />
      <DialogTrigger>
        <Button>Open the drawer</Button>
        <Drawer position={position} title="Filters">
          <Text>This drawer came in from the {position}.</Text>
        </Drawer>
      </DialogTrigger>
    </Stack>
  );
}
```

### From the bottom

`position="bottom"` is a sheet that snaps between half and full height.

```tsx
import { Button } from "@senvenli/react/action";
import { DialogTrigger, Drawer } from "@senvenli/react/overlay";
import { RadioGroup } from "@senvenli/react/selection";

export default function Example() {
  return (
    <DialogTrigger>
      <Button iconBefore="arrow-up-down">Sort</Button>
      <Drawer position="bottom" title="Sort orders">
        <RadioGroup
          label="Sort by"
          defaultValue="newest"
          items={[
            { value: "newest", label: "Newest first" },
            { value: "oldest", label: "Oldest first" },
            { value: "total", label: "Highest total" },
          ]}
        />
      </Drawer>
    </DialogTrigger>
  );
}
```

## Guidelines

### When to use it

- Viewing or editing a record while keeping the list in view.

### Use something else when

- [dialog](https://design.senvenli.com/docs/components/dialog.md): The task needs full attention.

## Accessibility

Keyboard:

- Esc: Closes, asking first when there are unsaved changes.

Built in:

- Unsaved changes ask before closing in a real nested modal.
- The bottom sheet has a grip to expand or close.

You provide:

- A title.

## API reference

### Drawer

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| isOpen | `boolean` |  | no | Open without a DialogTrigger (a URL, a row click). Inside a DialogTrigger, put `onOpenChange` on the trigger instead: the trigger owns the state there. |
| defaultOpen | `boolean` |  | no |  |
| onOpenChange | `(isOpen: boolean) => void` |  | no |  |
| title | `React.ReactNode` |  | yes |  |
| description | `React.ReactNode` |  | no | Under the title: "Jordan Lee · 14–18 Oct". When given, the drawer is described by it (`aria-describedby`); without it (or with `""`, `false`, `null`) nothing is rendered. |
| headerActions | `React.ReactNode` |  | no | Beside the close button: a status Badge, a menu. |
| children | `React.ReactNode` |  | yes |  |
| footer | `React.ReactNode` |  | no | Sticky at the bottom: the record's actions. |
| size | `OverlaySize` |  | no | small 380px, large 560px (medium reads as large). |
| position | `DrawerPosition` |  | no | The edge it slides in from, at every width (the app decides, e.g. `bottom` on phones): `end` (default) and `start` are the inline sides and mirror in right-to-left; `bottom` is a sheet that snaps between half and full height; `top` drops down from above. |
| hasUnsavedChanges | `boolean` |  | no | Closing (button, Esc, scrim, useDrawerClose) asks first: "Unsaved changes". |
| motion | `ComponentMotion` |  | no | `auto` (follow the device), `reduce` or `none`, for this component; the provider's setting otherwise. |

### Tokens it reads

`--ds-blanket`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-easing-enter`, `--ds-easing-exit`, `--ds-radius-full`, `--ds-radius-xlarge`, `--ds-shadow-overlay`, `--ds-space-025`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-subtle`

## Related

- [dialog](https://design.senvenli.com/docs/components/dialog.md)
