Skip to content
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

[Dialog & AlertDialog] fix: can't get id correctly in shadow dom #3384

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
5 changes: 2 additions & 3 deletions packages/react/alert-dialog/src/alert-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,8 @@ Alternatively, you can use your own component as a description by assigning it a
For more information, see https://radix-ui.com/primitives/docs/components/alert-dialog`;

React.useEffect(() => {
const hasDescription = document.getElementById(
contentRef.current?.getAttribute('aria-describedby')!
);
const describedById = contentRef.current?.getAttribute('aria-describedby');
const hasDescription = contentRef.current?.querySelector(`[id="${describedById}"]`);
if (!hasDescription) console.warn(MESSAGE);
}, [MESSAGE, contentRef]);

Expand Down
13 changes: 8 additions & 5 deletions packages/react/dialog/src/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,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 @@ -500,9 +500,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.
Expand All @@ -513,7 +516,7 @@ 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(`[id="${titleId}"]`);
if (!hasTitle) console.error(MESSAGE);
}
}, [MESSAGE, titleId]);
Expand All @@ -536,7 +539,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(`[id="${descriptionId}"]`);
if (!hasDescription) console.warn(MESSAGE);
}
}, [MESSAGE, contentRef, descriptionId]);
Expand Down