basalt-ui 2.21.0 → 2.23.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.
Files changed (254) hide show
  1. package/CHANGELOG.md +60 -0
  2. package/MIGRATING.md +452 -12
  3. package/agent/agents/basalt-design-reviewer.md +1 -1
  4. package/agent/rules/basalt-tokens.md +1 -1
  5. package/agent/skills/basalt-design/SKILL.md +1 -1
  6. package/agent/skills/basalt-design/references/anti-defaults.md +1 -1
  7. package/agent/skills/basalt-design/references/catalogue.md +1 -0
  8. package/dist/charts/BandStrip.d.ts.map +1 -1
  9. package/dist/charts/BandStrip.js +73 -3
  10. package/dist/charts/Bars.d.ts.map +1 -1
  11. package/dist/charts/Bars.js +6 -0
  12. package/dist/charts/CartesianChart.d.ts +10 -0
  13. package/dist/charts/CartesianChart.d.ts.map +1 -1
  14. package/dist/charts/CartesianChart.js +8 -5
  15. package/dist/charts/ChartCard.d.ts +12 -3
  16. package/dist/charts/ChartCard.d.ts.map +1 -1
  17. package/dist/charts/ChartCard.js +39 -8
  18. package/dist/charts/DualPanel.d.ts.map +1 -1
  19. package/dist/charts/DualPanel.js +5 -8
  20. package/dist/charts/Heatmap.d.ts +12 -1
  21. package/dist/charts/Heatmap.d.ts.map +1 -1
  22. package/dist/charts/Heatmap.js +34 -5
  23. package/dist/charts/Lines.d.ts +3 -0
  24. package/dist/charts/Lines.d.ts.map +1 -1
  25. package/dist/charts/Lines.js +29 -6
  26. package/dist/charts/MirroredBars.d.ts.map +1 -1
  27. package/dist/charts/MirroredBars.js +4 -6
  28. package/dist/charts/Sparkline.d.ts +14 -2
  29. package/dist/charts/Sparkline.d.ts.map +1 -1
  30. package/dist/charts/Sparkline.js +101 -22
  31. package/dist/charts/StackedArea.d.ts +4 -1
  32. package/dist/charts/StackedArea.d.ts.map +1 -1
  33. package/dist/charts/StackedArea.js +9 -3
  34. package/dist/charts/band-fold.d.ts +38 -1
  35. package/dist/charts/band-fold.d.ts.map +1 -1
  36. package/dist/charts/band-fold.js +18 -1
  37. package/dist/charts/chart-box.module.css +19 -0
  38. package/dist/charts/chart-card.module.css +25 -0
  39. package/dist/charts/chart-dates.d.ts +12 -0
  40. package/dist/charts/chart-dates.d.ts.map +1 -0
  41. package/dist/charts/chart-dates.js +16 -0
  42. package/dist/charts/chart-legend.module.css +7 -0
  43. package/dist/charts/compile.d.ts +25 -6
  44. package/dist/charts/compile.d.ts.map +1 -1
  45. package/dist/charts/compile.js +152 -72
  46. package/dist/charts/date-tick-format.d.ts +5 -5
  47. package/dist/charts/date-tick-format.d.ts.map +1 -1
  48. package/dist/charts/date-tick-format.js +3 -7
  49. package/dist/charts/labels.d.ts +21 -6
  50. package/dist/charts/labels.d.ts.map +1 -1
  51. package/dist/charts/labels.js +52 -17
  52. package/dist/charts/legend.d.ts +2 -0
  53. package/dist/charts/legend.d.ts.map +1 -1
  54. package/dist/charts/legend.js +7 -21
  55. package/dist/charts/marks.js +1 -1
  56. package/dist/charts/panel.d.ts +0 -6
  57. package/dist/charts/panel.d.ts.map +1 -1
  58. package/dist/charts/panel.js +1 -10
  59. package/dist/charts/ref-label.d.ts +43 -0
  60. package/dist/charts/ref-label.d.ts.map +1 -0
  61. package/dist/charts/ref-label.js +75 -0
  62. package/dist/charts/scales.d.ts +4 -1
  63. package/dist/charts/scales.d.ts.map +1 -1
  64. package/dist/charts/scales.js +14 -3
  65. package/dist/charts/sparkline.module.css +13 -17
  66. package/dist/charts/sparkline.module.css.d.ts +1 -2
  67. package/dist/charts/state.d.ts +2 -0
  68. package/dist/charts/state.d.ts.map +1 -1
  69. package/dist/charts/state.js +5 -2
  70. package/dist/charts/types.d.ts +18 -8
  71. package/dist/charts/types.d.ts.map +1 -1
  72. package/dist/cli/doctor-upgrade.d.ts +1 -0
  73. package/dist/cli/doctor-upgrade.d.ts.map +1 -1
  74. package/dist/cli/doctor-upgrade.js +1 -0
  75. package/dist/cli/init.d.ts +2 -0
  76. package/dist/cli/init.d.ts.map +1 -1
  77. package/dist/cli/init.js +68 -4
  78. package/dist/cli/shot/capture.d.ts +21 -2
  79. package/dist/cli/shot/capture.d.ts.map +1 -1
  80. package/dist/cli/shot/capture.js +202 -36
  81. package/dist/cli/shot/detect.d.ts.map +1 -1
  82. package/dist/cli/shot/detect.js +17 -1
  83. package/dist/cli/shot/playwright.d.ts +13 -2
  84. package/dist/cli/shot/playwright.d.ts.map +1 -1
  85. package/dist/cli/shot/playwright.js +5 -1
  86. package/dist/cli/shot/types.d.ts +4 -0
  87. package/dist/cli/shot/types.d.ts.map +1 -1
  88. package/dist/cli/shot/types.js +2 -0
  89. package/dist/cli/tokens-css.d.ts +5 -7
  90. package/dist/cli/tokens-css.d.ts.map +1 -1
  91. package/dist/cli/tokens-css.js +11 -12
  92. package/dist/controls/controls.module.css +11 -1
  93. package/dist/controls/filter-pill.d.ts.map +1 -1
  94. package/dist/controls/filter-pill.js +1 -0
  95. package/dist/controls/sync-button.d.ts.map +1 -1
  96. package/dist/controls/sync-button.js +22 -3
  97. package/dist/controls/sync-button.module.css +34 -2
  98. package/dist/controls/sync-button.module.css.d.ts +1 -0
  99. package/dist/dashboard/bar-list.d.ts +6 -1
  100. package/dist/dashboard/bar-list.d.ts.map +1 -1
  101. package/dist/dashboard/bar-list.js +15 -8
  102. package/dist/dashboard/bar-list.module.css +83 -0
  103. package/dist/dashboard/bar-list.module.css.d.ts +1 -0
  104. package/dist/dashboard/delta-badge.d.ts.map +1 -1
  105. package/dist/dashboard/delta-badge.js +2 -2
  106. package/dist/dashboard/index.d.ts +1 -0
  107. package/dist/dashboard/index.d.ts.map +1 -1
  108. package/dist/dashboard/index.js +2 -0
  109. package/dist/dashboard/meter-model.d.ts +84 -5
  110. package/dist/dashboard/meter-model.d.ts.map +1 -1
  111. package/dist/dashboard/meter-model.js +100 -15
  112. package/dist/dashboard/meter-parts.d.ts +9 -1
  113. package/dist/dashboard/meter-parts.d.ts.map +1 -1
  114. package/dist/dashboard/meter-parts.js +276 -74
  115. package/dist/dashboard/meter.d.ts +54 -65
  116. package/dist/dashboard/meter.d.ts.map +1 -1
  117. package/dist/dashboard/meter.js +21 -4
  118. package/dist/dashboard/meter.module.css +76 -0
  119. package/dist/dashboard/meter.module.css.d.ts +6 -0
  120. package/dist/dashboard/page-summary.d.ts +20 -0
  121. package/dist/dashboard/page-summary.d.ts.map +1 -0
  122. package/dist/dashboard/page-summary.js +28 -0
  123. package/dist/dashboard/page-summary.module.css +89 -0
  124. package/dist/dashboard/page-summary.module.css.d.ts +15 -0
  125. package/dist/dashboard/query-state.d.ts.map +1 -1
  126. package/dist/dashboard/query-state.js +1 -1
  127. package/dist/dashboard/section.d.ts +25 -2
  128. package/dist/dashboard/section.d.ts.map +1 -1
  129. package/dist/dashboard/section.js +10 -5
  130. package/dist/dashboard/section.module.css +23 -0
  131. package/dist/dashboard/section.module.css.d.ts +1 -0
  132. package/dist/dashboard/stat-card-parts.d.ts.map +1 -1
  133. package/dist/dashboard/stat-card-parts.js +1 -1
  134. package/dist/dashboard/stat-card.d.ts +4 -2
  135. package/dist/dashboard/stat-card.d.ts.map +1 -1
  136. package/dist/dashboard/stat-card.js +7 -5
  137. package/dist/dashboard/stat-card.module.css +36 -0
  138. package/dist/dashboard/stat-group.module.css +7 -0
  139. package/dist/dashboard/stat-strip.d.ts +22 -6
  140. package/dist/dashboard/stat-strip.d.ts.map +1 -1
  141. package/dist/dashboard/stat-strip.js +104 -60
  142. package/dist/dashboard/stat-strip.module.css +122 -53
  143. package/dist/dashboard/stat-strip.module.css.d.ts +4 -0
  144. package/dist/dashboard/visual-slot.d.ts +7 -1
  145. package/dist/dashboard/visual-slot.d.ts.map +1 -1
  146. package/dist/dashboard/visual-slot.js +3 -1
  147. package/dist/guard/index.d.ts.map +1 -1
  148. package/dist/guard/index.js +0 -5
  149. package/dist/guard/vx-names.d.ts.map +1 -1
  150. package/dist/guard/vx-names.js +2 -0
  151. package/dist/index.d.ts +1 -1
  152. package/dist/index.d.ts.map +1 -1
  153. package/dist/index.js +3 -1
  154. package/dist/styles.css +43 -4
  155. package/dist/theme/chrome-vars.d.ts +20 -5
  156. package/dist/theme/chrome-vars.d.ts.map +1 -1
  157. package/dist/theme/chrome-vars.js +46 -3
  158. package/dist/theme/controls.module.css +37 -0
  159. package/dist/theme/controls.module.css.d.ts +2 -0
  160. package/dist/theme/index.d.ts.map +1 -1
  161. package/dist/theme/index.js +18 -27
  162. package/dist/theme/status-verdict.d.ts.map +1 -1
  163. package/dist/theme/status-verdict.js +9 -1
  164. package/dist/tokens/core.d.ts +2 -1
  165. package/dist/tokens/core.d.ts.map +1 -1
  166. package/dist/tokens/core.js +6 -1
  167. package/dist/tokens/derive.d.ts +3 -1
  168. package/dist/tokens/derive.d.ts.map +1 -1
  169. package/dist/tokens/derive.js +16 -1
  170. package/dist/tokens/palette.d.ts +11 -10
  171. package/dist/tokens/palette.d.ts.map +1 -1
  172. package/dist/tokens/palette.js +11 -10
  173. package/dist/tokens/status-text.d.ts.map +1 -1
  174. package/dist/tokens/status-text.js +3 -1
  175. package/llms.txt +42 -22
  176. package/package.json +1 -1
  177. package/src/charts/BandStrip.tsx +83 -3
  178. package/src/charts/Bars.tsx +7 -0
  179. package/src/charts/CartesianChart.tsx +18 -3
  180. package/src/charts/ChartCard.tsx +54 -13
  181. package/src/charts/DualPanel.tsx +3 -6
  182. package/src/charts/Heatmap.tsx +54 -5
  183. package/src/charts/Lines.tsx +56 -10
  184. package/src/charts/MirroredBars.tsx +3 -5
  185. package/src/charts/Sparkline.tsx +135 -25
  186. package/src/charts/StackedArea.tsx +14 -4
  187. package/src/charts/band-fold.ts +50 -1
  188. package/src/charts/chart-box.module.css +19 -0
  189. package/src/charts/chart-card.module.css +25 -0
  190. package/src/charts/chart-dates.ts +30 -0
  191. package/src/charts/chart-legend.module.css +7 -0
  192. package/src/charts/compile.ts +253 -87
  193. package/src/charts/date-tick-format.ts +10 -13
  194. package/src/charts/labels.ts +81 -26
  195. package/src/charts/legend.tsx +10 -20
  196. package/src/charts/marks.ts +1 -1
  197. package/src/charts/panel.tsx +1 -16
  198. package/src/charts/ref-label.ts +131 -0
  199. package/src/charts/scales.ts +22 -2
  200. package/src/charts/sparkline.module.css +13 -17
  201. package/src/charts/sparkline.module.css.d.ts +1 -2
  202. package/src/charts/state.tsx +9 -4
  203. package/src/charts/types.ts +18 -8
  204. package/src/cli/doctor-upgrade.ts +1 -1
  205. package/src/cli/init.ts +100 -3
  206. package/src/cli/shot/capture.ts +266 -35
  207. package/src/cli/shot/detect.ts +18 -1
  208. package/src/cli/shot/playwright.ts +20 -4
  209. package/src/cli/shot/types.ts +5 -0
  210. package/src/cli/tokens-css.ts +18 -20
  211. package/src/controls/controls.module.css +11 -1
  212. package/src/controls/filter-pill.tsx +2 -0
  213. package/src/controls/sync-button.module.css +34 -2
  214. package/src/controls/sync-button.module.css.d.ts +1 -0
  215. package/src/controls/sync-button.tsx +40 -4
  216. package/src/dashboard/bar-list.module.css +83 -0
  217. package/src/dashboard/bar-list.module.css.d.ts +1 -0
  218. package/src/dashboard/bar-list.tsx +107 -69
  219. package/src/dashboard/delta-badge.tsx +6 -2
  220. package/src/dashboard/index.ts +1 -0
  221. package/src/dashboard/meter-model.ts +207 -24
  222. package/src/dashboard/meter-parts.tsx +349 -67
  223. package/src/dashboard/meter.module.css +76 -0
  224. package/src/dashboard/meter.module.css.d.ts +6 -0
  225. package/src/dashboard/meter.tsx +72 -18
  226. package/src/dashboard/page-summary.module.css +89 -0
  227. package/src/dashboard/page-summary.module.css.d.ts +15 -0
  228. package/src/dashboard/page-summary.tsx +95 -0
  229. package/src/dashboard/query-state.tsx +1 -1
  230. package/src/dashboard/section.module.css +23 -0
  231. package/src/dashboard/section.module.css.d.ts +1 -0
  232. package/src/dashboard/section.tsx +59 -11
  233. package/src/dashboard/stat-card-parts.tsx +5 -1
  234. package/src/dashboard/stat-card.module.css +36 -0
  235. package/src/dashboard/stat-card.tsx +12 -6
  236. package/src/dashboard/stat-group.module.css +7 -0
  237. package/src/dashboard/stat-strip.module.css +122 -53
  238. package/src/dashboard/stat-strip.module.css.d.ts +4 -0
  239. package/src/dashboard/stat-strip.tsx +176 -91
  240. package/src/dashboard/visual-slot.ts +12 -3
  241. package/src/guard/index.ts +0 -5
  242. package/src/guard/vx-names.ts +2 -0
  243. package/src/index.ts +2 -0
  244. package/src/styles.css +43 -4
  245. package/src/theme/__snapshots__/plain-theme.test.ts.snap +200 -68
  246. package/src/theme/chrome-vars.ts +77 -8
  247. package/src/theme/controls.module.css +37 -0
  248. package/src/theme/controls.module.css.d.ts +2 -0
  249. package/src/theme/index.ts +22 -32
  250. package/src/theme/status-verdict.tsx +5 -1
  251. package/src/tokens/core.ts +4 -1
  252. package/src/tokens/derive.ts +32 -3
  253. package/src/tokens/palette.ts +11 -10
  254. package/src/tokens/status-text.ts +2 -0
@@ -9,7 +9,14 @@ import type { Pair } from './real-data'
9
9
 
10
10
  import { finishMeasurement, measureAnchor } from './measure'
11
11
  import type { Box, PwBrowser, PwContext, PwLocator, PwPage, ShotOptions } from './playwright'
12
- import { PAGE_ANCHOR, SCHEMES, URL_STATES, VIEWPORTS, VIEWPORT_SPECS } from './types'
12
+ import {
13
+ NO_STATES_NOTE,
14
+ PAGE_ANCHOR,
15
+ SCHEMES,
16
+ URL_STATES,
17
+ VIEWPORTS,
18
+ VIEWPORT_SPECS,
19
+ } from './types'
13
20
  import type {
14
21
  AnchorMetrics,
15
22
  Measurement,
@@ -22,6 +29,12 @@ import type {
22
29
 
23
30
  const NAV_TIMEOUT_MS = 30_000
24
31
  const HOVER_TIMEOUT_MS = 5_000
32
+ /** Every Playwright wait a capture makes on one anchor: a hidden or tall target costs this, not 30 s. */
33
+ const STEP_TIMEOUT_MS = 5_000
34
+ const SHOT_TIMEOUT_MS = 15_000
35
+ const REVEAL_SETTLE_MS = 60
36
+ /** Chromium refuses screenshots past ~16k px; a taller anchor is captured as far as this. */
37
+ const MAX_VIEWPORT_PX = 16_000
25
38
  const TOOLTIP_TIMEOUT_MS = 3_000
26
39
  const TAB_PRESSES = 80
27
40
  const FOCUS_MARGIN_PX = 8
@@ -51,6 +64,8 @@ export type CaptureInput = {
51
64
  type PageFacts = {
52
65
  anchors: string[]
53
66
  declared: string[]
67
+ /** Anchors that hold a basalt state surface (ChartCard, StatCard, a chart state). */
68
+ stateful?: string[]
54
69
  /** Anchor → the URL states it declares itself absent in (`data-shot-absent="empty error"`). */
55
70
  absent?: Record<string, string[]>
56
71
  }
@@ -62,6 +77,8 @@ type Ctx = {
62
77
  route: string
63
78
  scheme: Scheme
64
79
  viewport: Viewport
80
+ /** The viewport's current height: a tall anchor grows it for one shot. */
81
+ height: number
65
82
  facts: PageFacts
66
83
  anchors: AnchorMetrics[]
67
84
  shots: ShotRecord[]
@@ -79,11 +96,26 @@ type Outcome = {
79
96
  status: ShotRecord['status']
80
97
  file?: string
81
98
  note?: string
99
+ stateful?: true
82
100
  }
83
101
 
84
102
  /** Thrown inside a capture when the anchor simply has no such target: recorded `not-declared`, not `error`. */
85
103
  class NotDeclared extends Error {}
86
104
 
105
+ /** Thrown when the anchor is genuinely not rendered at this viewport: recorded `absent` with the reason. */
106
+ class Absent extends Error {}
107
+
108
+ const sleep = (ms: number): Promise<void> => new Promise((done) => setTimeout(done, ms))
109
+
110
+ /** `work`, rejected after `ms`: one hung Playwright call never spends the 30 s default. */
111
+ function within<T>(what: string, work: Promise<T>, ms = STEP_TIMEOUT_MS): Promise<T> {
112
+ let timer: ReturnType<typeof setTimeout> | undefined
113
+ const late = new Promise<never>((_, reject) => {
114
+ timer = setTimeout(() => reject(new Error(`${what} timed out after ${ms / 1000} s`)), ms)
115
+ })
116
+ return Promise.race([work, late]).finally(() => clearTimeout(timer))
117
+ }
118
+
87
119
  const hash = (text: string): string => createHash('sha1').update(text).digest('hex').slice(0, 6)
88
120
  const slug = (text: string): string => text.replace(/[^\w-]+/g, '-').replace(/^-+|-+$/g, '')
89
121
  const firstLine = (error: unknown): string =>
@@ -108,9 +140,15 @@ export function withParam(route: string, state: ShotState): string {
108
140
  return `${path}${path.includes('?') ? '&' : '?'}shot-state=${state}${fragment}`
109
141
  }
110
142
 
111
- /** The rect union of `boxes`, grown by `margin` and clamped to the viewport. */
112
- export function unionClip(boxes: readonly Box[], margin: number, viewport: Viewport): Box {
113
- const { width, height } = VIEWPORT_SPECS[viewport]
143
+ /** The rect union of `boxes`, grown by `margin` and clamped to the viewport (`tall`: its current height). */
144
+ export function unionClip(
145
+ boxes: readonly Box[],
146
+ margin: number,
147
+ viewport: Viewport,
148
+ tall?: number,
149
+ ): Box {
150
+ const { width, height: base } = VIEWPORT_SPECS[viewport]
151
+ const height = tall ?? base
114
152
  const left = Math.max(0, Math.min(...boxes.map((b) => b.x)) - margin)
115
153
  const top = Math.max(0, Math.min(...boxes.map((b) => b.y)) - margin)
116
154
  const right = Math.min(width, Math.max(...boxes.map((b) => b.x + b.width)) + margin)
@@ -123,9 +161,16 @@ const snap = (
123
161
  target: { screenshot(options: ShotOptions): Promise<unknown> },
124
162
  path: string,
125
163
  extra: { fullPage?: boolean; clip?: Box } = {},
126
- ): Promise<unknown> => target.screenshot({ path, animations: 'disabled', caret: 'hide', ...extra })
164
+ ): Promise<unknown> =>
165
+ target.screenshot({
166
+ path,
167
+ animations: 'disabled',
168
+ caret: 'hide',
169
+ timeout: SHOT_TIMEOUT_MS,
170
+ ...extra,
171
+ })
127
172
 
128
- function record(c: Ctx, { name, state, status, file, note }: Outcome): void {
173
+ function record(c: Ctx, { name, state, status, file, note, stateful }: Outcome): void {
129
174
  const { route, scheme, viewport } = c
130
175
  c.shots.push({
131
176
  route,
@@ -136,6 +181,7 @@ function record(c: Ctx, { name, state, status, file, note }: Outcome): void {
136
181
  file: file ?? null,
137
182
  status,
138
183
  ...(note ? { note } : {}),
184
+ ...(stateful ? { stateful } : {}),
139
185
  })
140
186
  if (status === 'error')
141
187
  c.input.log(`[shot] ${route} ${name} ${scheme}/${viewport}/${state}: ${note}`)
@@ -157,12 +203,17 @@ export function tagAutoAnchors(): void {
157
203
  })
158
204
  }
159
205
 
206
+ /** The DOM marks of a basalt state surface: `ChartCard` and the chart state write theirs; `StatCard` has none but its `basalt-card` container. */
207
+ const STATE_SURFACE =
208
+ '[data-basalt-chart-card], [data-basalt-chart-state], [style*="container-name: basalt-card"]'
209
+
160
210
  const readFacts = (page: PwPage): Promise<PageFacts> =>
161
- page.evaluate(`(() => ({
211
+ page.evaluate(`(() => { const STATE_SURFACE = ${JSON.stringify(STATE_SURFACE)}; return {
162
212
  anchors: [...new Set([...document.querySelectorAll('[data-shot]')].map((e) => e.getAttribute('data-shot')).filter(Boolean))],
163
213
  declared: [...document.querySelectorAll('[data-shot-states]')].flatMap((e) => (e.getAttribute('data-shot-states') ?? '').split(/\\s+/)).filter(Boolean),
214
+ stateful: [...new Set([...document.querySelectorAll('[data-shot]')].filter((e) => e.matches(STATE_SURFACE) || e.querySelector(STATE_SURFACE)).map((e) => e.getAttribute('data-shot')).filter(Boolean))],
164
215
  absent: Object.fromEntries([...document.querySelectorAll('[data-shot][data-shot-absent]')].map((e) => [e.getAttribute('data-shot'), (e.getAttribute('data-shot-absent') ?? '').split(/\\s+/).filter(Boolean)])),
165
- }))()`)
216
+ }; })()`)
166
217
 
167
218
  const addProblem = (c: Ctx, problem: string): void => {
168
219
  if (!c.problems.includes(problem)) c.problems.push(problem)
@@ -212,11 +263,167 @@ function flagProblems(c: Ctx): void {
212
263
  c.input.log(`[shot] ${c.route} ${c.scheme}/${c.viewport}: ${note}`)
213
264
  }
214
265
 
215
- /** Brings the anchor into view; throws when it is absent, hidden or off-screen at this viewport. */
216
- async function reveal(anchor: PwLocator): Promise<void> {
217
- if ((await anchor.count()) === 0) throw new Error('anchor not found')
218
- await anchor.scrollIntoViewIfNeeded()
219
- if (!(await anchor.isVisible())) throw new Error('anchor is not visible at this viewport')
266
+ /**
267
+ * Runs in the page: why the anchor paints nothing (`display: none` on it or an ancestor, hidden,
268
+ * zero size), or '' after scrolling it into view itself. An anchor taller than the viewport is
269
+ * aligned to its top, a shorter one is centred so a tooltip has room around it. Playwright's own
270
+ * `scrollIntoViewIfNeeded` waits out its actionability checks, which is what cost 30 s on a Sparkline
271
+ * target, a hidden mobile pane and an anchor taller than the viewport.
272
+ */
273
+ export function revealInPage(el: Element): string {
274
+ for (let node: Element | null = el; node; node = node.parentElement) {
275
+ const where = node === el ? '' : ' on an ancestor'
276
+ const style = getComputedStyle(node)
277
+ if (style.display === 'none') return `display: none${where}`
278
+ if (node.hasAttribute('hidden')) return `hidden attribute${where}`
279
+ if (style.visibility === 'hidden' && node === el) return 'visibility: hidden'
280
+ }
281
+ const rect = el.getBoundingClientRect()
282
+ if (rect.width === 0 || rect.height === 0) return 'zero size'
283
+ el.scrollIntoView({
284
+ block: rect.height > window.innerHeight ? 'start' : 'center',
285
+ inline: 'nearest',
286
+ behavior: 'instant',
287
+ })
288
+ return ''
289
+ }
290
+
291
+ /**
292
+ * Brings the anchor into view. A hidden anchor is `absent` (with the reason) only where hiding is
293
+ * expected: a pointer/focus state, or a state it declared itself absent in. A hidden `rest` anchor
294
+ * (or URL state) is a defect, so it stays an `Error`, as does a missing one. No Playwright wait is
295
+ * involved, so none of them costs more than a step.
296
+ */
297
+ async function reveal(c: Ctx, name: string, state: ShotState): Promise<void> {
298
+ const anchor = anchorOf(c, name)
299
+ if ((await within('locating the anchor', anchor.count())) === 0)
300
+ throw new Error('anchor not found')
301
+ const verdict = await within('revealing the anchor', anchor.evaluate(revealInPage))
302
+ if (typeof verdict === 'string' && verdict !== '') {
303
+ const note = `not visible at this viewport: ${verdict}`
304
+ const expected = state === 'hover' || state === 'focus' || state === 'tooltip'
305
+ const declared = c.facts.absent?.[name]?.includes(state) === true
306
+ throw expected || declared ? new Absent(note) : new Error(note)
307
+ }
308
+ await sleep(REVEAL_SETTLE_MS)
309
+ }
310
+
311
+ /** The anchor's box once two reads agree: a smooth scroll or an animating card never shifts the clip. */
312
+ async function settledBox(target: PwLocator): Promise<Box | null> {
313
+ let box = await within('measuring the anchor', target.boundingBox())
314
+ for (let i = 0; i < TIP_SETTLE_READS; i++) {
315
+ await sleep(TIP_SETTLE_MS)
316
+ const next = await within('measuring the anchor', target.boundingBox())
317
+ const settled = JSON.stringify(next) === JSON.stringify(box)
318
+ box = next
319
+ if (settled) break
320
+ }
321
+ return box
322
+ }
323
+
324
+ const FRAMES = 'new Promise((done) => requestAnimationFrame(() => requestAnimationFrame(done)))'
325
+
326
+ async function resize(c: Ctx, height: number): Promise<void> {
327
+ await c.page.setViewportSize({ width: VIEWPORT_SPECS[c.viewport].width, height })
328
+ c.height = height
329
+ await c.page.evaluate(FRAMES)
330
+ }
331
+
332
+ /**
333
+ * Runs in the page: hides (`visibility`, kept in layout) every fixed or sticky element that is not
334
+ * the anchor, inside it, or around it, and not an open popup (a nav that merely owns a closed
335
+ * `role=menu` is chrome), so the shell's PageBar and bottom nav are
336
+ * never drawn over a crop. A popup that opens later is not hidden: it did not exist yet.
337
+ */
338
+ export function hideChrome(name: string): void {
339
+ const anchor = Array.from(document.querySelectorAll('[data-shot]')).find(
340
+ (el) => el.getAttribute('data-shot') === name,
341
+ )
342
+ const popup = '[role="tooltip"], [role="dialog"], [role="listbox"], [role="menu"]'
343
+ const shown = (e: Element): boolean => {
344
+ const { width, height } = e.getBoundingClientRect()
345
+ return width > 0 && height > 0
346
+ }
347
+ const isOpenPopup = (e: Element, sel: string): boolean =>
348
+ (e.matches(sel) && shown(e)) || Array.from(e.querySelectorAll(sel)).some(shown)
349
+ for (const el of Array.from(document.body.querySelectorAll('*'))) {
350
+ const { position } = getComputedStyle(el)
351
+ if (position !== 'fixed' && position !== 'sticky') continue
352
+ if (anchor && (el.contains(anchor) || anchor.contains(el))) continue
353
+ if (isOpenPopup(el, popup)) continue
354
+ const style = (el as HTMLElement).style
355
+ el.setAttribute('data-shot-chrome', style.getPropertyValue('visibility'))
356
+ style.setProperty('visibility', 'hidden', 'important')
357
+ }
358
+ }
359
+
360
+ /** Runs in the page: undoes `hideChrome`. */
361
+ export function showChrome(): void {
362
+ for (const el of Array.from(document.querySelectorAll('[data-shot-chrome]'))) {
363
+ const style = (el as HTMLElement).style
364
+ const before = el.getAttribute('data-shot-chrome') ?? ''
365
+ if (before) style.setProperty('visibility', before)
366
+ else style.removeProperty('visibility')
367
+ el.removeAttribute('data-shot-chrome')
368
+ }
369
+ }
370
+
371
+ /**
372
+ * Prepares the page for one anchor's crops: shell chrome hidden, and the viewport grown to hold an
373
+ * anchor taller than it (the anchor's own scrollport would otherwise paint it blank below the first
374
+ * screen). Returns the undo.
375
+ */
376
+ async function frame(c: Ctx, name: string): Promise<() => Promise<void>> {
377
+ const spec = VIEWPORT_SPECS[c.viewport]
378
+ const undo = async (): Promise<void> => {
379
+ await c.page.evaluate(showChrome, undefined)
380
+ if (c.height !== spec.height) await resize(c, spec.height)
381
+ }
382
+ await c.page.evaluate(hideChrome, name)
383
+ try {
384
+ const anchor = anchorOf(c, name)
385
+ // A missing anchor is left for `reveal` to name; measuring it would only wait out the step.
386
+ if ((await within('locating the anchor', anchor.count())) > 0) {
387
+ const height = Number(
388
+ await within(
389
+ 'measuring the anchor',
390
+ anchor.evaluate((el) => el.getBoundingClientRect().height),
391
+ ),
392
+ )
393
+ if (height > spec.height)
394
+ await resize(c, Math.min(MAX_VIEWPORT_PX, Math.ceil(height) + spec.height))
395
+ }
396
+ } catch (error) {
397
+ // The caller never gets an undo for a frame that threw: put the page back before it surfaces.
398
+ await undo()
399
+ throw error
400
+ }
401
+ return undo
402
+ }
403
+
404
+ /** Runs in the page: px of content that scroll inside a large inner scrollport, which `fullPage` cannot see. */
405
+ export function hiddenScrollPx(): number {
406
+ let extra = 0
407
+ for (const el of Array.from(document.querySelectorAll('*'))) {
408
+ if (el.clientHeight < window.innerHeight / 2) continue
409
+ const { overflowY } = getComputedStyle(el)
410
+ if (overflowY === 'auto' || overflowY === 'scroll')
411
+ extra = Math.max(extra, el.scrollHeight - el.clientHeight)
412
+ }
413
+ return extra
414
+ }
415
+
416
+ /** The whole page: the viewport grows by what an inner scrollport hides, with the chrome kept. */
417
+ async function fullPage(c: Ctx, path: string): Promise<void> {
418
+ const spec = VIEWPORT_SPECS[c.viewport]
419
+ const extra = Number(await c.page.evaluate(hiddenScrollPx, undefined))
420
+ const grown = extra > 0
421
+ if (grown) await resize(c, Math.min(MAX_VIEWPORT_PX, spec.height + Math.ceil(extra)))
422
+ try {
423
+ await snap(c.page, path, { fullPage: true })
424
+ } finally {
425
+ if (grown) await resize(c, spec.height)
426
+ }
220
427
  }
221
428
 
222
429
  /**
@@ -231,12 +438,17 @@ async function attempt(
231
438
  note?: string,
232
439
  ): Promise<void> {
233
440
  const file = fileName(c.route, name, c.scheme, c.viewport, state)
441
+ let unframe: (() => Promise<void>) | undefined
234
442
  try {
443
+ if (name !== PAGE_ANCHOR) unframe = await frame(c, name)
235
444
  await run(join(c.input.pngDir, file))
236
445
  } catch (error) {
237
- const status = error instanceof NotDeclared ? 'not-declared' : 'error'
446
+ const status =
447
+ error instanceof NotDeclared ? 'not-declared' : error instanceof Absent ? 'absent' : 'error'
238
448
  record(c, { name, state, status, note: firstLine(error) })
239
449
  return
450
+ } finally {
451
+ await unframe?.()
240
452
  }
241
453
  // The PNG is good: a measurement failure is recorded beside it, never instead of it.
242
454
  let notes = note ? [note] : []
@@ -266,14 +478,13 @@ const skipOnMobile = (c: Ctx, name: string, state: ShotState): boolean => {
266
478
  return true
267
479
  }
268
480
 
269
- const restCrop = async (c: Ctx, name: string, path: string): Promise<void> => {
270
- const anchor = anchorOf(c, name)
271
- await reveal(anchor)
272
- await snap(anchor, path)
481
+ const restCrop = async (c: Ctx, name: string, state: ShotState, path: string): Promise<void> => {
482
+ await reveal(c, name, state)
483
+ await snap(anchorOf(c, name), path)
273
484
  }
274
485
 
275
486
  async function captureRest(c: Ctx, name: string): Promise<void> {
276
- await attempt(c, name, 'rest', (path) => restCrop(c, name, path))
487
+ await attempt(c, name, 'rest', (path) => restCrop(c, name, 'rest', path))
277
488
  }
278
489
 
279
490
  /**
@@ -329,7 +540,7 @@ export function parseHoverAt(raw: string): { x: number; y: number } | null {
329
540
 
330
541
  export async function hoverTrigger(target: PwLocator): Promise<void> {
331
542
  const at = parseHoverAt(String(await target.evaluate(hoverAtOf)))
332
- const box = at ? await target.boundingBox() : null
543
+ const box = at ? await within('measuring the trigger', target.boundingBox()) : null
333
544
  const position = at && box ? { x: at.x * box.width, y: at.y * box.height } : undefined
334
545
  await target.hover({ timeout: HOVER_TIMEOUT_MS, ...(position && { position }) })
335
546
  }
@@ -367,15 +578,15 @@ async function captureHover(c: Ctx, name: string): Promise<void> {
367
578
  if (skipOnMobile(c, name, 'hover')) return
368
579
  await attempt(c, name, 'hover', async (path) => {
369
580
  const anchor = anchorOf(c, name)
370
- await reveal(anchor)
581
+ await reveal(c, name, 'hover')
371
582
  const inner = c.page.locator(`${sel(name)} [data-shot-hover]`).first()
372
583
  const target = (await inner.count()) > 0 ? inner : anchor
373
584
  await c.page.evaluate(markVisibleTooltips, undefined)
374
585
  await hoverTrigger(target)
375
586
  const describedBy = idOf(await target.evaluate(describedByOf))
376
- const [box, tip] = [await anchor.boundingBox(), await openTooltipBox(c, describedBy)]
587
+ const [box, tip] = [await settledBox(anchor), await openTooltipBox(c, describedBy)]
377
588
  if (!box || !tip) return void (await snap(anchor, path))
378
- await snap(c.page, path, { clip: unionClip([box, tip], FOCUS_MARGIN_PX, c.viewport) })
589
+ await snap(c.page, path, { clip: unionClip([box, tip], FOCUS_MARGIN_PX, c.viewport, c.height) })
379
590
  })
380
591
  await c.page.mouse.move(0, 0)
381
592
  }
@@ -419,10 +630,13 @@ async function captureFocus(c: Ctx, name: string): Promise<void> {
419
630
  const anchor = anchorOf(c, name)
420
631
  if (!(await anchor.evaluate(hasFocusTarget)))
421
632
  throw new NotDeclared('no focus target in the anchor')
422
- await reveal(anchor)
633
+ await reveal(c, name, 'focus')
423
634
  await c.page.evaluate(markVisibleTooltips, undefined)
424
635
  await tabInto(c.page, name)
425
- const box = await anchor.boundingBox()
636
+ // Focus scrolls the nearest scrollport on its own terms (under a sticky bar, mid-animation): put
637
+ // the anchor back where the clip is taken, then read its box once it stops moving.
638
+ await within('re-centring the anchor', anchor.evaluate(revealInPage))
639
+ const box = await settledBox(anchor)
426
640
  if (!box) throw new Error('anchor has no box')
427
641
  const focused = idOf(
428
642
  await c.page.evaluate(
@@ -432,7 +646,7 @@ async function captureFocus(c: Ctx, name: string): Promise<void> {
432
646
  )
433
647
  const tip = await openTooltipBox(c, focused)
434
648
  await snap(c.page, path, {
435
- clip: unionClip(tip ? [box, tip] : [box], FOCUS_MARGIN_PX, c.viewport),
649
+ clip: unionClip(tip ? [box, tip] : [box], FOCUS_MARGIN_PX, c.viewport, c.height),
436
650
  })
437
651
  })
438
652
  await c.page.evaluate('document.activeElement?.blur()')
@@ -444,15 +658,15 @@ async function captureTooltip(c: Ctx, name: string): Promise<void> {
444
658
  await attempt(c, name, 'tooltip', async (path) => {
445
659
  if ((await trigger.count()) === 0) throw new NotDeclared('no [data-shot-tooltip] in the anchor')
446
660
  const anchor = anchorOf(c, name)
447
- await reveal(anchor)
661
+ await reveal(c, name, 'tooltip')
448
662
  await c.page.evaluate(markVisibleTooltips, undefined)
449
663
  await hoverTrigger(trigger)
450
664
  const describedBy = idOf(await trigger.evaluate(describedByOf))
451
665
  const t = await openTooltipBox(c, describedBy, TOOLTIP_TIMEOUT_MS)
452
666
  if (!t) throw new Error('no visible [role="tooltip"] after hover')
453
- const a = await anchor.boundingBox()
667
+ const a = await settledBox(anchor)
454
668
  if (!a) throw new Error('tooltip or anchor has no box')
455
- await snap(c.page, path, { clip: unionClip([a, t], FOCUS_MARGIN_PX, c.viewport) })
669
+ await snap(c.page, path, { clip: unionClip([a, t], FOCUS_MARGIN_PX, c.viewport, c.height) })
456
670
  })
457
671
  await c.page.mouse.move(0, 0)
458
672
  }
@@ -469,7 +683,15 @@ async function captureUrlState(c: Ctx, state: ShotState): Promise<void> {
469
683
  const restNames = c.facts.anchors
470
684
  if (!c.facts.declared.includes(state)) {
471
685
  for (const name of restNames) {
472
- record(c, { name, state, status: 'not-declared', note: 'page does not declare this state' })
686
+ record(c, {
687
+ name,
688
+ state,
689
+ status: 'not-declared',
690
+ note: c.facts.declared.length === 0 ? NO_STATES_NOTE : 'page does not declare this state',
691
+ ...(c.facts.declared.length === 0 && c.facts.stateful?.includes(name)
692
+ ? { stateful: true as const }
693
+ : {}),
694
+ })
473
695
  }
474
696
  return
475
697
  }
@@ -494,7 +716,7 @@ async function captureUrlState(c: Ctx, state: ShotState): Promise<void> {
494
716
  continue
495
717
  }
496
718
  const note = restNames.includes(name) ? undefined : 'anchor only present in this state'
497
- await attempt(c, name, state, (path) => restCrop(c, name, path), note)
719
+ await attempt(c, name, state, (path) => restCrop(c, name, state, path), note)
498
720
  }
499
721
  }
500
722
 
@@ -584,11 +806,14 @@ export function groupScale({ mantine, vx }: ScaleRead): Steps {
584
806
  }
585
807
 
586
808
  async function capturePage(
587
- args: Omit<Ctx, 'facts' | 'anchors' | 'shots' | 'measured' | 'problems'> & { wantScale: boolean },
809
+ args: Omit<Ctx, 'facts' | 'anchors' | 'shots' | 'measured' | 'problems' | 'height'> & {
810
+ wantScale: boolean
811
+ },
588
812
  ) {
589
813
  const { wantScale, ...rest } = args
590
814
  const c: Ctx = {
591
815
  ...rest,
816
+ height: VIEWPORT_SPECS[rest.viewport].height,
592
817
  facts: { anchors: [], declared: [] },
593
818
  anchors: [],
594
819
  shots: [],
@@ -623,10 +848,16 @@ async function capturePage(
623
848
  if (usable.length === 0 && c.scheme === 'light' && c.viewport === 'desktop') {
624
849
  input.log(`[shot] no data-shot anchors on ${route}`)
625
850
  }
851
+ const asked = URL_STATES.filter((state) => input.states.includes(state))
852
+ if (c.facts.declared.length === 0 && usable.length > 0 && asked.length > 0) {
853
+ if (c.scheme === 'light' && c.viewport === 'desktop') {
854
+ input.log(
855
+ `[shot] ${route}: no data-shot-states declared, so ${asked.join('/')} will not render; add data-shot-states to an anchor`,
856
+ )
857
+ }
858
+ }
626
859
  if (input.states.includes('rest')) {
627
- await attempt(c, PAGE_ANCHOR, 'rest', (path) =>
628
- snap(c.page, path, { fullPage: true }).then(() => undefined),
629
- )
860
+ await attempt(c, PAGE_ANCHOR, 'rest', (path) => fullPage(c, path))
630
861
  }
631
862
  flagProblems(c)
632
863
  for (const name of usable) {
@@ -9,6 +9,7 @@
9
9
  * |-|-|-|
10
10
  * | scan/shot-error | content | P0 |
11
11
  * | scan/no-anchors | content | P0 |
12
+ * | craft-7/no-declared-states | content | P2 (only when an anchor renders a state surface) |
12
13
  * | craft-5/contrast | content | P0 |
13
14
  * | craft-5/assumed-backdrop | content | P2 |
14
15
  * | craft-1/scale-error, -missing| layout | P1 |
@@ -28,7 +29,7 @@ import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from '
28
29
  import { isAbsolute, join, relative, resolve } from 'node:path'
29
30
  import { parseArgs } from 'node:util'
30
31
 
31
- import { PAGE_ANCHOR, SHOT_HOME } from './types'
32
+ import { NO_STATES_NOTE, PAGE_ANCHOR, SHOT_HOME } from './types'
32
33
  import type { AnchorMetrics, Scale, ShotMetrics, Sides } from './types'
33
34
 
34
35
  export type Severity = 'P0' | 'P1' | 'P2' | 'P3'
@@ -290,6 +291,22 @@ export function detectFindings(m: ShotMetrics, lint?: LintReport): Finding[] {
290
291
  defect: `The shot failed${s.note ? `: ${s.note}` : ''}.`,
291
292
  })
292
293
  }
294
+ const stateful = new Map<string, Set<string>>()
295
+ for (const s of m.shots) {
296
+ if (s.status !== 'not-declared' || s.note !== NO_STATES_NOTE || !s.stateful) continue
297
+ stateful.set(s.route, (stateful.get(s.route) ?? new Set()).add(s.name))
298
+ }
299
+ for (const [route, names] of stateful) {
300
+ drafts.push({
301
+ ...scan,
302
+ rule: 'craft-7/no-declared-states',
303
+ severity: 'P2',
304
+ anchor: '—',
305
+ route,
306
+ cite: `metrics.json shots[${route} not-declared]`,
307
+ defect: `${[...names].join(', ')} render${names.size === 1 ? 's' : ''} a state surface but no anchor declares data-shot-states, so empty, loading and error were never scored: add data-shot-states="empty loading error" to ${names.size === 1 ? 'it' : 'each'}.`,
308
+ })
309
+ }
293
310
  if (m.scaleError || m.scale.length === 0) {
294
311
  drafts.push({
295
312
  ...scan,
@@ -13,6 +13,16 @@ export const INSTALL_LINE =
13
13
 
14
14
  const KILL_TIMEOUT_MS = 10_000
15
15
 
16
+ /**
17
+ * A headless Chromium with no pointing device reports `(hover: none) (pointer: coarse)`, so no
18
+ * `@media (hover: hover)` rule applies in a desktop hover shot. These blink settings make the
19
+ * browser's primary input a mouse (hover 2, fine 4); a `hasTouch` mobile context still emulates
20
+ * touch over them.
21
+ */
22
+ export const POINTER_ARGS = [
23
+ '--blink-settings=primaryHoverType=2,availableHoverTypes=2,primaryPointerType=4,availablePointerTypes=4',
24
+ ]
25
+
16
26
  export type Box = { x: number; y: number; width: number; height: number }
17
27
 
18
28
  export type ShotOptions = {
@@ -21,15 +31,14 @@ export type ShotOptions = {
21
31
  caret: 'hide'
22
32
  fullPage?: boolean
23
33
  clip?: Box
34
+ timeout?: number
24
35
  }
25
36
 
26
37
  export type PwLocator = {
27
38
  first(): PwLocator
28
39
  count(): Promise<number>
29
40
  hover(options?: { timeout?: number; position?: { x: number; y: number } }): Promise<void>
30
- isVisible(): Promise<boolean>
31
41
  boundingBox(): Promise<Box | null>
32
- scrollIntoViewIfNeeded(): Promise<void>
33
42
  waitFor(options: { state: 'visible'; timeout: number }): Promise<void>
34
43
  screenshot(options: ShotOptions): Promise<unknown>
35
44
  evaluate<R>(fn: (el: Element) => R): Promise<R>
@@ -38,6 +47,7 @@ export type PwLocator = {
38
47
  export type PwPage = {
39
48
  goto(url: string, options: { waitUntil: 'load' | 'networkidle' }): Promise<unknown>
40
49
  setDefaultTimeout(ms: number): void
50
+ setViewportSize(size: { width: number; height: number }): Promise<void>
41
51
  locator(selector: string): PwLocator
42
52
  evaluate<R>(expression: string): Promise<R>
43
53
  evaluate<R, A>(fn: (arg: A) => R, arg: A): Promise<R>
@@ -69,7 +79,11 @@ export type PwBrowser<C extends PwContext = PwContext> = {
69
79
  type PwServer = { wsEndpoint(): string; kill(): Promise<void> }
70
80
  export type Playwright<B extends PwBrowser = PwBrowser> = {
71
81
  chromium: {
72
- launchServer(options: { channel?: string; chromiumSandbox: boolean }): Promise<PwServer>
82
+ launchServer(options: {
83
+ channel?: string
84
+ chromiumSandbox: boolean
85
+ args: string[]
86
+ }): Promise<PwServer>
73
87
  connect(wsEndpoint: string): Promise<B>
74
88
  }
75
89
  }
@@ -132,7 +146,9 @@ export async function withBrowser<B extends PwBrowser, T>(
132
146
  let browser: B
133
147
  try {
134
148
  server = await pw.chromium.launchServer(
135
- channel ? { channel, chromiumSandbox } : { chromiumSandbox },
149
+ channel
150
+ ? { channel, chromiumSandbox, args: POINTER_ARGS }
151
+ : { chromiumSandbox, args: POINTER_ARGS },
136
152
  )
137
153
  browser = await pw.chromium.connect(server.wsEndpoint())
138
154
  } catch (error) {
@@ -7,6 +7,9 @@ export const URL_STATES = ['empty', 'loading', 'error'] as const
7
7
  /** Where shot keeps `last.json`, `history.json` and the default out dir. */
8
8
  export const SHOT_HOME = '.basalt/shots'
9
9
 
10
+ /** The note of a URL-state shot skipped because the route declares no `data-shot-states` at all (`shot detect` reads it). */
11
+ export const NO_STATES_NOTE = 'page declares no data-shot-states'
12
+
10
13
  /** The `name` of a route's full-page shot: not a valid `data-shot` value. */
11
14
  export const PAGE_ANCHOR = '_page'
12
15
 
@@ -69,6 +72,8 @@ export type ShotRecord = {
69
72
  /** `absent`: the anchor declared itself missing in this state (`data-shot-absent`): not a defect. */
70
73
  status: 'ok' | 'not-declared' | 'absent' | 'skipped' | 'error'
71
74
  note?: string
75
+ /** The anchor renders a basalt state surface (a ChartCard, StatCard or chart state): set on the `not-declared` shots of a route that declares no `data-shot-states`. */
76
+ stateful?: true
72
77
  }
73
78
 
74
79
  /** The theme's resolved steps (px) for one scheme × viewport; a missing custom property is omitted. */
@@ -183,17 +183,17 @@ export function tokensCss(flags: string[], cwd: string = process.cwd()): number
183
183
  }
184
184
 
185
185
  /** Every `--basalt-font-*` declaration in the shipped stylesheet, in source order. */
186
- function readShippedFontDecls(pkgRoot: string): { name: string; value: string }[] {
187
- const css = readSource(pkgRoot, 'dist/styles.css') ?? readSource(pkgRoot, 'src/styles.css')
188
- if (css === null) return []
189
- const decls: { name: string; value: string }[] = []
190
- for (const match of css.matchAll(/(--basalt-font[\w-]*)\s*:\s*([^;]+);/g)) {
191
- decls.push({
192
- name: match[1] as string,
193
- value: (match[2] as string).replace(/\s+/g, ' ').trim(),
194
- })
195
- }
196
- return decls
186
+ function readShippedFontDecls(pkgRoot: string): {
187
+ face: string
188
+ decls: { name: string; value: string }[]
189
+ } {
190
+ const css = readSource(pkgRoot, 'dist/styles.css') ?? readSource(pkgRoot, 'src/styles.css') ?? ''
191
+ const face = css.match(/@font-face\s*\{[^}]*'Basalt Head Digits'[^}]*\}\n*/)?.[0] ?? ''
192
+ const decls = [...css.matchAll(/(--basalt-font[\w-]*)\s*:\s*([^;]+);/g)].map((m) => ({
193
+ name: m[1] as string,
194
+ value: (m[2] as string).replace(/\s+/g, ' ').trim(),
195
+ }))
196
+ return { face: face && `${face.trim()}\n\n`, decls }
197
197
  }
198
198
 
199
199
  /**
@@ -202,17 +202,15 @@ function readShippedFontDecls(pkgRoot: string): { name: string; value: string }[
202
202
  * The typeface half of the framework-free route, which had no supported path at all: `tokens.css`
203
203
  * emits no font vars, `styles.css` is the one place the defaults live and a framework-free consumer
204
204
  * is told not to import it, and `buildFontsCss` returns `''` unless the caller already knows the
205
- * stacks. The one non-Mantine consumer lost its font identity entirely and hardcoded the stacks —
206
- * which the theme guard then flagged as `raw-font-family`.
207
- *
208
- * The declarations are READ from the shipped stylesheet rather than restated here, so this command
209
- * and `basalt-ui/styles.css` can never name different typefaces. Note the fonts themselves are
210
- * `@fontsource-variable/*` packages: without them the stacks fall through to their own system
211
- * fallbacks, which is the intended framework-free behaviour.
205
+ * stacks. The one non-Mantine consumer lost its font identity entirely and hardcoded the stacks.
206
+ * The declarations (and the `Basalt Head Digits` face the head stack leads with) are READ from the
207
+ * shipped stylesheet, so this command and `basalt-ui/styles.css` can never name different
208
+ * typefaces. The fonts are `@fontsource-variable/*` packages (the face's `url()` is a bare package
209
+ * specifier for a bundler); without them the stacks fall back to system fonts.
212
210
  */
213
211
  export function fontsCss(flags: string[], cwd: string = process.cwd()): number {
214
212
  const pkgRoot = packageRoot()
215
- const decls = readShippedFontDecls(pkgRoot)
213
+ const { face, decls } = readShippedFontDecls(pkgRoot)
216
214
  if (decls.length === 0) {
217
215
  console.error(
218
216
  'fonts:css: could not read the shipped font stacks from styles.css — this is a packaging ' +
@@ -220,7 +218,7 @@ export function fontsCss(flags: string[], cwd: string = process.cwd()): number {
220
218
  )
221
219
  return 1
222
220
  }
223
- const body = `:root {\n${decls.map((d) => ` ${d.name}: ${d.value};`).join('\n')}\n}\n`
221
+ const body = `${face}:root {\n${decls.map((d) => ` ${d.name}: ${d.value};`).join('\n')}\n}\n`
224
222
  const header = generatedHeader(readFrameworkVersion(pkgRoot), 'fonts:css', flags)
225
223
  return emitGeneratedCss(`${header}${body}`, flags, cwd, 'fonts:css')
226
224
  }