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.

"No focusable element" error thrown if children are hidden.

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
liudonghua123commented, Nov 2, 2018

I have faced the same problem while I was using preact-material-components@1.5.4, the dependences are strange.

D:\code\electron\epmd-master>npm ls focus-trap
epmd@0.1.0 D:\code\electron\epmd-master
`-- preact-material-components@1.5.4
  +-- @material/dialog@0.39.3
  | `-- focus-trap@2.4.6
  `-- @material/drawer@0.39.3
    `-- focus-trap@3.0.0

image

2reactions
0xF013commented, Feb 13, 2018

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, meaning tabbable.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 to

      if (this.props.active) {
        setTimeout(() => this.focusTrap.activate());
      }

makes 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.

Read more comments on GitHub >

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

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