diff --git a/packages/cli/src/utils/contrastMotion.test.ts b/packages/cli/src/utils/contrastMotion.test.ts
index 4dfc8521a1..9124e1c179 100644
--- a/packages/cli/src/utils/contrastMotion.test.ts
+++ b/packages/cli/src/utils/contrastMotion.test.ts
@@ -160,3 +160,80 @@ describe("contrast motion policy with real GSAP", () => {
root.kill();
});
});
+
+describe("a layer under the text", () => {
+ afterEach(() => {
+ Reflect.deleteProperty(window, "__timelines");
+ Reflect.deleteProperty(window, "__contrastAuditRestores");
+ realGsap.ticker.sleep();
+ });
+
+ function layers() {
+ document.body.innerHTML =
+ '
';
+ const byId = (id: string) => document.getElementById(id) as HTMLElement;
+ const place = (el: HTMLElement, left: number, right: number) =>
+ Object.defineProperty(el, "getBoundingClientRect", {
+ value: () => ({ left, right, top: 0, bottom: 100 }),
+ });
+ place(byId("bg"), 0, 1000);
+ place(byId("label"), 100, 400);
+ place(byId("aside"), 700, 900);
+ return { bg: byId("bg"), label: byId("label"), aside: byId("aside") };
+ }
+
+ const inMotionAt = (time: number, animate: (root: RealTimeline) => void) => {
+ const { label } = layers();
+ const root = realGsap.timeline({ paused: true });
+ animate(root);
+ root.totalTime(time, true);
+ Reflect.set(window, "__timelines", { root });
+ prepared(label);
+ try {
+ const sample = readContrastMotion()[0];
+ return sample ? measuredInMotion(sample) : false;
+ } finally {
+ root.kill();
+ }
+ };
+
+ it("counts as motion while it fades in under still text, and not once it has settled", () => {
+ const fade = (root: RealTimeline) =>
+ root.to(document.getElementById("bg")!, { opacity: 1, duration: 1.5 }, 1);
+ expect(inMotionAt(1.1, fade)).toBe(true);
+ expect(inMotionAt(2.9, fade)).toBe(false);
+ });
+
+ it("does not count a layer that moves beside the text, not under it", () => {
+ const aside = (root: RealTimeline) =>
+ root.to(document.getElementById("aside")!, { x: 50, duration: 1.5 }, 1);
+ expect(inMotionAt(1.1, aside)).toBe(false);
+ });
+});
+
+describe("a sub-composition's timeline", () => {
+ afterEach(() => {
+ Reflect.deleteProperty(window, "__timelines");
+ Reflect.deleteProperty(window, "__contrastAuditRestores");
+ realGsap.ticker.sleep();
+ });
+
+ it("is moving when it is registered paused and seeked with the root, and stopped when its clock is elsewhere", () => {
+ const { card, label } = stage(1);
+ const root = realGsap.timeline({ paused: true });
+ const scene = realGsap.timeline({ paused: true });
+ scene.to(card, { x: 100, duration: 1.5 });
+ root.add(scene, 1).to({}, { duration: 1 }, 3);
+ root.totalTime(1.1, true);
+ scene.totalTime(0.1, true);
+ Reflect.set(window, "__timelines", { root, scene });
+ prepared(label);
+ try {
+ expect(measuredInMotion(readContrastMotion()[0]!)).toBe(true);
+ scene.totalTime(0, true);
+ expect(measuredInMotion(readContrastMotion()[0]!)).toBe(false);
+ } finally {
+ root.kill();
+ }
+ });
+});
diff --git a/packages/cli/src/utils/contrastMotion.ts b/packages/cli/src/utils/contrastMotion.ts
index 4ebc54fe05..64955685fc 100644
--- a/packages/cli/src/utils/contrastMotion.ts
+++ b/packages/cli/src/utils/contrastMotion.ts
@@ -17,7 +17,7 @@ function isAmbientTween(tween: ContrastTweenState): boolean {
return tween.infiniteAncestor || tween.repeat !== 0 || tween.duration >= AMBIENT_TWEEN_SECONDS;
}
-/** 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. */
+/** True when text, an ancestor or a layer under it is mid-tween (not ambient), or the text is not fully opaque. */
export function measuredInMotion(sample: ContrastMotionSample): boolean {
if (Math.round(sample.opacity * 1000) < 1000) return true;
return sample.tweens.some((tween) => !isAmbientTween(tween) && tween.active);
@@ -58,8 +58,12 @@ export function readContrastMotion(): (ContrastMotionSample | null)[] {
function activeInRoot(animation: Animation, root: Animation): boolean {
if (animation === root) return true;
const parent = animation.parent;
- if (!parent || animation.paused() || animation.timeScale() === 0) return false;
+ if (!parent || animation.timeScale() === 0) return false;
const time = parent.time();
+ const seekedWithParent =
+ registered.has(animation) &&
+ Math.abs(animation.time() - (time - animation.startTime())) <= 1e-3;
+ if (animation.paused() && !seekedWithParent) return false;
return (
animation.startTime() <= time + 1e-6 &&
time < animation.endTime() - 1e-6 &&
@@ -92,6 +96,17 @@ export function readContrastMotion(): (ContrastMotionSample | null)[] {
}
}
+ function tweensUnder(text: Element): ContrastTweenState[] {
+ const box = text.getBoundingClientRect();
+ return [...byTarget].flatMap(([node, states]) => {
+ if (node.contains(text) || text.contains(node)) return [];
+ const other = node.getBoundingClientRect();
+ const across = Math.min(box.right, other.right) - Math.max(box.left, other.left);
+ const down = Math.min(box.bottom, other.bottom) - Math.max(box.top, other.top);
+ return across > 0 && down > 0 ? states : [];
+ });
+ }
+
function sampleOf(element: unknown): ContrastMotionSample | null {
if (!(element instanceof Element)) return null;
const tweens: ContrastTweenState[] = [];
@@ -101,6 +116,7 @@ export function readContrastMotion(): (ContrastMotionSample | null)[] {
const own = Number.parseFloat(getComputedStyle(node).opacity);
if (Number.isFinite(own)) opacity *= own;
}
+ tweens.push(...tweensUnder(element));
return tweens.length > 0 ? { opacity, tweens } : null;
}