# ChatDock

> A small chat window that floats in the bottom-right corner over any page, so a person can talk to the assistant without leaving their work. Ctrl+J (Cmd+J on a Mac) opens and closes it from anywhere, a text field included. It is not modal: there is no scrim (the dimmed layer a Dialog puts over the page), and the page behind it keeps working.

- **Best for:** A chat with the assistant, one keystroke away on every page of an app.
- **Not for:** A page whose main job is the conversation: put a ChatThread and a PromptInput in the page itself.
- Organism. Source: `src/components/organisms/chat-dock/ChatDock.tsx`
- Rendered docs page: `/components/chat-dock`

## Import

```tsx
import { ChatDock } from 'src/components/organisms/chat-dock/ChatDock'
```

Paths are from the Division DS repository root. Installed with shadcn they start `src/division-ds/` instead of `src/` (`src/division-ds/components/...`); import them relative to your file. An install has no index barrels: import each component from its own file, never from `components/molecules`.

## Requires

- A parent that holds open (and minimized, if it can be minimized) in state, mounted once near the root of the app so Ctrl+J works on every page.
- A ChatThread with className="min-h-0 flex-1" as the child, so the thread scrolls and the dock keeps its size.

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `open` | `boolean` |  | Required. Whether the dock is shown. Closed, it renders nothing. |
| `onOpenChange` | `(open: boolean) => void` |  | Required. Called with false by Close, Escape and Ctrl+J, and with true by Ctrl+J while the dock is closed. |
| `title` | `string` |  | Required. The visible heading and the dock's accessible name: "Assistant", or the chat's own title. |
| `footer` | `ReactNode` |  | Required. Pinned under the body: usually a PromptInput. Pass null for a dock with nothing to type in. |
| `actions` | `ReactNode` |  | Controls in the header bar, before Minimize and Close: New chat, Open in window. Keep it to one or two icon buttons. |
| `minimized` | `boolean` | `false` | Only the header bar shows. The body and the footer stay mounted and hidden, so a draft and the scroll position survive. |
| `onMinimizedChange` | `(minimized: boolean) => void` |  | Shows the Minimize button, which turns into Restore. Ctrl+J on a minimized dock restores it, and a dock closed while minimized is called with false when it opens again, so it opens restored. Leave it out and the dock cannot be minimized. |
| `shortcut` | `boolean` | `true` | Ctrl+J (Cmd+J on a Mac) opens and closes the dock from anywhere, a text field included. The key is left alone inside a modal dialog, and in a field whose own keydown handler called preventDefault (a terminal). Turn it off when a second dock or another feature on the page owns the key. |
| `className` | `string` |  | Merged with the dock classes. When Tailwind utility classes clash, the later one wins: this is where a different size or offset goes. |
| `ref` | `Ref<HTMLElement>` |  | The section element. |

## Examples

Each is compiled and rendered on the docs page. Imports are omitted; see above.

### Over the page, bottom right: open it with the button or Ctrl+J, minimize it, and send a message

