An open source design system that's fully customizable and agent ready
TypeScript
fix(TopNav): under reduced motion the mega-menu panel never rises (#8612)
Cindy Zhangfbbe89780e0c
diff --git a/.changeset/topnav-mega-menu-reduced-motion.md b/.changeset/topnav-mega-menu-reduced-motion.mdnew file mode 100644index 0000000000..14b150be51--- /dev/null+++ b/.changeset/topnav-mega-menu-reduced-motion.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] TopNav: with reduced motion on, the mega-menu panel no longer rises as it opens and closes. On a touch screen it fades, in 200 ms or less; with a mouse it opens and closes at once (design:reduced-motion).++@cixzhangdiff --git a/packages/core/src/TopNav/TopNavMegaMenu.reduced-motion.source-build.test.mjs b/packages/core/src/TopNav/TopNavMegaMenu.reduced-motion.source-build.test.mjsnew file mode 100644index 0000000000..6b954c1a9c--- /dev/null+++ b/packages/core/src/TopNav/TopNavMegaMenu.reduced-motion.source-build.test.mjs@@ -0,0 +1,83 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file TopNavMegaMenu.reduced-motion.source-build.test.mjs+ * @input Uses Babel, the core StyleX build config, and the TopNavMegaMenu+ * source+ * @output Verifies the shipped panel follows design:reduced-motion: under+ * reduced motion it never rises (DR1), cross-fades in 200 ms on a touch+ * screen (DR2) and opens and closes at once with a mouse (DR3)+ * @position Regression test for the compiled CSS of the mega-menu panel;+ * jsdom evaluates no media queries or transitions+ */++import fs from 'node:fs/promises';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {transformAsync} from '@babel/core';+import {describe, expect, it} from 'vitest';++const __dirname = path.dirname(fileURLToPath(import.meta.url));+const CORE_ROOT = path.resolve(__dirname, '../..');+const SOURCE = path.join(__dirname, 'TopNavMegaMenu.tsx');++async function compiledRules() {+ const result = await transformAsync(await fs.readFile(SOURCE, 'utf8'), {+ babelrc: false,+ configFile: path.join(CORE_ROOT, 'babel.config.json'),+ filename: SOURCE,+ });+ return (result?.metadata?.stylex ?? []).map(([, rule]) => rule.ltr);+}++const REDUCED = /@media \(prefers-reduced-motion: ?reduce\)\{/;+const REDUCED_COARSE =+ /@media \(prefers-reduced-motion: ?reduce\) and \(pointer: ?coarse\)\{/;+const REDUCED_FINE =+ /@media \(prefers-reduced-motion: ?reduce\) and \(not \(pointer: ?coarse\)\)\{/;++/** The declarations the rules matching `media` set, as one string each. */+function declarationsUnder(rules, media) {+ return rules+ .filter(rule => media.test(rule))+ .map(rule => rule.replace(/\s/g, ''));+}++describe('the mega-menu panel under reduced motion', () => {+ it('never rises: no transform transition, and no offset (DR1)', async () => {+ const reduced = declarationsUnder(await compiledRules(), REDUCED);+ expect(reduced).toContainEqual(+ expect.stringContaining('transition-property:opacity,overlay,display'),+ );+ expect(reduced).toContainEqual(expect.stringContaining('transform:none'));+ });++ it('cross-fades in 200 ms on easeInOut under a coarse pointer (DR2)', async () => {+ const rules = await compiledRules();+ // StyleX rewrites sibling media queries so they never overlap; every+ // rule that sets the 200 ms still applies only to reduced motion under a+ // coarse pointer.+ const fade = rules.filter(rule =>+ /transition-duration:(\.2s|200ms)/.test(rule),+ );+ expect(fade.length).toBeGreaterThan(0);+ for (const rule of fade) {+ expect(rule).toMatch(+ /\(prefers-reduced-motion: ?reduce\) and \(pointer: ?coarse\)/,+ );+ }+ const coarse = declarationsUnder(rules, REDUCED_COARSE);+ expect(coarse).toContainEqual(+ expect.stringMatching(+ /transition-timing-function:cubic-bezier\(0?\.42,0,0?\.58,1\)/,+ ),+ );+ });++ it('opens and closes at once under a fine pointer (DR3)', async () => {+ const fine = declarationsUnder(await compiledRules(), REDUCED_FINE);+ expect(fine).toContainEqual(+ expect.stringContaining('transition-duration:0s'),+ );+ });+});diff --git a/packages/core/src/TopNav/TopNavMegaMenu.tsx b/packages/core/src/TopNav/TopNavMegaMenu.tsxindex e3112079f9..5e0eb64ed3 100644--- a/packages/core/src/TopNav/TopNavMegaMenu.tsx+++ b/packages/core/src/TopNav/TopNavMegaMenu.tsx@@ -120,7 +120,11 @@ const styles = stylex.create({chevronOpen: {transform: 'rotate(180deg)',},- // Animation styles applied to the layer's popover element.+ // Animation styles applied to the layer's popover element. Under+ // `prefers-reduced-motion: reduce` the panel never rises (design:reduced-motion+ // DR1): on a touch screen it cross-fades, 200 ms on easeInOut whatever the+ // theme's duration (DR2, DR4), and with a mouse it opens and closes at once+ // (DR3).panelAnimation: {opacity: {default: 0,@@ -129,10 +133,23 @@ const styles = stylex.create({transform: {default: 'translateY(-4px)',':popover-open': 'translateY(0)',+ '@media (prefers-reduced-motion: reduce)': 'none',+ },+ transitionProperty: {+ default: 'opacity, transform, overlay, display',+ '@media (prefers-reduced-motion: reduce)': 'opacity, overlay, display',+ },+ transitionDuration: {+ default: durationVars['--duration-medium-min'],+ '@media (prefers-reduced-motion: reduce) and (pointer: coarse)': '200ms',+ '@media (prefers-reduced-motion: reduce) and (not (pointer: coarse))':+ '0s',+ },+ transitionTimingFunction: {+ default: easeVars['--ease-standard'],+ '@media (prefers-reduced-motion: reduce) and (pointer: coarse)':+ 'cubic-bezier(0.42, 0, 0.58, 1)',},- transitionProperty: 'opacity, transform, overlay, display',- transitionDuration: durationVars['--duration-medium-min'],- transitionTimingFunction: easeVars['--ease-standard'],transitionBehavior: 'allow-discrete','@starting-style': {opacity: 0,