Can't interact with elements outside of the container tree
See original GitHub issueHello!
Description
I have a form that works as a focus trap container, and inside of it I have an input which works as a location picker root (using Google Maps Autocomplete Widget):
Document:
-------------------------
| focus trap container |
| [autocomplete] |
-------------------------
[google autocomplete results]
The problem is that the dropdown with results that Google widget produces is appended to the page outside of the container, and later positioned absolute
ly to sit exactly below the input:
The current implementation of this lib makes it impossible to select dropdown elements, because handler for click looks like that:
function checkClick(e) {
if (config.clickOutsideDeactivates) return;
if (container.contains(e.target)) return; // => false
e.preventDefault();
e.stopImmediatePropagation();
}
Second condition isn’t meet because dropdown actually sits outside of the container, but from the user’s perspective, it’s still the same form one would like to interact with. As a result, you can’t pick items from this dropdown.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Why can't an element with a z-index value cover its child?
Stacking contexts formed by positioned descendants with z-indices greater than or equal to 1 in z-index order (smallest first) then tree order.
Read more >Creating An Outside Focus And Click Handler React Component
In this article, we'll look at how to create an outside focus and click handler with React. We will recreate an open source...
Read more >Using shadow DOM - Web Components | MDN
Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with...
Read more >Managing DOM components with ReactDOM - LogRocket Blog
Learn to expertly manage DOM components in a React app, including a deep dive into each ReactDOM method, with this comprehensive tutorial.
Read more >5. Working with the Shadow DOM - Modern JavaScript [Book]
The shadow DOM is essentially a way to define a new DOM tree whose root container, or host, is visible in the document,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Instead of passing some list, which might not be enough for all the scenarios, I think the best future solution would be to just allow the user to define a function that tells “should this click be blocked?”.
Something similar to https://github.com/willmcpo/body-scroll-lock#allowtouchmove .
So we could add a parameter
allowOutsideClick(event: MouseEvent)
tocreateOptions
( https://github.com/davidtheclark/focus-trap#focustrap--createfocustrapelement-createoptions ).So the function above would be like
Although, I agree with @dougalg, that sometimes it might be a big code/DOM-smell. (This should be noted in README.)
Another question: If you let somebody click on an element outside of the DOM tree, the focus will jump to that element. Will focus trap still work then (shift-tab / tab should lead back to the focus trap)? That’s something that we’d need to check while implementing this
allowOutsideClick
as well.Closing issues older than 2020. Please LMK if this is still a problem.