"No focusable element" error thrown if children are hidden.
See original GitHub issueI have a hidden dialog box wrapped in a FocusTrap
. I make the dialog box visible at the same time that I activate the FocusTrap
. This causes the error:
index.js:163 Uncaught Error: You can't have a focus-trap without at least one focusable element
.
It seems that as the FocusTrap activates, it’s upset that there isn’t already a non-hidden focusable element, even though I’m trying to create one simultaneously. If I add a fake always-visible focusable element it stops complaining. But I don’t want that in production. My setup is basically this:
<FocusTrap active={showIt}>
<input style={{visibility: "visible"}} tabIndex={0}/> // FAKE focusable element makes it work.
<div style={{visibility: showIt ? "visible" : "hidden"}}>
<input style={{visibility: "inherit"}} tabIndex={0}/>
</div>
</FocusTrap>
I don’t think I can use fallbackFocus
since none of the contained elements are focusable while they’re hidden. Is there a way just to suppress this error from being thrown?
Thanks!
Issue Analytics
- State:
- Created 6 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
"No focusable element" error thrown if children are hidden.
I have a hidden dialog box wrapped in a FocusTrap . I make the dialog box visible at the same time that I...
Read more >Element with presentational children has no focusable content ...
Description. This rule checks that elements with a role that makes its children presentational do not contain focusable elements.
Read more >An invalid form control with name='' is not focusable
This issue occurs on Chrome if a form field fails validation, but due to the respective invalid control not being focusable the browser's...
Read more >there are no focusable elements inside the <FocusTrap />
This error was being thrown when I tried to use the Simpler Alert Modal of TailwindUI. After googling, the ff resources helped me...
Read more >Element.querySelector() - Web APIs | MDN
If no matches are found, the returned value is null . Exceptions. SyntaxError DOMException. Thrown if the specified selectors are invalid.
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
I have faced the same problem while I was using
preact-material-components@1.5.4
, the dependences are strange.it looks like it fails if it’s activated by default in
componentDidMount()
. The reason for this is probably that the actual attachment is done asynchronously by React or that componentDidMount happens a tick before the redraw, meaningtabbable.js
finds zero focusable elements (since they are either not in the DOM at that moment or hidden or I don’t know. Changing that callback’s code tomakes it work (hurray angular v1 solutions).
I suggest doing this in a fork in the meanwhile until I figure out what’s up with React or I or someone else does a PR.