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 7756a08c6bd83..c01475f4d1327 100644
--- a/docs/specs/components/DropdownMenu/DropdownMenu.spec.md
+++ b/docs/specs/components/DropdownMenu/DropdownMenu.spec.md
@@ -203,7 +203,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 498ed9030d841..cb724ba022bf2 100644
--- a/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
+++ b/packages/core/src/DropdownMenu/DropdownMenu.test.tsx
@@ -4281,6 +4281,57 @@ 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 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 1c81c9a0666af..79f8a5279ceb8 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';
@@ -395,7 +395,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;
@@ -572,11 +576,24 @@ 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.
*/
+
function resolveCustomTriggerMenuName(
callerLabel: unknown,
triggerName: string | undefined,
@@ -973,6 +990,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);
@@ -1153,6 +1175,7 @@ function DropdownMenuPopover({
resetFocusReturn();
if (renderTrigger != null) {
setTriggerName(readAccessibleName(buttonRef.current));
+ setTriggerLabelledBy(readNameReference(buttonRef.current));
}
if (notifyClickOnShowRef.current) {
notifyClickOnShowRef.current = false;
@@ -1928,7 +1951,7 @@ function DropdownMenuPopover({
isDrilled
? undefined
: renderTrigger != null && rest['aria-label'] == null
- ? triggerId
+ ? (triggerLabelledBy ?? triggerId)
: (rest['aria-labelledby'] as string | undefined)
}
onKeyDown={listKeyDown}
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) {