From bdbf754d4cfc1bbaf7c01ea6a6cd692359beda4e Mon Sep 17 00:00:00 2001 From: Cindy Zhang Date: Mon, 23 Nov 2026 08:04:27 -0800 Subject: [PATCH 1/2] fix(DropdownMenu): a custom trigger named by aria-labelledby names its menu --- .../dropdown-menu-labelledby-trigger-name.md | 7 +++++ .../DropdownMenu/DropdownMenu.spec.md | 4 ++- .../src/DropdownMenu/DropdownMenu.test.tsx | 27 +++++++++++++++++++ .../core/src/DropdownMenu/DropdownMenu.tsx | 14 +++++++++- 4 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 .changeset/dropdown-menu-labelledby-trigger-name.md diff --git a/.changeset/dropdown-menu-labelledby-trigger-name.md b/.changeset/dropdown-menu-labelledby-trigger-name.md new file mode 100644 index 0000000000000..52d7ae639178f --- /dev/null +++ b/.changeset/dropdown-menu-labelledby-trigger-name.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[fix] DropdownMenu: a custom trigger (`renderTrigger`) named by `aria-labelledby` now names its open menu too. The menu points at the same ids, so a screen reader announces "Project actions menu" instead of an unnamed menu. + +@cixzhang diff --git a/docs/specs/components/DropdownMenu/DropdownMenu.spec.md b/docs/specs/components/DropdownMenu/DropdownMenu.spec.md index f91879ddcab16..b9f91221de7d9 100644 --- a/docs/specs/components/DropdownMenu/DropdownMenu.spec.md +++ b/docs/specs/components/DropdownMenu/DropdownMenu.spec.md @@ -202,7 +202,9 @@ visibility helper. - **AR1 — A custom trigger still names the menu.** With `renderTrigger`, the menu carries `aria-labelledby` pointing at the control's `id` (the Button path keeps `aria-label`), and the control carries `aria-haspopup="menu"`, - `aria-expanded` and `aria-controls`. + `aria-expanded` and `aria-controls`. When the control names itself by + `aria-labelledby`, the open menu points at the same ids, because a name + reference does not follow a second reference. - **AR2 — A row that navigates is a link element.** A row with an `href` is the anchor itself: in the pointer menu it keeps `role="menuitem"` so the menu stays one menu, while a modified click or Enter keeps the browser's link diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx index b5174c568bc27..816dc49b9261c 100644 --- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx +++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx @@ -4227,6 +4227,33 @@ describe('DropdownMenu custom trigger', () => { ); }); + it('a custom trigger named by aria-labelledby names the open menu by the same ids (FR9, AR1)', async () => { + const user = userEvent.setup(); + render( + <> +

Project actions

+ ( + + )}> + {}} /> + + , + ); + const trigger = screen.getByRole('button', {name: 'Project actions'}); + await user.click(trigger); + await waitFor(() => + expect(trigger).toHaveAttribute('aria-expanded', 'true'), + ); + const menu = screen.getByRole('menu', { + name: 'Project actions', + hidden: true, + }); + expect(menu).toHaveAttribute('aria-labelledby', 'project-heading'); + }); + it('a disabled custom trigger never opens the bottom sheet', async () => { const user = userEvent.setup(); render( diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx index 614252d22956b..aacb5c0d7ee24 100644 --- a/packages/core/src/DropdownMenu/DropdownMenu.tsx +++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx @@ -576,6 +576,12 @@ function holdInvokerThroughPress( * when it is a non-empty string, else the trigger's own accessible name. The * popover and the bottom sheet both name the menu through this. */ +/** The ids an element names itself by with `aria-labelledby`, if any. */ +function readNameReference(element: HTMLElement | null): string | undefined { + const ids = element?.getAttribute('aria-labelledby')?.trim(); + return ids ? ids : undefined; +} + function resolveCustomTriggerMenuName( callerLabel: unknown, triggerName: string | undefined, @@ -972,6 +978,11 @@ function DropdownMenuPopover({ // The name a custom trigger gives the menu, read off it as the menu opens. const [triggerName, setTriggerName] = useState(undefined); + // The ids a custom trigger names itself by. A name reference does not follow + // a second reference, so the menu points at these ids, not at the trigger. + const [triggerLabelledBy, setTriggerLabelledBy] = useState< + string | undefined + >(undefined); // Open state const [internalIsOpen, setInternalIsOpen] = useState(false); @@ -1152,6 +1163,7 @@ function DropdownMenuPopover({ resetFocusReturn(); if (renderTrigger != null) { setTriggerName(readAccessibleName(buttonRef.current)); + setTriggerLabelledBy(readNameReference(buttonRef.current)); } if (notifyClickOnShowRef.current) { notifyClickOnShowRef.current = false; @@ -1922,7 +1934,7 @@ function DropdownMenuPopover({ } aria-labelledby={ renderTrigger != null && rest['aria-label'] == null - ? triggerId + ? (triggerLabelledBy ?? triggerId) : (rest['aria-labelledby'] as string | undefined) } onKeyDown={listKeyDown} From 4a34b4cf6e8c6203e7af362afb27490ab83e3289 Mon Sep 17 00:00:00 2001 From: Cindy Zhang Date: Tue, 24 Nov 2026 16:00:35 -0800 Subject: [PATCH 2/2] fix(DropdownMenu): a dangling aria-labelledby on a custom trigger falls back to the trigger id Co-authored-by: Febiadi Wisnu Akbar --- .../src/DropdownMenu/DropdownMenu.test.tsx | 24 +++++++++++++ .../core/src/DropdownMenu/DropdownMenu.tsx | 25 +++++++++---- packages/core/src/utils/accessibleName.ts | 35 ++++++++++++------- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx index 5bcf5542dc6f4..88f6c054358bc 100644 --- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx +++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx @@ -4274,6 +4274,30 @@ describe('DropdownMenu custom trigger', () => { expect(menu).toHaveAttribute('aria-labelledby', 'project-heading'); }); + it('a custom trigger whose aria-labelledby resolves to nothing still names the menu by its own text', async () => { + const user = userEvent.setup(); + render( + ( + + )}> + {}} /> + , + ); + const trigger = screen.getByRole('button', {name: 'Project actions'}); + await user.click(trigger); + await waitFor(() => + expect(trigger).toHaveAttribute('aria-expanded', 'true'), + ); + const menu = screen.getByRole('menu', { + name: 'Project actions', + hidden: true, + }); + expect(menu).toHaveAttribute('aria-labelledby', trigger.id); + }); + it('a disabled custom trigger never opens the bottom sheet', async () => { const user = userEvent.setup(); render( diff --git a/packages/core/src/DropdownMenu/DropdownMenu.tsx b/packages/core/src/DropdownMenu/DropdownMenu.tsx index 4ef1b5a70f64b..091492a95db7c 100644 --- a/packages/core/src/DropdownMenu/DropdownMenu.tsx +++ b/packages/core/src/DropdownMenu/DropdownMenu.tsx @@ -46,7 +46,7 @@ import React, { type ReactNode, } from 'react'; import {useCommittedRefInto} from '../hooks/useCommittedRef'; -import {readAccessibleName} from '../utils/accessibleName'; +import {labelledByText, readAccessibleName} from '../utils/accessibleName'; import * as stylex from '@stylexjs/stylex'; import {usePopoverInternal} from '../Popover/usePopover'; import {holdEntranceInput} from '../Layer/holdEntranceInput'; @@ -394,7 +394,11 @@ export type DropdownMenuPresentation = AdaptivePresentation; export interface DropdownMenuTriggerProps { /** Attaches the control as the menu's anchor and focus-return target. */ ref: (element: HTMLElement | null) => void; - /** The menu points at this id with `aria-labelledby`. */ + /** + * The menu points at this id with `aria-labelledby`. When the trigger names + * itself by its own `aria-labelledby` and those ids resolve to text, the + * menu points at those ids instead. + */ id: string; onClick: (event: React.MouseEvent) => void; onClickCapture: (event: React.MouseEvent) => void; @@ -571,16 +575,23 @@ function holdInvokerThroughPress( doc.addEventListener('pointercancel', onPressEnd, true); } +/** + * The ids an element names itself by with `aria-labelledby`, when they resolve + * to non-empty text. A dangling or empty reference names nothing, so the + * element keeps the name it has from elsewhere. + */ +function readNameReference(element: HTMLElement | null): string | undefined { + if (element == null || labelledByText(element) == null) { + return undefined; + } + return element.getAttribute('aria-labelledby')?.trim(); +} + /** * The name of a menu opened from a custom trigger: the caller's `aria-label` * when it is a non-empty string, else the trigger's own accessible name. The * popover and the bottom sheet both name the menu through this. */ -/** The ids an element names itself by with `aria-labelledby`, if any. */ -function readNameReference(element: HTMLElement | null): string | undefined { - const ids = element?.getAttribute('aria-labelledby')?.trim(); - return ids ? ids : undefined; -} function resolveCustomTriggerMenuName( callerLabel: unknown, diff --git a/packages/core/src/utils/accessibleName.ts b/packages/core/src/utils/accessibleName.ts index 14e735e0259f6..187c21c14e1f2 100644 --- a/packages/core/src/utils/accessibleName.ts +++ b/packages/core/src/utils/accessibleName.ts @@ -32,6 +32,26 @@ export function visibleText(element: Element): string { return (copy.textContent ?? '').replace(/\s+/g, ' ').trim(); } +/** + * The text of the elements `element` names itself by with `aria-labelledby`, + * or `undefined` when no id resolves to non-empty text. + */ +export function labelledByText(element: HTMLElement): string | undefined { + const labelledBy = element.getAttribute('aria-labelledby'); + if (labelledBy == null || labelledBy.trim() === '') { + return undefined; + } + const text = labelledBy + .trim() + .split(/\s+/) + .map(id => element.ownerDocument.getElementById(id)) + .filter((node): node is HTMLElement => node != null) + .map(visibleText) + .join(' ') + .trim(); + return text === '' ? undefined : text; +} + /** * The accessible name of `element`: its own `aria-labelledby` targets, its * `aria-label`, its labels, an input button's value, its text, or its @@ -44,18 +64,9 @@ export function readAccessibleName( if (element == null) { return undefined; } - const labelledBy = element.getAttribute('aria-labelledby'); - if (labelledBy != null && labelledBy.trim() !== '') { - const text = labelledBy - .split(/\s+/) - .map(id => element.ownerDocument.getElementById(id)) - .filter((node): node is HTMLElement => node != null) - .map(visibleText) - .join(' ') - .trim(); - if (text !== '') { - return text; - } + const labelledBy = labelledByText(element); + if (labelledBy != null) { + return labelledBy; } const label = element.getAttribute('aria-label')?.trim(); if (label) {