fix(Portal): click inside detection #2384
Merged
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.
Fixes #1831
Problem
We've had a cryptic bug for a while where clicks inside of a popup cause it to close. This is because clicks inside are detected by checking if the popup
.contains()
the clickede.target
node.This fails if the result of the click also removes the node. Example, a popup containing a Dropdown with removable Labels. When the
x
is clicked on the label, the label is removed from the Dropdown. The Popup then checks to see if the label is within the Popup node. It has been removed, so the Popup does not.contain()
thee.target
. The Popup then assumes the click originated outside of the Popup (body click) and closes.Test case: https://codesandbox.io/s/53ykvz7rk4
Solution
Instead of checking for nodes containing other nodes, we now check that the coordinates of the click event are within the bounds of the Portal. This way, we're shielded from DOM updates within the Portal as a result of click handlers.