question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't interact with elements outside of the container tree

See original GitHub issue

Hello!

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 absolutely to sit exactly below the input:

image

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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
jtomaszewskicommented, Jun 27, 2019

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) to createOptions ( https://github.com/davidtheclark/focus-trap#focustrap--createfocustrapelement-createoptions ).

So the function above would be like

function checkClick(e) {
    if (config.clickOutsideDeactivates) return;
    if (container.contains(e.target)) return;
    if (config.allowOutsideClick && config.allowOutsideClick(e)) {
         return;
    }

    e.preventDefault();
    e.stopImmediatePropagation();
  }

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.

0reactions
stefcameroncommented, Apr 17, 2021

Closing issues older than 2020. Please LMK if this is still a problem.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found