Skip to content

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

TypeScript

fix(core): a Reduce Motion landing waits for a held fade (#10378)

Christopher Chedeaued0adf30821b
diff --git a/.changeset/animator-landing-held-fade.md b/.changeset/animator-landing-held-fade.md
new file mode 100644
index 0000000000..b271062b4e
--- /dev/null
+++ b/.changeset/animator-landing-held-fade.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Under Reduce Motion, a cross-fade's move and its call's settle wait while a sampler pauses its fade, so the element never moves, and an exit's element never goes, while the paused fade still shows.
+
+@vjeux
diff --git a/packages/core/src/motion/animator.test.ts b/packages/core/src/motion/animator.test.ts
index 86c75b2ad3..db1282048f 100644
--- a/packages/core/src/motion/animator.test.ts
+++ b/packages/core/src/motion/animator.test.ts
@@ -52,6 +52,8 @@ interface Recorded {
/** Finished from outside (a sampler's `finish()`), keeping its start. */
finished: boolean;
readonly playState: 'idle' | 'paused' | 'running' | 'finished';
+ /** Its timing, as the browser reports it: the duration it was given. */
+ readonly effect: {getTiming(): {duration: number}};
ready: Promise<Recorded>;
commit(at: number): void;
finish(): void;
@@ -136,6 +138,11 @@ beforeEach(() => {
cancelled: false,
paused: false,
finished: false,
+ effect: {
+ getTiming: () => ({
+ duration: Number((options as KeyframeAnimationOptions)?.duration),
+ }),
+ },
get playState() {
return this.cancelled
? 'idle'
@@ -3049,6 +3056,94 @@ describe('Reduced Motion (design:reduced-motion)', () => {
}
});
+ it('a landing waits while a sampler pauses its fade after the browser started it, and lands once the fade resumes', async () => {
+ const {animator} = reduced();
+ const handle = animator.animate({x: 100}, {spring: springs.navigationPush});
+ let settled: boolean | undefined;
+ void handle.finished.then(value => {
+ settled = value;
+ });
+ await commitFrame(clock);
+ await advance(fade / 4);
+ // A harness holds the fade mid-way, after the browser started it.
+ const [fadeOut] = live('opacity');
+ fadeOut.paused = true;
+ await advance(5000);
+ // The travel waits for the fade, which still shows the element.
+ expect(animator.get('x')).toBe(0);
+ expect(settled).toBeUndefined();
+ fadeOut.paused = false;
+ for (let step = 0; step < 16; step++) {
+ await advance(fade / 4);
+ }
+ expect(animator.get('x')).toBe(100);
+ expect(animator.get('opacity')).toBe(1);
+ expect(settled).toBe(true);
+ });
+
+ it('a landing waits while a resumed fade has no start yet, and lands once the fade plays to its end', async () => {
+ const {animator} = reduced();
+ const handle = animator.animate({x: 100}, {spring: springs.navigationPush});
+ let settled: boolean | undefined;
+ void handle.finished.then(value => {
+ settled = value;
+ });
+ await commitFrame(clock);
+ await advance(fade / 4);
+ const [fadeOut] = live('opacity');
+ fadeOut.paused = true;
+ // The fade shows the frame it was paused at, a quarter of the way in.
+ Object.defineProperty(fadeOut, 'currentTime', {
+ configurable: true,
+ get: () => fade / 4,
+ });
+ for (let step = 0; step < 300; step++) {
+ await advance(1);
+ }
+ // play(): the fade waits for the browser's next frame, with no start.
+ fadeOut.paused = false;
+ fadeOut.startTime = null;
+ fadeOut.startDelayFrames = 1_000;
+ for (let step = 0; step < 300; step++) {
+ await advance(1);
+ }
+ expect(animator.get('x')).toBe(0);
+ expect(settled).toBeUndefined();
+ // The browser starts it, and it plays to its end.
+ fadeOut.startDelayFrames = 0;
+ Reflect.deleteProperty(fadeOut, 'currentTime');
+ for (let step = 0; step < 16; step++) {
+ await advance(fade / 4);
+ }
+ expect(animator.get('x')).toBe(100);
+ expect(settled).toBe(true);
+ });
+
+ it('an exit whose fade a sampler pauses after its start settles only once the fade resumes', async () => {
+ const {animator} = reduced();
+ const handle = animator.animate(
+ {x: 100, opacity: 0},
+ {spring: springs.navigationPush},
+ );
+ let settled: boolean | undefined;
+ void handle.finished.then(value => {
+ settled = value;
+ });
+ await commitFrame(clock);
+ await advance(fade / 4);
+ const [fadeOut] = live('opacity');
+ fadeOut.paused = true;
+ await advance(5000);
+ // A component that removes the element when the call settles keeps it
+ // while the fade is held on screen.
+ expect(settled).toBeUndefined();
+ fadeOut.paused = false;
+ for (let step = 0; step < 16; step++) {
+ await advance(fade / 4);
+ }
+ expect(settled).toBe(true);
+ });
+
it('a landing later than the whole fade back in shows the element at once and settles then', async () => {
// A hold released long after the dip was due: the landing\u2019s timer runs
// more than the whole fade back in late, so the element appears at once
diff --git a/packages/core/src/motion/animator.ts b/packages/core/src/motion/animator.ts
index 2a7aaa49ce..0843f545a6 100644
--- a/packages/core/src/motion/animator.ts
+++ b/packages/core/src/motion/animator.ts
@@ -2995,6 +2995,61 @@ export function createMotionEngine(
landings.delete(landing);
};
+ /**
+ * How long `landing` still waits for its fade, in ms. A sampler that pauses
+ * the fade holds it, so it checks again later. A resumed fade holds it until
+ * the fade starts again. A fade that runs past the landing's due time, as
+ * after a pause, holds it for the rest of the fade. Any other fade holds
+ * nothing.
+ */
+ const fadeHeldFor = (landing: Landing): number => {
+ const animation = running.get('opacity');
+ const owner = segments.get('opacity')?.owner;
+ if (
+ animation == null ||
+ owner == null ||
+ (owner !== landing.owner && !landing.carried.includes(owner))
+ ) {
+ return 0;
+ }
+ if (animation.playState === 'paused') {
+ return RETIRE_RECHECK_MS;
+ }
+ // The timeline reads the frame before a long task, so the fade's current
+ // time would date a fade that has ended as still running.
+ const start = animation.startTime;
+ const duration = Number(animation.effect?.getTiming?.().duration);
+ if (!Number.isFinite(duration)) {
+ return 0;
+ }
+ if (start == null) {
+ // A fade resumed with play() has no start until its next frame.
+ return animation.playState === 'running' ? END_RECHECK_MS : 0;
+ }
+ // A fade that ends by the landing's due time holds nothing. A timer runs
+ // in whole milliseconds, so it often fires a fraction early.
+ const end = Number(start) + duration - END_HOLD_MS;
+ if (landing.due != null && end <= landing.due) {
+ return 0;
+ }
+ const left = end - now();
+ return left > 0 ? left : 0;
+ };
+
+ /**
+ * The landing's timer: it lands once its fade lets it. A call that takes the
+ * landing over lands it directly, at once.
+ */
+ const landWhenFaded = (landing: Landing) => {
+ const held = fadeHeldFor(landing);
+ if (held > 0) {
+ landing.due = now() + held;
+ landing.timer = setTimeout(() => landWhenFaded(landing), held);
+ return;
+ }
+ land(landing);
+ };
+
/** Arms a landing from the call's start (the browser's, for a call from rest). */
const scheduleLanding = (
owner: HandleState,
@@ -3026,7 +3081,7 @@ export function createMotionEngine(
}
landing.due = start + landing.at;
landing.timer = setTimeout(
- () => land(landing),
+ () => landWhenFaded(landing),
Math.max(0, landing.due - now()),
);
});