--- name: playwright-visual-implementation description: Use Playwright screenshots to implement or repair a frontend against one or more visual references. Trigger when a user asks to match a screenshot, reproduce an existing page, fix visual differences, perform screenshot-driven UI development, or investigate a visual regression across view modes or viewport sizes. --- # Playwright Visual Implementation Implement against rendered evidence, not memory. Iterate through controlled screenshots until the requested state matches the reference without regressing adjacent states. ## 1. Establish The Comparison Contract Identify before editing: - Which image is current and which is expected. - Exact route, viewport, device scale factor, and whether either image is cropped. - Required UI state: selected tab, view mode, filters, expanded panels, scroll position, and responsive breakpoint. - Persistent inputs such as local storage, cookies, feature flags, and server preferences. - Data assumptions that affect layout: item count, title lengths, missing images, progress values, and loading/error states. Do not compare screenshots from different states. Reproduce the reference state first. If the state is persisted, set or preserve the real persistence mechanism rather than hard-coding a component default. ## 2. Inspect Before Editing Read the page component, styles, shared components, route, fixtures, tests, and relevant generated types. Check the dirty worktree and preserve unrelated changes. Determine whether the difference comes from: 1. Wrong structure or view variant. 2. Wrong content source or state. 3. Wrong asset or aspect ratio. 4. Wrong dimensions, spacing, typography, color, or borders. Fix in that order. CSS cannot repair the wrong component structure or data state. ## 3. Capture A Controlled Baseline Use the repository's Playwright installation. Capture to `/tmp` unless the project has an established visual-test artifact directory. Build the website first, then use the screenshot CLI. The CLI starts a temporary Vite preview server for the built `dist` assets, applies the configured `/p1` API proxy, selects an unused local port, and closes the server after capture. Do not ask the user to start or keep a Vite dev server running for visual work. Prefer the repository's screenshot CLI, `pnpm website screenshot` (`packages/website/scripts/screenshot.mjs`): ```bash pnpm run build pnpm website screenshot [options] ``` Useful options: - `--width` / `--height`: viewport size (default 1440×900) - `--device-scale-factor `: device scale factor (default 1) - `--quality <0-100>`: JPEG quality (default 60); output must end in `.jpg`/`.jpeg`, PNG is not supported - `--full-page`: capture the full scrollable page - `--element `: capture only the element matching a CSS selector (e.g. `#user-avatar`) at its natural size instead of the whole page; mutually exclusive with `--full-page`. Use it to screenshot a single UI component in isolation - `--wait-until `: `commit`, `domcontentloaded`, `load`, or `networkidle` (default `domcontentloaded`) - `--wait-for `: wait for a selector to become visible - `--wait-ms `: additional delay after loading (default 1000) - `--local-storage key=value`: set local storage before navigation (repeatable) - `--storage-state `: Playwright storage-state JSON for authentication Examples: ```bash pnpm run build pnpm website screenshot /user/sai /tmp/user.jpg --full-page pnpm website screenshot /user/sai /tmp/avatar.jpg --element '#user-avatar' pnpm website screenshot --wait-for main --local-storage view=grid ``` Pass a route such as `/anime`, rather than the address of a manually started server. The CLI accepts a legacy loopback URL by extracting its route, but new commands should use a route. Use the same `--mode` for the build and screenshot command when a non-default Vite mode is required. Extend `packages/website/scripts/screenshot.mjs` when the CLI options cannot express the needed state — for example clicks, form input, or waiting for an element to appear or disappear. It is the repository's capture tool and is meant to be modified as needed: add the extra steps (such as a repeatable `--click ` option) to the capture flow, then run it with `pnpm website screenshot` as usual. It starts a temporary Vite preview server for the built assets; do not point it at a dev server, and build the website first (`pnpm run build`). Write a separate temporary Playwright script only if extending the CLI is impractical. Place it inside `packages/website/` (e.g. `packages/website/scripts/`) and run it with `pnpm website run