# Adding a component to `@aisquare/ui`

The library lacks what you need. This is the procedure. It is one PR, and it is the only
sanctioned path — a component built in an app is a fork that never receives another fix.

## 0. Prove it is missing

Most "missing" components are named something you did not search for.

```
search_components { query: "<what it DOES, in plain language>" }
list_components   { layer: "component", category: "<the closest area>" }
plan_new_component { name, description }     # does the overlap check for you
```

Read the near-misses with `get_component`. Three outcomes:

| Finding | Action |
|---|---|
| One of them fits | Use it. Done. |
| One almost fits | **Add a prop or a cva variant to it.** Not a new component, not a fork. |
| Nothing fits | Continue below. |

An almost-fit is the common case and the one most often got wrong. `Card` gaining an
`interactive` prop was correct; a second `ClickableCard` would not have been.

Also ask whether it belongs in the library at all. A one-off arrangement of existing components
is app layout. Promote it only when it repeats across surfaces, carries real behaviour or a11y
wiring, or encodes a brand decision (rule SRC-4).

## 1. Pick the layer

| Layer | Directory | For |
|---|---|---|
| `stock` | `src/ui/` | Byte-for-byte shadcn. **Only** via `pnpm import:shadcn` — never hand-written or hand-edited. |
| `primitive` | `src/primitives/` | A brand-flavored wrapper of a primitive. Same API surface as stock, plus tokens/motion/defaults. |
| `component` | `src/components/` | A composed, AISquare-original pattern. |
| `block` | `src/components/blocks/` | A chat/AI transcript block. Also register it in the block registry. |

Wrapping an upstream shadcn primitive that is not in `src/ui/` yet? Add its name to
`scripts/import-from-shadcn.mjs`, run `pnpm import:shadcn <name>`, and leave the imported file
alone. Your wrapper goes in `src/primitives/`.

## 2. Scaffold

```
scaffold_component {
  name: "PascalName",
  layer: "component",
  category: "Inputs",           # playground grouping
  description: "what it does",
  interactive: true,            # renders a <button>: focusable, keyboard-operable
  variants: { size: ["sm","default","lg"], tone: ["neutral","accent"] },
  dryRun: true                  # review first, then dryRun: false
}
```

Five files, already conforming:

1. `src/<layer>/<Name>.tsx` — `cn` + `cva` + `forwardRef` + `data-slot`, `className` merged last.
2. `src/<layer>/<Name>.stories.tsx` — colocated, imports from `./<Name>`, no custom `title`.
3. the layer barrel — the export line.
4. `src/index.test.ts` — the `CORE_EXPORTS_PER_PRIMITIVE` entry (this is the check that catches
   "the file exists but nothing can import it").
5. `playground/src/demos/<Name>.demo.tsx` + a `DEMOS` row in `playground/src/lib/registry.ts`.

Doing it by hand? Those five files are the whole checklist, and the `DEMOS` row needs a real
`since` version added to the `VERSIONS` tuple.

## 3. Write the component

The scaffold leaves TODOs. What matters:

- **The JSDoc is not optional** (DOC-1). Say what it is and **when to reach for it over its
  neighbours** — that is the part types cannot carry, and it is what `get_component` and an IDE
  hover both surface. Do not restate the signature.
- **Token utilities only.** No hex, no `bg-blue-500`, no `rounded-[12px]`. Run `get_tokens`.
- **Variants via `cva`**, exported (`<name>Variants`) so callers can reuse the recipe.
- **`className` merged last** through `cn`, ref forwarded, rest spread. Extend
  `React.ComponentProps<'button'>` rather than inventing a closed prop list.
- **Interactive means a real `<button>`/`<a>`** with an accessible name; state announced via
  `aria-busy` / `aria-checked` / `aria-expanded` / `aria-invalid`, never by color alone.
- **Motion gated by `useReducedMotion()`**, timings from the shared presets.
- **No browser globals at module scope** — it throws on import under SSR and in the node tests.
- **The notch is for clickable rectangles.** If you notch it: elevation via `elev-*`, outline via
  `nx-rim`. An outer `box-shadow` is clipped away; a real `border` is cut open at the chamfer.

Then:

```
review_code { file: "src/components/<Name>.tsx", context: "library" }
```

## 4. Story

One story per state that matters. A `play` function only where it proves something the render
alone does not — an interaction, an aria state, async, a portal. Assert roles and accessible
names, not class strings. Keep `tags: ['ai-generated']` until a human owns it; drop
`'needs-work'` once `pnpm test:storybook` passes.

Do **not** add a second `CssCheck` story. Exactly one exists, in `Button.stories.tsx`, and it is
the canary proving the preview loaded Tailwind and the tokens at all.

## 5. Verify and ship

```bash
pnpm typecheck && pnpm lint && pnpm test:ci && pnpm build
pnpm test:storybook    # CI does NOT gate this — run it locally
pnpm size              # if you added a dependency
pnpm agent:index       # refresh the MCP component index
```

Or `verify { checks: ["typecheck","lint","test:ci","build"] }`.

A new component is a **minor** bump (VER-1). Open the PR as a draft (VER-3).

## 6. Then use it

Import it from `@aisquare/ui` in the app. If the app must ship before the library release, link
the package or use a prerelease build — do not copy the source across.
