-
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
Ensure anchored components are properly stacked on top of Dialog
components
#3111
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When you render a `Listbox` in a `Dialog`, then clicking outside of the `Listbox` will only close the `Listbox` and not the `Dialog`. This is because the `Listbox` is rendered _inside_ the `Dialog`, and the `useOutsideClick` hook will prevent the event from propagating to the `Dialog` therefore it stays open. Then, if you add the `anchor` prop to the `ListboxOptions` then a few things will happen: 1. We will render the `ListboxOptions` in a `Modal`, which portals the component to the end of the `body` (aka, it won't be in the `Dialog` anymore). 2. The `anchor` prop, will use Floating UI to position the element correctly. If you now click outside of the open `Listbox`, then the `Dialog` will receive the click event (because it is rendered somewhere else in the DOM) and therefore the `Listbox` **and** the `Dialog` will close. The `Dialog` also uses a `StackProvider` to know if it is the top-level `Dialog` or not. The problem is that the `Modal` doesn't use that `StackProvider` to tell the `Dialog` that something is stacked on top of the current `Dialog`. That's what this commit fixes, the `Modal` will now use a `StackProvider` to tell the `Dialog` that it's not the top-most element anymore so it shouldn't enable the `useOutsideClick` behavior. That said, this is one of the things that will be changed in the future to make "parallel" dialogs possible. Essentially, we will track a global stack and the top-most element (last one that was "opened") will win. Then hooks such as `useOutsideClick` and `useScrollLock` will use that information to know if they should undo scroll locking for example if another element is still open.
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
thecrypticace
approved these changes
Apr 19, 2024
This was referenced May 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When you render a
Listbox
in aDialog
, then clicking outside of theListbox
will only close theListbox
and not theDialog
.This is because the
Listbox
is rendered inside theDialog
, and theuseOutsideClick
hook will prevent the event from propagating to theDialog
therefore it stays open.Then, if you add the
anchor
prop to theListboxOptions
then a few things will happen:ListboxOptions
in aModal
, which portals the component to the end of thebody
(aka, it won't be in theDialog
anymore).anchor
prop, will use Floating UI to position the element correctly.If you now click outside of the open
Listbox
, then theDialog
will receive the click event (because it is rendered somewhere else in the DOM) and therefore theListbox
and theDialog
will close.The
Dialog
also uses aStackProvider
to know if it is the top-levelDialog
or not. The problem is that theModal
doesn't use thatStackProvider
to tell theDialog
that something is stacked on top of the currentDialog
.That's what this commit fixes, the
Modal
will now use aStackProvider
to tell theDialog
that it's not the top-most element anymore so it shouldn't enable theuseOutsideClick
behavior.That said, this is one of the things that will be changed in the future to make "parallel" dialogs possible. Essentially, we will track a global stack and the top-most element (last one that was "opened") will win.
Then hooks such as
useOutsideClick
anduseScrollLock
will use that information to know if they should undo scroll locking for example if another element is still open.Fixes: #3108