Repository navigation
fix(DropdownMenu): compute accessible name when custom trigger uses aria-labelledby (#13651) #13671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| const [triggerLabelledBy, setTriggerLabelledBy] = useState< | ||
| string | undefined | ||
| >(undefined); | ||
|
|
||
| // Open state | ||
| const [internalIsOpen, setInternalIsOpen] = useState(false); | ||
|
|
@@ -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(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Effect sets state from a DOM read after every render where |
||
| if (renderTrigger != null) { | ||
| const next = | ||
| buttonRef.current?.getAttribute('aria-labelledby')?.trim() || undefined; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| 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(); | ||
|
|
@@ -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; | ||
|
|
@@ -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) ?? | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With 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 => { | ||
|
|
||
There was a problem hiding this comment.
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.