Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dropdown-menu-custom-trigger-labelledby.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@astryxdesign/core': patch
---

[fix] Ensure DropdownMenu computes accessible name when a custom trigger is named via aria-labelledby. The menu points directly at the trigger's label targets, avoiding recursive aria-labelledby lookups prohibited by AccName.
@rheatkhs
25 changes: 25 additions & 0 deletions packages/core/src/DropdownMenu/DropdownMenu.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4227,6 +4227,31 @@ describe('DropdownMenu custom trigger', () => {
);
});

it('computes accessible name from custom trigger named by aria-labelledby', async () => {
const user = userEvent.setup();
render(
<div>
<span id="project-label">Project actions</span>
<DropdownMenu
renderTrigger={props => (
<button {...props} type="button" aria-labelledby="project-label">
<span aria-hidden="true">⋯</span>
</button>
)}
items={[{label: 'Archive'}]}
/>
</div>,
);
const trigger = screen.getByRole('button', {name: 'Project actions'});
const menu = screen.getByRole('menu', {hidden: true});
expect(menu).toHaveAttribute('aria-labelledby', 'project-label');
expect(menu).toHaveAccessibleName('Project actions');

await user.click(trigger);
expect(menu).toHaveAttribute('aria-labelledby', 'project-label');
expect(menu).toHaveAccessibleName('Project actions');
});

it('a disabled custom trigger never opens the bottom sheet', async () => {
const user = userEvent.setup();
render(
Expand Down
28 changes: 25 additions & 3 deletions packages/core/src/DropdownMenu/DropdownMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,10 @@ function DropdownMenuPopover({

// 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.

const [triggerLabelledBy, setTriggerLabelledBy] = useState<
string | undefined
>(undefined);

// Open state
const [internalIsOpen, setInternalIsOpen] = useState(false);
Expand Down Expand Up @@ -1143,6 +1147,20 @@ function DropdownMenuPopover({
}
}, [renderTrigger, hasRegions, triggerName]);

// 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.

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.

if (next !== triggerLabelledBy) {
// eslint-disable-next-line @eslint-react/set-state-in-effect -- aria-labelledby is read from the custom trigger's DOM
setTriggerLabelledBy(next);
}
}
}, [renderTrigger, triggerLabelledBy]);

const handleLayerShow = useCallback(() => {
acceptedOpenRef.current = true;
holdOpeningPressRef.current();
Expand All @@ -1152,6 +1170,9 @@ function DropdownMenuPopover({
resetFocusReturn();
if (renderTrigger != null) {
setTriggerName(readAccessibleName(buttonRef.current));
setTriggerLabelledBy(
buttonRef.current?.getAttribute('aria-labelledby')?.trim() || undefined,
);
}
if (notifyClickOnShowRef.current) {
notifyClickOnShowRef.current = false;
Expand Down Expand Up @@ -1921,9 +1942,10 @@ function DropdownMenuPopover({
: button.label
}
aria-labelledby={
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)
}

(renderTrigger != null && rest['aria-label'] == null
? (triggerLabelledBy ?? triggerId)
: undefined)
}
onKeyDown={listKeyDown}
onFocus={event => {
Expand Down