Skip to content

Commit 7862e27

Browse files
fix(cli): check skips a contrast sample measured over a layer that is mid-motion (#7205)
* fix(cli): check skips a contrast sample measured over a layer that is mid-motion * fix(cli): move the backdrop overlap read out of sampleOf, and add no comment lines * fix(cli): shorten the measuredInMotion comment
1 parent 3b3d7c2 commit 7862e27

2 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎packages/cli/src/utils/contrastMotion.test.ts‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,80 @@ describe("contrast motion policy with real GSAP", () => {
160160
root.kill();
161161
});
162162
});
163+
164+
describe("a layer under the text", () => {
165+
afterEach(() => {
166+
Reflect.deleteProperty(window, "__timelines");
167+
Reflect.deleteProperty(window, "__contrastAuditRestores");
168+
realGsap.ticker.sleep();
169+
});
170+
171+
function layers() {
172+
document.body.innerHTML =
173+
'<div id="stage"><div id="bg"></div><h1 id="label">Hello</h1><div id="aside"></div></div>';
174+
const byId = (id: string) => document.getElementById(id) as HTMLElement;
175+
const place = (el: HTMLElement, left: number, right: number) =>
176+
Object.defineProperty(el, "getBoundingClientRect", {
177+
value: () => ({ left, right, top: 0, bottom: 100 }),
178+
});
179+
place(byId("bg"), 0, 1000);
180+
place(byId("label"), 100, 400);
181+
place(byId("aside"), 700, 900);
182+
return { bg: byId("bg"), label: byId("label"), aside: byId("aside") };
183+
}
184+
185+
const inMotionAt = (time: number, animate: (root: RealTimeline) => void) => {
186+
const { label } = layers();
187+
const root = realGsap.timeline({ paused: true });
188+
animate(root);
189+
root.totalTime(time, true);
190+
Reflect.set(window, "__timelines", { root });
191+
prepared(label);
192+
try {
193+
const sample = readContrastMotion()[0];
194+
return sample ? measuredInMotion(sample) : false;
195+
} finally {
196+
root.kill();
197+
}
198+
};
199+
200+
it("counts as motion while it fades in under still text, and not once it has settled", () => {
201+
const fade = (root: RealTimeline) =>
202+
root.to(document.getElementById("bg")!, { opacity: 1, duration: 1.5 }, 1);
203+
expect(inMotionAt(1.1, fade)).toBe(true);
204+
expect(inMotionAt(2.9, fade)).toBe(false);
205+
});
206+
207+
it("does not count a layer that moves beside the text, not under it", () => {
208+
const aside = (root: RealTimeline) =>
209+
root.to(document.getElementById("aside")!, { x: 50, duration: 1.5 }, 1);
210+
expect(inMotionAt(1.1, aside)).toBe(false);
211+
});
212+
});
213+
214+
describe("a sub-composition's timeline", () => {
215+
afterEach(() => {
216+
Reflect.deleteProperty(window, "__timelines");
217+
Reflect.deleteProperty(window, "__contrastAuditRestores");
218+
realGsap.ticker.sleep();
219+
});
220+
221+
it("is moving when it is registered paused and seeked with the root, and stopped when its clock is elsewhere", () => {
222+
const { card, label } = stage(1);
223+
const root = realGsap.timeline({ paused: true });
224+
const scene = realGsap.timeline({ paused: true });
225+
scene.to(card, { x: 100, duration: 1.5 });
226+
root.add(scene, 1).to({}, { duration: 1 }, 3);
227+
root.totalTime(1.1, true);
228+
scene.totalTime(0.1, true);
229+
Reflect.set(window, "__timelines", { root, scene });
230+
prepared(label);
231+
try {
232+
expect(measuredInMotion(readContrastMotion()[0]!)).toBe(true);
233+
scene.totalTime(0, true);
234+
expect(measuredInMotion(readContrastMotion()[0]!)).toBe(false);
235+
} finally {
236+
root.kill();
237+
}
238+
});
239+
});

‎packages/cli/src/utils/contrastMotion.ts‎

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function isAmbientTween(tween: ContrastTweenState): boolean {
1717
return tween.infiniteAncestor || tween.repeat !== 0 || tween.duration >= AMBIENT_TWEEN_SECONDS;
1818
}
1919

20-
/** True when a non-ambient tween on the text or an ancestor is running at the current seek, or the text is not fully opaque. */
20+
/** True when text, an ancestor or a layer under it is mid-tween (not ambient), or the text is not fully opaque. */
2121
export function measuredInMotion(sample: ContrastMotionSample): boolean {
2222
if (Math.round(sample.opacity * 1000) < 1000) return true;
2323
return sample.tweens.some((tween) => !isAmbientTween(tween) && tween.active);
@@ -58,8 +58,12 @@ export function readContrastMotion(): (ContrastMotionSample | null)[] {
5858
function activeInRoot(animation: Animation, root: Animation): boolean {
5959
if (animation === root) return true;
6060
const parent = animation.parent;
61-
if (!parent || animation.paused() || animation.timeScale() === 0) return false;
61+
if (!parent || animation.timeScale() === 0) return false;
6262
const time = parent.time();
63+
const seekedWithParent =
64+
registered.has(animation) &&
65+
Math.abs(animation.time() - (time - animation.startTime())) <= 1e-3;
66+
if (animation.paused() && !seekedWithParent) return false;
6367
return (
6468
animation.startTime() <= time + 1e-6 &&
6569
time < animation.endTime() - 1e-6 &&
@@ -92,6 +96,17 @@ export function readContrastMotion(): (ContrastMotionSample | null)[] {
9296
}
9397
}
9498

99+
function tweensUnder(text: Element): ContrastTweenState[] {
100+
const box = text.getBoundingClientRect();
101+
return [...byTarget].flatMap(([node, states]) => {
102+
if (node.contains(text) || text.contains(node)) return [];
103+
const other = node.getBoundingClientRect();
104+
const across = Math.min(box.right, other.right) - Math.max(box.left, other.left);
105+
const down = Math.min(box.bottom, other.bottom) - Math.max(box.top, other.top);
106+
return across > 0 && down > 0 ? states : [];
107+
});
108+
}
109+
95110
function sampleOf(element: unknown): ContrastMotionSample | null {
96111
if (!(element instanceof Element)) return null;
97112
const tweens: ContrastTweenState[] = [];
@@ -101,6 +116,7 @@ export function readContrastMotion(): (ContrastMotionSample | null)[] {
101116
const own = Number.parseFloat(getComputedStyle(node).opacity);
102117
if (Number.isFinite(own)) opacity *= own;
103118
}
119+
tweens.push(...tweensUnder(element));
104120
return tweens.length > 0 ? { opacity, tweens } : null;
105121
}
106122

0 commit comments

Comments
 (0)