# Doc comments

> Review threads beside the text they quote: the one you choose lines up with its line, and the rest make way.

Most comment UIs list threads in a sidebar that ignores where they belong, or open popovers over the very words you are discussing. Here every thread sits in the margin level with the line it quotes, and threads that would overlap stack downwards. Choose one, or click its highlighted words, and it slides to sit exactly beside its line while the threads above and below slide out of its way. Select text and a Comment pill appears in the margin beside it; it grows into the composer, and once posted the composer becomes the thread in place. Replies rise in and the count rolls. A comment or reply that fails keeps your words and turns its button into Try again. Resolve fades the highlight and holds "Resolved · Undo" over a draining hairline before the card folds away. In a narrow column there is no margin: a count sits at the end of the line, and the paragraph opens a gap under itself for the thread. Threads find their text by quoting it, so they survive edits around them, and highlights are drawn under the text, never written into it.

## Install

```bash
npx shadcn@latest add @hairline/doc-comments
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `status-button`, `text-morph`
- Docs and live preview: https://hairlineui.com/ui/doc-comments
- Registry item (source): https://hairlineui.com/r/doc-comments.json

## Usage

```tsx
import { DocComments, type DocThread } from "@/components/doc-comments";

const [threads, setThreads] = useState<DocThread[]>([]);

<DocComments threads={threads} onThreadsChange={setThreads} user={me} onCreate={save} onReply={reply} onResolve={resolve}>
  <article>{markdown}</article>
</DocComments>
```

## Review comments on a spec, saved to your API

The document is whatever you render; threads are anchored by quoting it. Each callback saves one change: return the stored thread or comment to take its ids, throw to keep the words and offer Try again.

```tsx
"use client";
import { useState } from "react";
import { DocComments, type DocThread, type CommentAnchor } from "@/components/doc-comments";

export function SpecReview({ doc, initial, me }: { doc: React.ReactNode; initial: DocThread[]; me: { id: string; name: string; image?: string } }) {
  const [threads, setThreads] = useState(initial);

  const send = async (url: string, body: unknown) => {
    const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(body) });
    if (!res.ok) throw new Error(await res.text());
    return res.json();
  };

  return (
    <DocComments
      threads={threads}
      onThreadsChange={setThreads}
      user={me}
      onCreate={(anchor: CommentAnchor, body) => send("/api/threads", { anchor, body })}
      onReply={(thread, body) => send(`/api/threads/${thread.id}/comments`, { body })}
      onResolve={(thread) => send(`/api/threads/${thread.id}/resolve`, {})}
    >
      <article className="prose">{doc}</article>
    </DocComments>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` | – | The document. Any markup; it's read to find quotes and never changed. |
| `threads / defaultThreads` | `DocThread[]` | – | { id, anchor: { quote, prefix?, suffix? }, comments: { id, author, body, createdAt }[], resolved? }. Resolved threads aren't shown; a thread whose quote can't be found waits until it can. |
| `onThreadsChange` | `(threads: DocThread[]) => void` | – | Every new thread, reply and resolve. |
| `user` | `{ id, name, image? }` | – | Who is commenting. |
| `onCreate` | `(anchor, body) => Promise<DocThread \| void>` | – | Turns on commenting on a selection. Resolve with the stored thread; throw to keep the words and offer Try again. |
| `onReply` | `(thread, body) => Promise<DocComment \| void>` | – | Turns on replies. Resolve with the stored comment; throw to keep the words and offer Try again. |
| `onResolve` | `(thread) => Promise<void>` | – | Turns on ✓. Runs once the chance to undo is over; throw to open the thread again. |
| `undoFor` | `number` | `4000` | How long Undo is offered after resolving, ms. |
| `locales` | `string` | – | For dates older than yesterday. |
| `label` | `string` | `"Comments"` | Accessible name of the whole. |

## States

- **margin**: Each card sits 10px above the top of its quote's first line. Cards that would overlap are pushed down, 8px apart; the margin grows to fit the last one.
- **focus**: Choosing a thread (its card, Enter on it, or a click on its highlight) places it exactly at its line; the cards after it are pushed below it and the cards before it are pulled above it, all gliding 360ms. Its highlight deepens with an underline, its card takes a ring and opens to every reply and a reply field, its height easing 260ms.
- **comment**: A selection in the document shows a Comment pill in the margin beside its first line (or under the selection when narrow), rising out of a blur in 220ms. The pill grows into the composer by one clip in 320ms; the selection becomes a highlight so it stays visible while you type. Ctrl+Alt+M does the same from the keyboard.
- **post**: Comment shows Posting while onCreate runs, then the composer becomes the thread in the same card. A reply rises 4px out of a blur and the "2 replies" count rolls. If either throws, the words stay and the button says Try again.
- **resolve**: ✓ fades the highlight (320ms) and turns the card into "Resolved · Undo" over a hairline that drains for 4s (paused while hovered). Then onResolve runs and the card goes while the others glide up; if it throws, the thread opens again with a note.
- **narrow**: Below 620px there's no margin: a speech-bubble count sits beside the first line of each thread. Choosing it opens a gap under the paragraph (its margin eases 280ms) and the card sits in the gap, so nothing covers the text.

## What it handles

Most comment UIs list threads in a sidebar that ignores where they belong, or open popovers over the very words being discussed. Here each thread sits in the margin beside the text it quotes.

- **Threads beside their line**: Each thread sits level with the line it quotes, and threads that would overlap stack downwards.
- **Select text to comment**: A Comment pill appears beside the selection and grows into the composer. Ctrl+Alt+M does the same from the keyboard.
- **Anchors survive edits**: A thread finds its text by quoting it, so edits elsewhere don’t move it. Highlights sit under the text and never change it.
- **Replies and resolve**: Replies rise in and the count rolls. Resolve holds “Resolved · Undo” for four seconds before the thread goes.
- **Failures keep your words**: If a comment or reply fails to save, the words stay and the button reads Try again.
- **Works on narrow screens**: Below 620px a count sits at the end of the line, and the paragraph opens a gap for the thread.

## Replaces

a comments sidebar, text selection handling, anchor matching, overlap layout for cards, resolve with undo

## Where it fits

- **Specs and documents under review**: Reviewers comment on exact sentences in a spec or a proposal.
- **Contracts and approvals**: Collect feedback on a quoted clause and mark it resolved.
- **CMS and editorial tools**: Editors leave notes on drafts beside the text they refer to.
- **Client proposals**: Let a client comment on a shared page without a separate tool.

## Notes

- Each thread is an article named by its author and the words it quotes ("Priya Nair on “Tuesday, October 24”, 1 reply"), in the order the quotes appear, so Tab walks the document's threads from top to bottom. Enter opens one, Escape lets it go, ⌘ or Ctrl+Enter sends what you typed.
- The Comment pill can be reached with Ctrl+Alt+M while text is selected. New threads, replies, failures and resolves are announced in a status region.
- An anchor is the quote plus up to 32 characters either side, so the same words twice in a document find the right one, and edits elsewhere don't move it. Highlights are boxes drawn in a layer under the text from the quote's line rectangles, measured again only when the document changes size or content.
- Cards move by transform only; heights are measured from what's inside each card, so a card easing open never makes the margin re-solve on every frame.
- Reduced motion places cards, opens threads and resolves in place; highlights, rings and the Undo line still say what happened.

License: MIT (https://hairlineui.com/license#components)
