Skip to content

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

TypeScript

fix(rtl-audit): settle finishes animations a story started on mount (#9296)

Cindy Zhang6bc8efa8d1e9
diff --git a/.github/scripts/rtl-audit-coverage.test.mjs b/.github/scripts/rtl-audit-coverage.test.mjs
index 66544f5937..fa5e55f722 100644
--- a/.github/scripts/rtl-audit-coverage.test.mjs
+++ b/.github/scripts/rtl-audit-coverage.test.mjs
@@ -20,6 +20,7 @@ import {
classifyLogicalInlinePair,
collectDirectionalDecorations,
filterStoryRoutesByPackages,
+ finishFiniteAnimations,
orderD1StoryCandidates,
resolveD1Applicability,
storyIdsForComponentFilters,
@@ -978,3 +979,38 @@ describe('audited package and story routing', () => {
);
});
});
+
+describe('finishFiniteAnimations', () => {
+ const animation = (endTime, {throws = false} = {}) => {
+ const record = {finished: false};
+ record.effect = {getComputedTiming: () => ({endTime})};
+ record.finish = () => {
+ if (throws) {
+ throw new Error('InvalidStateError');
+ }
+ record.finished = true;
+ };
+ return record;
+ };
+
+ it('finishes a running entrance transition and leaves infinite ones', () => {
+ const entrance = animation(300);
+ const spinner = animation(Infinity);
+ const doc = {getAnimations: () => [entrance, spinner]};
+ expect(finishFiniteAnimations(doc)).toBe(1);
+ expect(entrance.finished).toBe(true);
+ expect(spinner.finished).toBe(false);
+ });
+
+ it('skips an animation that cannot finish and continues', () => {
+ const stuck = animation(500, {throws: true});
+ const fade = animation(200);
+ const doc = {getAnimations: () => [stuck, fade]};
+ expect(finishFiniteAnimations(doc)).toBe(1);
+ expect(fade.finished).toBe(true);
+ });
+
+ it('does nothing in a document without the Web Animations API', () => {
+ expect(finishFiniteAnimations({})).toBe(0);
+ });
+});
diff --git a/apps/storybook/rtl-audit/rtl-audit-coverage.mjs b/apps/storybook/rtl-audit/rtl-audit-coverage.mjs
index a9f5935590..1d6657f042 100644
--- a/apps/storybook/rtl-audit/rtl-audit-coverage.mjs
+++ b/apps/storybook/rtl-audit/rtl-audit-coverage.mjs
@@ -500,6 +500,32 @@ function isMirrorOf(rtl, ltr) {
return expected.every((value, index) => Math.abs(value - rtl[index]) < 0.01);
}
+/**
+ * Jump every finite animation in `doc` to its end state and return how many
+ * were finished. The audit's zero-duration style only reaches transitions and
+ * animations that start after it is injected; one a story started on mount (a
+ * drawer sliding in) keeps running, so LTR and RTL would otherwise be sampled
+ * at different points of it. Infinite animations (spinners) are left alone.
+ * Runs in the page: keep it self-contained.
+ */
+export function finishFiniteAnimations(doc) {
+ let finished = 0;
+ for (const animation of doc.getAnimations?.() ?? []) {
+ const endTime = animation.effect?.getComputedTiming?.().endTime;
+ if (!Number.isFinite(endTime)) {
+ continue;
+ }
+ try {
+ animation.finish();
+ finished++;
+ } catch {
+ // A zero-playback-rate or infinite-effect animation cannot finish;
+ // leave it.
+ }
+ }
+ return finished;
+}
+
/** Classify one stable LTR/RTL contextual-decoration pair. */
export function classifyDirectionalDecorationPair(ltr, rtl) {
if (!ltr || !rtl) {
diff --git a/apps/storybook/rtl-audit/rtl-audit.mjs b/apps/storybook/rtl-audit/rtl-audit.mjs
index 7450b73cb8..393d366633 100644
--- a/apps/storybook/rtl-audit/rtl-audit.mjs
+++ b/apps/storybook/rtl-audit/rtl-audit.mjs
@@ -66,6 +66,7 @@ import {
componentFromTarget,
evaluateDirectionalDecorations,
filterStoryRoutesByPackages,
+ finishFiniteAnimations,
orderD1StoryCandidates,
resolveD1Applicability,
validateStoryRtlAuditParameters,
@@ -214,6 +215,12 @@ async function settle(page) {
'*,*::before,*::after{animation-duration:0s!important;transition-duration:0s!important;animation-delay:0s!important;caret-color:transparent!important}',
})
.catch(() => {});
+ // Transitions a story started before the style above (an entrance on mount)
+ // keep their own duration: finish them so both directions measure the
+ // resting layout.
+ await page
+ .evaluate(`(${finishFiniteAnimations.toString()})(document)`)
+ .catch(() => {});
await page.waitForTimeout(200);
}
diff --git a/apps/storybook/rtl-audit/targets.json b/apps/storybook/rtl-audit/targets.json
index f40745d160..719d3bea8d 100644
--- a/apps/storybook/rtl-audit/targets.json
+++ b/apps/storybook/rtl-audit/targets.json
@@ -605,5 +605,14 @@
"prev": ".astryx-command-palette-footer > span:first-child",
"next": ".astryx-command-palette-footer > span:last-child"
}
+ },
+ {
+ "component": "DrawerHeader",
+ "storyId": "core-drawerheader--audit",
+ "dims": ["D2"],
+ "selectors": {
+ "prev": ".astryx-drawer-header .astryx-heading",
+ "next": ".astryx-drawer-header button[aria-label=\"Close\"]"
+ }
}
]