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.

Trap doesn't work if the children are rendered into a React portal

See original GitHub issue

Test case - https://codesandbox.io/s/9y9lmmko84

A stated use case for React’s portals it to avoid overflow issues with things like modals & tooltips. In Elastic UI we use quite a few portals, and have a case where our focused-trapped modal (in a portal) tries to open a focus-trapped context menu (in another portal), but the context menu items won’t receive mouse clicks because the on-outside-click detection doesn’t understand the portal.

FWIW I don’t think this is solvable by focus-trap-react without underlying changes to react-dom, but I wanted to start the conversation here. Since outside click detection is often implemented by asking if the parent DOM element contains the even target, I think ReactDOM should expose a similar contains method that continues through portals, just as event bubbling does.

Any similar change would require focus-trap to allow a configuration parameter to specify a contains function, which could retain the current logic as the default for backwards compatibility.

Please let me know your thoughts on this (if the would-be required changes here and focus-trap make sense, etc), and I’m happy to open the related issue on the React github.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:17

github_iconTop GitHub Comments

1reaction
davidtheclarkcommented, Dec 1, 2018

@chandlerprall Sorry for the delay — just needed to step back from the open source work for a bit. I reviewed the focus-trap PR and it looks great 👍 Have you tried it with focus-trap-react? I think we may want to verify that it works as expected when mounting & unmounting React components before we merge & release.

0reactions
theKasheycommented, Jan 24, 2019

@chandlerprall - try react-focus-lock, it’s a bit more portals friendly, as long it uses React event system and focus events “bubbles” thought portals without issues.

Meanwhile, you may use react-dom-reflection to discover all portals within the parent node.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Trap doesn't work if the children are rendered into a React portal
In Elastic UI we use quite a few portals, and have a case where our focused-trapped modal (in a portal) tries to open...
Read more >
Portals - React
Portals. Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component....
Read more >
Using React Portals to Render Children Outside the DOM ...
To render the Portal component's children, we make use of ReactDOM.createPortal() . This is a special ReactDOM method that accepts the children ...
Read more >
How to use ReactDOM.createPortal() in React 16?
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
Read more >
React 16 - Render a child component into another DOM node ...
In this post we'll take a closer look at another new feature called Portals. Rendering child components in an outer DOM node. By...
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