-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(dialog): use getRootNode() for Shadow DOM accessibility check #3839
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
e631d18
d899430
6b21461
d0960ca
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,5 @@ | ||
| --- | ||
| '@radix-ui/react-dialog': patch | ||
| --- | ||
|
|
||
| Fix accessibility check for `DialogTitle` and `DialogDescription` when rendered inside Shadow DOM. Previously used `document.getElementById` which fails in Shadow DOM contexts; now uses `element.getRootNode()` to search within the correct document scope. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -414,7 +414,7 @@ const DialogContentImpl = React.forwardRef<DialogContentImplElement, DialogConte | |
| </FocusScope> | ||
| {process.env.NODE_ENV !== 'production' && ( | ||
| <> | ||
| <TitleWarning titleId={context.titleId} /> | ||
| <TitleWarning contentRef={contentRef} titleId={context.titleId} /> | ||
| <DescriptionWarning contentRef={contentRef} descriptionId={context.descriptionId} /> | ||
| </> | ||
| )} | ||
|
|
@@ -503,9 +503,12 @@ const [WarningProvider, useWarningContext] = createContext(TITLE_WARNING_NAME, { | |
| docsSlug: 'dialog', | ||
| }); | ||
|
|
||
| type TitleWarningProps = { titleId?: string }; | ||
| type TitleWarningProps = { | ||
| contentRef: React.RefObject<DialogContentElement | null>; | ||
| titleId?: string; | ||
| }; | ||
|
|
||
| const TitleWarning: React.FC<TitleWarningProps> = ({ titleId }) => { | ||
| const TitleWarning: React.FC<TitleWarningProps> = ({ contentRef, titleId }) => { | ||
| const titleWarningContext = useWarningContext(TITLE_WARNING_NAME); | ||
|
|
||
| const MESSAGE = `\`${titleWarningContext.contentName}\` requires a \`${titleWarningContext.titleName}\` for the component to be accessible for screen reader users. | ||
|
|
@@ -516,10 +519,13 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl | |
|
|
||
| React.useEffect(() => { | ||
| if (titleId) { | ||
| const hasTitle = document.getElementById(titleId); | ||
| // Use getRootNode() to support Shadow DOM contexts where document.getElementById | ||
| // would fail to find elements rendered inside a shadow root. | ||
| const rootNode = contentRef.current?.getRootNode() ?? document; | ||
| const hasTitle = (rootNode as Document | ShadowRoot).getElementById(titleId); | ||
| if (!hasTitle) console.error(MESSAGE); | ||
|
Comment on lines
+564
to
574
|
||
| } | ||
| }, [MESSAGE, titleId]); | ||
| }, [MESSAGE, contentRef, titleId]); | ||
|
|
||
| return null; | ||
| }; | ||
|
|
@@ -539,7 +545,10 @@ const DescriptionWarning: React.FC<DescriptionWarningProps> = ({ contentRef, des | |
| const describedById = contentRef.current?.getAttribute('aria-describedby'); | ||
| // if we have an id and the user hasn't set aria-describedby={undefined} | ||
| if (descriptionId && describedById) { | ||
| const hasDescription = document.getElementById(descriptionId); | ||
| // Use getRootNode() to support Shadow DOM contexts where document.getElementById | ||
| // would fail to find elements rendered inside a shadow root. | ||
| const rootNode = contentRef.current?.getRootNode() ?? document; | ||
| const hasDescription = (rootNode as Document | ShadowRoot).getElementById(descriptionId); | ||
| if (!hasDescription) console.warn(MESSAGE); | ||
|
Comment on lines
+596
to
606
|
||
| } | ||
| }, [MESSAGE, contentRef, descriptionId]); | ||
|
|
||
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.
This change adds Shadow DOM-specific behavior, but the existing test suite for these warnings doesn’t cover rendering the dialog inside a
ShadowRoot. Adding a regression test (e.g. mounting into an element withattachShadow({mode: 'open'})and asserting the title/description warnings don’t fire) would help ensure this doesn’t break in future refactors.