An open source design system that's fully customizable and agent ready
TypeScript
fix(Layer): the touch cross-fade uses the motion foundation's Reduce Motion values (#8355)
Christopher Chedeaue629e7dffc04
diff --git a/.changeset/layer-touch-reduced-motion-values.md b/.changeset/layer-touch-reduced-motion-values.mdnew file mode 100644index 0000000000..f235db0556--- /dev/null+++ b/.changeset/layer-touch-reduced-motion-values.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] With Reduce Motion on, a layer that opens under a finger fades in over 200 ms on easeInOut, the motion foundation's Reduce Motion values, instead of the theme's fast duration and curve. Under a mouse it still appears at once.++@vjeuxdiff --git a/packages/core/src/Layer/__tests__/LayerReducedMotion.a11y.chromium.spec.ts b/packages/core/src/Layer/__tests__/LayerReducedMotion.a11y.chromium.spec.tsindex fe23ac30b9..a027eccefc 100644--- a/packages/core/src/Layer/__tests__/LayerReducedMotion.a11y.chromium.spec.ts+++ b/packages/core/src/Layer/__tests__/LayerReducedMotion.a11y.chromium.spec.ts@@ -59,17 +59,22 @@ const cases = [* The open layer's entrance: its animation name and the properties its* keyframes animate (read from the page's style sheets).*/+/** Core Animation's easeInOut, as the source writes it or as a build names it. */+const EASE_IN_OUT = ['ease-in-out', 'cubic-bezier(0.42, 0, 0.58, 1)'];+async function entranceOf(page: Page, selector: string) {await expect(page.locator(`${selector}:popover-open`)).toHaveCount(1);return page.evaluate(sel => {const layer = document.querySelector<HTMLElement>(`${sel}:popover-open`);if (layer == null) {- return {name: 'missing', properties: [], duration: NaN};+ return {name: 'missing', properties: [], duration: NaN, easing: ''};}const computed = getComputedStyle(layer);const name = computed.animationName;// Seconds; the first animation's duration.const duration = parseFloat(computed.animationDuration.split(',')[0]);+ // A curve set in the first keyframe overrides the element's.+ let easing = computed.animationTimingFunction.split(/,(?![^(]*\))/)[0];const properties = new Set<string>();for (const sheet of Array.from(document.styleSheets)) {let rules: CSSRuleList;@@ -82,9 +87,20 @@ async function entranceOf(page: Page, selector: string) {for (const rule of Array.from(list)) {if (rule instanceof CSSKeyframesRule && rule.name === name) {for (const frame of Array.from(rule.cssRules)) {- const style = (frame as CSSKeyframeRule).style;+ const keyframe = frame as CSSKeyframeRule;+ const style = keyframe.style;for (let i = 0; i < style.length; i += 1) {- properties.add(style.item(i));+ const property = style.item(i);+ if (property === 'animation-timing-function') {+ if (+ keyframe.keyText === '0%' ||+ keyframe.keyText === 'from'+ ) {+ easing = style.getPropertyValue(property).trim();+ }+ } else {+ properties.add(property);+ }}}} else if ('cssRules' in rule) {@@ -94,7 +110,7 @@ async function entranceOf(page: Page, selector: string) {};visit(rules);}- return {name, properties: Array.from(properties).sort(), duration};+ return {name, properties: Array.from(properties).sort(), duration, easing};}, selector);}@@ -124,6 +140,9 @@ for (const {name, story, trigger, layer} of cases) {// design:reduced-motion DR2: a cross-fade of 200 ms or less.expect(entrance.duration).toBeGreaterThan(0);expect(entrance.duration).toBeLessThanOrEqual(0.2);+ // The motion foundation's cross-fade: Core Animation's easeInOut.+ // Chromium reports the curve as written, so accept either spelling.+ expect(EASE_IN_OUT).toContain(entrance.easing);});});@@ -145,6 +164,9 @@ for (const {name, story, trigger, layer} of cases) {expect(entrance.properties).toEqual(['opacity']);expect(entrance.duration).toBeGreaterThan(0);expect(entrance.duration).toBeLessThanOrEqual(0.2);+ // The motion foundation's cross-fade: Core Animation's easeInOut.+ // Chromium reports the curve as written, so accept either spelling.+ expect(EASE_IN_OUT).toContain(entrance.easing);});});@@ -168,6 +190,10 @@ for (const {name, story, trigger, layer} of cases) {await page.goto(`${storybook.origin}/iframe.html?id=${story}&viewMode=story`,);+ // A fine primary pointer in this context.+ expect(+ await page.evaluate(() => matchMedia('(pointer: coarse)').matches),+ ).toBe(false);await page.getByText(trigger, {exact: true}).hover();const entrance = await entranceOf(page, layer);expect(entrance.name).toBe('none');diff --git a/packages/core/src/Layer/layerAnimations.stylex.ts b/packages/core/src/Layer/layerAnimations.stylex.tsindex b624fe6eda..921e767ae5 100644--- a/packages/core/src/Layer/layerAnimations.stylex.ts+++ b/packages/core/src/Layer/layerAnimations.stylex.ts@@ -2,7 +2,8 @@/*** @file layerAnimations.stylex.ts- * @input Uses StyleX, theme duration/easing tokens+ * @input Uses StyleX, theme duration/easing tokens, the Reduce Motion fade+ * constants* @output Exports shared entry animation styles for layer-based components* @position Shared animation primitives; consumed by DropdownMenu, Popover,* HoverCard, Tooltip, Selector, and any component using useLayer@@ -27,6 +28,7 @@*/import * as stylex from '@stylexjs/stylex';+import {reducedMotionFadeConsts} from '../motion/reducedMotion.stylex';import {durationVars, easeVars, spacingVars} from '../theme/tokens.stylex';const enterBelow = stylex.keyframes({@@ -84,17 +86,21 @@ const enterStartRtl = stylex.keyframes({// Reduce Motion under a finger: the same entrance without its travel. A touch// presentation substitutes a cross-fade for motion, never a jump.+// The fade's curve rides in its first keyframe, so the published class type+// of `animationTimingFunction` stays the theme ease's.const fadeIn = stylex.keyframes({- from: {opacity: 0},+ from: {opacity: 0, animationTimingFunction: reducedMotionFadeConsts.easing},to: {opacity: 1},});+// No theme value lengthens the Reduce Motion fade, so its values are constants.+// The media keys stay literal: StyleX gives an imported at-rule key a later+// priority layer (see reducedMotionKeys).const animationBase = {animationDuration: {default: durationVars['--duration-fast-max'],- // The cross-fade is 200 ms or less, whatever the theme's duration- // (design:reduced-motion DR2).- '@media (prefers-reduced-motion: reduce) and (pointer: coarse)': `min(${durationVars['--duration-fast-max']}, 200ms)`,+ '@media (prefers-reduced-motion: reduce) and (pointer: coarse)':+ reducedMotionFadeConsts.duration,},animationTimingFunction: easeVars['--ease-standard'],animationFillMode: 'backwards' as const,@@ -106,19 +112,18 @@ const animationBase = {* Keyed by LayerPlacement ('above' | 'below' | 'start' | 'end')* for easy lookup: `layerAnimations[placement]`.*- * Each entry disables its keyframe animation under- * `prefers-reduced-motion: reduce` so the layer appears instantly instead of- * translating/scaling in (infra-6). Under a finger (the primary pointer is- * coarse) it fades in instead, opacity only and in 200 ms or less: on a touch- * screen Reduce Motion replaces motion with a cross-fade rather than a jump- * (design:reduced-motion DR2, DR3), as iOS does for its menus, popovers and- * tips.+ * Under `prefers-reduced-motion: reduce` each entry drops its travel and+ * scale. Under a finger (the primary pointer is coarse) the layer fades in,+ * opacity only, in 200 ms on easeInOut: on a touch screen Reduce Motion+ * replaces motion with a cross-fade rather than a jump; under any other+ * pointer it appears at once.*/export const layerAnimations = stylex.create({below: {animationName: {default: enterBelow,- '@media (prefers-reduced-motion: reduce)': 'none',+ '@media (prefers-reduced-motion: reduce) and (not (pointer: coarse))':+ 'none','@media (prefers-reduced-motion: reduce) and (pointer: coarse)': fadeIn,},...animationBase,@@ -126,7 +131,8 @@ export const layerAnimations = stylex.create({above: {animationName: {default: enterAbove,- '@media (prefers-reduced-motion: reduce)': 'none',+ '@media (prefers-reduced-motion: reduce) and (not (pointer: coarse))':+ 'none','@media (prefers-reduced-motion: reduce) and (pointer: coarse)': fadeIn,},...animationBase,@@ -135,7 +141,8 @@ export const layerAnimations = stylex.create({animationName: {default: enterEnd,':is([dir="rtl"] *)': enterEndRtl,- '@media (prefers-reduced-motion: reduce)': 'none',+ '@media (prefers-reduced-motion: reduce) and (not (pointer: coarse))':+ 'none','@media (prefers-reduced-motion: reduce) and (pointer: coarse)': fadeIn,},...animationBase,@@ -144,7 +151,8 @@ export const layerAnimations = stylex.create({animationName: {default: enterStart,':is([dir="rtl"] *)': enterStartRtl,- '@media (prefers-reduced-motion: reduce)': 'none',+ '@media (prefers-reduced-motion: reduce) and (not (pointer: coarse))':+ 'none','@media (prefers-reduced-motion: reduce) and (pointer: coarse)': fadeIn,},...animationBase,