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.

(React) Menu steals focus from modal on Menu.Item click

See original GitHub issue

Hi there,

First of all, thanks so much for tailwind and tailwindui!

The issue I’m facing relates to the latest release of @headlessui/react.

I have a Menu component with a Menu Item that opens a modal (react-modal). The modal contains a form with an autofocus input that should grab and maintain focus on modal open. The problem is that the Menu Button steals focus away from the modal (so - focus is on the Menu Button behind the modal overlay) after Menu.Item click.

As a temporary-but-not-great workaround, I found that using setTimeout to slightly delay opening the modal after Menu.Item click usually results in the modal form getting and keeping focus.

I think this might be related to #47? Is there a proper way to prevent the Menu Button focus?

Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7

github_iconTop GitHub Comments

2reactions
dylancomcommented, Mar 18, 2022

Same problem here and using refs didn’t fix it. Currently as a quick fix I use a timeout of 50ms to open a modal. In that case react-modal’s focus management works fine. https://github.com/getAlby/lightning-browser-extension/pull/709

0reactions
manodrumcommented, Oct 14, 2022

Posting here for future reference, this was fixed in https://github.com/tailwindlabs/headlessui/pull/1782

Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent restoring of focus when closing popup menu opened ...
I need to prevent the restoration of the focus to the input element. How can I achieve this behaviour? import React from "react";...
Read more >
React Menu component - Material UI - MUI
If used for item selection, when opened, simple menus places the initial focus on the selected menu item. The currently selected menu item...
Read more >
Creating An Outside Focus And Click Handler React Component
In this article, we'll look at how to create an outside focus and click handler with React. We will recreate an open source...
Read more >
Restoring ActiveElement Focus After A User-Interaction In ...
In a modal window scenario, where we are drawing focus away from the "trigger" element and into the modal window context, we can...
Read more >
Using JavaScript to trap focus in an element | hidde.blog
Elements appear and disappear on click, scroll or even form validation. ... these cases we would like to trap focus in the modal,...
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