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

Fix Dialog cleanup when the Dialog becomes hidden #2303

Merged
merged 4 commits into from
Feb 24, 2023

Conversation

RobinMalfait
Copy link
Member

@RobinMalfait RobinMalfait commented Feb 24, 2023

This PR fixes an issue with the Dialog component where it doesn't properly closes and therefore doesn't properly cleans up the scroll locking, focus trapping, ...

We already had the IntersectionObserver in place for this. But this didn't always trigger properly. For example if the Dialog itself didn't have a proper size, but it's content did.

We could update the root of the intersection observer to a different element but it becomes hard to know which element to use exactly. Just using the parentElement of the Dialog seemed enough, but performing some tests broke other components.

Instead, let's use a ResizeObserver (see browser support: https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver#browser_compatibility) which gets properly trigger when a Dialog becomes hidden and when it becomes visible again.

We have some code that allows us to auto-close the dialog the moment it
gets hidden. This is useful if you use a dialog for a mobile menu and
you resizet he browser. If you wrap the dialog in a `md:hidden` then it
auto closes. If we don't do this, then the dialog is still locking the
scrolling, keeping the focus in the dialog, ... but it is not visible.

To solve this we use an `IntersectionObserver` to verify that the
`boundingClientRect` is "gone" (x = 0, y = 0, width = 0 and height = 0).

However, the intersection observer is not always triggered. This happens
if the main content is scrollable.

Setting the `root` of the `IntersectionObserver` to the parent of the
`Dialog` does seem to solve it.

Not 100% sure what causes this behaviour exactly.
@vercel
Copy link

vercel bot commented Feb 24, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 24, 2023 at 0:19AM (UTC)
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Feb 24, 2023 at 0:19AM (UTC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant