An open source design system that's fully customizable and agent ready
TypeScript
fix(lab): TabBar's held lens shows the selected look under Reduce Motion (#10603)
Christopher Chedeau5d1bfbc97431
diff --git a/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.ts b/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.tsindex 4862bd44ae..56e13c8bc0 100644--- a/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.ts+++ b/packages/lab/src/TabBar/__tests__/TabBar.interaction.browser.spec.ts@@ -148,6 +148,17 @@ function animationLog(): void {},true,);+ // A press can replace the touched element, as the selected look's icon is.+ // The touch's pointerup still reaches the window; its touchend does not.+ window.addEventListener(+ 'pointerup',+ event => {+ if (event.pointerType === 'touch') {+ w.__tabBarLifted = now();+ }+ },+ true,+ );}/** When a finger last lifted on the page, on the document timeline (0: never). */@@ -1945,6 +1956,264 @@ 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 held lens shows the touched tab in its selected look, as UIKit’s does, through the lift to rest', async ({+ page,+ }) => {+ await page.emulateMedia({reducedMotion: 'reduce'});+ const {driver} = await openBar(page);+ const to = await itemCenter(page, 'Profile');+ // Every frame from here: whether Profile shows its selected look. Either+ // the lens holds the opaque selected copy with Profile's at its centre, or+ // the copy is gone and Profile's own item draws the look.+ await page.evaluate(() => {+ const w = window as unknown as {+ __copyFrames: {t: number; selected: boolean; copy: boolean}[];+ __copyDown: number;+ __copyUp: number;+ };+ w.__copyFrames = [];+ w.__copyDown = 0;+ w.__copyUp = 0;+ window.addEventListener(+ 'touchstart',+ () => {+ w.__copyDown = performance.now();+ },+ {capture: true, once: true},+ );+ window.addEventListener(+ 'touchend',+ () => {+ w.__copyUp = performance.now();+ },+ {capture: true, once: true},+ );+ const centre = (element: Element | null) => {+ const box = element?.getBoundingClientRect();+ return box == null ? NaN : box.left + box.width / 2;+ };+ const step = () => {+ const copy = document.querySelector('[data-tab-bar-copy]');+ const item = copy?.querySelector('[data-tab-bar-copy-item="profile"]');+ const lens = document.querySelector('[data-tab-bar-lens-body]');+ const label = Array.from(+ document.querySelectorAll(+ '[role="tab"][data-tab-bar-value="profile"] span',+ ),+ ).find(+ span => span.children.length === 0 && span.textContent === 'Profile',+ );+ const inLens =+ copy != null &&+ Number(getComputedStyle(copy).opacity) >= 0.99 &&+ Math.abs(centre(item ?? null) - centre(lens)) < 1;+ // Its effective opacity: its own and its ancestors', multiplied.+ let opacity = 1;+ for (+ let node = label ?? null;+ node != null;+ node = node.parentElement+ ) {+ opacity *= Number(getComputedStyle(node).opacity);+ }+ const own =+ copy == null &&+ label != null &&+ Number(getComputedStyle(label).fontWeight) >= 600 &&+ opacity >= 0.99;+ w.__copyFrames.push({+ t: performance.now(),+ selected: inLens || own,+ copy: copy != null,+ });+ if (w.__copyFrames.length < 600) {+ requestAnimationFrame(step);+ }+ };+ requestAnimationFrame(step);+ });+ await driver.play(tapScript(to, {holdMs: 300}));+ await expect(tab(page, 'Profile')).toHaveAttribute('aria-selected', 'true');+ await waitForRest(lensBody(page));+ await expect(lensCopy(page)).toHaveCount(0);+ const {frames, down, up} = await page.evaluate(() => {+ const w = window as unknown as {+ __copyFrames: {t: number; selected: boolean; copy: boolean}[];+ __copyDown: number;+ __copyUp: number;+ };+ return {frames: w.__copyFrames, down: w.__copyDown, up: w.__copyUp};+ });+ expect(down, 'no touch reached the page').toBeGreaterThan(0);+ expect(up, 'no lift reached the page').toBeGreaterThan(down);+ // The first frame drawn after the touch already holds the copy.+ const first = frames.find(frame => frame.t > down);+ expect(first?.copy, 'the first frame after the touch').toBe(true);+ // Enough frames to judge the hold, once the touch's first frames are+ // drawn (WebKit draws its first frame after a touch late), and the lift.+ requireFrames(frames, {+ min: 8,+ during: 'the held press',+ where: frame => frame.t >= down + 120 && frame.t <= down + 280,+ });+ requireFrames(frames, {+ min: 10,+ during: 'the lift and its settle',+ where: frame => frame.t >= up && frame.t <= up + 250,+ });+ expect(+ frames+ .filter(frame => frame.t > down)+ .filter(frame => !frame.selected)+ .map(frame => Math.round(frame.t - down)),+ 'ms after the touch of each frame whose Profile shows no selected look',+ ).toEqual([]);+ });++ test('on a touch screen a drag from a disabled tab onto an enabled one lifts the lens with that tab in its selected look', async ({+ page,+ }) => {+ await page.emulateMedia({reducedMotion: 'reduce'});+ const {driver} = await openBar(page, 'disabled-tab');+ const browse = await itemCenter(page, 'Browse');+ const profile = await itemCenter(page, 'Profile');+ // Every frame from here: how lifted the lens is, by its glass (the clip's+ // ::before). Also whether it holds the opaque selected copy, with+ // Profile's at its centre. The lift is dated by the touch's pointerup,+ // which reaches the window whatever the press replaced.+ await page.evaluate(() => {+ const w = window as unknown as {+ __dragFrames: {+ t: number;+ glass: number;+ copy: boolean;+ atProfile: boolean;+ }[];+ __dragUp: number;+ };+ w.__dragFrames = [];+ w.__dragUp = 0;+ window.addEventListener(+ 'pointerup',+ event => {+ if (event.pointerType === 'touch') {+ w.__dragUp = performance.now();+ }+ },+ true,+ );+ const centre = (element: Element | null) => {+ const box = element?.getBoundingClientRect();+ return box == null ? NaN : box.left + box.width / 2;+ };+ const step = () => {+ const copy = document.querySelector('[data-tab-bar-copy]');+ const item = copy?.querySelector('[data-tab-bar-copy-item="profile"]');+ const lens = document.querySelector('[data-tab-bar-lens-body]');+ const clip = document.querySelector('[data-tab-bar-lens-clip]');+ w.__dragFrames.push({+ t: performance.now(),+ glass:+ clip == null+ ? 0+ : Number(getComputedStyle(clip, '::before').opacity),+ copy: copy != null && Number(getComputedStyle(copy).opacity) >= 0.99,+ atProfile: Math.abs(centre(item ?? null) - centre(lens)) < 1,+ });+ if (w.__dragFrames.length < 600) {+ requestAnimationFrame(step);+ }+ };+ requestAnimationFrame(step);+ });+ await driver.play(drag(browse, profile, {durationMs: 400, restMs: 300}));+ await expect(tab(page, 'Profile')).toHaveAttribute('aria-selected', 'true');+ const {frames, up} = await page.evaluate(() => {+ const w = window as unknown as {+ __dragFrames: {+ t: number;+ glass: number;+ copy: boolean;+ atProfile: boolean;+ }[];+ __dragUp: number;+ };+ return {frames: w.__dragFrames, up: w.__dragUp};+ });+ expect(up, 'no lift reached the page').toBeGreaterThan(0);+ // The lens lifts once the finger leaves the disabled tab. Every frame it+ // shows lifted before the finger lifts holds the opaque copy.+ const isLifted = (frame: {t: number; glass: number}) =>+ frame.t < up && frame.glass > 0.01;+ expect(frames.some(isLifted), 'a frame whose lens is lifted').toBe(true);+ const lifted = requireFrames(frames, {+ min: 8,+ during: 'the lifted drag',+ where: isLifted,+ });+ expect(+ lifted+ .filter(frame => !frame.copy)+ .map(frame => Math.round(up - frame.t)),+ 'ms before the lift of each lifted frame without the opaque copy',+ ).toEqual([]);+ // At the lift, Profile's selected look sits at the lens centre.+ expect(+ lifted[lifted.length - 1].atProfile,+ 'Profile at the lens at the lift',+ ).toBe(true);+ });++ test('on a touch screen a finger on the selected tab’s icon, which the copy replaces, still drags the lens, and its lift chooses the tab under it', async ({+ page,+ }) => {+ await page.emulateMedia({reducedMotion: 'reduce'});+ const {driver} = await openBar(page);+ // Home is selected, so its icon is the selected one. The copy's mount+ // replaces it while the finger is on it.+ const box = await tab(page, 'Home').locator('svg').first().boundingBox();+ if (box == null) {+ throw new Error('Home has no icon');+ }+ const start = {x: box.x + box.width / 2, y: box.y + box.height / 2};+ const library = await itemCenter(page, 'Library');+ await page.evaluate(() => {+ const w = window as unknown as {+ __swapTarget: EventTarget | null;+ __swapAttached: boolean | null;+ };+ w.__swapTarget = null;+ w.__swapAttached = null;+ window.addEventListener(+ 'touchstart',+ event => {+ w.__swapTarget = event.target;+ },+ {capture: true, once: true},+ );+ window.addEventListener(+ 'pointerup',+ event => {+ if (event.pointerType === 'touch' && w.__swapTarget instanceof Node) {+ w.__swapAttached = document.contains(w.__swapTarget);+ }+ },+ true,+ );+ });+ await driver.play(drag(start, library, {durationMs: 400, restMs: 300}));+ // The lift chooses Library, so the press saw the finger's moves and its+ // lift, after the element it landed on left the document.+ await expect(tab(page, 'Library')).toHaveAttribute('aria-selected', 'true');+ const attached = await page.evaluate(+ () =>+ (window as unknown as {__swapAttached: boolean | null}).__swapAttached,+ );+ expect(attached, 'the touched icon is in the document at the lift').toBe(+ false,+ );+ });+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 9272e23a9d..55cf24330b 100644--- a/packages/lab/src/TabBar/useTabBarLens.ts+++ b/packages/lab/src/TabBar/useTabBarLens.ts@@ -314,6 +314,20 @@ export function useTabBarLens({);};+ /**+ * Use after `body.animator.set()`. Under Reduce Motion, `play()` turns the+ * copy's travel into a cross-fade.+ */+ const placeCopy = () => {+ copy.animator.set(+ copyTransform(+ body.animator.get('x'),+ body.animator.get('scaleX'),+ body.animator.get('scaleY'),+ ),+ );+ };+/*** Lifts (true) or lowers (false) a lens `size` px wide and flexes the* capsule. Under Reduce Motion a press scales nothing: the glass and its@@ -391,14 +405,13 @@ export function useTabBarLens({if (selected < 0 || isIgnored) {return;}- // The lifted lens magnifies the selected look: its copy mounts now, in- // this event (its animators attach to it with their curves running).- // Under Reduce Motion nothing lifts or travels, so no copy mounts.+ // The lifted lens shows the selected look: its copy mounts now, in this+ // event (its animators attach to it with their curves running). It mounts+ // under Reduce Motion too. UIKit's held lens keeps the touched tab's+ // selected look. Only the magnification is motion.motionRef.current += 1;const isReduced = body.animator.reducedMotion({}) !== 'off';- if (!isReduced) {- flushSync(() => setIsCopyShown(true));- }+ flushSync(() => setIsCopyShown(true));const at = performance.now();const travel = {x: chaseTarget(x, centers) - centers[selected]};// Under Reduce Motion the lens is put under the finger at once, as@@ -409,7 +422,11 @@ export function useTabBarLens({body.animator.animate(travel, {spring: tabBar.lensTravel}, {at});}lift(true, at, size);- playCopy(at);+ if (isReduced) {+ placeCopy();+ } else {+ playCopy(at);+ }};const move = (clientX: number, tab: HTMLElement | null) => {@@ -432,9 +449,8 @@ export function useTabBarLens({press.isIgnored = false;if (press.hasLens && selected >= 0) {motionRef.current += 1;- if (body.animator.reducedMotion({}) === 'off') {- flushSync(() => setIsCopyShown(true));- }+ // The copy mounts under Reduce Motion too, as at a press's start.+ flushSync(() => setIsCopyShown(true));lift(true, performance.now(), press.size);}}@@ -445,6 +461,7 @@ export function useTabBarLens({const chase = {x: chaseTarget(x, press.centers) - press.centers[selected]};if (body.animator.reducedMotion({}) !== 'off') {body.animator.set(chase);+ placeCopy();return;}body.animator.animate(@@ -492,7 +509,9 @@ export function useTabBarLens({};// 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') {+ const isReduced =+ body.animator.reducedMotion({touchRelease: true}) !== 'off';+ if (isReduced) {body.animator.set(settle);} else {body.animator.animate(@@ -502,7 +521,11 @@ export function useTabBarLens({);}lift(false, at, press.size, true);- playCopy(at);+ if (isReduced) {+ placeCopy();+ } else {+ playCopy(at);+ }dropCopyAtRest();if (chosen < 0) {return;@@ -526,14 +549,15 @@ export function useTabBarLens({const now = performance.now();if (body.animator.reducedMotion({touchRelease: true}) !== 'off') {body.animator.set({x: 0});+ placeCopy();} else {body.animator.animate({x: 0},{spring: tabBar.lensSettle},{at: now, touchRelease: true},);+ playCopy(now);}- playCopy(now);dropCopyAtRest();}};@@ -606,6 +630,25 @@ export function useTabBarLens({const velocity = body.animator.getVelocity('x', now);body.animator.set({x: offset});const press = pressRef.current;+ // Under Reduce Motion, set the finger's lens and its copy in place. A played+ // curve fades the copy out and back in at the lift.+ if (+ (press != null || fromLift) &&+ body.animator.reducedMotion({}) !== 'off'+ ) {+ body.animator.set({+ x:+ press == null+ ? 0+ : chaseTarget(press.lastX, press.centers) -+ press.centers[selectedIndex],+ });+ placeCopy();+ if (press == null) {+ dropCopyAtRest();+ }+ return;+ }if (press != null) {// A finger is down (the selection changed under it): the lens keeps// heading for the finger, now measured from its new resting place.