Skip to content

fix: edit dialog accessiblity validation logic available in shadow dom #3507

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions .changeset/cool-moles-invite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@radix-ui/react-dialog': patch
---

- Improved accessibility check logic in Dialog component
- Restricted title and description element checks to be scoped within Dialog Content for more accurate validation
- Enhanced compatibility with Shadow DOM environments
15 changes: 9 additions & 6 deletions packages/react/dialog/src/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ const DialogContentImpl = React.forwardRef<DialogContentImplElement, DialogConte
</FocusScope>
{process.env.NODE_ENV !== 'production' && (
<>
<TitleWarning titleId={context.titleId} />
<TitleWarning titleId={context.titleId} contentRef={contentRef} />
<DescriptionWarning contentRef={contentRef} descriptionId={context.descriptionId} />
</>
)}
Expand Down Expand Up @@ -503,9 +503,12 @@ const [WarningProvider, useWarningContext] = createContext(TITLE_WARNING_NAME, {
docsSlug: 'dialog',
});

type TitleWarningProps = { titleId?: string };
type TitleWarningProps = {
titleId?: string;
contentRef: React.RefObject<DialogContentElement | null>;
};

const TitleWarning: React.FC<TitleWarningProps> = ({ titleId }) => {
const TitleWarning: React.FC<TitleWarningProps> = ({ titleId, contentRef }) => {
const titleWarningContext = useWarningContext(TITLE_WARNING_NAME);

const MESSAGE = `\`${titleWarningContext.contentName}\` requires a \`${titleWarningContext.titleName}\` for the component to be accessible for screen reader users.
Expand All @@ -516,10 +519,10 @@ For more information, see https://radix-ui.com/primitives/docs/components/${titl

React.useEffect(() => {
if (titleId) {
const hasTitle = document.getElementById(titleId);
const hasTitle = contentRef.current?.querySelector(`#${titleId}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

titleId needs to be sanitized in case it contains : - which is the case for react <19

Suggested change
const hasTitle = contentRef.current?.querySelector(`#${titleId}`);
const hasTitle = contentRef.current?.querySelector(`#${titleId.replaceAll(':', '\\:')}`);

could also use CSS.escape instead of manual string replace

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello @radnan thx for review.
I found this PR was posted before, so I plan to close this PR and post your review on the previous PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chaance This bug is in high demand for a fix. What can I do?

if (!hasTitle) console.error(MESSAGE);
}
}, [MESSAGE, titleId]);
}, [MESSAGE, contentRef, titleId]);

return null;
};
Expand All @@ -539,7 +542,7 @@ 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);
const hasDescription = contentRef.current?.querySelector(`#${descriptionId}`);
if (!hasDescription) console.warn(MESSAGE);
}
}, [MESSAGE, contentRef, descriptionId]);
Expand Down