Skip to content

fix(DropdownMenu): compute accessible name when custom trigger uses aria-labelledby (#13651) - #13671

Closed
rheatkhs wants to merge 1 commit into
meta:mainfrom
rheatkhs:fix/dropdown-menu-custom-trigger-labelledby
Closed

rheatkhs wants to merge 1 commit into
meta:mainfrom
rheatkhs:fix/dropdown-menu-custom-trigger-labelledby

Conversation

@rheatkhs

Copy link
Copy Markdown
Contributor

User impact

When developers use DropdownMenu with renderTrigger and name the custom trigger via aria-labelledby, screen reader users hear an unnamed menu. Restoring name resolution ensures assistive technology announces the menu's accessible name matching the trigger.

Fixes #13651

Problem and solution fit

  • Why 1: DropdownMenuPopover sets aria-labelledby on role="menu" pointing to the trigger element's id.
  • Why 2: Under W3C AccName 1.2 Step 2.B, user agents must not follow recursive aria-labelledby references on traversed elements. When the custom trigger only has aria-labelledby and no direct text, the menu computes an empty accessible name.
  • Why 3: Screen reader users cannot identify the purpose of the menu container when it opens.
  • Solution fit: The menu's aria-labelledby points directly to the custom trigger's label targets when present, bypassing the recursive reference.

Expected behavior and authority

spec:AST-009 FR9 states the menu is named by the trigger control. component:DropdownMenu AR1 requires that a custom trigger still names the menu. W3C AccName 1.2 Step 2.B governs accessible name resolution.

Smallest restoration

In DropdownMenuPopover, track triggerLabelledBy from the custom trigger's DOM aria-labelledby attribute. When present, the menu's aria-labelledby uses triggerLabelledBy, falling back to triggerId when absent. No new props or visual changes are introduced.

Evidence

  • Reproduction before the change: In DropdownMenu.test.tsx, expect(menu).toHaveAttribute('aria-labelledby', 'project-label') failed with Received: aria-labelledby="_r_8_".
  • Result after the change: The menu receives aria-labelledby="project-label" and computes toHaveAccessibleName('Project actions').
  • Representative unchanged path: Custom triggers without aria-labelledby continue to point at triggerId and pass existing accessible name assertions.
  • Regression test or other mutation-sensitive proof: Added test computes accessible name from custom trigger named by aria-labelledby in packages/core/src/DropdownMenu/DropdownMenu.test.tsx.

Scope

Components: DropdownMenu

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

  • pnpm vitest run packages/core/src/DropdownMenu/DropdownMenu.test.tsx -t "computes accessible name from custom trigger named by aria-labelledby"
  • pnpm --filter @astryxdesign/core exec eslint src/DropdownMenu/DropdownMenu.tsx
  • pnpm --filter @astryxdesign/core typecheck
  • pnpm check:changesets
  • pnpm check:knowledge
  • node scripts/pre-review.mjs

…ria-labelledby (meta#13651)

When a custom trigger uses aria-labelledby, pointing the menu at the trigger's id leaves the menu unnamed. AccName prohibits recursive aria-labelledby lookups on elements reached by reference.

The menu now points directly at the trigger's own label targets when present. When the trigger does not use aria-labelledby, it falls back to the trigger id.
@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 23, 2026 3:46pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 23, 2026
@astracat-bot

astracat-bot Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
❓ Needs maintainer input (for maintainers only) Nov 23, 2026, 4:01 PM UTC

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Nov 23, 2026

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pull request needs an engineering owner's decision before it can be approved, and it needs four fixes either way.

Owner decision: with a renderTrigger control named by aria-labelledby, the menu now points at the control's own label ids instead of the control's id, which the DropdownMenu record's AR1 and FR9 state as the wiring; an engineering owner records the decision by commenting /approve-spec <full head SHA> on the pull request or by amending AR1 and FR9, and the author may do so if they are an engineering owner.

Required fixes:

  • With renderTrigger, an aria-labelledby passed to DropdownMenu now overrides the trigger reference (DropdownMenu.tsx:1945), so with a header the dialog reads "Edit" while the menu inside reads the caller's label; keep the previous order, where the trigger reference applies whenever the caller gives no aria-label.
  • When the trigger's aria-labelledby names no element in the document, the menu copies the dead reference and is unnamed although the trigger is still named by its text (DropdownMenu.tsx:1155-1156), so use the trigger's ids only when one of them resolves and fall back to the trigger id otherwise.
  • The new layout Effect (DropdownMenu.tsx:1153-1162) sets state from a DOM read after renders and its lint-disable reason names no external system, so remove it and read the attribute in the trigger's ref callback and in handleLayerShow, which already reads it as the menu opens.
  • The description cites spec:AST-009 FR9 for "the menu is named by the trigger control", which is the DropdownMenu record's FR9, and states that no separable tagalong remains, so correct the citation and describe or drop the precedence change.

The fix itself holds for the reported case: in Chromium's accessibility tree the menu from an icon-only trigger is unnamed on main and named "Project actions" here, and the new test fails on main and passes here.

Claims and files checked

Review mode: full; reason: no-earlier-review

  • Claim: on main the menu's aria-labelledby points at the trigger's id: verified (DropdownMenu.tsx on main, the menu's aria-labelledby expression).
  • Claim: accessible-name computation does not follow the trigger's own aria-labelledby, so an icon-only trigger leaves the menu unnamed: verified (Chromium accessibility tree on main: empty menu name).
  • Claim: the assistive-technology verification record's FR9 says the menu is named by the trigger control: false (that is the DropdownMenu record's FR9).
  • Claim: the DropdownMenu record's AR1 requires a custom trigger to name the menu: verified, though AR1 also states the id wiring this change departs from.
  • Claim: the smallest restoration only adds the trigger's label ids with the trigger id as fallback and adds no props: partly false (it also reorders a caller's aria-labelledby; no new props, verified by the API comparison).
  • Claim: before the change the new test fails with the trigger's generated id: verified (the new test on main).
  • Claim: after the change the menu carries project-label and computes "Project actions": verified (jsdom test and Chromium accessibility tree).
  • Claim: custom triggers without aria-labelledby still point at the trigger id: false when DropdownMenu itself receives aria-labelledby.
  • Claim: separable tagalongs were removed: false (the caller precedence change).
  • Claim: lint, typecheck, changeset and knowledge checks pass: verified (eslint on the two changed source files, the Core typecheck, the changeset check and the knowledge check pass); the pre-review checklist was not repeated.
  • Changeset: DropdownMenu computes the menu's name when a custom trigger is named via aria-labelledby, by pointing at the trigger's label targets: verified for the pointer menu; the bottom sheet already took its name from the trigger.
  • File .changeset/dropdown-menu-custom-trigger-labelledby.md: clear.
  • File packages/core/src/DropdownMenu/DropdownMenu.test.tsx: clear (all 246 cases pass; the new case fails on main).
  • File packages/core/src/DropdownMenu/DropdownMenu.tsx: the four required fixes above, plus a comment at line 975 that restates the state's name.

Automated review (maintainer link)

renderTrigger != null && rest['aria-label'] == null
? triggerId
: (rest['aria-labelledby'] as string | undefined)
(rest['aria-labelledby'] as string | undefined) ??

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With renderTrigger, a caller's aria-labelledby now wins over the trigger reference, which main did not do. With a header the dialog then reads "Edit" while the menu inside reads the caller's label. Keep the previous order:

aria-labelledby={
  renderTrigger != null && rest['aria-label'] == null
    ? (triggerLabelledBy ?? triggerId)
    : (rest['aria-labelledby'] as string | undefined)
}

useIsomorphicLayoutEffect(() => {
if (renderTrigger != null) {
const next =
buttonRef.current?.getAttribute('aria-labelledby')?.trim() || undefined;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If none of these ids names an element in the document, the trigger is still named by its text, but the menu copies the dead reference and ends up unnamed (main names it after the text). Use the trigger's ids only when one of them resolves, for example ids.some(id => doc.getElementById(id)), and fall back to triggerId otherwise.

// Synchronizes the trigger's aria-labelledby from the custom trigger's DOM.
// AccName prohibits recursive aria-labelledby lookups. Pointing the menu at
// the trigger's label targets avoids chaining.
useIsomorphicLayoutEffect(() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This Effect sets state from a DOM read after every render where renderTrigger changes identity, and the lint-disable reason names no external system it synchronizes. handleLayerShow already reads the attribute as the menu opens; an attach-time read fits the trigger's ref callback triggerRef. Removing the Effect also avoids the stale value when a memoized renderTrigger changes its label id without changing identity.


// The name a custom trigger gives the menu, read off it as the menu opens.
const [triggerName, setTriggerName] = useState<string | undefined>(undefined);
// The trigger's own aria-labelledby, read off a custom trigger.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: this comment restates the state's name. Consider dropping it.

@cixzhang

Copy link
Copy Markdown
Contributor

🤖 (Posted by Cindy's agent.) Thanks for picking up #13651 so quickly, @rheatkhs! An answer to the open question in the review: pointing the menu at the trigger's own label ids is a consistency fix, so it needs no separate owner decision. DropdownMenu's record wants the open menu named after its control, and "points at the control's id" was only the mechanism. A name reference doesn't follow a second aria-labelledby, so that mechanism fails for a trigger named that way.

To finish:

  1. Add one sentence to AR1 in the DropdownMenu record, for example: "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; when none of those ids resolves, it points at the control's id."
  2. Fold in the three code fixes from the review (keep the previous aria-labelledby precedence, fall back to the trigger id for unresolved ids, and drop the layout Effect in favour of reading the attribute in the ref callback and handleLayerShow), and correct the description's citation.
  3. Keep the test for a trigger named only by aria-labelledby, asserting the name once the menu is open.

After that, a maintainer will take it through the remaining review.

@cixzhang

Copy link
Copy Markdown
Contributor

🤖 (Posted by Cindy's agent.) Thanks again for this, @rheatkhs! To keep #13651 moving, we've opened #13730, which carries your fix forward with you credited as co-author. It adds the record sentence, a test where the trigger is named only by aria-labelledby, and a fallback to the trigger's id. If you'd rather finish it here, you're welcome to push to this pull request; otherwise we'll close this one with thanks once #13730 merges.

@cixzhang

Copy link
Copy Markdown
Contributor

(Posted by Cindy's agent.) Thank you for this, @rheatkhs! Your fix for naming a DropdownMenu from an aria-labelledby trigger shipped in #13730, with you credited as co-author, together with the record sentence, a test for a trigger named only by aria-labelledby, and a fallback to the trigger's id. Closing this one since that change covers it.

@cixzhang cixzhang closed this Nov 25, 2026

This branch was successfully deployed

1 active deployment
Preview — fff710a6 Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] DropdownMenu: a custom trigger named by aria-labelledby opens an unnamed menu

2 participants