An open source design system that's fully customizable and agent ready
TypeScript
fix(Skeleton): the pulse keeps going in Safari while JavaScript runs (#8184)
Christopher Chedeau4d604689663c
diff --git a/.changeset/skeleton-pulse-compositor.md b/.changeset/skeleton-pulse-compositor.mdnew file mode 100644index 0000000000..3ca533de8b--- /dev/null+++ b/.changeset/skeleton-pulse-compositor.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] Skeleton keeps pulsing in Safari while a script runs. Its stepped pulse used a `steps()` timing function, which Safari never animates off the main thread, so the placeholders froze for as long as the page ran JavaScript; Chromium and Firefox were unaffected. The same ten steps are now written as keyframes with a linear timing function, which every engine animates on its compositor. The pulse looks and times exactly as before, including its stagger and reduced-motion behavior.++@vjeuxdiff --git a/packages/core/src/Skeleton/Skeleton.test.tsx b/packages/core/src/Skeleton/Skeleton.test.tsxindex 3407d94af4..029ce5f744 100644--- a/packages/core/src/Skeleton/Skeleton.test.tsx+++ b/packages/core/src/Skeleton/Skeleton.test.tsx@@ -3,7 +3,63 @@import {describe, it, expect} from 'vitest';import {render, screen} from '@testing-library/react';import {Skeleton} from './Skeleton';-import {getForcedColorsRules} from '../__tests__/forcedColors';+import {+ getAllInjectedCss,+ getForcedColorsRules,+} from '../__tests__/forcedColors';++/** The declarations the element's atomic classes carry, as `prop: value`. */+function declarationsOf(el: Element, css: string): string[] {+ const out: string[] = [];+ for (const cls of Array.from(el.classList)) {+ const re = new RegExp(`\\.${cls}\\b[^{]*\\{([^}]*)\\}`, 'g');+ for (const match of css.matchAll(re)) {+ out.push(+ ...match[1]+ .split(';')+ .map(d => d.trim())+ .filter(Boolean),+ );+ }+ }+ return out;+}++/** The whole `@keyframes <name> { … }` rule, braces matched. */+function keyframesRule(css: string, name: string): string {+ const start = css.search(new RegExp(`@keyframes ${name}\\b`, 'i'));+ expect(start).toBeGreaterThanOrEqual(0);+ let depth = 0;+ for (let i = css.indexOf('{', start); i < css.length; i += 1) {+ if (css[i] === '{') {+ depth += 1;+ }+ if (css[i] === '}' && --depth === 0) {+ return css.slice(start, i + 1);+ }+ }+ throw new Error(`unterminated @keyframes ${name}`);+}++/** The named animation's opacity keyframes as [offset 0..1, opacity], in order. */+function opacityKeyframes(css: string, name: string): [number, number][] {+ return Array.from(+ keyframesRule(css, name).matchAll(/([\d.]+)%\s*\{\s*opacity:\s*([\d.]+)/g),+ m => [Number(m[1]) / 100, Number(m[2])] as [number, number],+ );+}++/** Opacity at iteration progress x, linear between keyframes. */+function opacityAt(frames: [number, number][], x: number): number {+ for (let i = 1; i < frames.length; i += 1) {+ const [x0, y0] = frames[i - 1];+ const [x1, y1] = frames[i];+ if (x <= x1) {+ return x1 === x0 ? y1 : y0 + ((y1 - y0) * (x - x0)) / (x1 - x0);+ }+ }+ return frames[frames.length - 1][1];+}describe('Skeleton', () => {it('renders a placeholder element', () => {@@ -25,6 +81,57 @@ describe('Skeleton', () => {});});+describe('pulse', () => {+ function pulse() {+ render(<Skeleton width={200} height={20} data-testid="sk" />);+ const css = getAllInjectedCss();+ const declarations = declarationsOf(screen.getByTestId('sk'), css);+ const value = (prop: string) =>+ declarations+ .filter(d => d.startsWith(`${prop}:`))+ .map(d => d.slice(prop.length + 1).trim());+ return {css, value};+ }++ // WebKit runs no animation with a steps() timing function on the+ // compositor, so a stepped pulse would freeze in Safari while a script runs.+ it('keeps steps() out of the timing function so every engine can composite it', () => {+ const {css, value} = pulse();+ expect(value('animation-timing-function')).toEqual(['linear']);+ expect(value('animation-direction')).toEqual(['alternate']);+ expect(value('animation-iteration-count')).toEqual(['infinite']);+ const name = value('animation-name').find(v => v !== 'none');+ expect(name).toBeTruthy();+ const rule = keyframesRule(css, name ?? '');+ expect(rule).toMatch(/opacity/);+ expect(rule).not.toMatch(/steps\(|animation-timing-function/);+ });++ it('climbs and falls through the same ten opacity steps at the same times', () => {+ const {css, value} = pulse();+ const name = value('animation-name').find(v => v !== 'none') ?? '';+ const frames = opacityKeyframes(css, name);+ expect(frames[0]).toEqual([0, 0.25]);+ // The previous pulse: 0.25 → 1 through steps(10, end), played back down+ // by `alternate`. Sample each tenth away from its edges.+ for (let step = 0; step < 10; step += 1) {+ for (const within of [0.02, 0.5, 0.98]) {+ const x = (step + within) / 10;+ const expected = 0.25 + 0.75 * (step / 10);+ expect(opacityAt(frames, x)).toBeCloseTo(expected, 6);+ }+ }+ // Each change between steps takes 0.01% of an iteration.+ for (let i = 1; i < frames.length; i += 1) {+ const [x0, y0] = frames[i - 1];+ const [x1, y1] = frames[i];+ if (y1 !== y0) {+ expect(x1 - x0).toBeCloseTo(0.0001, 6);+ }+ }+ });+});+// jsdom cannot emulate forced-colors rendering, so these assert that the// compiled output includes the forced-colors rules; visual behavior needs// manual verification under Windows High Contrast.diff --git a/packages/core/src/Skeleton/Skeleton.tsx b/packages/core/src/Skeleton/Skeleton.tsxindex d8d0692935..705331604f 100644--- a/packages/core/src/Skeleton/Skeleton.tsx+++ b/packages/core/src/Skeleton/Skeleton.tsx@@ -39,9 +39,36 @@ const STAGGER_TIME = 100;// Animation Keyframes// =============================================================================+/**+ * The pulse climbs from 0.25 to 0.925 opacity in ten equal steps, then plays+ * back down (`alternate`). The steps are written out as holds joined by+ * 0.01% ramps, with a linear timing function, rather than produced by a+ * `steps()` timing function: WebKit never runs a `steps()` animation on the+ * compositor, so Safari would repaint the pulse on the main thread and freeze+ * it whenever a script runs. These keyframes give the same values at the same+ * times, which every engine can animate off the main thread.+ */const skeletonFade = stylex.keyframes({'0%': {opacity: 0.25},- '100%': {opacity: 1},+ '9.99%': {opacity: 0.25},+ '10%': {opacity: 0.325},+ '19.99%': {opacity: 0.325},+ '20%': {opacity: 0.4},+ '29.99%': {opacity: 0.4},+ '30%': {opacity: 0.475},+ '39.99%': {opacity: 0.475},+ '40%': {opacity: 0.55},+ '49.99%': {opacity: 0.55},+ '50%': {opacity: 0.625},+ '59.99%': {opacity: 0.625},+ '60%': {opacity: 0.7},+ '69.99%': {opacity: 0.7},+ '70%': {opacity: 0.775},+ '79.99%': {opacity: 0.775},+ '80%': {opacity: 0.85},+ '89.99%': {opacity: 0.85},+ '90%': {opacity: 0.925},+ '100%': {opacity: 0.925},});// =============================================================================@@ -78,7 +105,8 @@ const styles = stylex.create({default: skeletonFade,'@media (prefers-reduced-motion: reduce)': 'none',},- animationTimingFunction: 'steps(10, end)',+ // Linear between the keyframes' holds; see skeletonFade.+ animationTimingFunction: 'linear',},});