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.

Dialog throws error if there are no focusable elements

See original GitHub issue

I get the following error if I create a Dialog with no interactive components (just text): Error: There are no focusable elements inside the <FocusTrap />

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:36
  • Comments:26

github_iconTop GitHub Comments

43reactions
Terkeacommented, Jul 14, 2021

I found a way around that.

simply add a ref to the dialog and to a div inside the body UPDATE: found this mentioned in the docs https://headlessui.dev/react/dialog#managing-focus-within-your-dialog

let refDiv = useRef(null)

<Dialog
	initialFocus={refDiv}
	as="div"
	className="fixed inset-0 z-10 overflow-y-auto bg-gray-500 bg-opacity-60"
	onClose={closeModal}
>

<div ref={refDiv}>
    {children}
</div>


34reactions
xairoocommented, Apr 14, 2021

You have to set initialFocus and also use it in your return code.

Based on the example code @ https://headlessui.dev/react/dialog#rendering-to-a-portal they forgot to place ref={cancelButtonRef} inside an element (doesn’t have to be a button, could be a div or so too).

const cancelButtonRef = useRef();

...

initialFocus={cancelButtonRef}

...

<button
	ref={cancelButtonRef}
	type="button"
	className="inline-flex justify-center px-4 py-2 text-sm font-medium text-blue-900 bg-blue-100 border border-transparent rounded-md hover:bg-blue-200 focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-blue-500"
	onClick={closeModal}
>
	Got it, thanks!
</button>
Read more comments on GitHub >

github_iconTop Results From Across the Web

there are no focusable elements inside the <FocusTrap />
This error was being thrown when I tried to use the Simpler Alert Modal of TailwindUI. ... your Dialog needs a focusable element...
Read more >
Getting a "There are no focusable elements inside the ...
I am trying to run this code but getting this error, I want to create a button that opens a headless UI. I...
Read more >
ERROR Error: You can't have a focus-trap without at least one ...
If you are opening a dialog, please make sure the dialog does not render no content and there is no element to focus....
Read more >
Dialog Components: Go Native HTML or Roll Your Own?
There's a new native HTML dialog element making the rounds and I ... And even if it's not my everyday primary focus, I...
Read more >
How to Make Dialogs (The Java™ Tutorials > Creating a GUI ...
Most Dialogs present an error message or warning to a user, but Dialogs can ... When a modal Dialog is visible, it blocks...
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