New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(React) Menu steals focus from modal on Menu.Item click #259
Comments
Hey! Thank you for your bug report! The Menu should not "steal" focus, however if you click on a Menu.Item it should restore focus to the Menu.Button for accessibility reasons (otherwise screenreader users could get lost when suddenly the body is focused). Do you mean that once you clicked on the Menu.Item that it should restore the focus to the <Menu.Item as="button" onClick={() => {
// Do what you need to do here
inputRef.focus()
}}>
...
</Menu.Item> If this doesn't work, or the behaviour you want is different, please provide a reproduction repo/codesandbox. |
Hey @RobinMalfait - thank you for taking the time to respond. I appreciate that the current behavior is probably desired in most cases and so hesitated to actually call this a bug in my opening comment. I was able to reproduce what I'm experiencing in codesandbox here. Apologies in advance for the quick and dirty code :) I started out going the ref route like you suggest, but the code was starting to look a bit convoluted - I reuse various "base" modals, each of which can correspond to different types of records, contain different forms, inputs, etc, and the forms themselves are abstracted a bit and can be a few layers deep. Ideally, there would be a boolean toggle to skip the restoration of focus to Menu.Button. Let me know if any additional information would be helpful. Thanks again for your consideration! |
Hi @RobinMalfait - I am no longer experiencing this issue after refactoring my code and switching from formik + react-modal to react-hook-form + the headless ui modal implementation. Thanks for your consideration. |
Hi @RobinMalfait - I'm having the same problem where the focus is "stolen" by the Menu.Button upon closing of the Menu.Items. I'd like to provide an "actions" menu option to edit a user generated field by focusing on it (in my case I'm using textarea, but this could also be a text input). The use case seems common enough, for example Monday.com has a menu item to "Rename board" which will focus the board name input. Using similar example code that you provided, I'm trying to focus a text area onClick of a Menu.Item. https://codesandbox.io/s/stoic-bose-xvn1nz?file=/src/App.js You can see that the textarea flashes because it it focused, then immediately focus is "stolen" by the Menu.Button. You can also see the logged output for onFocus and onBlur events for the textarea for confirmation. Hope this helps provide some context. I agree with @reczy that it would be great to have an option to skip the focus to Menu.Button. |
Same problem here and using refs didn't fix it. |
I also hit the issue triggering the As mentioned, I am opening a I am still investigating in light of my use case and trying to make sure the issue is not "operator error", so far doesn't seem to be. If anyone has hit this and has help, please share. Thanks (using Edit I should add (since it might not clear from my description) that the |
Posting here for future reference, this was fixed in #1782 |
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!
The text was updated successfully, but these errors were encountered: