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
4 changes: 2 additions & 2 deletions packages/react/dialog/src/dialog.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ describe('given a default Dialog', () => {
});

describe('when no description has been provided', () => {
it('should warn to the console', () => {
expect(consoleWarnMockFunction).toHaveBeenCalledTimes(1);
it('should not warn to the console', () => {
expect(consoleWarnMockFunction).not.toHaveBeenCalled();
});
});

Expand Down
14 changes: 12 additions & 2 deletions packages/react/dialog/src/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type DialogContextValue = {
contentId: string;
titleId: string;
descriptionId: string;
hasDescription: boolean;
setHasDescription(value: boolean): void;
open: boolean;
onOpenChange(open: boolean): void;
onOpenToggle(): void;
Expand Down Expand Up @@ -58,6 +60,7 @@ const Dialog: React.FC<DialogProps> = (props: ScopedProps<DialogProps>) => {
} = props;
const triggerRef = React.useRef<HTMLButtonElement>(null);
const contentRef = React.useRef<DialogContentElement>(null);
const [hasDescription, setHasDescription] = React.useState(false);
const [open, setOpen] = useControllableState({
prop: openProp,
defaultProp: defaultOpen ?? false,
Expand All @@ -73,6 +76,8 @@ const Dialog: React.FC<DialogProps> = (props: ScopedProps<DialogProps>) => {
contentId={useId()}
titleId={useId()}
descriptionId={useId()}
hasDescription={hasDescription}
setHasDescription={setHasDescription}
open={open}
onOpenChange={setOpen}
onOpenToggle={React.useCallback(() => setOpen((prevOpen) => !prevOpen), [setOpen])}
Expand Down Expand Up @@ -404,7 +409,7 @@ const DialogContentImpl = React.forwardRef<DialogContentImplElement, DialogConte
<DismissableLayer
role="dialog"
id={context.contentId}
aria-describedby={context.descriptionId}
aria-describedby={context.hasDescription ? context.descriptionId : undefined}
aria-labelledby={context.titleId}
data-state={getState(context.open)}
{...contentProps}
Expand All @@ -415,7 +420,7 @@ const DialogContentImpl = React.forwardRef<DialogContentImplElement, DialogConte
{process.env.NODE_ENV !== 'production' && (
<>
<TitleWarning titleId={context.titleId} />
<DescriptionWarning contentRef={contentRef} descriptionId={context.descriptionId} />

</>
)}
</>
Expand Down Expand Up @@ -457,6 +462,11 @@ const DialogDescription = React.forwardRef<DialogDescriptionElement, DialogDescr
(props: ScopedProps<DialogDescriptionProps>, forwardedRef) => {
const { __scopeDialog, ...descriptionProps } = props;
const context = useDialogContext(DESCRIPTION_NAME, __scopeDialog);
React.useEffect(() => {
context.setHasDescription(true);
return () => context.setHasDescription(false);
}, [context]);

return <Primitive.p id={context.descriptionId} {...descriptionProps} ref={forwardedRef} />;
},
);
Expand Down