styleproof 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +204 -0
- package/LICENSE +21 -0
- package/README.md +661 -0
- package/bin/styleproof-diff.mjs +110 -0
- package/bin/styleproof-init.mjs +190 -0
- package/bin/styleproof-report.mjs +106 -0
- package/dist/capture.d.ts +79 -0
- package/dist/capture.js +323 -0
- package/dist/diff.d.ts +51 -0
- package/dist/diff.js +121 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +4 -0
- package/dist/report.d.ts +45 -0
- package/dist/report.js +463 -0
- package/dist/runner.d.ts +45 -0
- package/dist/runner.js +35 -0
- package/docs/demo-composite.png +0 -0
- package/package.json +86 -0
package/README.md
ADDED
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
# StyleProof
|
|
2
|
+
|
|
3
|
+
> **Prove a CSS refactor changed nothing.** Capture the browser's _computed styles_ and diff before against after. If the diff is empty, the refactor is certified: not "looks the same", but _resolves byte-for-byte the same_.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/styleproof)
|
|
6
|
+
[](https://github.com/BenSheridanEdwards/styleproof/actions/workflows/ci.yml)
|
|
7
|
+
[](./LICENSE)
|
|
8
|
+
|
|
9
|
+
StyleProof captures every resolved CSS longhand on every element, every
|
|
10
|
+
pseudo-element (`::before`/`::after`/`::marker`/`::placeholder`), and every forced
|
|
11
|
+
`:hover`/`:focus`/`:active` state, swept across your breakpoints, keyed by DOM
|
|
12
|
+
structure rather than class names. Then it diffs two captures and tells you the exact
|
|
13
|
+
element, property, and state that drifted, or certifies that nothing did.
|
|
14
|
+
|
|
15
|
+
Built for CSS-to-Tailwind migrations, design-system swaps, stylesheet consolidation,
|
|
16
|
+
and any refactor where "trust me, it's identical" isn't good enough.
|
|
17
|
+
|
|
18
|
+
```
|
|
19
|
+
home@1280: 1 element(s) differ
|
|
20
|
+
body > main:nth-child(2) > section:nth-child(5) > a:nth-child(1) (.cta)
|
|
21
|
+
border-bottom-color: rgb(95, 202, 219) → rgb(229, 231, 235)
|
|
22
|
+
border-bottom-style: none → solid
|
|
23
|
+
|
|
24
|
+
[:hover] body > nav:nth-child(1) > a:nth-child(3)
|
|
25
|
+
border-color: rgb(95, 202, 219) → (state no longer changes it)
|
|
26
|
+
|
|
27
|
+
✗ 1 DOM change(s), 2 computed-style difference(s), 1 state-delta difference(s) across 12 surfaces
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
## Why computed styles certify a refactor where pixels cannot
|
|
33
|
+
|
|
34
|
+
Pixel diffing is the right tool for _catching_ visual drift, but it cannot _certify_ a
|
|
35
|
+
refactor, because most of a stylesheet is invisible to a screenshot:
|
|
36
|
+
|
|
37
|
+
- **Hover, focus, and active states.** A deleted `:hover` rule renders identically
|
|
38
|
+
until a human points a mouse at it. This tool forces each pseudo-class through the
|
|
39
|
+
Chrome DevTools Protocol (`CSS.forcePseudoState`) and records exactly what each
|
|
40
|
+
state changes, including parent-state rules that restyle descendants. (`:focus`
|
|
41
|
+
forces `:focus` and `:focus-visible` together.)
|
|
42
|
+
- **Hidden elements.** A closed mobile menu is `display: none` in every screenshot,
|
|
43
|
+
but its panel, items, and animations still have computed styles, and they're
|
|
44
|
+
compared.
|
|
45
|
+
- **Between-breakpoint rules.** Screenshots sample two or three viewports. The style
|
|
46
|
+
map sweeps one width per `@media` band, so a dropped `max-width: 680px` override is
|
|
47
|
+
caught even if your screenshot widths never land in that band.
|
|
48
|
+
- **Sub-threshold drift.** Every pixel comparison needs a tolerance for antialiasing,
|
|
49
|
+
and a tolerance is a place for a real 1px change to hide. Computed values need no
|
|
50
|
+
tolerance: `13.5px` either equals `13.5px` or it doesn't.
|
|
51
|
+
- **Declared motion.** Transitions and animations are captured as declared longhands
|
|
52
|
+
(then frozen so every other value is a settled end state), so changing
|
|
53
|
+
`transition: all .2s` to `.3s` is a diff even though no still image could see it.
|
|
54
|
+
|
|
55
|
+
Screenshots and style maps complement each other: pixels catch what you forgot to
|
|
56
|
+
model, the style map certifies what pixels can't see. Use both.
|
|
57
|
+
|
|
58
|
+
## Install
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
# from npm (peer dep: @playwright/test):
|
|
62
|
+
npm i -D styleproof @playwright/test
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Until the first npm release you can install straight from GitHub:
|
|
66
|
+
|
|
67
|
+
```sh
|
|
68
|
+
npm i -D github:BenSheridanEdwards/styleproof @playwright/test
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Requirements: Node 18+, `@playwright/test` >= 1.40, a Chromium-capable Playwright
|
|
72
|
+
install (`npx playwright install chromium`). The only runtime dependency is
|
|
73
|
+
[`pngjs`](https://www.npmjs.com/package/pngjs) (pure JS, no native build).
|
|
74
|
+
|
|
75
|
+
A runnable spec lives in [`example/`](example/).
|
|
76
|
+
|
|
77
|
+
## 60-second quickstart
|
|
78
|
+
|
|
79
|
+
**1. Write a capture spec** listing your **surfaces** — each one a deterministic page
|
|
80
|
+
state plus the viewport widths to sweep. `npx styleproof-init` scaffolds the file below
|
|
81
|
+
(with a `settle()` helper and a `playwright.config.ts` if you don't have one); or write
|
|
82
|
+
it by hand:
|
|
83
|
+
|
|
84
|
+
```ts
|
|
85
|
+
// e2e/styleproof.spec.ts
|
|
86
|
+
import { defineStyleMapCapture, type Surface } from 'styleproof';
|
|
87
|
+
|
|
88
|
+
const SURFACES: Surface[] = [
|
|
89
|
+
{
|
|
90
|
+
key: 'home',
|
|
91
|
+
go: async (page) => {
|
|
92
|
+
await page.goto('/', { waitUntil: 'networkidle' });
|
|
93
|
+
await page.evaluate(() => document.fonts.ready);
|
|
94
|
+
},
|
|
95
|
+
ignore: ['.live-feed'], // nondeterministic regions, skipped entirely
|
|
96
|
+
widths: [1280, 768, 390], // one per @media band of the route's CSS
|
|
97
|
+
},
|
|
98
|
+
{
|
|
99
|
+
key: 'home-menu-open', // states matter: model them as surfaces
|
|
100
|
+
go: async (page) => {
|
|
101
|
+
await page.goto('/', { waitUntil: 'networkidle' });
|
|
102
|
+
await page.getByRole('button', { name: 'Menu' }).click();
|
|
103
|
+
},
|
|
104
|
+
widths: [390],
|
|
105
|
+
},
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
defineStyleMapCapture({ surfaces: SURFACES, dir: process.env.STYLEMAP_DIR });
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The spec is **inert** unless `STYLEMAP_DIR` is set, so it costs nothing in a normal
|
|
112
|
+
test run.
|
|
113
|
+
|
|
114
|
+
**2. Capture a baseline and commit it** — **always against a production build**, dev
|
|
115
|
+
servers inject their own styles:
|
|
116
|
+
|
|
117
|
+
```sh
|
|
118
|
+
STYLEMAP_DIR=baseline npx playwright test styleproof # writes __stylemaps__/baseline/*.json.gz + *.png
|
|
119
|
+
git add e2e/__stylemaps__/baseline && git commit -m "stylemap baseline"
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The `.json.gz` maps are small (a content-heavy page gzips to ~80 KB); the `.png`
|
|
123
|
+
full-page screenshots let the report crop changed regions later.
|
|
124
|
+
|
|
125
|
+
**3. Let CI diff every push** against that committed baseline:
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
STYLEMAP_DIR=ci npx playwright test styleproof
|
|
129
|
+
npx styleproof-diff e2e/__stylemaps__/baseline e2e/__stylemaps__/ci
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
An empty diff exits `0` (certified). A non-empty diff exits `1` and names every
|
|
133
|
+
element, property, and state that drifted. After an _intentional_ change, regenerate
|
|
134
|
+
the baseline and commit it with your diff — the gate is a ratchet, not a freeze.
|
|
135
|
+
|
|
136
|
+
## Core concepts
|
|
137
|
+
|
|
138
|
+
### StyleMap
|
|
139
|
+
|
|
140
|
+
A `StyleMap` is the JSON snapshot of one page state at one width. It has three layers:
|
|
141
|
+
|
|
142
|
+
- **`elements`** — every element's computed style, pruned against per-tag UA defaults
|
|
143
|
+
(measured live in a stylesheet-free iframe) so files stay small, plus its
|
|
144
|
+
pseudo-elements and its document-space bounding box (so reports can crop the
|
|
145
|
+
screenshot around it).
|
|
146
|
+
- **`states`** — for every interactive element (`a, button, input, textarea, select,
|
|
147
|
+
summary, [role="button"], [tabindex]`), the delta that `:hover`, `:focus` /
|
|
148
|
+
`:focus-visible`, and `:active` apply, forced via CDP with no real mouse or focus,
|
|
149
|
+
captured over the element's whole subtree so parent-state descendant rules are seen.
|
|
150
|
+
- **`defaults`** — the per-tag UA baseline used to prune `elements`, kept so the diff
|
|
151
|
+
can resolve a pruned property back to its default when only one side set it.
|
|
152
|
+
|
|
153
|
+
### Surfaces
|
|
154
|
+
|
|
155
|
+
A **surface** is one deterministic page state worth certifying. You give it a `key`, a
|
|
156
|
+
`go(page)` function that navigates and drives the page to that state (ending
|
|
157
|
+
_settled_: fonts loaded, entrance animations done), an optional `ignore` list of
|
|
158
|
+
selectors for nondeterministic regions, and the `widths` to sweep. Distinct states
|
|
159
|
+
(menu open, dialog open, selected tab, form error) are distinct surfaces.
|
|
160
|
+
|
|
161
|
+
### DOM-structure keys
|
|
162
|
+
|
|
163
|
+
Every element is keyed by its structural path, `body > nav:nth-child(3) >
|
|
164
|
+
a:nth-child(2)`, **never by class name**. A migration can rewrite every `class`
|
|
165
|
+
attribute freely while the map stays comparable. If the DOM itself changes, the diff
|
|
166
|
+
says so loudly under a `DOM` finding: a CSS-only refactor must not touch structure.
|
|
167
|
+
|
|
168
|
+
Custom properties (`--*`) are deliberately ignored: they are inputs, not outcomes.
|
|
169
|
+
Every visual effect of a variable lands in a real longhand that _is_ compared, so
|
|
170
|
+
renaming a token is invisible while changing what an element resolves to is not.
|
|
171
|
+
(This also silences Tailwind's `--tw-*` machinery.)
|
|
172
|
+
|
|
173
|
+
### Derived / reflow-noise filtering
|
|
174
|
+
|
|
175
|
+
The **certification differ** (`styleproof-diff`) compares everything, including
|
|
176
|
+
layout-derived longhands (`width`, `height`, `top`, `transform-origin`…), because a
|
|
177
|
+
reflow _is_ a change to certify. The **report** filters those by default: on a reflow
|
|
178
|
+
they change all the way up the ancestor chain (`body`, `main`, `section`…), and an
|
|
179
|
+
element whose _only_ changes are derived is a reflow casualty, not a styling change,
|
|
180
|
+
so it must not anchor a crop region (that would zoom the crop to the whole page).
|
|
181
|
+
Pass `includeLayoutNoise: true` to keep them in the report too.
|
|
182
|
+
|
|
183
|
+
### The readable report
|
|
184
|
+
|
|
185
|
+
When a diff is _intentional_, you want to look at it, not read a wall of longhands.
|
|
186
|
+
`styleproof-report` crops the before/after screenshots around the **outermost changed
|
|
187
|
+
element** (descendants fold into their ancestor, nearby regions merge), stitches each
|
|
188
|
+
pair into one labelled side-by-side image (grey bar = before, blue bar = after), and
|
|
189
|
+
writes the exact changes as a per-element table. It collapses the noise: shorthand
|
|
190
|
+
families (`padding: 26px 24px → 28px`), logical / `currentColor` duplicates dropped,
|
|
191
|
+
repeated tokens folded (`368px ×3`), identical sibling elements grouped (`×5`),
|
|
192
|
+
labelled by their semantic class, colours in their own cells so GitHub renders
|
|
193
|
+
swatches.
|
|
194
|
+
|
|
195
|
+

|
|
196
|
+
|
|
197
|
+
<sub>◀ before · after ▶ — grey bar = before, blue bar = after</sub>
|
|
198
|
+
|
|
199
|
+
> **`div.who-grid`**
|
|
200
|
+
>
|
|
201
|
+
> | Property | Before | After |
|
|
202
|
+
> | ----------------------- | -------------------- | -------------------- |
|
|
203
|
+
> | `grid-template-columns` | `368px ×3` | `548px ×2` |
|
|
204
|
+
> | `gap` | `1px` | `12px` |
|
|
205
|
+
> | `border-color` | `rgb(150, 172, 205)` | `rgb(229, 231, 235)` |
|
|
206
|
+
>
|
|
207
|
+
> **`h3`** ×5 · **`div.who`** ×3 …
|
|
208
|
+
|
|
209
|
+
The whole imaging pipeline is Playwright + Node, **no browser interaction**. Captures
|
|
210
|
+
save a full-page screenshot next to each map by default, so the committed baseline
|
|
211
|
+
carries both the facts and the pixels; generating a report never rebuilds the old code.
|
|
212
|
+
|
|
213
|
+
## API reference
|
|
214
|
+
|
|
215
|
+
Everything is exported from the package root:
|
|
216
|
+
|
|
217
|
+
```ts
|
|
218
|
+
import {
|
|
219
|
+
captureStyleMap,
|
|
220
|
+
saveStyleMap,
|
|
221
|
+
loadStyleMap,
|
|
222
|
+
defineStyleMapCapture,
|
|
223
|
+
diffStyleMaps,
|
|
224
|
+
diffStyleMapDirs,
|
|
225
|
+
findingLabel,
|
|
226
|
+
generateStyleMapReport,
|
|
227
|
+
summarizeProps,
|
|
228
|
+
prettyLabel,
|
|
229
|
+
} from 'styleproof';
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
| Export | Signature | Description |
|
|
233
|
+
| ------------------------ | --------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
234
|
+
| `captureStyleMap` | `(page: Page, options?: CaptureOptions) => Promise<StyleMap>` | Capture the page's complete style map. Drive the page to the target state first; it reads whatever is in front of it. Records elements, pseudo-elements, forced `:hover`/`:focus`/`:active` deltas, declared motion, and each element's bounding box. |
|
|
235
|
+
| `saveStyleMap` | `(filePath: string, map: StyleMap) => void` | Write a map to disk; gzipped when the path ends in `.gz`. Creates parent dirs. |
|
|
236
|
+
| `loadStyleMap` | `(filePath: string) => StyleMap` | Read a map written by `saveStyleMap` (`.json` or `.json.gz`). |
|
|
237
|
+
| `defineStyleMapCapture` | `(opts: DefineOptions) => void` | Generate one Playwright test per surface × width, saving `<baseDir>/<dir>/<key>@<width>.json.gz` (+ `.png`). Skips entirely when `dir` is undefined, so the spec is inert in normal runs. Call at module top level in a `.spec.ts`. |
|
|
238
|
+
| `diffStyleMaps` | `(a: StyleMap, b: StyleMap) => Finding[]` | Structured diff of two maps for the same surface: DOM, style, and state findings. |
|
|
239
|
+
| `diffStyleMapDirs` | `(dirA: string, dirB: string) => { surfaces: SurfaceDiff[]; counts: DiffCounts }` | Diff every same-named capture between two directories. Throws if neither dir has any `.json(.gz)` captures. |
|
|
240
|
+
| `findingLabel` | `(path: string, cls: string) => string` | Human label: structural path plus a truncated `.class` hint (first 3 classes). |
|
|
241
|
+
| `generateStyleMapReport` | `(opts: ReportOptions) => ReportResult` | Crop the before/after screenshots around changed regions and write `report.md`, `report.json`, and `crops/*.png`. |
|
|
242
|
+
| `summarizeProps` | `(props: PropChange[]) => PropChange[]` | The report's property collapser: dedupe logical aliases, fold `currentColor` echoes, collapse shorthand families (`padding: 26px 24px`), round and run-length values. Exported for downstream report builders. |
|
|
243
|
+
| `prettyLabel` | `(path: string, cls: string) => string` | Label an element by its semantic marker class (`div.who-grid`) rather than its structural path. |
|
|
244
|
+
|
|
245
|
+
### Key exported types
|
|
246
|
+
|
|
247
|
+
```ts
|
|
248
|
+
type Rect = [number, number, number, number]; // document-space [x, y, w, h], rounded
|
|
249
|
+
|
|
250
|
+
type StyleMap = {
|
|
251
|
+
defaults: Record<string, Props>; // per-tag UA baseline
|
|
252
|
+
elements: Record<string, ElementEntry>; // keyed by structural path
|
|
253
|
+
states: Record<string, Record<string, Record<string, Props>>>; // path → state → subpath → props
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
type ElementEntry = {
|
|
257
|
+
tag: string;
|
|
258
|
+
cls: string;
|
|
259
|
+
rect?: Rect;
|
|
260
|
+
style: Props; // Record<string, string>
|
|
261
|
+
pseudo?: Record<string, Props>; // '::before' | '::after' | '::marker' | '::placeholder'
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
type CaptureOptions = {
|
|
265
|
+
ignore?: string[]; // selectors skipped with their subtrees
|
|
266
|
+
captureStates?: boolean; // capture forced :hover/:focus/:active deltas (default true)
|
|
267
|
+
maxInteractive?: number; // cap forced-state elements per surface (default 800)
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
type Surface = {
|
|
271
|
+
key: string; // unique file-name prefix
|
|
272
|
+
go: (page: Page) => Promise<void>;
|
|
273
|
+
ignore?: string[];
|
|
274
|
+
widths: number[]; // one per @media band
|
|
275
|
+
height?: number | ((width: number) => number); // default 800
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
type DefineOptions = {
|
|
279
|
+
surfaces: Surface[];
|
|
280
|
+
dir: string | undefined; // capture label; undefined = skip
|
|
281
|
+
baseDir?: string; // default '__stylemaps__'
|
|
282
|
+
screenshots?: boolean; // default true
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
type PropChange = { prop: string; before: string; after: string };
|
|
286
|
+
|
|
287
|
+
type Finding =
|
|
288
|
+
| { kind: 'dom'; path: string; cls: string; change: 'added' | 'removed' | 'retagged'; detail?: string }
|
|
289
|
+
| { kind: 'style'; path: string; cls: string; pseudo: string | null; props: PropChange[] }
|
|
290
|
+
| { kind: 'state'; path: string; cls: string; state: string; sub: string; props: PropChange[] };
|
|
291
|
+
|
|
292
|
+
type SurfaceDiff = { surface: string; missing?: 'before' | 'after'; findings: Finding[] };
|
|
293
|
+
type DiffCounts = { dom: number; style: number; state: number };
|
|
294
|
+
|
|
295
|
+
type ReportOptions = {
|
|
296
|
+
beforeDir: string;
|
|
297
|
+
afterDir: string;
|
|
298
|
+
outDir: string;
|
|
299
|
+
imageBaseUrl?: string; // prefix for image URLs in report.md (default: relative paths)
|
|
300
|
+
pad?: number; // padding around changed rects (default 24)
|
|
301
|
+
minWidth?: number; // default 320
|
|
302
|
+
minHeight?: number; // default 180
|
|
303
|
+
maxHeight?: number; // crops clamped to this (default 1600)
|
|
304
|
+
maxCrops?: number; // regions per surface before collapsing to one (default 6)
|
|
305
|
+
includeLayoutNoise?: boolean; // keep size/position-derived longhands (default false)
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
type ReportResult = {
|
|
309
|
+
changedSurfaces: number;
|
|
310
|
+
totalFindings: number;
|
|
311
|
+
reportMdPath: string;
|
|
312
|
+
reportJsonPath: string;
|
|
313
|
+
};
|
|
314
|
+
```
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
// Example: programmatic use, same engines as the CLIs.
|
|
318
|
+
const map = await captureStyleMap(page, { ignore: ['.live-feed'] });
|
|
319
|
+
saveStyleMap('maps/home@1280.json.gz', map);
|
|
320
|
+
const before = loadStyleMap('maps/home@1280.json.gz');
|
|
321
|
+
|
|
322
|
+
const findings = diffStyleMaps(before, map);
|
|
323
|
+
const { surfaces, counts } = diffStyleMapDirs('maps/before', 'maps/after');
|
|
324
|
+
generateStyleMapReport({ beforeDir: 'maps/before', afterDir: 'maps/after', outDir: 'report' });
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
## CLI reference
|
|
328
|
+
|
|
329
|
+
The three bins are installed on your PATH (run via `npx` or an npm script). They read
|
|
330
|
+
the built `dist/`, so they work from the installed package out of the box. Each takes
|
|
331
|
+
`-h`/`--help`.
|
|
332
|
+
|
|
333
|
+
### `styleproof-init` — scaffold a capture spec
|
|
334
|
+
|
|
335
|
+
```
|
|
336
|
+
styleproof-init [--dir <path>] [--base-url <url>] [--force]
|
|
337
|
+
```
|
|
338
|
+
|
|
339
|
+
| Flag | Default | Description |
|
|
340
|
+
| ------------------ | ------------------------ | ----------------------------------------------------------------------- |
|
|
341
|
+
| `--dir <path>` | `e2e/styleproof.spec.ts` | Where to write the starter spec (with a `settle()` helper + a Surface). |
|
|
342
|
+
| `--base-url <url>` | `http://localhost:3000` | `baseURL` for a generated `playwright.config.ts` (only if none exists). |
|
|
343
|
+
| `--force` | off | Overwrite the spec if it already exists. Idempotent without it. |
|
|
344
|
+
|
|
345
|
+
Exit `0` on success (or nothing to do), `2` on a usage error. An existing
|
|
346
|
+
`playwright.config.ts` is never touched.
|
|
347
|
+
|
|
348
|
+
### `styleproof-diff` — the certification gate
|
|
349
|
+
|
|
350
|
+
```
|
|
351
|
+
styleproof-diff <beforeDir> <afterDir> [--max N] [--json <file>]
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
| Flag | Default | Description |
|
|
355
|
+
| ------------------------------------ | ------- | --------------------------------------------------------------------------------------------- |
|
|
356
|
+
| `<beforeDir>` | — | Directory of baseline `.json(.gz)` captures (required). |
|
|
357
|
+
| `<afterDir>` | — | Directory of fresh `.json(.gz)` captures (required). |
|
|
358
|
+
| `--max N` (or `--max=N`) | `40` | Max diff lines printed per surface before truncating. (The GitHub Action passes `--max=200`.) |
|
|
359
|
+
| `--json <file>` (or `--json=<file>`) | — | Also write the structured `{ counts, surfaces }` result to `<file>`. |
|
|
360
|
+
|
|
361
|
+
| Exit code | Meaning |
|
|
362
|
+
| --------- | ---------------------------------------------------------------------------------------------- |
|
|
363
|
+
| `0` | Identical — every computed style, pseudo-element, and state matches. **Certified.** |
|
|
364
|
+
| `1` | Differences found (DOM, style, and/or state). |
|
|
365
|
+
| `2` | Usage or capture error (bad args, missing dir, no captures, non-finite `--max`, unknown flag). |
|
|
366
|
+
|
|
367
|
+
### `styleproof-report` — the reviewable visual report
|
|
368
|
+
|
|
369
|
+
```
|
|
370
|
+
styleproof-report <beforeDir> <afterDir> --out <dir> [options]
|
|
371
|
+
```
|
|
372
|
+
|
|
373
|
+
| Flag | Default | Description |
|
|
374
|
+
| -------------------------------------- | ------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
375
|
+
| `<beforeDir>` | — | Baseline captures (`.json.gz` + `.png` for crops). |
|
|
376
|
+
| `<afterDir>` | — | Fresh captures (`.json.gz` + `.png` for crops). |
|
|
377
|
+
| `--out <dir>` (or `--out=<dir>`) | `styleproof-report` | Output directory: writes `report.md`, `report.json`, `crops/*.png`. |
|
|
378
|
+
| `--image-base-url <url>` (or `=<url>`) | relative paths | Prefix for image URLs in `report.md` (e.g. a raw GitHub URL). Omit to keep relative paths that render from a committed file. |
|
|
379
|
+
| `--pad <px>` | `24` | Padding around the changed rects when cropping. |
|
|
380
|
+
| `--max-crops <n>` | `6` | Max crop regions per surface before collapsing to one union crop. |
|
|
381
|
+
| `--min-width <px>` | `320` | Minimum crop width, for context around tiny changes. |
|
|
382
|
+
| `--min-height <px>` | `180` | Minimum crop height. |
|
|
383
|
+
| `--include-layout-noise` | off | Keep size/position-derived longhands (`height`, `width`, `transform-origin`, `top`…) in the report instead of filtering them. |
|
|
384
|
+
|
|
385
|
+
| Exit code | Meaning |
|
|
386
|
+
| --------- | ----------------------------------------------------------------------------- |
|
|
387
|
+
| `0` | No changes — an empty report was written. |
|
|
388
|
+
| `1` | Report generated (one or more changed surfaces). |
|
|
389
|
+
| `2` | Usage error (wrong arg count, unknown/non-numeric flag, or capture/IO error). |
|
|
390
|
+
|
|
391
|
+
Numeric flags also accept the `--flag=value` form. The remaining `maxHeight` knob
|
|
392
|
+
(crop height clamp, default 1600) is available through the programmatic
|
|
393
|
+
`generateStyleMapReport` API.
|
|
394
|
+
|
|
395
|
+
## GitHub Action
|
|
396
|
+
|
|
397
|
+
The repo ships a composite action that diffs against your committed baseline and, on
|
|
398
|
+
changes, commits a compact report to an orphan branch and posts it to the PR. It
|
|
399
|
+
fails the job while changes are unapproved; **approving = regenerating the committed
|
|
400
|
+
baseline** and pushing it with the PR. The comment updates in place on every push and
|
|
401
|
+
flips to ✓ when clean.
|
|
402
|
+
|
|
403
|
+
```yaml
|
|
404
|
+
- name: Capture style maps
|
|
405
|
+
run: STYLEMAP_DIR=ci npx playwright test styleproof
|
|
406
|
+
|
|
407
|
+
- name: Style-map report
|
|
408
|
+
uses: BenSheridanEdwards/styleproof@v1
|
|
409
|
+
with:
|
|
410
|
+
baseline-dir: e2e/__stylemaps__/baseline
|
|
411
|
+
fresh-dir: e2e/__stylemaps__/ci
|
|
412
|
+
# report-branch: styleproof-reports # default (orphan; created on first run)
|
|
413
|
+
# inline-images: auto # auto | always | never
|
|
414
|
+
# fail-on-diff: 'true' # default
|
|
415
|
+
```
|
|
416
|
+
|
|
417
|
+
### Inputs
|
|
418
|
+
|
|
419
|
+
| Input | Required | Default | Description |
|
|
420
|
+
| --------------- | -------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
421
|
+
| `baseline-dir` | yes | — | Directory with the committed baseline captures (`.json.gz` + `.png`). |
|
|
422
|
+
| `fresh-dir` | yes | — | Directory with the freshly captured maps to compare. |
|
|
423
|
+
| `report-branch` | no | `styleproof-reports` | Orphan branch that stores reports (created on first run). One `pr-<n>/` folder per PR; never pruned. |
|
|
424
|
+
| `inline-images` | no | `auto` | `auto` \| `always` \| `never`. `auto` embeds composites in the comment for public repos and links the report for private repos. |
|
|
425
|
+
| `github-token` | no | `${{ github.token }}` | Token used to push the report branch and post the comment. |
|
|
426
|
+
| `fail-on-diff` | no | `'true'` | Fail the job when differences are found. |
|
|
427
|
+
|
|
428
|
+
### Outputs
|
|
429
|
+
|
|
430
|
+
| Output | Description |
|
|
431
|
+
| ------------ | ------------------------------------------------------------------- |
|
|
432
|
+
| `changed` | `"true"` when any computed style, pseudo-element, or state changed. |
|
|
433
|
+
| `report-url` | Blob URL of the committed report (when changed). |
|
|
434
|
+
|
|
435
|
+
### Why two image modes (and why it's automatic)
|
|
436
|
+
|
|
437
|
+
The two ways an image can appear on a PR have **opposite** privacy behaviour, so
|
|
438
|
+
`inline-images: auto` picks per repo:
|
|
439
|
+
|
|
440
|
+
| Placement | How GitHub fetches it | Private repo |
|
|
441
|
+
| ----------------------------------------------------- | -------------------------------------- | --------------------------------------------------- |
|
|
442
|
+
| **comment body** `` | anonymously, via the Camo proxy | private URL **404s → broken**; needs a public URL |
|
|
443
|
+
| **committed file** (`report.md` + relative `crops/…`) | through **your authenticated session** | **renders inline** — like a private README's images |
|
|
444
|
+
|
|
445
|
+
- **Public repo** → composites are embedded **directly in the comment** via the public
|
|
446
|
+
`raw.githubusercontent.com/…` URL; you see the side-by-side without clicking.
|
|
447
|
+
- **Private repo** → the comment **links** the committed `report.md`; one click shows
|
|
448
|
+
the crops inline, no public hosting, no browser.
|
|
449
|
+
|
|
450
|
+
(Embedding an image _directly in a private comment body_ is genuinely impossible from
|
|
451
|
+
CI — GitHub's only private-friendly image URL is `user-attachments`, whose upload
|
|
452
|
+
endpoint rejects API tokens with HTTP 422 and needs a logged-in browser session.)
|
|
453
|
+
|
|
454
|
+
### Orphan-branch layout and stable links
|
|
455
|
+
|
|
456
|
+
The report branch is an **orphan** (reports only, never your code), so its root is one
|
|
457
|
+
folder per PR plus a README:
|
|
458
|
+
|
|
459
|
+
```
|
|
460
|
+
styleproof-reports (orphan)
|
|
461
|
+
├── README.md
|
|
462
|
+
├── pr-9/ report.md + crops/*-composite.png
|
|
463
|
+
└── pr-12/ …
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
- **Stable links.** Each run overwrites `pr-<n>/`, so
|
|
467
|
+
`…/blob/styleproof-reports/pr-9/report.md` is permanent for the life of the PR.
|
|
468
|
+
Reports are **never pruned**; per-run history lives in the branch's git commits.
|
|
469
|
+
- **Compact.** Only the **composite** is committed (alpha dropped, max deflate,
|
|
470
|
+
adaptive filtering): ~30–90 KB per change, a few hundred KB per PR.
|
|
471
|
+
- **Concurrency-safe.** Different PRs touch different folders; a rejected push just
|
|
472
|
+
rebase-replays (the action retries).
|
|
473
|
+
- **Reclaiming history.** Git keeps every overwritten image, so a busy repo's branch
|
|
474
|
+
history grows. The _tree_ (what serves the links) stays small; to shrink `.git`,
|
|
475
|
+
squash the orphan branch when it's quiet — current reports keep their URLs:
|
|
476
|
+
```sh
|
|
477
|
+
git checkout --orphan tmp styleproof-reports && git commit -qm "squash reports" \
|
|
478
|
+
&& git branch -M tmp styleproof-reports && git push -f origin styleproof-reports
|
|
479
|
+
```
|
|
480
|
+
|
|
481
|
+
## CI recipes
|
|
482
|
+
|
|
483
|
+
Every recipe is the same shape: produce a **production build**, serve it, point
|
|
484
|
+
`BASE_URL` (or your Playwright `baseURL`) at it, capture, diff against the committed
|
|
485
|
+
baseline. Use the Action step from above to also post a PR comment.
|
|
486
|
+
|
|
487
|
+
### Next.js
|
|
488
|
+
|
|
489
|
+
```yaml
|
|
490
|
+
- uses: actions/checkout@v4
|
|
491
|
+
- uses: actions/setup-node@v4
|
|
492
|
+
with: { node-version: 20, cache: npm }
|
|
493
|
+
- run: npm ci
|
|
494
|
+
- run: npx playwright install --with-deps chromium
|
|
495
|
+
- run: npm run build
|
|
496
|
+
- run: npx next start -p 3000 &
|
|
497
|
+
- run: npx wait-on http://localhost:3000
|
|
498
|
+
- run: BASE_URL=http://localhost:3000 STYLEMAP_DIR=ci npx playwright test styleproof
|
|
499
|
+
- run: npx styleproof-diff e2e/__stylemaps__/baseline e2e/__stylemaps__/ci
|
|
500
|
+
```
|
|
501
|
+
|
|
502
|
+
### Vite / SPA
|
|
503
|
+
|
|
504
|
+
```yaml
|
|
505
|
+
- run: npm ci && npx playwright install --with-deps chromium
|
|
506
|
+
- run: npm run build # → dist/
|
|
507
|
+
- run: npx vite preview --port 4173 & # serves the production build
|
|
508
|
+
- run: npx wait-on http://localhost:4173
|
|
509
|
+
- run: BASE_URL=http://localhost:4173 STYLEMAP_DIR=ci npx playwright test styleproof
|
|
510
|
+
- run: npx styleproof-diff e2e/__stylemaps__/baseline e2e/__stylemaps__/ci
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
### Plain static site
|
|
514
|
+
|
|
515
|
+
```yaml
|
|
516
|
+
- run: npm ci && npx playwright install --with-deps chromium
|
|
517
|
+
- run: npm run build # → public/ or dist/
|
|
518
|
+
- run: npx serve -l 5000 dist & # any static server
|
|
519
|
+
- run: npx wait-on http://localhost:5000
|
|
520
|
+
- run: BASE_URL=http://localhost:5000 STYLEMAP_DIR=ci npx playwright test styleproof
|
|
521
|
+
- run: npx styleproof-diff e2e/__stylemaps__/baseline e2e/__stylemaps__/ci
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
### Generic build → serve → capture → diff
|
|
525
|
+
|
|
526
|
+
```sh
|
|
527
|
+
<your build command> # produce a PRODUCTION build
|
|
528
|
+
<your static/app server> & # serve it on a known port
|
|
529
|
+
npx wait-on http://localhost:<port> # wait until it answers
|
|
530
|
+
BASE_URL=http://localhost:<port> STYLEMAP_DIR=ci npx playwright test styleproof
|
|
531
|
+
npx styleproof-diff e2e/__stylemaps__/baseline e2e/__stylemaps__/ci
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
Your `playwright.config.ts` reads `BASE_URL` for `use.baseURL`; surfaces use relative
|
|
535
|
+
paths (`page.goto('/')`).
|
|
536
|
+
|
|
537
|
+
## Baselines and the env-parity gotcha
|
|
538
|
+
|
|
539
|
+
A baseline is the committed `before` of your refactor and the certified state CI
|
|
540
|
+
compares against. **Capture it in the same environment CI uses.** Anything that
|
|
541
|
+
changes what renders must match between the baseline capture and CI's fresh capture:
|
|
542
|
+
|
|
543
|
+
- **Feature flags** that show or hide a panel.
|
|
544
|
+
- **API tokens** that gate a section (e.g. a live-data widget only present when a key
|
|
545
|
+
is set).
|
|
546
|
+
- **Env-dependent copy** (a `mailto:` line built from an env var, a conditional embed).
|
|
547
|
+
|
|
548
|
+
If your local `.env` makes the page render _more_ than CI will, baselines captured
|
|
549
|
+
locally can never pass on the runner: the extra elements exist on one side only and
|
|
550
|
+
show up as DOM findings. The fix is to capture the baseline with the **same** env the
|
|
551
|
+
runner has (typically no `.env.local`), then commit it. After an _intentional_ style
|
|
552
|
+
change, regenerate the committed baseline from a production build in that same
|
|
553
|
+
environment and commit it with your diff.
|
|
554
|
+
|
|
555
|
+
## Determinism
|
|
556
|
+
|
|
557
|
+
Captures read whatever is in front of them, so the page must be **settled and
|
|
558
|
+
repeatable** before `captureStyleMap` runs. The differ has zero tolerance, so any
|
|
559
|
+
nondeterminism becomes a false diff. In your surface's `go`:
|
|
560
|
+
|
|
561
|
+
- **Fonts.** `await page.evaluate(() => document.fonts.ready)` before capturing —
|
|
562
|
+
unloaded fonts change `font-family` fallbacks and metrics.
|
|
563
|
+
- **Animations / transitions.** The capture freezes them so every value is a settled
|
|
564
|
+
end state, but _entrance_ animations driven by JS or IntersectionObserver must
|
|
565
|
+
already have finished. Scroll the page to trigger reveals, then wait; or inject CSS
|
|
566
|
+
forcing your reveal classes to their final values
|
|
567
|
+
(`.reveal{opacity:1!important;transform:none!important}`).
|
|
568
|
+
- **Scroll-reveal.** Walk the page top to bottom (see `example/styleproof.spec.ts`'s
|
|
569
|
+
`settle` helper) so every observer fires, then scroll back to `0`.
|
|
570
|
+
- **Live / nondeterministic regions.** List them in `ignore` (live feeds, ads,
|
|
571
|
+
third-party embeds, timestamps); the elements and their subtrees are skipped.
|
|
572
|
+
- **Same build state.** Layout-derived values are part of the map by design, so if
|
|
573
|
+
text content differs between captures, expect diffs. Capture the same build.
|
|
574
|
+
|
|
575
|
+
## Limitations
|
|
576
|
+
|
|
577
|
+
- **Chromium only for forced-state capture.** The `:hover`/`:focus`/`:active` deltas
|
|
578
|
+
use CDP (`CSS.forcePseudoState`), which is Chromium-only. Base element and
|
|
579
|
+
pseudo-element capture work in any Playwright browser.
|
|
580
|
+
- **Same machine, same browser version for before/after.** Computed values are far
|
|
581
|
+
less platform-sensitive than pixels, but font metrics can still differ across OSes
|
|
582
|
+
and browser builds. Capture both sides on the same runner image.
|
|
583
|
+
- **No shadow-DOM piercing.** Capture walks `document.querySelectorAll('body *')`; it
|
|
584
|
+
does not descend into shadow roots, so a refactor inside a web component's shadow
|
|
585
|
+
tree would be falsely certified identical. Capture emits a one-time warning counting
|
|
586
|
+
the shadow hosts it skipped, so the gap is loud, not silent.
|
|
587
|
+
- **No iframe piercing.** Iframe content (same- or cross-origin) is not traversed for
|
|
588
|
+
the same reason; same-origin frames are counted in the same warning. To certify a
|
|
589
|
+
frame, point a separate surface at its document directly.
|
|
590
|
+
- **Layout-derived values are included** (used track sizes, element heights, offsets).
|
|
591
|
+
This is intentional for the certification differ, but it means a content or reflow
|
|
592
|
+
change produces diffs; the report filters these unless `includeLayoutNoise: true`.
|
|
593
|
+
- **Forced-state capture is O(interactive elements × 3 states).** A content-heavy page
|
|
594
|
+
takes a few seconds per surface.
|
|
595
|
+
|
|
596
|
+
## Troubleshooting
|
|
597
|
+
|
|
598
|
+
| Symptom | Likely cause / fix |
|
|
599
|
+
| ------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
600
|
+
| `npx playwright test styleproof` does nothing / spec skipped | `STYLEMAP_DIR` is unset. The capture spec is inert by design until you set it (`STYLEMAP_DIR=before …`). |
|
|
601
|
+
| `no .json(.gz) captures found in …` | You diffed before capturing, or pointed at the wrong dir. Check `<baseDir>/<label>/`. |
|
|
602
|
+
| `styleproof: interactive-element count skew …; skipping forced … capture` | The DOM changed between the CDP query and the page evaluate (e.g. a late-rendering widget). The base + pseudo capture still succeed; only the forced-state layer is skipped for that surface. Settle the surface fully, or `ignore` the unstable region, then re-run. |
|
|
603
|
+
| Capture warns about shadow hosts / iframes | Styles inside shadow roots and frames are not captured (see Limitations). Point a separate surface at the frame's document, or accept the gap. |
|
|
604
|
+
| Diffs you didn't expect, all `width`/`height`/`top`/… | A real reflow (content or layout changed). The differ keeps these; use the _report_ (which filters them) to see the styling intent, or capture the identical build state. |
|
|
605
|
+
| Baseline passes locally, fails in CI with DOM findings | Env parity: your local env renders elements CI doesn't (or vice versa). Capture the baseline in CI's environment — see _Baselines and the env-parity gotcha_. |
|
|
606
|
+
| Diffs every run, fonts-related | Fonts weren't ready at capture. Await `document.fonts.ready` (and any web-font load) in `go`. |
|
|
607
|
+
| Private-repo PR comment shows no images, only a link | Expected: GitHub can't render images in a private comment body. Click the link — the committed report renders the crops inline through your session. |
|
|
608
|
+
| `styleproof-diff: command not found` from a fresh clone | The bins import the built `dist/`. Run `npm run build` first (the published npm package ships a prebuilt `dist/`). |
|
|
609
|
+
|
|
610
|
+
## How this compares to pixel snapshot tools
|
|
611
|
+
|
|
612
|
+
| | **StyleProof** | **Percy / Chromatic** | **Playwright `toHaveScreenshot`** |
|
|
613
|
+
| -------------------------------------------------------------------------- | ------------------------------- | ------------------------- | --------------------------------- |
|
|
614
|
+
| Compares | computed CSS longhands | rendered pixels | rendered pixels |
|
|
615
|
+
| Certifies invisible state (hover/focus/active, hidden, between-breakpoint) | **yes** | no | no |
|
|
616
|
+
| Tolerance needed | **none** (exact values) | antialiasing threshold | `maxDiffPixels` threshold |
|
|
617
|
+
| Cross-browser / cross-OS fidelity | values only (no real render) | **strong** (real renders) | good |
|
|
618
|
+
| Catches what it wasn't told to model | no (model your surfaces) | **yes** (whole frame) | yes (the frame) |
|
|
619
|
+
| Hosted dashboard / approvals UI | git-based (commit the baseline) | **yes** (SaaS) | local/CI files |
|
|
620
|
+
| Cost | free, MIT, self-hosted | paid SaaS (free tiers) | free |
|
|
621
|
+
|
|
622
|
+
They solve different halves of the problem. Pixel tools _catch_ drift you didn't think
|
|
623
|
+
to check, across real browser renders. The style map _certifies_ that a refactor
|
|
624
|
+
changed nothing, including the states and rules a screenshot can never see, with no
|
|
625
|
+
tolerance to hide a real change in. **Run both:** screenshots for discovery, the style
|
|
626
|
+
map for proof.
|
|
627
|
+
|
|
628
|
+
## Battle-tested: what it caught
|
|
629
|
+
|
|
630
|
+
This tool was extracted from a real CSS-to-Tailwind migration (~680 lines of bespoke
|
|
631
|
+
CSS across four stylesheets, certified to zero diff). Every one of these was caught by
|
|
632
|
+
the style map and invisible or ambiguous to pixels:
|
|
633
|
+
|
|
634
|
+
- **A base-layer reset eating button borders.** `button { border: none }` in the
|
|
635
|
+
global stylesheet meant `border` + `border-color` utilities (width and colour only)
|
|
636
|
+
rendered _no border at all_. Every bordered button needed an explicit
|
|
637
|
+
`border-solid`. The diff named all of them.
|
|
638
|
+
- **`grid-cols-2` is not `1fr 1fr`.** Tailwind's `repeat(2, minmax(0, 1fr))` removes
|
|
639
|
+
the min-content floor. One panel had been quietly overflowing its grid track by 8px;
|
|
640
|
+
the utility version clamped it, reflowing 50 elements. On `display: none` elements
|
|
641
|
+
the two forms even serialize differently.
|
|
642
|
+
- **`outline-none` is not `outline: none`.** Tailwind's utility is a 2px transparent
|
|
643
|
+
outline (an accessibility affordance). The forced-`:focus` capture flagged three new
|
|
644
|
+
longhands the original never set.
|
|
645
|
+
- **Shorthand resets.** `border-bottom: none` resets style _and_ colour to initial;
|
|
646
|
+
zeroing just the width (`border-b-0`) leaves the preflight's gray `solid` behind.
|
|
647
|
+
Same story for `hover:shadow-*` (ring placeholders), `rounded-full` (`9999px` vs
|
|
648
|
+
`50%`), `items-start` (`flex-start` vs `start`), and named font utilities (dropped
|
|
649
|
+
fallbacks from the stack).
|
|
650
|
+
- **A dropped `:hover` rule on a link** that every screenshot tool sailed past, because
|
|
651
|
+
nothing hovers in a screenshot.
|
|
652
|
+
|
|
653
|
+
## Contributing
|
|
654
|
+
|
|
655
|
+
Issues and PRs welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for the dev setup, the
|
|
656
|
+
build/test loop, and the release process. Changes are recorded in
|
|
657
|
+
[CHANGELOG.md](CHANGELOG.md).
|
|
658
|
+
|
|
659
|
+
## License
|
|
660
|
+
|
|
661
|
+
MIT © Ben Sheridan-Edwards. See [LICENSE](LICENSE).
|