Replies: 7 comments 9 replies
-
Hey @tonyxiao, closing a dialog when clicking outside of it is a very common feature of basic dialogs. Here's an extract from the aria guidelines for the dialog pattern:
We decided that the functionality is common enough that it should be on by default with an easy way to prevent the default behaviour. Conversely, for Hope that sheds some light on the reasoning. ✌️ |
Beta Was this translation helpful? Give feedback.
-
Agree, this may cause the user to accidentally close the dialog, e.g. I have filled out the form and accidentally closed the dialog. So I had to fill out the form again. |
Beta Was this translation helpful? Give feedback.
-
While I am not arguing the default, Is the current way really an easy way though? Isn't this discussion a testament to it being contrary? Looking at the docs for example, it is not easy for me at a glance to distinguish the difference between Wouldn't it be more obvious to have a prop like For example, while I do not think that putting a form in a Dialog component is the best UX generally speaking, the fact that Dialog:
means that this is UX is even worse than it could be. |
Beta Was this translation helpful? Give feedback.
-
I tried this
and it not working. Clicking outside dismisses the modal. Any tips on how to disable dismissal on clicking outside the dialog? |
Beta Was this translation helpful? Give feedback.
-
You should pass |
Beta Was this translation helpful? Give feedback.
-
An example component that uses the Radix Dialog, and avoids the default event behavior: import React from "react";
import * as Dialog from "@radix-ui/react-dialog";
interface CustomDialogProps {
isOpen: boolean;
onClose: () => void;
children: React.ReactNode;
}
export const CustomDialog: React.FC<CustomDialogProps> = ({ isOpen, onClose, children }) => {
/** Generic function that prevents any default event behavior. */
const avoidDefaultDomBehavior = (e: Event) => {
e.preventDefault();
};
return (
<Dialog.Root open={isOpen} onOpenChange={onClose}>
<Dialog.Portal>
<Dialog.Overlay>
<Dialog.Content
onPointerDownOutside={avoidDefaultDomBehavior}
onInteractOutside={avoidDefaultDomBehavior}
>
{children}
</Dialog.Content>
</Dialog.Overlay>
</Dialog.Portal>
</Dialog.Root>
);
}; The components within the dialog can have a button/interactable element that closes the dialog. |
Beta Was this translation helpful? Give feedback.
-
For me it was the case of not styling the |
Beta Was this translation helpful? Give feedback.
-
Took me a solid hour to figure out that
modal
dialog can still be dismissed by clicking outside, and in order to change this behavior we need to passonPointerDownOutside={(e)> e.preventDefault()}
. This is surprising, esp given that theDialogContentModal
component passesdisableOutsidePointerEvents
to theDismissableLayer
component. At the minimum this common need should be documented, but i'm more interested in why clicks outside are not trapped by default and results in dialog closing.Beta Was this translation helpful? Give feedback.
All reactions