fix: add dev warning when dialog has no accessible title#9819
Open
mvanhorn wants to merge 2 commits intoadobe:mainfrom
Open
fix: add dev warning when dialog has no accessible title#9819mvanhorn wants to merge 2 commits intoadobe:mainfrom
mvanhorn wants to merge 2 commits intoadobe:mainfrom
Conversation
When a dialog is rendered without an aria-label, aria-labelledby, or a visible title element, emit a console.warn in development mode to help developers catch this common accessibility mistake early. The warning fires once per dialog instance and is tree-shaken in production builds. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Check the DOM element directly for aria-label/aria-labelledby attributes instead of relying on hook props, since wrapper components (e.g. RAC Dialog) may add aria-labelledby from context after useDialog runs. Add the warning pattern to the allowed warnings list in setupTests.js so existing tests that render dialogs without accessible labels are not broken.
| const WARNING_PATTERNS_WE_SHOULD_FIX_BUT_ALLOW = [ | ||
| 'Browserslist: caniuse-lite is outdated' | ||
| 'Browserslist: caniuse-lite is outdated', | ||
| 'A dialog must have a visible title for accessibility' |
Member
There was a problem hiding this comment.
we should not have this turned on. do you need it? I looks like you're already catching the warning with a jest spy/mock
| let hasAriaLabelledby = el.hasAttribute('aria-labelledby'); | ||
| if (!hasAriaLabel && !hasAriaLabelledby) { | ||
| console.warn( | ||
| 'A dialog must have a visible title for accessibility. ' + |
Member
There was a problem hiding this comment.
Suggested change
| 'A dialog must have a visible title for accessibility. ' + | |
| 'A dialog must have a title for accessibility. ' + |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
aria-label,aria-labelledby, or visible title element withtitleProps)process.env.NODE_ENV !== 'production'and tree-shaken in production buildsCloses #5402
Context
When
useDialoganduseOverlayTriggerStateare used in the same component,useSlotIdmay returnundefinedbecause the title element is not in the DOM when the slot query runs. This silently produces an inaccessible dialog with noaria-labelledby. The new warning catches this common mistake and tells the developer exactly how to fix it.Test plan
aria-labelis providedaria-labelledbyis providedtitlePropsaria-label(they were previously untitled)yarn jest packages/react-aria/test/dialog/useDialog.test.js- 8/8 passingThis contribution was developed with AI assistance (Claude Code).