Skip to content

Commit 61362fc

Browse files
authored
fix(DropdownMenu, ContextMenu): a drilled-in sub-menu is the one menu in the accessibility tree (#13678)
* fix(DropdownMenu, ContextMenu): a drilled-in view is the one menu * test(DropdownMenu): browser specs find the root box by the trigger's aria-controls * test(DropdownMenu): the submenu audit finds the root box by the trigger's aria-controls * test(DropdownMenu): the audit keeps the first shown menu while a regions view steps the root aside * Merge remote-tracking branch 'origin/main' into fix/dropdown-menu-drill-in-one-menu # Conflicts: # packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx
1 parent 4f4b365 commit 61362fc

8 files changed

Lines changed: 130 additions & 26 deletions
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] DropdownMenu, ContextMenu: a drilled-in sub-menu (on a phone, or with `presentation="drill-in"`) is the only menu in the accessibility tree. The root menu no longer wraps it as a second menu while the sub-menu's rows show, and Back restores the root menu.
6+
7+
@cixzhang

‎packages/core/src/ContextMenu/ContextMenu.test.tsx‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,46 @@ describe('ContextMenu', () => {
432432
expect(HTMLElement.prototype.showPopover).not.toHaveBeenCalled();
433433
});
434434

435+
it('exposes only the drilled-in view as a menu on compact touch (FR7)', async () => {
436+
vi.stubGlobal(
437+
'matchMedia',
438+
vi.fn().mockImplementation((query: string) => ({
439+
matches: query === COMPACT_TOUCH_PRESENTATION_QUERY,
440+
media: query,
441+
onchange: null,
442+
addEventListener: vi.fn(),
443+
removeEventListener: vi.fn(),
444+
addListener: vi.fn(),
445+
removeListener: vi.fn(),
446+
dispatchEvent: vi.fn(),
447+
})),
448+
);
449+
const user = userEvent.setup();
450+
render(
451+
<ContextMenu
452+
presentation="popover"
453+
label="Actions"
454+
items={[
455+
{label: 'Rename'},
456+
{label: 'Move to', items: [{label: 'Folder A'}]},
457+
]}>
458+
<div>Right-click me</div>
459+
</ContextMenu>,
460+
);
461+
fireEvent.contextMenu(screen.getByText('Right-click me'));
462+
const isShown = (el: HTMLElement) => el.closest('[hidden]') == null;
463+
const moveTo = screen
464+
.getAllByRole('menuitem', {name: /Move to/, hidden: true})
465+
.find(isShown)!;
466+
await user.click(moveTo);
467+
const view = await waitFor(() =>
468+
screen
469+
.getAllByRole('menu', {name: 'Move to', hidden: true})
470+
.find(isShown)!,
471+
);
472+
expect(view.parentElement?.closest('[role="menu"]')).toBeNull();
473+
});
474+
435475
it('closes on Escape even when opened without auto-focus', () => {
436476
render(
437477
<ContextMenu items={[{label: 'Item 1'}]}>

‎packages/core/src/ContextMenu/ContextMenu.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,9 @@ export function ContextMenu({
810810

811811
// The drill-in view stack for sub-menus on a phone; the bottom sheet has
812812
// its own drill-in.
813-
const {drillIn, wrapContent} = useMenuDrillIn(isOpen && !usesBottomSheet);
813+
const {drillIn, isDrilled, wrapContent} = useMenuDrillIn(
814+
isOpen && !usesBottomSheet,
815+
);
814816
const contextValue = useMemo<DropdownMenuContextValue>(
815817
() => ({closeMenu, menuSize: size, drillIn, menuLabel: label}),
816818
[closeMenu, size, drillIn, label],
@@ -840,10 +842,12 @@ export function ContextMenu({
840842
<div
841843
ref={listRef}
842844
id={menuId}
843-
role="menu"
845+
// While a sub-menu's view shows in place of the rows, that view is the
846+
// one menu, so this box stops being a menu around it.
847+
role={isDrilled ? 'none' : 'menu'}
844848
data-autofocus={usesBottomSheet ? '' : undefined}
845849
tabIndex={usesBottomSheet ? 0 : -1}
846-
aria-label={label}
850+
aria-label={isDrilled ? undefined : label}
847851
onKeyDown={listKeyDown}
848852
onContextMenu={e => e.preventDefault()}
849853
{...menuPress.menuProps}

‎packages/core/src/DropdownMenu/DropdownMenu.tsx‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,7 +1717,7 @@ function DropdownMenuPopover({
17171717
: styles.popoverMatchTrigger;
17181718

17191719
// The drill-in view stack for sub-menus on a phone.
1720-
const {drillIn, wrapContent} = useMenuDrillIn(isOpen);
1720+
const {drillIn, isDrilled, wrapContent} = useMenuDrillIn(isOpen);
17211721
// The surface host is owned through a ref. State keeps only a key that
17221722
// changes whenever a host element attaches, the host included when it
17231723
// moves (a header or footer coming or going), so sub-menus re-render into
@@ -1904,7 +1904,9 @@ function DropdownMenuPopover({
19041904
el?.closest<HTMLDivElement>('[popover]') ?? null;
19051905
}}
19061906
id={menuId}
1907-
role="menu"
1907+
// While a sub-menu's view shows in place of the rows, that view is
1908+
// the one menu, so this box stops being a menu around it.
1909+
role={isDrilled ? 'none' : 'menu'}
19081910
// Pointer opens focus the container so arrows, typeahead, Escape and
19091911
// Tab remain owned by the menu without pre-highlighting an item.
19101912
// An overflowing menu joins the Tab order so its scrollable region is
@@ -1915,14 +1917,18 @@ function DropdownMenuPopover({
19151917
// screen readers announce e.g. "Actions menu" rather than an unnamed
19161918
// menu (menus-13). A custom trigger names it by reference.
19171919
aria-label={
1918-
renderTrigger != null
1919-
? (rest['aria-label'] as string | undefined)
1920-
: button.label
1920+
isDrilled
1921+
? undefined
1922+
: renderTrigger != null
1923+
? (rest['aria-label'] as string | undefined)
1924+
: button.label
19211925
}
19221926
aria-labelledby={
1923-
renderTrigger != null && rest['aria-label'] == null
1924-
? triggerId
1925-
: (rest['aria-labelledby'] as string | undefined)
1927+
isDrilled
1928+
? undefined
1929+
: renderTrigger != null && rest['aria-label'] == null
1930+
? triggerId
1931+
: (rest['aria-labelledby'] as string | undefined)
19261932
}
19271933
onKeyDown={listKeyDown}
19281934
onFocus={event => {

‎packages/core/src/DropdownMenu/DropdownMenuSubMenu.test.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1592,6 +1592,26 @@ describe('DropdownMenuSubMenu drill-in on a phone', () => {
15921592
const shownMenu = (name: string) =>
15931593
screen.getAllByRole('menu', {name, ...h}).find(isShown)!;
15941594

1595+
it('exposes only the drilled-in view as a menu, and the root again after Back (AR3)', async () => {
1596+
stubCompactTouch(true);
1597+
const user = userEvent.setup();
1598+
render(<PhoneMenu />);
1599+
const trigger = screen.getByRole('button', {name: /Actions/});
1600+
await user.click(trigger);
1601+
await waitFor(() =>
1602+
expect(trigger).toHaveAttribute('aria-expanded', 'true'),
1603+
);
1604+
await user.click(shownRow('Move to'));
1605+
const view = await waitFor(() => shownMenu('Move to'));
1606+
expect(view.parentElement?.closest('[role="menu"]')).toBeNull();
1607+
expect(
1608+
screen.queryAllByRole('menu', {name: 'Actions', ...h}).filter(isShown),
1609+
).toEqual([]);
1610+
1611+
await user.click(shownRow('Back to Actions'));
1612+
await waitFor(() => expect(shownMenu('Actions')).toBeDefined());
1613+
});
1614+
15951615
it('drills in from a menu that mounts open, as from one opened by its trigger (FR11)', async () => {
15961616
stubCompactTouch(true);
15971617
const user = userEvent.setup();

‎packages/core/src/DropdownMenu/__tests__/DropdownMenuDrillIn.a11y.chromium.spec.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,22 @@ test.describe('on a phone (coarse pointer)', () => {
8787

8888
const drilled = page.getByRole('menu', {name: 'Move to'});
8989
await expect(drilled).toBeVisible();
90-
// Same box: the drilled list sits inside the root menu, no new popover.
90+
// Same box: the drilled list sits inside the root's box, no new popover.
91+
const rootId = await trigger.getAttribute('aria-controls');
9192
expect(
9293
await drilled.evaluate(
93-
(el, rootName) =>
94+
(el, id) =>
9495
el.closest('[popover]') ===
95-
document
96-
.querySelector(`[role="menu"][aria-label="${rootName}"]`)
97-
?.closest('[popover]'),
98-
'Actions',
96+
document.getElementById(id ?? '')?.closest('[popover]'),
97+
rootId,
9998
),
10099
).toBe(true);
100+
// The drilled view is the one menu: the root's box is no menu around it.
101+
await expect(page.locator(`[id="${rootId}"]`)).toHaveAttribute(
102+
'role',
103+
'none',
104+
);
105+
await expect(page.getByRole('menu', {name: 'Actions'})).toHaveCount(0);
101106
expect(await perceivablePopovers(page).count()).toBe(popoversBefore);
102107
await expect(shownRows(page)).toHaveText([
103108
'Back to Actions',
@@ -125,6 +130,7 @@ test.describe('on a phone (coarse pointer)', () => {
125130
await page.keyboard.press('Escape');
126131
await expect(moveTo).toBeFocused();
127132
await expect(trigger).toHaveAttribute('aria-expanded', 'true');
133+
await expect(rootMenu).toBeVisible();
128134
await expect(shownRows(page)).toHaveText(['Rename', 'Move to', 'Delete']);
129135

130136
// Keyboard drill-in lands on the first row after Back; a second level

‎packages/core/src/DropdownMenu/__tests__/DropdownMenuSubMenuAudit.a11y.chromium.spec.ts‎

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,29 @@ for (const item of cases) {
459459
};
460460
});
461461
};
462-
const menus = Array.from(
463-
document.querySelectorAll<HTMLElement>('[role="menu"]'),
464-
).filter(node => node.getClientRects().length > 0);
465-
const rootMenu = menus[0] ?? null;
462+
// The root's box is the element the trigger controls. While a view
463+
// shows in place of its rows, that box is no menu (role="none").
464+
const trigger = document.querySelector<HTMLElement>(
465+
'[data-testid="dropdown-menu-submenu-audit"] [aria-haspopup][aria-controls]',
466+
);
467+
const controlled = document.getElementById(
468+
trigger?.getAttribute('aria-controls') ?? '',
469+
);
470+
const rootBox =
471+
controlled === null ||
472+
controlled.matches('[role="menu"], [role="none"]')
473+
? controlled
474+
: controlled.querySelector<HTMLElement>(
475+
'[role="menu"], [role="none"]',
476+
);
477+
// A view with a header or footer shows beside the root, which steps
478+
// aside; the first shown menu stands in for it then.
479+
const rootMenu =
480+
rootBox !== null && rootBox.getClientRects().length > 0
481+
? rootBox
482+
: (Array.from(
483+
document.querySelectorAll<HTMLElement>('[role="menu"]'),
484+
).find(node => node.getClientRects().length > 0) ?? null);
466485
const subject =
467486
rootMenu === null
468487
? null
@@ -505,6 +524,7 @@ for (const item of cases) {
505524
rootMenu === null
506525
? null
507526
: {
527+
role: rootMenu.getAttribute('role'),
508528
box: boxOf(rootMenu),
509529
backdrop: hex(backdrop(rootMenu)),
510530
rows: describeRows(rootMenu),
@@ -599,7 +619,8 @@ for (const item of cases) {
599619
item.interaction !== 'drill-in' ||
600620
(observed.flyout?.rows[0]?.name.startsWith('Back to Actions') ===
601621
true &&
602-
rows.length === 0),
622+
rows.length === 0 &&
623+
observed.rootMenu?.role === 'none'),
603624
flyoutSide:
604625
!opensSideways ||
605626
(subjectBox !== undefined &&

‎packages/core/src/DropdownMenu/__tests__/SubMenuHeaderFooter.a11y.chromium.spec.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,13 @@ test.describe('on a phone (coarse pointer)', () => {
214214
await expect(rootMenu).toBeHidden();
215215
expect(await isInsideAMenu(page, 'Labels')).toBe(false);
216216
// Same popover: the view replaces the root's rows in place.
217+
const rootId = await trigger.getAttribute('aria-controls');
217218
expect(
218219
await dialog.evaluate(
219-
el =>
220+
(el, id) =>
220221
el.closest('[popover]') ===
221-
document
222-
.querySelector('[role="menu"][aria-label="Session"]')
223-
?.closest('[popover]'),
222+
document.getElementById(id ?? '')?.closest('[popover]'),
223+
rootId,
224224
),
225225
).toBe(true);
226226
await expect(

0 commit comments

Comments
 (0)