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

Close modal on outside click #7280

Closed
GenatHadri opened this issue Oct 11, 2023 · 1 comment
Closed

Close modal on outside click #7280

GenatHadri opened this issue Oct 11, 2023 · 1 comment
Labels
answered Issues answered by the EUI team - auto-closes open issues in 7 days if no follow-up response question

Comments

@GenatHadri
Copy link

GenatHadri commented Oct 11, 2023

Hi everyone,

I have a requirement to close modals when clicking outside of them. I've done some research, and I've found that Elastic UI doesn't natively support this feature. However, I wanted to ask if anyone has come up with a workaround solution for this issue?

I attempted to use a custom hook I have for detecting clicks outside an element, in this case, the div in which I wrapped the modal. However, I've encountered some unexpected behavior with the div, possibly due to the modal being a portal. You can see the issue in the following screenshot:https://github.com/elastic/eui/assets/63067800/aa9a3fe8-d0bf-4baa-b24b-0d3e6dd1603f

@cee-chen
Copy link
Member

Unfortunately, the intention of the modal component is specifically to block underlying clicks. The lack of click outside to close is actually a design decision, per the component docs:

A modal works best for focusing users' attention on a short amount of content and getting them to make a decision. Use it to temporarily interrupt a user’s current task and block interactions to the content below it.

If your modal content is more complex, or requires considerable time to complete, consider using an EuiFlyout instead.
[...]
Modals come a wrapping EuiOverlayMask to obscure the content beneath, but unlike flyouts, modals cannot be dismissed by clicking on the overlay mask. This is inline with our modal usage guidelines which requires there to be a primary action button, even if that button simply closes the modal.

For those reasons, we don't allow easily passing props to the modal's overlay mask or focus trap to enable outside click to close. If you absolutely must use your own UX and break away from EUI guidelines, you'd need to add your own window.addEventListener('click', yourEvent), and use the event.target to figure out if the mask or the modal was clicked (you can probably fairly safely target the .euiOverlayMask and .euiModal selectors).

Alternatively, depending on your use case, consider using the EuiFlyout component instead.

@cee-chen cee-chen closed this as not planned Won't fix, can't repro, duplicate, stale Oct 12, 2023
@cee-chen cee-chen added the answered Issues answered by the EUI team - auto-closes open issues in 7 days if no follow-up response label Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered Issues answered by the EUI team - auto-closes open issues in 7 days if no follow-up response question
Projects
None yet
Development

No branches or pull requests

2 participants