Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(ProgressBar): a determinate fill keeps moving while JavaScript runs (#8196)

Christopher Chedeau0d67f0f6b23c
diff --git a/.changeset/progress-bar-compositor-fill.md b/.changeset/progress-bar-compositor-fill.md
new file mode 100644
index 0000000000..9313d2b6d6
--- /dev/null
+++ b/.changeset/progress-bar-compositor-fill.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A determinate ProgressBar keeps moving to its new value while a script runs. The fill animated its `width`, which lays out and repaints on the main thread every frame, so a value change stalled for as long as the page ran JavaScript and then jumped. The fill now spans the track and slides into place with a transform that the browser animates on its compositor, in Chromium and in Safari, laying out and painting nothing per frame. Under reduced motion a new value now lands at once, on every pointer. Otherwise duration, easing, colors, marks and right-to-left drawing are unchanged (the fill mirrors with the bar's own direction, so a bar in a left-to-right region of a right-to-left page still fills from the left); the track now clips its fill to the rounded rail in both modes, so below about one track height of fill the leading end follows the track's curve instead of drawing a narrower full-height capsule, and a runtime change of the bar's direction slides the fill across to the other side instead of jumping there.
+
+@vjeux
diff --git a/packages/core/src/ProgressBar/ProgressBar.test.tsx b/packages/core/src/ProgressBar/ProgressBar.test.tsx
index 47476480c8..5e1e0ab6ee 100644
--- a/packages/core/src/ProgressBar/ProgressBar.test.tsx
+++ b/packages/core/src/ProgressBar/ProgressBar.test.tsx
@@ -140,9 +140,9 @@ describe('ProgressBar', () => {
expect(progressbar).toHaveAttribute('aria-valuenow', '0');
expect(progressbar.getAttribute('aria-valuetext')).toBe('0%');
expect(screen.queryByText(/NaN/)).not.toBeInTheDocument();
- // The fill width must be a real percentage, not "NaN%".
+ // The filled share must be a real percentage, not "NaN%".
const fill = progressbar.firstElementChild as HTMLElement;
- expect(fill.style.width).toBe('0%');
+ expect(fill.style.getPropertyValue('--_progress-bar-fill')).toBe('0%');
});
it('treats a NaN max as an empty range instead of leaking "NaN"', () => {
@@ -653,16 +653,16 @@ describe('ProgressBar', () => {
);
const progressbar = screen.getByRole('progressbar');
const fill = progressbar.firstElementChild as HTMLElement;
- expect(fill.style.width).toBe('50%');
+ expect(fill.style.getPropertyValue('--_progress-bar-fill')).toBe('50%');
expect(fill.classList.contains('astryx-progress-bar-mark')).toBe(false);
expect(container.querySelectorAll(MARK)).toHaveLength(1);
});
- it('does not clip marks in determinate mode (track carries no overflow:hidden)', () => {
- // In determinate mode the track must NOT clip, so a themed taller mark
- // can overhang the bar. (Indeterminate mode re-adds the clip — covered
- // separately — but marks are suppressed there, so nothing overhangs.)
- render(
+ it('keeps marks outside the clipping track, so a themed tall mark can still overhang', () => {
+ // The track clips its fill to the rounded rail. Marks sit beside the
+ // track in its unclipped container, never inside it, so a theme that
+ // makes a mark taller than the bar still sees it overhang both edges.
+ const {container} = render(
<ProgressBar
value={50}
label="Progress"
@@ -670,9 +670,36 @@ describe('ProgressBar', () => {
/>,
);
const progressbar = screen.getByRole('progressbar');
- const trackClass = Array.from(progressbar.classList).find(c =>
- c.startsWith('x'),
- );
+ const marks = Array.from(container.querySelectorAll<HTMLElement>(MARK));
+ expect(marks).toHaveLength(1);
+ let css = '';
+ for (const sheet of Array.from(document.styleSheets)) {
+ for (const rule of Array.from(sheet.cssRules)) {
+ css += rule.cssText + '\n';
+ }
+ }
+ const clips = (el: Element) =>
+ Array.from(el.classList).some(cls =>
+ new RegExp(`\\.${cls}\\b[^{]*\\{[^}]*overflow:\\s*hidden`, 'i').test(
+ css,
+ ),
+ );
+ for (const mark of marks) {
+ expect(progressbar.contains(mark)).toBe(false);
+ // No ancestor between the mark and the bar's root clips it.
+ let node = mark.parentElement;
+ while (node != null && node !== container) {
+ expect(clips(node)).toBe(false);
+ node = node.parentElement;
+ }
+ }
+ });
+
+ it('clips the track in determinate mode (the fill slides in from the start)', () => {
+ // The determinate fill spans the whole track and is moved back by the
+ // unfilled share; the track's clip keeps the moved-out part hidden.
+ render(<ProgressBar value={30} label="Progress" />);
+ const progressbar = screen.getByRole('progressbar');
let css = '';
for (const sheet of Array.from(document.styleSheets)) {
try {
@@ -683,34 +710,83 @@ describe('ProgressBar', () => {
// ignore cross-origin sheets
}
}
- css += Array.from(document.querySelectorAll('style'))
- .map(s => s.textContent || '')
- .join('\n');
- // The atomic class that would set overflow:hidden must not be applied to
- // the track. Sanity-check the bar still rendered with a StyleX class.
- expect(trackClass).toBeDefined();
- // No rule targeting the track's classes sets overflow:hidden. (StyleX
- // atomic classes are unique per declaration; if overflow:hidden were on
- // the track we'd see it applied. We assert the track element's computed
- // intent by checking no overflow:hidden atomic is in its class list's
- // rules — simplest robust check: the track style object omits it.)
const trackHasOverflowHidden = Array.from(progressbar.classList).some(
- cls => {
- const re = new RegExp(
- `\\.${cls}\\b[^{]*\\{[^}]*overflow:\\s*hidden`,
- 'i',
- );
- return re.test(css);
- },
+ cls =>
+ new RegExp(`\\.${cls}\\b[^{]*\\{[^}]*overflow:\\s*hidden`, 'i').test(
+ css,
+ ),
);
- expect(trackHasOverflowHidden).toBe(false);
+ expect(trackHasOverflowHidden).toBe(true);
+ });
+
+ it('moves the determinate fill with a transform, not its width, except under forced colors', () => {
+ // A width transition lays out and repaints on the main thread every
+ // frame, so it stalls while a script runs; a transform transition runs
+ // on the compositor. The fill spans the track and its leading edge sits
+ // at the filled share, mirrored in RTL.
+ render(<ProgressBar value={30} label="Progress" />);
+ const fill = screen.getByRole('progressbar')
+ .firstElementChild as HTMLElement;
+ // The stylesheet's rules, apart and under forced colors.
+ let css = '';
+ let forcedCss = '';
+ const collect = (rules: CSSRuleList, forced: boolean) => {
+ for (const rule of Array.from(rules)) {
+ if ('cssRules' in rule && 'conditionText' in rule) {
+ collect(
+ (rule as CSSMediaRule).cssRules,
+ forced ||
+ /forced-colors:\s*active/.test(
+ (rule as CSSMediaRule).conditionText,
+ ),
+ );
+ } else if (forced) {
+ forcedCss += rule.cssText + '\n';
+ } else {
+ css += rule.cssText + '\n';
+ }
+ }
+ };
+ for (const sheet of Array.from(document.styleSheets)) {
+ collect(sheet.cssRules, false);
+ }
+ const declarationsIn = (text: string) =>
+ Array.from(fill.classList)
+ .flatMap(cls =>
+ Array.from(
+ text.matchAll(new RegExp(`\\.${cls}\\b[^{]*\\{([^}]*)\\}`, 'g')),
+ m => m[1].trim(),
+ ),
+ )
+ .join('\n');
+ const joined = declarationsIn(css);
+ expect(fill.style.width).toBe('');
+ expect(fill.style.getPropertyValue('--_progress-bar-fill')).toBe('30%');
+ expect(joined).toMatch(/width:\s*100%/);
+ expect(joined).toMatch(/transition-property:\s*transform/);
+ expect(joined).not.toMatch(/transition-property:\s*width/);
+ expect(joined).toMatch(/will-change:\s*transform/);
+ expect(joined).toMatch(
+ /transform:\s*translateX\(calc\(var\(--_progress-bar-fill,\s*0%\) - 100%\)\)/,
+ );
+ expect(css).toMatch(
+ // The bar's own direction (`:dir(rtl)`), not any RTL ancestor.
+ /:dir\(rtl\)[^{]*\{[^}]*transform:\s*translateX\(calc\(100% - var\(--_progress-bar-fill,\s*0%\)\)\)/,
+ );
+ // Under forced colors the fill is sized to the value instead, so the
+ // track's clip never cuts its CanvasText outline (theme-tokens INV11);
+ // the Chromium spec checks the drawn outline.
+ const forced = declarationsIn(forcedCss);
+ expect(forced).toMatch(/width:\s*var\(--_progress-bar-fill,\s*0%\)/);
+ expect(forced).toMatch(/transform:\s*none/);
+ expect(forced).toMatch(/transition-property:\s*width/);
+ expect(forced).toMatch(/will-change:\s*auto/);
});
it('clips the track in indeterminate mode (the sliding fill must not escape)', () => {
// Regression: the indeterminate fill slides from translateX -100% to
// 250%, deliberately overshooting the track, and relies on the track
- // clipping it to the visible window. Determinate mode drops the clip so
- // marks can overhang — indeterminate mode must keep it.
+ // clipping it to the visible window.
render(<ProgressBar isIndeterminate label="Loading" />);
const progressbar = screen.getByRole('progressbar');
let css = '';
diff --git a/packages/core/src/ProgressBar/ProgressBar.tsx b/packages/core/src/ProgressBar/ProgressBar.tsx
index 50937fb663..12305887fd 100644
--- a/packages/core/src/ProgressBar/ProgressBar.tsx
+++ b/packages/core/src/ProgressBar/ProgressBar.tsx
@@ -17,7 +17,7 @@
* - /packages/cli/assets/templates/blocks/components/ProgressBar/ (showcase blocks)
*/
-import {lazy, Suspense, useId} from 'react';
+import {lazy, Suspense, useId, type CSSProperties} from 'react';
import * as stylex from '@stylexjs/stylex';
import {
@@ -223,19 +223,37 @@ const styles = stylex.create({
position: 'relative',
width: '100%',
},
- // The semantic rail holds only the fill, which rounds its own corners.
+ // The semantic rail holds only the fill and clips it to the rounded rail:
+ // the determinate fill slides in from the inline-start side and the
+ // indeterminate one overshoots both ends. Marks live outside the track (in
+ // trackContainer), so the clip never cuts a themed tall mark.
track: {
width: '100%',
height: '8px',
backgroundColor: colorVars['--color-background-muted'],
borderRadius: radiusVars['--radius-full'],
- },
- // The indeterminate fill overshoots the track on both sides and must be
- // clipped to the visible rail. Marks are suppressed in this mode.
- trackClipped: {
overflow: 'hidden',
},
+ // The determinate fill spans the whole track and slides along it with a
+ // transform, so a value change animates on the compositor and keeps moving
+ // while a script runs; animating `width` would lay out and repaint on the
+ // main thread every frame and stall with it. `--_progress-bar-fill` is the
+ // filled share of the track (set inline); the fill's leading edge sits
+ // exactly there, mirrored in RTL. The mirror follows the bar's own
+ // direction (`:dir(rtl)`, the nearest `dir`), not any RTL ancestor, so a bar
+ // in an LTR region of an RTL page fills from the left like its label and
+ // marks. The layer is declared up front so Safari has the whole bar painted
+ // before it first moves.
+ //
+ // Under forced colors the fill is sized to the value instead (its `width`,
+ // no transform), as before: the track's clip would cut the CanvasText
+ // outline's start cap off a slid fill, and that outline is what keeps the
+ // fill's shape visible there (architecture:theme-tokens INV11).
fill: {
+ width: {
+ default: '100%',
+ '@media (forced-colors: active)': 'var(--_progress-bar-fill, 0%)',
+ },
height: '100%',
// Forced colors replace status fills with Canvas; this boundary keeps the
// fill's shape visible (architecture:theme-tokens INV11).
@@ -247,8 +265,32 @@ const styles = stylex.create({
},
outlineOffset: {default: null, '@media (forced-colors: active)': '-1px'},
borderRadius: radiusVars['--radius-full'],
- transitionProperty: 'width',
- transitionDuration: durationVars['--duration-medium'],
+ transform: {
+ default: 'translateX(calc(var(--_progress-bar-fill, 0%) - 100%))',
+ ':dir(rtl)': {
+ default: 'translateX(calc(100% - var(--_progress-bar-fill, 0%)))',
+ '@media (forced-colors: active)': 'none',
+ },
+ '@media (forced-colors: active)': 'none',
+ },
+ willChange: {
+ default: 'transform',
+ '@media (forced-colors: active)': 'auto',
+ },
+ transitionProperty: {
+ default: 'transform',
+ '@media (forced-colors: active)': 'width',
+ },
+ // Under reduced motion a new value lands at once, on every pointer: the
+ // fill never travels (design:reduced-motion DR1). The keys are the shared
+ // reduced-motion queries, a fine pointer's and a coarse pointer's, spelled
+ // as the record's other transitions spell them.
+ transitionDuration: {
+ default: durationVars['--duration-medium'],
+ '@media (prefers-reduced-motion: reduce) and (not (pointer: coarse))':
+ '0s',
+ '@media (prefers-reduced-motion: reduce) and (pointer: coarse)': '0s',
+ },
transitionTimingFunction: easeVars['--ease-standard'],
},
indeterminateFill: {
@@ -383,6 +425,11 @@ const markOnTrackStyles = stylex.create({
},
});
+// `CSSProperties` has no index signature for custom properties, so the fill's
+// inline style — which publishes the filled share as the private
+// `--_progress-bar-fill` — needs this augmentation to typecheck.
+type FillStyle = CSSProperties & Record<'--_progress-bar-fill', string>;
+
function defaultFormatValueLabel(value: number, max: number): string {
const pct = max > 0 ? Math.round((value / max) * 100) : 0;
return `${pct}%`;
@@ -462,6 +509,7 @@ export function ProgressBar({
const showValueLabel = hasValueLabel && !isIndeterminate;
const fillVariant = isDisabled ? 'disabled' : variant;
+ const fillStyle: FillStyle = {'--_progress-bar-fill': `${percentage}%`};
// Marks make no sense without a determinate value, so they are only drawn
// in determinate mode. Non-finite mark values are dropped; the rest are
@@ -536,7 +584,7 @@ export function ProgressBar({
aria-valuetext={isIndeterminate ? undefined : valueText}
{...mergeProps(
themeProps('progress-bar-track'),
- stylex.props(styles.track, isIndeterminate && styles.trackClipped),
+ stylex.props(styles.track),
)}>
{isIndeterminate ? (
<div
@@ -554,7 +602,7 @@ export function ProgressBar({
themeProps('progress-bar-fill', {variant: fillVariant}),
stylex.props(styles.fill, variantStyles[fillVariant]),
)}
- style={{width: `${percentage}%`}}
+ style={fillStyle}
/>
)}
</div>
diff --git a/packages/core/src/ProgressBar/__tests__/ProgressBarMotion.a11y.chromium.spec.ts b/packages/core/src/ProgressBar/__tests__/ProgressBarMotion.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..6ecf063833
--- /dev/null
+++ b/packages/core/src/ProgressBar/__tests__/ProgressBarMotion.a11y.chromium.spec.ts
@@ -0,0 +1,831 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file ProgressBarMotion.a11y.chromium.spec.ts
+ * @input Built ProgressBar stories in real Chromium (CDP screencast, tracing)
+ * @output Browser evidence that a determinate value change moves the fill on
+ * the compositor: it keeps moving while a script holds the main thread,
+ * lays out and paints nothing per frame, and its leading edge lands on the
+ * value (mirrored in RTL). Writes a storyboard (contact sheet + JSON trace)
+ * to test-results/progress-bar-motion/.
+ * @position Chromium proof for ProgressBar's sliding fill; the iOS Safari side
+ * of the same proof is a simulator recording attached to the pull request.
+ *
+ * SYNC: Keep in step with ProgressBar.tsx (`styles.fill`, `styles.track`).
+ */
+
+import fs from 'node:fs';
+import path from 'node:path';
+import {expect, test, type Page} from '@playwright/test';
+import {
+ DEFAULT_STORYBOOK_DIR,
+ serveStorybook,
+ type StaticServer,
+} from '@astryxdesign/a11y-spec/storybook';
+
+let storybook: StaticServer;
+test.beforeAll(async () => {
+ storybook = await serveStorybook(
+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,
+ );
+});
+test.afterAll(async () => {
+ await storybook?.close();
+});
+
+// Playwright empties test-results/ at the start of every run, so evidence
+// meant to outlive the run goes where ASTRYX_MOTION_EVIDENCE points.
+const EVIDENCE = path.resolve(
+ process.env.ASTRYX_MOTION_EVIDENCE ??
+ path.join('test-results', 'progress-bar-motion'),
+);
+const STORY = 'core-progressbar--default';
+const BLOCK_MS = 700;
+const DPR = 2;
+
+test.use({viewport: {width: 402, height: 200}, deviceScaleFactor: DPR});
+
+async function open(page: Page, value: number, globals = '') {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=${STORY}&viewMode=story${globals ? `&globals=${globals}` : ''}`,
+ );
+ await expect(page.getByRole('progressbar')).toBeVisible();
+ await setValue(page, value);
+ // Let any transition from the story's own value finish.
+ await settle(page);
+}
+
+/** Re-renders the story with a new value and resolves once it rendered. */
+async function setValue(page: Page, value: number) {
+ await page.evaluate(
+ async ({storyId, updatedArgs}) =>
+ new Promise<void>(resolve => {
+ const channel = (
+ window as unknown as {
+ __STORYBOOK_ADDONS_CHANNEL__: {
+ emit: (event: string, payload: unknown) => void;
+ once: (event: string, handler: () => void) => void;
+ };
+ }
+ ).__STORYBOOK_ADDONS_CHANNEL__;
+ channel.once('storyRendered', () => resolve());
+ channel.emit('updateStoryArgs', {storyId, updatedArgs});
+ setTimeout(resolve, 1500);
+ }),
+ {storyId: STORY, updatedArgs: {value}},
+ );
+}
+
+/**
+ * Sets a new value and, once the fill's transition has started and one more
+ * frame has been presented, holds the main thread: the pattern of a progress
+ * update followed by the work it reports on. Returns the block's start and
+ * end (epoch ms).
+ */
+async function setValueThenBlock(page: Page, value: number, ms: number) {
+ return page.evaluate(
+ async ({storyId, updatedArgs, duration}) =>
+ new Promise<{from: number; to: number}>(resolve => {
+ const channel = (
+ window as unknown as {
+ __STORYBOOK_ADDONS_CHANNEL__: {
+ emit: (event: string, payload: unknown) => void;
+ once: (event: string, handler: () => void) => void;
+ };
+ }
+ ).__STORYBOOK_ADDONS_CHANNEL__;
+ const onStart = (event: Event) => {
+ const bar = document.querySelector('[role="progressbar"]');
+ if (bar == null || !bar.contains(event.target as Node)) {
+ return;
+ }
+ document.removeEventListener('transitionstart', onStart, true);
+ requestAnimationFrame(() =>
+ setTimeout(() => {
+ const from = performance.timeOrigin + performance.now();
+ const end = performance.now() + duration;
+ while (performance.now() < end) {
+ // hold
+ }
+ resolve({from, to: performance.timeOrigin + performance.now()});
+ }),
+ );
+ };
+ document.addEventListener('transitionstart', onStart, true);
+ channel.emit('updateStoryArgs', {storyId, updatedArgs});
+ }),
+ {storyId: STORY, updatedArgs: {value}, duration: ms},
+ );
+}
+
+interface Frame {
+ /** Presentation time, epoch ms. */
+ readonly t: number;
+ readonly data: string;
+}
+
+/** Compositor frames, captured off the main thread. */
+async function startScreencast(page: Page) {
+ const cdp = await page.context().newCDPSession(page);
+ const frames: Frame[] = [];
+ const acks: Promise<unknown>[] = [];
+ const ackFailures: string[] = [];
+ cdp.on('Page.screencastFrame', frame => {
+ frames.push({
+ t: (frame.metadata.timestamp ?? Date.now() / 1000) * 1000,
+ data: frame.data,
+ });
+ acks.push(
+ cdp
+ .send('Page.screencastFrameAck', {sessionId: frame.sessionId})
+ .catch((error: unknown) => {
+ ackFailures.push(String(error));
+ }),
+ );
+ });
+ const viewport = page.viewportSize() ?? {width: 402, height: 200};
+ await cdp.send('Page.startScreencast', {
+ format: 'png',
+ maxWidth: viewport.width * DPR,
+ maxHeight: viewport.height * DPR,
+ everyNthFrame: 1,
+ });
+ return {
+ async stop() {
+ await cdp.send('Page.stopScreencast');
+ await Promise.all(acks);
+ await cdp.detach();
+ // A lost acknowledgement stalls the screencast and thins the frames.
+ expect(ackFailures).toEqual([]);
+ return frames;
+ },
+ };
+}
+
+interface Box {
+ x: number;
+ y: number;
+ width: number;
+ height: number;
+}
+
+async function trackBox(page: Page): Promise<Box> {
+ const box = await page.getByRole('progressbar').boundingBox();
+ if (box == null) {
+ throw new Error('no track box');
+ }
+ return box;
+}
+
+/**
+ * The fill's leading edge in each frame, in CSS px from the track's start:
+ * along the track's middle row, the last pixel that still has the fill's
+ * color (sampled at the track's start, where the fill always is once p > 0).
+ */
+async function leadingEdges(
+ page: Page,
+ frames: ReadonlyArray<Frame>,
+ track: Box,
+): Promise<number[]> {
+ const scratch = await page.context().newPage();
+ const viewportWidth = page.viewportSize()?.width ?? 402;
+ try {
+ const out: number[] = [];
+ for (let i = 0; i < frames.length; i += 10) {
+ const batch = frames.slice(i, i + 10).map(f => f.data);
+ out.push(
+ ...(await scratch.evaluate(
+ async ({items, box, vw}) => {
+ const result: number[] = [];
+ for (const item of items) {
+ const bitmap = await createImageBitmap(
+ await (await fetch(`data:image/png;base64,${item}`)).blob(),
+ );
+ const scale = bitmap.width / vw;
+ const w = Math.round(box.width * scale);
+ const canvas = new OffscreenCanvas(w, 1);
+ const context = canvas.getContext('2d');
+ if (context == null) {
+ throw new Error('no 2d context');
+ }
+ const y = Math.round((box.y + box.height / 2) * scale);
+ context.drawImage(
+ bitmap,
+ Math.round(box.x * scale),
+ y,
+ w,
+ 1,
+ 0,
+ 0,
+ w,
+ 1,
+ );
+ const {data} = context.getImageData(0, 0, w, 1);
+ // The fill's color, a few pixels in from the start; the track's
+ // color at the far end.
+ const at = (x: number) => [
+ data[x * 4],
+ data[x * 4 + 1],
+ data[x * 4 + 2],
+ ];
+ const fill = at(Math.round(6 * scale));
+ const rest = at(w - Math.round(6 * scale));
+ const near = (c: number[], d: number[]) =>
+ Math.abs(c[0] - d[0]) +
+ Math.abs(c[1] - d[1]) +
+ Math.abs(c[2] - d[2]) <
+ Math.abs(c[0] - rest[0]) +
+ Math.abs(c[1] - rest[1]) +
+ Math.abs(c[2] - rest[2]);
+ let edge = 0;
+ for (let x = 0; x < w; x += 1) {
+ if (near(at(x), fill)) {
+ edge = x + 1;
+ }
+ }
+ result.push(edge / scale);
+ }
+ return result;
+ },
+ {items: batch, box: track, vw: viewportWidth},
+ )),
+ );
+ }
+ return out;
+ } finally {
+ await scratch.close();
+ }
+}
+
+test('the fill keeps moving to a new value while a script holds the main thread', async ({
+ page,
+}) => {
+ await open(page, 10);
+ const track = await trackBox(page);
+ const cast = await startScreencast(page);
+ await page.waitForTimeout(300);
+ const busy = await setValueThenBlock(page, 90, BLOCK_MS);
+ await page.waitForTimeout(300);
+ const frames = await cast.stop();
+ const edges = await leadingEdges(page, frames, track);
+ const start = track.width * 0.1;
+ const target = track.width * 0.9;
+ // Inside the block, a frame clear of each edge.
+ const inside = frames
+ .map((frame, i) => ({t: frame.t, edge: edges[i]}))
+ .filter(f => f.t >= busy.from + 20 && f.t <= busy.to - 20);
+ // The transition (the theme's `--duration-medium`) is shorter than the
+ // block, so the fill must make its whole trip inside it: frame after frame
+ // through distinct positions…
+ const positions = new Set(inside.map(f => Math.round(f.edge)));
+ expect(positions.size).toBeGreaterThanOrEqual(5);
+ // …and arrives before the block ends (the transition is shorter than it).
+ const last = inside[inside.length - 1].edge;
+ expect(Math.abs(last - target)).toBeLessThan(2);
+ // It never runs backwards.
+ for (let i = 1; i < inside.length; i += 1) {
+ expect(inside[i].edge).toBeGreaterThanOrEqual(inside[i - 1].edge - 1);
+ }
+ expect(inside[0].edge).toBeLessThan(target - (target - start) * 0.2);
+});
+
+test('moving the fill lays out and paints nothing per frame', async ({
+ page,
+ browser,
+}) => {
+ await open(page, 20);
+ await browser.startTracing(page, {
+ categories: ['devtools.timeline', 'disabled-by-default-devtools.timeline'],
+ });
+ await setValue(page, 80);
+ // Traced until the change has finished, however long the machine takes.
+ await settle(page);
+ const trace = JSON.parse((await browser.stopTracing()).toString()) as {
+ traceEvents: {
+ name: string;
+ ph: string;
+ pid: number;
+ tid: number;
+ args?: {name?: string};
+ }[];
+ };
+ const mainThreads = new Set(
+ trace.traceEvents
+ .filter(e => e.ph === 'M' && e.name === 'thread_name')
+ .filter(e => e.args?.name === 'CrRendererMain')
+ .map(e => `${e.pid}:${e.tid}`),
+ );
+ const count = (name: string) =>
+ trace.traceEvents.filter(
+ e => e.name === name && mainThreads.has(`${e.pid}:${e.tid}`),
+ ).length;
+ // The update itself may lay out and paint once; a width transition then
+ // lays out and repaints every frame until it ends (about 18 times here).
+ expect(count('Layout')).toBeLessThanOrEqual(2);
+ expect(count('Paint')).toBeLessThanOrEqual(2);
+});
+
+/** Resolves once the fill's transitions, if any, have finished. */
+async function settle(page: Page) {
+ await page.evaluate(async () => {
+ const fill = document.querySelector(
+ '[role="progressbar"]',
+ )?.firstElementChild;
+ if (fill == null) {
+ throw new Error('no progressbar');
+ }
+ // A new value starts its transition on the frame that applies it.
+ await new Promise(requestAnimationFrame);
+ await new Promise(requestAnimationFrame);
+ await Promise.all(
+ fill.getAnimations().map(async animation => animation.finished),
+ );
+ });
+}
+
+// The story's own direction makes a real RTL region (Storybook keeps the
+// story inside its own `dir`); an LTR region inside an RTL page fills from
+// the left, like its label and marks.
+for (const {name, globals, nested, fromLeft} of [
+ {name: 'ltr', globals: '', nested: false, fromLeft: true},
+ {name: 'rtl', globals: 'direction:rtl', nested: false, fromLeft: false},
+ {
+ name: 'ltr inside rtl',
+ globals: 'direction:rtl',
+ nested: true,
+ fromLeft: true,
+ },
+]) {
+ test(`the leading edge sits at the value, mirrored in an RTL region: ${name}`, async ({
+ page,
+ }) => {
+ await open(page, 0, globals);
+ if (nested) {
+ await page.evaluate(() => {
+ const bar = document.querySelector('[role="progressbar"]');
+ bar?.parentElement?.setAttribute('dir', 'ltr');
+ });
+ }
+ for (const value of [0, 25, 60, 100]) {
+ await setValue(page, value);
+ await settle(page);
+ const {track, fill, direction} = await page.evaluate(() => {
+ const bar = document.querySelector('[role="progressbar"]');
+ const child = bar?.firstElementChild;
+ if (bar == null || child == null) {
+ throw new Error('no progressbar');
+ }
+ const r = (e: Element) => {
+ const b = e.getBoundingClientRect();
+ return {left: b.left, right: b.right, width: b.width};
+ };
+ return {
+ track: r(bar),
+ fill: r(child),
+ direction: getComputedStyle(bar).direction,
+ };
+ });
+ expect(direction, name).toBe(fromLeft ? 'ltr' : 'rtl');
+ // The fill spans the track and is moved back by the unfilled share.
+ expect(fill.width).toBeCloseTo(track.width, 1);
+ const filled = (track.width * value) / 100;
+ if (fromLeft) {
+ expect(fill.right - track.left, `${name} ${value}`).toBeCloseTo(
+ filled,
+ 0,
+ );
+ } else {
+ expect(track.right - fill.left, `${name} ${value}`).toBeCloseTo(
+ filled,
+ 0,
+ );
+ }
+ }
+ });
+}
+
+test('an ordinary change keeps the theme\u2019s duration and easing', async ({
+ page,
+}) => {
+ await open(page, 20);
+ const box = await trackBox(page);
+ // The transition the width used to run: the same tokens, now on transform.
+ const timing = await page.evaluate(() => {
+ const fill = document.querySelector('[role="progressbar"]')
+ ?.firstElementChild as HTMLElement;
+ const root = getComputedStyle(document.documentElement);
+ const style = getComputedStyle(fill);
+ return {
+ property: style.transitionProperty,
+ duration: style.transitionDuration,
+ easing: style.transitionTimingFunction,
+ tokenDuration:
+ getComputedStyle(fill).getPropertyValue('--duration-medium').trim() ||
+ root.getPropertyValue('--duration-medium').trim(),
+ };
+ });
+ expect(timing.property).toBe('transform');
+ // Sample the change itself: 20% → 80%, held at five points of its time.
+ await page.evaluate(
+ async ({storyId}) =>
+ new Promise<void>(resolve => {
+ const onStart = () => {
+ document.removeEventListener('transitionstart', onStart, true);
+ const fill = document.querySelector('[role="progressbar"]')
+ ?.firstElementChild as HTMLElement;
+ for (const animation of fill.getAnimations()) {
+ animation.pause();
+ }
+ resolve();
+ };
+ document.addEventListener('transitionstart', onStart, true);
+ (
+ window as unknown as {
+ __STORYBOOK_ADDONS_CHANNEL__: {
+ emit: (event: string, payload: unknown) => void;
+ };
+ }
+ ).__STORYBOOK_ADDONS_CHANNEL__.emit('updateStoryArgs', {
+ storyId,
+ updatedArgs: {value: 80},
+ });
+ }),
+ {storyId: STORY},
+ );
+ const samples = await page.evaluate(() => {
+ const fill = document.querySelector('[role="progressbar"]')
+ ?.firstElementChild as HTMLElement;
+ const [transition] = fill.getAnimations();
+ const duration = Number(transition.effect?.getTiming().duration);
+ const easing = String(transition.effect?.getTiming().easing);
+ // The easing, evaluated by the browser through a detached animation.
+ const probe = new Animation(
+ new KeyframeEffect(null, null, {duration: 1000, easing, fill: 'both'}),
+ );
+ const out: {t: number; eased: number; right: number}[] = [];
+ for (const t of [0, 0.25, 0.5, 0.75, 1]) {
+ transition.currentTime = Math.min(t * duration, duration - 0.01);
+ probe.currentTime = t * 1000;
+ out.push({
+ t,
+ eased: probe.effect?.getComputedTiming().progress ?? t,
+ right: fill.getBoundingClientRect().right,
+ });
+ }
+ return {duration, easing, out};
+ });
+ // The duration is the theme's `--duration-medium`, the easing its
+ // `--ease-standard`, as before.
+ expect(samples.duration).toBeCloseTo(
+ Number.parseFloat(timing.duration) *
+ (timing.duration.endsWith('ms') ? 1 : 1000),
+ 0,
+ );
+ expect(samples.easing).toBe(timing.easing);
+ for (const {t, eased, right} of samples.out) {
+ const expected = box.x + box.width * (0.2 + 0.6 * eased);
+ expect(Math.abs(right - expected), `edge at ${t}`).toBeLessThan(1);
+ }
+});
+
+for (const pointer of ['a mouse', 'a finger'] as const) {
+ test.describe(`with ${pointer}`, () => {
+ test.use(
+ pointer === 'a finger'
+ ? {hasTouch: true, isMobile: true}
+ : {hasTouch: false, isMobile: false},
+ );
+
+ test('under reduced motion a new value lands at once', async ({page}) => {
+ // Both shared reduced-motion queries, a fine pointer's and a coarse
+ // pointer's, set the fill's duration to 0 s.
+ await page.emulateMedia({reducedMotion: 'reduce'});
+ await open(page, 20);
+ expect(
+ await page.evaluate(() => matchMedia('(pointer: coarse)').matches),
+ ).toBe(pointer === 'a finger');
+ // Read the fill in the same task React commits the new value: a running
+ // transition shows up there, before any frame could finish it.
+ const state = await page.evaluate(
+ async ({storyId}) =>
+ new Promise<{running: number; edge: number}>(resolve => {
+ const track = document.querySelector(
+ '[role="progressbar"]',
+ ) as Element;
+ const fill = track.firstElementChild as HTMLElement;
+ const observer = new MutationObserver(() => {
+ observer.disconnect();
+ const t = track.getBoundingClientRect();
+ resolve({
+ running: fill.getAnimations().length,
+ edge: (fill.getBoundingClientRect().right - t.left) / t.width,
+ });
+ });
+ observer.observe(fill, {
+ attributes: true,
+ attributeFilter: ['style'],
+ });
+ (
+ window as unknown as {
+ __STORYBOOK_ADDONS_CHANNEL__: {
+ emit: (event: string, payload: unknown) => void;
+ };
+ }
+ ).__STORYBOOK_ADDONS_CHANNEL__.emit('updateStoryArgs', {
+ storyId,
+ updatedArgs: {value: 80},
+ });
+ }),
+ {storyId: STORY},
+ );
+ // No transition, and the fill is already at its new value: nothing travels.
+ expect(state.running).toBe(0);
+ expect(state.edge).toBeCloseTo(0.8, 2);
+ });
+ });
+}
+
+test('below one track height of fill, the leading end follows the track\u2019s curve', async ({
+ page,
+}) => {
+ await open(page, 0);
+ await setValue(page, 1);
+ await settle(page);
+ const box = await trackBox(page);
+ const colors = await page.evaluate(() => {
+ const bar = document.querySelector('[role="progressbar"]') as HTMLElement;
+ const parse = (value: string) =>
+ (value.match(/[\d.]+/g) ?? []).slice(0, 3).map(Number);
+ return {
+ fill: parse(
+ getComputedStyle(bar.firstElementChild as Element).backgroundColor,
+ ),
+ track: parse(getComputedStyle(bar).backgroundColor),
+ };
+ });
+ const shot = await page.screenshot({
+ clip: {x: box.x, y: box.y, width: 24, height: box.height},
+ });
+ const ink = await page.evaluate(
+ async ({png, h, fill, track}) => {
+ const bitmap = await createImageBitmap(
+ await (await fetch(`data:image/png;base64,${png}`)).blob(),
+ );
+ const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
+ const context = canvas.getContext('2d');
+ if (context == null) {
+ throw new Error('no 2d context');
+ }
+ context.drawImage(bitmap, 0, 0);
+ const {data, width, height} = context.getImageData(
+ 0,
+ 0,
+ bitmap.width,
+ bitmap.height,
+ );
+ // How much of each pixel is the fill: its color projected on the line
+ // from the track's color to the fill's (the page around the track's
+ // rounded end projects below zero).
+ const axis = [0, 1, 2].map(k => fill[k] - track[k]);
+ const norm = axis.reduce((sum, v) => sum + v * v, 0);
+ const coverage = (x: number, y: number) => {
+ const i = (y * width + x) * 4;
+ const dot = [0, 1, 2].reduce(
+ (sum, k) => sum + (data[i + k] - track[k]) * axis[k],
+ 0,
+ );
+ return Math.min(1, Math.max(0, dot / norm));
+ };
+ const columns: number[] = [];
+ for (let x = 0; x < width; x += 1) {
+ let count = 0;
+ for (let y = 0; y < height; y += 1) {
+ count += coverage(x, y);
+ }
+ columns.push(count);
+ }
+ const scale = bitmap.height / h;
+ return {
+ scale,
+ width: columns.filter(c => c > 0.5).length / scale,
+ heights: columns.map(c => c / scale),
+ };
+ },
+ {png: shot.toString('base64'), h: box.height, ...colors},
+ );
+ // About 1% of the track wide...
+ expect(ink.width).toBeGreaterThan(1);
+ expect(ink.width).toBeLessThanOrEqual((box.width * 1) / 100 + 1.5);
+ // ...and shaped by the track's own rounded end (radius half its height),
+ // column by column, where a squeezed capsule (main) rounds off with half of
+ // its own 3.7 px width and stands nearly full height at once.
+ const radius = box.height / 2;
+ for (let column = 0; column < 4; column += 1) {
+ const x = (column + 0.5) / ink.scale;
+ const expected = 2 * Math.sqrt(radius ** 2 - (radius - x) ** 2);
+ expect(
+ Math.abs(ink.heights[column] - expected),
+ `height at ${x.toFixed(2)} px`,
+ ).toBeLessThanOrEqual(1);
+ }
+});
+
+test('in forced colors the fill keeps its whole outline, a closed capsule inside the track', async ({
+ page,
+}) => {
+ // architecture:theme-tokens INV11: forced colors paint the fill with
+ // Canvas, and its CanvasText outline keeps its shape visible. The track
+ // clips what it holds, so the outline stays whole only when the fill ends
+ // where it is drawn: under forced colors the fill is sized to the value, not
+ // slid along the track.
+ await page.emulateMedia({forcedColors: 'active'});
+ for (const {globals, fromLeft} of [
+ {globals: '', fromLeft: true},
+ {globals: 'direction:rtl', fromLeft: false},
+ ]) {
+ await open(page, 0, globals);
+ for (const value of [2, 50]) {
+ await setValue(page, value);
+ await settle(page);
+ const box = await trackBox(page);
+ const {track, fill, outline} = await page.evaluate(() => {
+ const bar = document.querySelector(
+ '[role="progressbar"]',
+ ) as HTMLElement;
+ const child = bar.firstElementChild as HTMLElement;
+ const r = (e: Element) => {
+ const b = e.getBoundingClientRect();
+ return {left: b.left, right: b.right};
+ };
+ return {
+ track: r(bar),
+ fill: r(child),
+ outline: (getComputedStyle(child).outlineColor.match(/[\d.]+/g) ?? [])
+ .slice(0, 3)
+ .map(Number),
+ };
+ });
+ const label = `${globals || 'ltr'} ${value}%`;
+ // The fill's box lies inside the track and ends at the value.
+ const filled = ((track.right - track.left) * value) / 100;
+ if (fromLeft) {
+ expect(fill.left - track.left, label).toBeCloseTo(0, 0);
+ expect(fill.right - track.left, label).toBeCloseTo(filled, 0);
+ } else {
+ expect(track.right - fill.right, label).toBeCloseTo(0, 0);
+ expect(track.right - fill.left, label).toBeCloseTo(filled, 0);
+ }
+ // The outline's cap at the track's inline-start edge is drawn: at mid
+ // height the edge's first pixels are the outline's color.
+ const shot = await page.screenshot({
+ clip: {
+ x: fromLeft ? box.x : box.x + box.width - 2,
+ y: box.y,
+ width: 2,
+ height: box.height,
+ },
+ });
+ const nearest = await page.evaluate(
+ async ({png, outline}) => {
+ const bitmap = await createImageBitmap(
+ await (await fetch(`data:image/png;base64,${png}`)).blob(),
+ );
+ const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
+ const context = canvas.getContext('2d');
+ if (context == null) {
+ throw new Error('no 2d context');
+ }
+ context.drawImage(bitmap, 0, 0);
+ const {data, width, height} = context.getImageData(
+ 0,
+ 0,
+ bitmap.width,
+ bitmap.height,
+ );
+ const y = Math.floor(height / 2);
+ let best = Infinity;
+ for (let x = 0; x < width; x += 1) {
+ const i = (y * width + x) * 4;
+ const distance = Math.max(
+ ...[0, 1, 2].map(k => Math.abs(data[i + k] - outline[k])),
+ );
+ best = Math.min(best, distance);
+ }
+ return best;
+ },
+ {png: shot.toString('base64'), outline},
+ );
+ expect(nearest, `${label} start cap`).toBeLessThanOrEqual(64);
+ }
+ }
+});
+
+test('storyboard: the fill moving from 20% to 80% @storyboard', async ({
+ page,
+}, testInfo) => {
+ await open(page, 20);
+ const track = await trackBox(page);
+ const cast = await startScreencast(page);
+ await page.waitForTimeout(200);
+ await setValue(page, 80);
+ // Recorded until the change has finished, however long the machine takes.
+ await settle(page);
+ const frames = await cast.stop();
+ const edges = await leadingEdges(page, frames, track);
+ // The trace is the assertion: the edge moves forward only, from 20% to
+ // 80%, with no jump bigger than a quarter of the travel between frames.
+ const travel = track.width * 0.6;
+ expect(edges[0]).toBeCloseTo(track.width * 0.2, 0);
+ expect(edges[edges.length - 1]).toBeCloseTo(track.width * 0.8, 0);
+ for (let i = 1; i < edges.length; i += 1) {
+ expect(edges[i]).toBeGreaterThanOrEqual(edges[i - 1] - 1);
+ expect(edges[i] - edges[i - 1]).toBeLessThan(travel / 4);
+ }
+ const moving = frames
+ .map((frame, i) => ({t: frame.t, edge: edges[i], data: frame.data}))
+ .filter(
+ (f, i) =>
+ i === 0 ||
+ Math.abs(f.edge - edges[i - 1]) > 0.25 ||
+ i === edges.length - 1,
+ );
+ fs.mkdirSync(EVIDENCE, {recursive: true});
+ fs.writeFileSync(
+ path.join(EVIDENCE, 'progress-bar-fill-move.json'),
+ JSON.stringify(
+ {
+ story: STORY,
+ from: 20,
+ to: 80,
+ trackWidth: track.width,
+ frames: moving.map(f => ({
+ t: Math.round(f.t - moving[0].t),
+ edge: Math.round(f.edge * 10) / 10,
+ })),
+ },
+ null,
+ 1,
+ ),
+ );
+ // The contact sheet: the track in every frame where the fill moved.
+ const sheet = await page.context().newPage();
+ try {
+ const png = await sheet.evaluate(
+ async ({items, box, vw}) => {
+ const decoded = await Promise.all(
+ items.map(async item =>
+ createImageBitmap(
+ await (await fetch(`data:image/png;base64,${item}`)).blob(),
+ ),
+ ),
+ );
+ const scale = decoded[0].width / vw;
+ const pad = 6 * scale;
+ const w = Math.round(box.width * scale + pad * 2);
+ const h = Math.round(box.height * scale + pad * 2);
+ const canvas = new OffscreenCanvas(w, decoded.length * h);
+ const context = canvas.getContext('2d');
+ if (context == null) {
+ throw new Error('no 2d context');
+ }
+ context.fillStyle = '#fff';
+ context.fillRect(0, 0, canvas.width, canvas.height);
+ decoded.forEach((bitmap, i) => {
+ context.drawImage(
+ bitmap,
+ Math.round(box.x * scale - pad),
+ Math.round(box.y * scale - pad),
+ w,
+ h,
+ 0,
+ i * h,
+ w,
+ h,
+ );
+ });
+ const blob = await canvas.convertToBlob({type: 'image/png'});
+ const bytes = new Uint8Array(await blob.arrayBuffer());
+ let binary = '';
+ for (const byte of bytes) {
+ binary += String.fromCharCode(byte);
+ }
+ return btoa(binary);
+ },
+ {
+ items: moving.map(f => f.data),
+ box: track,
+ vw: page.viewportSize()?.width ?? 402,
+ },
+ );
+ fs.writeFileSync(
+ path.join(EVIDENCE, 'progress-bar-fill-move.png'),
+ Buffer.from(png, 'base64'),
+ );
+ await testInfo.attach('progress-bar-fill-move.png', {
+ path: path.join(EVIDENCE, 'progress-bar-fill-move.png'),
+ contentType: 'image/png',
+ });
+ } finally {
+ await sheet.close();
+ }
+});
diff --git a/packages/core/src/theme/derivedVarRegistry.test.ts b/packages/core/src/theme/derivedVarRegistry.test.ts
index 74cd3ff656..d8ca838b6a 100644
--- a/packages/core/src/theme/derivedVarRegistry.test.ts
+++ b/packages/core/src/theme/derivedVarRegistry.test.ts
@@ -106,6 +106,10 @@ const STRUCTURAL_VARS = new Set([
// authored by a theme.
'--_layout-flush-block-start',
'--_layout-flush-block-end',
+ // ProgressBar's filled share of the track, written inline from `value` on
+ // every render and read by the fill's transform. Runtime state, never
+ // authored by a theme.
+ '--_progress-bar-fill',
]);
/**