Adds `pnpm storybook:screenshot <story-id>`, which saves a PNG of a
story from a running Storybook dev server. We keep hand-rolling
Playwright or agent-browser snippets for this, and the Storybook MCP
server only returns preview URLs, not images.
```sh
cd site
pnpm storybook # in another terminal
pnpm storybook:screenshot pages-loginpage--example -t light
# /tmp/storybook-screenshots/pages-loginpage--example.png
```
The script loads the story's `iframe.html` in headless Chromium (via the
existing `@playwright/test` dependency), waits for Storybook's render
events, fonts, and network idle, then prints the output path. Options
are `-o/--out` and `-t/--theme`; `$STORYBOOK_URL` overrides the server
address.
Readiness uses Storybook's channel events (`STORY_RENDERED` after play
functions, or `STORY_ERRORED` / `STORY_THREW_EXCEPTION` /
`PLAY_FUNCTION_THREW_EXCEPTION`), all contained in `loadStory()`. This
is the same mechanism `@coder/pixel-storybook` uses. A failed render
still captures but exits 1. A component that throws during render exits
0, because the router addon's error boundary renders its own error page
and Storybook reports success.
<details>
<summary>Decision log</summary>
- Storybook's MCP server (`@storybook/addon-mcp` 10.6.2) has no
screenshot tool; `stories-preview` returns URLs only.
- Trimmed an earlier version with title path and URL inputs, viewport,
full-page, selector, and timeout flags, and "did you mean" suggestions,
to keep the script short.
- Replaced polling `__STORYBOOK_PREVIEW__.currentRender.phase`
(undocumented internal state) with channel events from
`storybook/internal/core-events`.
- Listeners attach on `DOMContentLoaded`, not via
`requestAnimationFrame` polling: Storybook's `vite-app.js` module
creates the preview synchronously, module scripts run before
`DOMContentLoaded`, and the first render starts after. Measured on three
stories: the preview existed at `DOMContentLoaded` with no render
started.
</details>
<details>
<summary>Verification</summary>
Against a local `pnpm storybook`:
- Page and component stories capture and exit 0; `-t light` applies the
theme.
- Temporary stories with a throwing play function and a throwing loader
exit 1 with `playFunctionThrewException` and `storyThrewException`.
- Unknown story ID exits 1 with `Story not found`.
- `knip` passes. Biome and oxlint ignore `scripts/*.mjs`.
</details>
---
_Generated by Coder Agents on behalf of @jeremyruppel._