Style probe
Read what a browser actually computed for an element, as values a test can compare.
Reads an element's computed style from a live page and returns typed values, so a test can assert what a component really draws. It is how haus's Figma parity check measures components.
Install
npm install @hipuku/style-probeUsage
Two halves: extractRawElements runs in the page, and normaliseElement turns its strings into
comparable values anywhere. With Playwright:
import { extractRawElements, normaliseElement } from '@hipuku/style-probe'
const raw = await page.evaluate(extractRawElements, { root: '#probe' })
const [button] = raw.map(normaliseElement)
button.styles.backgroundColor // '#2563eb'
button.styles.padding // [8, 16, 8, 16]root reads one element and its subtree. Leave it out to read the whole page.
API
| Export | Signature |
|---|---|
extractRawElements | (options?: ProbeOptions) => RawElement[] |
toHex | (input: string) => string | null |
normaliseElement | (el: RawElement) => ExtractedElement |
rgbToHex | (input: string) => string | null |
firstFontFamily | (stack: string) => string | null |
pxToNumber | (value: string | undefined | null) => number | null |
normaliseFontWeight | (value: string) => number | null |
normaliseLineHeight | (value: string, fontSizePx: number | null) => number | null |
normaliseLetterSpacing | (value: string, fontSizePx: number | null) => number |
normaliseBoxShadow | (value: string) => string | null |
The full reference is the package README.