-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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: Unable to disable "outside click" behavior #621
Comments
You can make it a react example could be something like this const foo = () => {
const [isOpen, setIsOpen] = useState(false);
return (
<>
{!isOpen && (
<button onClick={() => setIsOpen(true)}>Open dialog</button>
)}
{isOpen && (
<Dialog static open={isOpen} onClose={() => setIsOpen(false)}>
// ...
</Dialog>
)}
</>
);
}
You can read more about it under Transitions |
Hi there, I forgot to post my solution for this problem. The trick is not the To solve the problem, we can provide an empty function as the <Dialog
// ...
static
onClose={() => null}>
</Dialog> |
I only needed to set the onClose to null using the solution from @wengtytt for this to work. Adding the static property kept the custom close events from working.
|
Amazing. This works. Thank you! |
Oh 🤦♂️ I might've forgotten what i did to solve this. Maybe i solved this myself by exchanging the overlay component with my own div, setting aria-hidden="true" and using the same classes. That way you don't loose out on the close on esc etc. |
Bypassing If we look in
Simple solution: add Inline: ...which will effectively disable click events outside of the Dialog's body. If you have other interactive elements higher in the z-index, such as a toast notification, you can capture the
Notes on the eslint disable: https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/master/docs/rules/no-static-element-interactions.md#case-the-event-handler-is-only-being-used-to-capture-bubbled-events |
Wow you're absolutely right - esc key not working is an issue with the onclick={()=>null} solution. This should be the accepted answer! Many thanks! |
static this solution doesn't work for me , anyone help? |
you can also return the empty obj, it prevents the outside click ! |
This is exactly what i was looking for, many thanks !!! |
I tried to set static and create a dummy @close function but its not working i was still able to close the dialog as i click outside |
<Dialog
ref={containerRef}
as='div'
className='relative z-10'
onClose={() => {}}
onClick={(event) => {
/**
* disable Close modal when click outside of modal
*/
if (
isElementChild(
containerRef.current!,
event.target as HTMLElement,
)
) {
closeModal()
}
}}
> I want to close the modal on click outside, but |
That's the solution that worked for me i mentioned above |
i had same error so i tried to solve my way and this actually work ` onClose: () => {
}, Note, i did it with small timeout to make sure that first all modal scripts are completed and then im manually removing that. Hope it helps! |
The version UI 1.6 have been depreacted the Dialog.Overlay. https://headlessui.com/react/dialog#dialog-overlay At least with version 1.7.17 the issue it not happen. |
The
If you want to also prevent the ESC key handler, you can add a key listener with capture mode: With Vue, it'd be something like this.
|
Just make DialogPanel fullscreen, then add dialog content div as a direct child of DialogPanel and style that as though it is DialogPanel. This way we don't lose built in functionality such as close on ESC key. |
What is the solution for this on v2? |
Is there an option to disable "outside click" behaviour? For example, I'd like to keep the dialog opened when click outside. It would be great if we can have the condition inside the useWindowEvent function.
Originally posted by @wengtytt in #212 (comment)
There also seems to be multiple feature requests regarding this. Is there a way to override this default behavior currently?
The text was updated successfully, but these errors were encountered: