Skip to content
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}`);
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