Skip to content

Commit 1f1941e

Browse files
committed
Allow aria-description to describe Dialog and AlertDialog
The aria-description attribute can now be used instead of passing a DialogDescription component as a child or using aria-describedby. A warning is only logged if all of those options are missing.
1 parent 36d954d commit 1f1941e

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

packages/react/alert-dialog/src/alert-dialog.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,16 +243,21 @@ type DescriptionWarningProps = {
243243
contentRef: React.RefObject<AlertDialogContentElement | null>;
244244
};
245245

246-
const DescriptionWarning: React.FC<DescriptionWarningProps> = ({ contentRef }) => {
246+
const DescriptionWarning: React.FC<DescriptionWarningProps> = ({ contentRef, descriptionId }) => {
247247
const MESSAGE = `\`${CONTENT_NAME}\` requires a description for the component to be accessible for screen reader users.
248248
249249
You can add a description to the \`${CONTENT_NAME}\` by passing a \`${DESCRIPTION_NAME}\` component as a child, which also benefits sighted users by adding visible context to the dialog.
250250
251-
Alternatively, you can use your own component as a description by assigning it an \`id\` and passing the same value to the \`aria-describedby\` prop in \`${CONTENT_NAME}\`. If the description is confusing or duplicative for sighted users, you can use the \`@radix-ui/react-visually-hidden\` primitive as a wrapper around your description component.
251+
Alternatively, you can either use \`aria-description\` or your own component as a description by assigning it an \`id\` and passing the same value to the \`aria-describedby\` prop in \`${CONTENT_NAME}\`. If the description is confusing or duplicative for sighted users, you can use the \`@radix-ui/react-visually-hidden\` primitive as a wrapper around your description component.
252252
253253
For more information, see https://radix-ui.com/primitives/docs/components/alert-dialog`;
254254

255255
React.useEffect(() => {
256+
if (contentRef.current?.getAttribute('aria-description')) {
257+
// the alert dialog is described by the aria-description attribute
258+
return;
259+
}
260+
256261
const hasDescription = document.getElementById(
257262
contentRef.current?.getAttribute('aria-describedby')!
258263
);

packages/react/dialog/src/dialog.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,9 +533,14 @@ type DescriptionWarningProps = {
533533

534534
const DescriptionWarning: React.FC<DescriptionWarningProps> = ({ contentRef, descriptionId }) => {
535535
const descriptionWarningContext = useWarningContext(DESCRIPTION_WARNING_NAME);
536-
const MESSAGE = `Warning: Missing \`Description\` or \`aria-describedby={undefined}\` for {${descriptionWarningContext.contentName}}.`;
536+
const MESSAGE = `Warning: Missing \`Description\` or \`aria-description\` or \`aria-describedby={undefined}\` for {${descriptionWarningContext.contentName}}.`;
537537

538538
React.useEffect(() => {
539+
if (contentRef.current?.getAttribute('aria-description')) {
540+
// the dialog is described by the aria-description attribute
541+
return;
542+
}
543+
539544
const describedById = contentRef.current?.getAttribute('aria-describedby');
540545
// if we have an id and the user hasn't set aria-describedby={undefined}
541546
if (descriptionId && describedById) {

0 commit comments

Comments
 (0)