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.

[TrapFocus] Steals focus from nested portal

See original GitHub issue
  • This is not a v0.x issue.
  • I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior šŸ¤”

TrapFocus ensures focus is kept on elements within its React element spanning tree.

Current Behavior 😯

Since TrapFocus keeps the focus on elements within its DOM hierarchy, focusable elements within its React hierarchy but outside its DOM hierarchy (e.g. inside of a Popper or a portal) can’t be focused.

Steps to Reproduce šŸ•¹

Example: https://codesandbox.io/s/llv0m7r5jq

Click on the [Open Dialog] button, then on the [Open Popper] button. In the console, you’ll see that the TextField within the Popper is focused (as it should since it’s autoFocus), then instantly blurred back by the TrapFocus.

Context šŸ”¦

I’m building an autocomplete component with a ā€œpopoutā€ behaviour similar to the one demonstrated at the bottom of react-select’s Advanced doc.

The autocomplete button itself is rendered within a dialog, and I’m using a popper to display the text input and options. Right now I’m disabling the portal behaviour so it’s kept inside the modal’s DOM hierarchy, but this causes issues with the select’s option list overflowing from the modal. My final solution seems very hacky and prone to unexpected failure, so I’d love this to be handled by MUI itself.

Your Environment šŸŒŽ

For the stack, see codesandbox.

Tech Version
Browser Chrome 74.0.3729.131

Duplicates

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:19 (12 by maintainers)

github_iconTop GitHub Comments

15reactions
oliviertassinaricommented, Jul 24, 2019

@verbart Thanks, it looks like the same problem than the author of this issue has. disabledPortal allows to partially work around the problem, as well as disableEnforceFocus.

Do we have a better solution than to wait for the release of the <FocusScope> component?

elements nested in portals will traverse as expected via the React fiber tree rather than the DOM tree.

https://github.com/facebook/react/pull/15487

0reactions
eps1loncommented, Sep 25, 2019

@emirotin That sounds like #17497

Read more comments on GitHub >

github_iconTop Results From Across the Web

[TrapFocus] Steals focus from nested portal #15694 - GitHub
TrapFocus ensures focus is kept on elements within its React element spanning tree. Current Behavior. Since TrapFocus keeps the focus onĀ ...
Read more >
[Modal] TrapFocus steals focus from nested portal #15694
[Modal] TrapFocus steals focus from nested portal #15694 ... TrapFocus ensures focus is kept on elements within its React element spanning tree.
Read more >
A CSS Approach to Trap Focus Inside of an Element
While experimenting, I discovered a neat CSS trick on how to trap focus within the element, a common accessibility requirement for modals,Ā ...
Read more >
Vue component that helps you to to trap focus in an element
Vue component that helps you to to trap focus in an element.When developing accessible components, in certain behaviors it is important to trap...
Read more >
React.js - input losing focus when rerendering - Stack Overflow
Every element in this nested Array re-renders each time I updated the parent element. And so the inputs lose there "ref" prop every...
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