An open source design system that's fully customizable and agent ready
TypeScript
fix(lab): TabBar's lens jumps to the finger under Reduce Motion (#10581)
Christopher Chedeaude74088cead4
diff --git a/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.ts b/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.tsindex ecc9a66605..777672633b 100644--- a/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.ts+++ b/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.ts@@ -37,6 +37,7 @@ import {PARTIAL_LIFT_SKIP,PEN_SKIP,releaseAnimations,+ requireFrames,sampleCurve,sampleRects,springStep,@@ -1624,6 +1625,71 @@ test.describe('with Reduce Motion (design:reduced-motion)', () => {expect(Math.abs((await lensCenterX(page)) - to.x)).toBeLessThan(0.5);});+ test('on a touch screen a tap held 150 ms puts the lens on the touched tab at once: a lens on every frame, and no fade of its own', async ({+ page,+ }) => {+ await page.emulateMedia({reducedMotion: 'reduce'});+ const {driver} = await openBar(page);+ const to = await itemCenter(page, 'Profile');+ // Every frame from here: the lens's own opacity, and how many opacity+ // animations run on the lens itself. Its glass and shadow are+ // pseudo-elements, with animations of their own.+ await lensBody(page).evaluate(body => {+ const w = window as unknown as {+ __lensFrames: {t: number; opacity: number; fades: number}[];+ __lensDown: number;+ };+ w.__lensFrames = [];+ w.__lensDown = 0;+ window.addEventListener(+ 'touchstart',+ () => {+ w.__lensDown = performance.now();+ },+ {capture: true, once: true},+ );+ const step = () => {+ w.__lensFrames.push({+ t: performance.now(),+ opacity: Number(getComputedStyle(body).opacity),+ fades: body+ .getAnimations()+ .filter(animation =>+ (animation.effect as KeyframeEffect)+ .getKeyframes()+ .some(frame => frame.opacity != null),+ ).length,+ });+ if (w.__lensFrames.length < 600) {+ requestAnimationFrame(step);+ }+ };+ requestAnimationFrame(step);+ });+ await driver.play(tapScript(to, {holdMs: 150}));+ await expect(tab(page, 'Profile')).toHaveAttribute('aria-selected', 'true');+ await waitForRest(lensBody(page));+ const {frames, down} = await page.evaluate(() => {+ const w = window as unknown as {+ __lensFrames: {t: number; opacity: number; fades: number}[];+ __lensDown: number;+ };+ return {frames: w.__lensFrames, down: w.__lensDown};+ });+ expect(down, 'no touch reached the page').toBeGreaterThan(0);+ // The tap's own window: its 150 ms hold and the 200 ms a settle takes.+ const tap = requireFrames(frames, {+ min: 12,+ during: 'the tap and its settle',+ where: frame => frame.t >= down && frame.t <= down + 350,+ });+ expect(+ tap.filter(frame => frame.opacity < 0.9 || frame.fades > 0),+ 'a frame with the lens faded, or fading',+ ).toEqual([]);+ expect(Math.abs((await lensCenterX(page)) - to.x)).toBeLessThan(0.5);+ });+test('a press neither lifts nor flexes, and a drag’s release cross-fades the settle', async ({page,browserName,diff --git a/packages/lab/src/TabBar/useTabBarLens.ts b/packages/lab/src/TabBar/useTabBarLens.tsindex d56d3037a5..7954425ef0 100644--- a/packages/lab/src/TabBar/useTabBarLens.ts+++ b/packages/lab/src/TabBar/useTabBarLens.ts@@ -172,8 +172,11 @@ function useAnimatorList(count: number) {* new resting place. On `lensTravel` from rest (a key, the app), or continuing* the settle a lift started. Every animation runs on the compositor; a finger's* moves retarget them, so the lens keeps moving through a busy main thread.- * Under Reduce Motion the animator cross-fades instead of moving and a press- * scales nothing. Each spring plays as measured, from the UIKit table.+ * Under Reduce Motion a press scales nothing, and a selection change from a+ * key or the app cross-fades instead of moving. Under a finger the lens is+ * put where the finger is at once, as UIKit's is. Only its lifted look fades,+ * since a travel would play as a dip, with frames that show no lens. Each+ * spring plays as measured, from the UIKit table.** Under a fine pointer nothing here runs. A press there is a plain button* press: its click chooses the tab, and TabBar's CSS slides the lens.@@ -373,15 +376,19 @@ export function useTabBarLens({// this event (its animators attach to it with their curves running).// Under Reduce Motion nothing lifts or travels, so no copy mounts.motionRef.current += 1;- if (body.animator.reducedMotion({}) === 'off') {+ const isReduced = body.animator.reducedMotion({}) !== 'off';+ if (!isReduced) {flushSync(() => setIsCopyShown(true));}const at = performance.now();- body.animator.animate(- {x: chaseTarget(x, centers) - centers[selected]},- {spring: tabBar.lensTravel},- {at},- );+ const travel = {x: chaseTarget(x, centers) - centers[selected]};+ // Under Reduce Motion the lens is put under the finger at once, as+ // UIKit's is. A travel would play as a dip, a frame with no lens.+ if (isReduced) {+ body.animator.set(travel);+ } else {+ body.animator.animate(travel, {spring: tabBar.lensTravel}, {at});+ }lift(true, at, size);playCopy(at);};@@ -401,8 +408,13 @@ export function useTabBarLens({return;}const at = performance.now();+ const chase = {x: chaseTarget(x, press.centers) - press.centers[selected]};+ if (body.animator.reducedMotion({}) !== 'off') {+ body.animator.set(chase);+ return;+ }body.animator.animate(- {x: chaseTarget(x, press.centers) - press.centers[selected]},+ chase,{spring: press.isDragging ? tabBar.lensTrack : tabBar.lensTravel},{at},);@@ -439,11 +451,20 @@ export function useTabBarLens({}const target = chosen >= 0 ? chosen : selected;const at = performance.now();- body.animator.animate(- {x: isStale ? 0 : press.centers[target] - press.centers[selected]},- {spring: tabBar.lensSettle},- {at, touchRelease: true},- );+ const settle = {+ x: isStale ? 0 : press.centers[target] - press.centers[selected],+ };+ // Under Reduce Motion the lens is already under the finger: it settles+ // in place, with no dip, and only its lifted look fades.+ if (body.animator.reducedMotion({touchRelease: true}) !== 'off') {+ body.animator.set(settle);+ } else {+ body.animator.animate(+ settle,+ {spring: tabBar.lensSettle},+ {at, touchRelease: true},+ );+ }lift(false, at, press.size, true);playCopy(at);dropCopyAtRest();@@ -467,11 +488,15 @@ export function useTabBarLens({// The app kept the old selection: the lens settles back.settleRef.current = null;const now = performance.now();- body.animator.animate(- {x: 0},- {spring: tabBar.lensSettle},- {at: now, touchRelease: true},- );+ if (body.animator.reducedMotion({touchRelease: true}) !== 'off') {+ body.animator.set({x: 0});+ } else {+ body.animator.animate(+ {x: 0},+ {spring: tabBar.lensSettle},+ {at: now, touchRelease: true},+ );+ }playCopy(now);dropCopyAtRest();}