# Copilot drawer

An in-app copilot beside the page: it shows what it reads as chips, rings each part on the page, lets you leave parts out, and docks or floats.

> chatcn Pro: needs a Pro license (https://chatcn.dev/pro). Install from the `@chatcn-pro` registry with your license token; setup at https://chatcn.dev/docs/chatcn-pro.

Example: Copilot drawer. A freight shipment held at customs, with the copilot docked beside it: it reads the page, then a charge you pick. Preview: https://chatcn.dev/preview/copilot-drawer-demo

## Installation

```bash
npx shadcn@latest add @chatcn-pro/copilot-drawer
```

## Usage

```tsx
import { CopilotDrawer, CopilotDrawerTrigger } from "@/components/ui/chatcn/pro/copilot-drawer"

<CopilotDrawer
  messages={messages}
  status={status}
  onSend={(text, { context }) => ask(text, context)}
  context={[
    { id: "invoice", label: "INV-2081", kind: "page", target: "summary" },
    { id: "lines", label: "Line items", target: "lines" },
  ]}
>
  <PageHeader actions={<CopilotDrawerTrigger />} />
  <section data-copilot-target="summary">…</section>
  <section data-copilot-target="lines">…</section>
</CopilotDrawer>
```

Wrap the page in it; the page keeps working while the drawer is open. Under the input, chips show exactly what the copilot will read. Pointing at a chip draws a ring around the matching `data-copilot-target` on the page, and the ring glides from part to part rather than blinking, so it is always clear where it looks. Clicking a chip leaves that part out (struck through, with an eye off mark), and `onSend` only receives what is left in. New context, such as a row the reader selects, flashes its ring once. The drawer either docks beside the page and pushes it or floats over it; on a narrow host it always floats as a full sheet. Drag the edge, or focus it and use the arrow keys, to resize; width and mode are remembered under `storageKey`. Mod I toggles it.

## Examples

### States

Example: States. Closed, open with starters, one part left out, floating over the page, a failed answer with Try again, and disabled. Preview: https://chatcn.dev/preview/copilot-drawer-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `children` | `ReactNode` | The host page |
| `messages` | `ChatcnMessage[]` | The thread, in the kit's message contract |
| `status`, `error`, `onStop`, `onRetry` | `"ready" | "submitted" | "streaming" | "error"` | Send becomes Stop while it streams; an error offers Try again |
| `value`, `defaultValue`, `onValueChange` | `string` | The draft, controlled or not |
| `onSend` | `(text, { context }) => void` | `context` holds only the items left in |
| `context` | `CopilotContextItem[]` | `{ id, label, detail?, kind?, target? }`; `target` matches `data-copilot-target` |
| `excluded`, `defaultExcluded`, `onExcludedChange` | `string[]` | Ids of the parts left out |
| `open`, `defaultOpen`, `onOpenChange` | `boolean` |  |
| `mode`, `defaultMode`, `onModeChange` | `"push" | "overlay"` | Docked or floating |
| `width`, `defaultWidth`, `onWidthChange`, `minWidth`, `maxWidth` | `number` | In px. Default 384 |
| `storageKey` | `string | null` | Where width and mode are remembered; null turns it off |
| `suggestions` | `string[]` | Starter questions in the empty state |
| `hotkey`, `hotkeyTarget` | `string | null` | Default `mod+i` |
| `disabled` | `boolean` |  |
| `labels` | `Partial<CopilotDrawerLabels>` | Every string, for translation |

## Keyboard

| Key | Action |
| --- | --- |
| Mod I | Open or close the drawer |
| Escape | Close it, focus back where it was opened from |
| Arrow toward the page, away from it | Resize by 16 px, with Shift by 64 px |
| Home, End, Enter on the edge | Narrowest, widest, back to the default width |
| Enter, Shift Enter | Send, new line |

## Accessibility

- The drawer is a labelled complementary region; opening it moves focus to the input and closing returns it.
- The edge is a separator with its current, minimum and maximum width.
- Each chip says what clicking does: Leave out Charges, or Include Charges.
- How many parts it reads is in words under the input, so the ring is never the only signal.

## Motion

The drawer slides in from the inline end on the kit's spring and the docked page makes room in the same motion. The page ring glides between parts in 220 ms. Under reduced motion the drawer fades, and the ring jumps.

Source: https://chatcn.dev/docs/components/copilot-drawer