```tsx
type Turn = { id: number; role: 'user' | 'assistant'; text: string }

const GREETING: Turn[] = [
  { id: 1, role: 'user', text: 'Which replicas are running low at Depot 4?' },
  {
    id: 2,
    role: 'assistant',
    text: 'Two lines are under their reorder point: the K-41 field radio has 6 left against 10, and the trail pack has 3 left against 8.',
  },
]

const REPLY = 'Noted. The reorder for both lines is drafted; it goes to the supplier once someone approves it.'

function AssistantDock() {
  const [open, setOpen] = useState(false)
  const [minimized, setMinimized] = useState(false)
  const [turns, setTurns] = useState<Turn[]>(GREETING)
  const [draft, setDraft] = useState('')
  const [busy, setBusy] = useState(false)
  const nextId = useRef(GREETING.length + 1)

  // The reply arrives a moment after the question, as it would from a real assistant.
  useEffect(() => {
    if (!busy) return
    const timer = window.setTimeout(() => {
      setTurns(list => [...list, { id: nextId.current++, role: 'assistant', text: REPLY }])
      setBusy(false)
    }, 900)
    return () => window.clearTimeout(timer)
  }, [busy])

  const send = (text: string) => {
    setTurns(list => [...list, { id: nextId.current++, role: 'user', text }])
    setDraft('')
    setBusy(true)
  }

  return (
    <div className="flex flex-wrap items-center gap-3">
      <Button variant="secondary" aria-keyshortcuts="Control+J Meta+J" onClick={() => setOpen(true)}>
        Open chat
      </Button>
      <span aria-hidden="true" className="inline-flex gap-1">
        <Kbd>Ctrl</Kbd>
        <Kbd>J</Kbd>
      </span>
      <ChatDock
        open={open}
        onOpenChange={setOpen}
        minimized={minimized}
        onMinimizedChange={setMinimized}
        title="Assistant"
        actions={
          <IconButton
            label="New chat"
            variant="ghost"
            size="sm"
            onClick={() => {
              setTurns([])
              setBusy(false)
            }}
          >
            <SquarePen aria-hidden="true" className="h-4 w-4" />
          </IconButton>
        }
        footer={
          <PromptInput
            label="Message the assistant"
            placeholder="Ask about stock or orders"
            value={draft}
            onValueChange={setDraft}
            onSubmit={send}
            busy={busy}
            onStop={() => setBusy(false)}
          />
        }
      >
        <ChatThread label="Conversation with the assistant" busy={busy} className="min-h-0 flex-1">
          {turns.map(turn => (
            <ChatMessage key={turn.id} role={turn.role} author={turn.role === 'user' ? 'You' : 'Assistant'}>
              <p>{turn.text}</p>
            </ChatMessage>
          ))}
        </ChatThread>
      </ChatDock>
    </div>
  )
}

<AssistantDock />
```

## Keyboard

| Keys | Result |
|---|---|
| `Ctrl` `J` | Opens the dock and puts focus in its text box; closes it again, from anywhere, a text field included. On a minimized dock, restores it. Inside a modal dialog, or in a field that handles the key itself, it does nothing. Cmd+J on a Mac. |
| `Esc` | Inside the dock, closes it and sends focus back to where it was before it opened. While a reply is busy, Esc in the PromptInput stops the reply instead. |
| `Tab` | Moves through the dock and on into the page: the dock traps nothing. |

## Do

- Mount one dock near the root of the app, so the conversation and Ctrl+J survive moving between pages.
- Put a ChatThread in it with className="min-h-0 flex-1", and a PromptInput in the footer.
- Show the shortcut beside whatever opens it, with Kbd.
- Keep a draft in the parent, so closing and opening the dock again does not lose it.

## Don't

- Don't use it for a task the page has to wait for. That is a Dialog.
- Don't mount two docks with the shortcut on: both would toggle on one Ctrl+J.
- Don't put long forms in it. Open a page or a SidePanel for that, and link to it from the conversation.
- Don't pass minimized without onMinimizedChange: the dock shows only its header bar, with no Restore button, and nothing can restore it.
- Don't put anything a person must read in the bottom of the dock while toasts are likely: the toast viewport sits in the same bottom-right corner and draws over the dock.

## Anatomy

- **Dock**: A section with role="dialog" and aria-modal="false", named by the title. 400 by 560 pixels, 16px in from the bottom-right corner; below 768px it runs the full width along the bottom. surface.base with a strong hairline border. It arrives with the snap-in motion, which stops when reduced motion is on.
- **Header bar**: The title, then the actions, Minimize (or Restore) and Close. Minimized, it is all that shows.
- **Title**: Required. An h2, and the dock's accessible name. Cut short with an ellipsis when it does not fit.
- **Actions**: Optional controls before Minimize: New chat, Open in window.
- **Minimize / Restore**: A ghost icon button, shown only when onMinimizedChange is passed.
- **Close**: A ghost icon button at the end of the header bar.
- **Body**: The children, usually a ChatThread, which does its own scrolling.
- **Footer**: Under the body, above the bottom edge: usually a PromptInput. Its first text box takes focus when the dock opens.
