An open source design system that's fully customizable and agent ready
TypeScript
fix(DropdownMenu): a finger's hold-slide-lift no longer flashes the focus outline on the menu button (#10079)
Cindy Zhang2309691dba95
diff --git a/.changeset/dropdown-menu-touch-focus-ring.md b/.changeset/dropdown-menu-touch-focus-ring.mdnew file mode 100644index 0000000000..e34b66421c--- /dev/null+++ b/.changeset/dropdown-menu-touch-focus-ring.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[fix] DropdownMenu: a finger's hold-slide-lift no longer flashes the focus outline on the menu button. When a held press opens the menu, the button takes focus first, so focus can return to it when the menu closes; that focus now shows the outline only after keyboard input. A keyboard open and close still show the outline on the button.++@cixzhangdiff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsxindex eb5f216f0e..78a85aedda 100644--- a/packages/core/src/DropdownMenu/DropdownMenu.tsx+++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx@@ -82,7 +82,7 @@ import {useDropdownMenuPress} from '../hooks/useMenuPress';import type {MenuPressPointerType} from '../hooks/menuPressGesture';import {useTypeahead} from '../hooks/useTypeahead';import {useFocusReturnVisibility} from '../hooks/useFocusReturnVisibility';-import {returnFocusForLastInput} from '../utils/focusReturn';+import {focusForLastInput, returnFocusForLastInput} from '../utils/focusReturn';import {PressGroupContext} from '../hooks/pressGroupContext';import {useMenuOverflow} from './useMenuOverflow';import {moveRegionsTab} from './menuRegionStops';@@ -1439,8 +1439,11 @@ function DropdownMenuPopover({// remember whatever was focused BEFORE the press as the element to hand// focus back to when it closes. Focusing the trigger first gives the// popover the state a click-open already has: the trigger is where- // Escape or a pick returns focus (the APG menu-button pattern).- buttonRef.current?.focus({preventScroll: true});+ // Escape or a pick returns focus (the APG menu-button pattern). A+ // finger's press shows no ring here.+ if (buttonRef.current != null) {+ focusForLastInput(buttonRef.current, {preventScroll: true});+ }openingPressRef.current = {gesture: currentGesture()};const didOpen = openAndFocus('pointer', true);// A mouse can release away from the trigger, or the trigger can movediff --git a/packages/core/src/DropdownMenu/__tests__/DropdownMenuTouchFocusRing.a11y.chromium.spec.ts b/packages/core/src/DropdownMenu/__tests__/DropdownMenuTouchFocusRing.a11y.chromium.spec.tsnew file mode 100644index 0000000000..885085ad9c--- /dev/null+++ b/packages/core/src/DropdownMenu/__tests__/DropdownMenuTouchFocusRing.a11y.chromium.spec.ts@@ -0,0 +1,195 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file DropdownMenuTouchFocusRing.a11y.chromium.spec.ts+ * @input Built DropdownMenu stories and real Chromium touch (CDP) and keyboard+ * input+ * @output Browser evidence that the menu's own focus moves follow the last+ * input: a finger's hold-slide-lift and a finger's tap paint no focus ring+ * on the trigger in any frame, and a keyboard open and close still ring it+ * @position Real-engine proof for architecture:interaction-modality INV1 on+ * the menu trigger; jsdom does not compute :focus-visible+ */++import {+ expect,+ test,+ type Page,+ type CDPSession,+} from '@astryxdesign/a11y-spec/playwright';+import {+ DEFAULT_STORYBOOK_DIR,+ serveStorybook,+ type StaticServer,+} from '@astryxdesign/a11y-spec/storybook';+import {waitForRest} from '@astryxdesign/interaction-harness';++let storybook: StaticServer;+test.beforeAll(async () => {+ storybook = await serveStorybook(+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,+ );+});+test.afterAll(async () => {+ await storybook?.close();+});++const MENU_STORY = 'core-dropdownmenu--default';+const TRIGGER = '#storybook-root [aria-haspopup="menu"]';++async function mount(page: Page) {+ await page.goto(+ `${storybook.origin}/iframe.html?id=${MENU_STORY}&viewMode=story`,+ );+ await expect(page.locator(TRIGGER)).toBeVisible();+}++async function enableTouch(page: Page): Promise<CDPSession> {+ const cdp = await page.context().newCDPSession(page);+ await cdp.send('Emulation.setTouchEmulationEnabled', {+ enabled: true,+ maxTouchPoints: 2,+ });+ await cdp.send('Emulation.setEmitTouchEventsForMouse', {enabled: false});+ return cdp;+}++async function center(page: Page, selector: string, name?: string) {+ const locator =+ name == null+ ? page.locator(selector)+ : page.getByRole('menuitem', {name, exact: true});+ const box = await locator.boundingBox();+ if (box == null) {+ throw new Error(`${selector} ${name ?? ''} has no rendered bounds`);+ }+ return {x: box.x + box.width / 2, y: box.y + box.height / 2};+}++async function touchAt(+ cdp: CDPSession,+ type: 'touchStart' | 'touchMove',+ p: {x: number; y: number},+) {+ await cdp.send('Input.dispatchTouchEvent', {+ type,+ touchPoints: [{x: p.x, y: p.y, id: 1}],+ });+}++async function touchEnd(cdp: CDPSession) {+ await cdp.send('Input.dispatchTouchEvent', {+ type: 'touchEnd',+ touchPoints: [],+ });+}++/**+ * Records, every frame, whether the trigger paints its focus ring: it matches+ * `:focus-visible` while it has focus. A one-frame flash counts.+ */+async function recordTriggerRing(page: Page) {+ await page.evaluate(selector => {+ const trigger = document.querySelector(selector);+ const frames: boolean[] = [];+ const record = () => {+ frames.push(+ trigger != null &&+ document.activeElement === trigger &&+ trigger.matches(':focus-visible'),+ );+ (window as {__ringFrame?: number}).__ringFrame =+ requestAnimationFrame(record);+ };+ (window as {__ringFrames?: boolean[]}).__ringFrames = frames;+ record();+ }, TRIGGER);+}++async function stopRecording(page: Page) {+ return page.evaluate(() => {+ const view = window as {__ringFrame?: number; __ringFrames?: boolean[]};+ if (view.__ringFrame != null) {+ cancelAnimationFrame(view.__ringFrame);+ }+ const frames = view.__ringFrames ?? [];+ return {frames: frames.length, ringed: frames.filter(Boolean).length};+ });+}++test.describe('DropdownMenu trigger focus ring follows the last input (Chromium)', () => {+ test('touch: a hold that opens the menu, slides onto a row and lifts paints no ring on the trigger in any frame', async ({+ page,+ }) => {+ const cdp = await enableTouch(page);+ await mount(page);+ await recordTriggerRing(page);++ const trigger = await center(page, TRIGGER);+ await touchAt(cdp, 'touchStart', trigger);+ const menu = page.getByRole('menu');+ // The long-press delay opens the menu with the finger still down.+ await expect(menu).toBeVisible();+ const row = await center(page, 'menuitem', 'Duplicate');+ const steps = 8;+ for (let i = 1; i <= steps; i++) {+ await touchAt(cdp, 'touchMove', {+ x: trigger.x + ((row.x - trigger.x) * i) / steps,+ y: trigger.y + ((row.y - trigger.y) * i) / steps,+ });+ }+ await touchEnd(cdp);+ await expect(menu).toBeHidden();+ await page.waitForTimeout(300);++ const {frames, ringed} = await stopRecording(page);+ expect(frames).toBeGreaterThan(10);+ expect(ringed, `${ringed} of ${frames} frames ringed the trigger`).toBe(0);+ });++ test('touch: a tap open and a tap on a row paint no ring on the trigger in any frame', async ({+ page,+ }) => {+ const cdp = await enableTouch(page);+ await mount(page);+ await recordTriggerRing(page);++ await touchAt(cdp, 'touchStart', await center(page, TRIGGER));+ await touchEnd(cdp);+ const menu = page.getByRole('menu');+ await expect(menu).toBeVisible();+ // The menu ignores a tap while it is still entering, so tap the row once+ // the entrance rests.+ await waitForRest(page.locator('[popover]:has([role="menu"])').first());+ await touchAt(+ cdp,+ 'touchStart',+ await center(page, 'menuitem', 'Duplicate'),+ );+ await touchEnd(cdp);+ await expect(menu).toBeHidden();+ await page.waitForTimeout(300);++ const {frames, ringed} = await stopRecording(page);+ expect(ringed, `${ringed} of ${frames} frames ringed the trigger`).toBe(0);+ });++ test('keyboard: an open from the trigger and an Escape close still ring the trigger', async ({+ page,+ }) => {+ await mount(page);+ const trigger = page.locator(TRIGGER);+ await page.keyboard.press('Tab');+ await expect(trigger).toBeFocused();+ await page.keyboard.press('Enter');+ const menu = page.getByRole('menu');+ await expect(menu).toBeVisible();++ await page.keyboard.press('Escape');+ await expect(menu).toBeHidden();+ await expect(trigger).toBeFocused();+ expect(await trigger.evaluate(el => el.matches(':focus-visible'))).toBe(+ true,+ );+ });+});