-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Focus not returned to triggering button when closing modal #3320
Comments
Hey thanks for the bug report, your idea is a good idea, but instead of checking if the element is the focusable element when figuring out the element to restore, I resolved the closest focusable element when recording the click instead. This should be fixed by #3365, and will be available in the next release. You can already try it using:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What package within Headless UI are you using?
@headlessui/react
What version of that package are you using?
v1.7.17 - v.2.x.x
What browser are you using?
Chrome
Reproduction URL
Sandbox
Describe your issue
When opening a Dialog by clicking on a button with its content wrapped in a
span
, focus is not properly restored to the triggering button after the dialog closes. Focus is instead returned tobody
.I believe this is due to the
span
element being added as the latest focused element in the active element history, and when trying to focus thespan
the browser falls back to focusingbody
instead.A solution could be to find the first focusable element in the history array and trying to focus that? Something like this:
Here:
headlessui/packages/@headlessui-react/src/components/focus-trap/focus-trap.tsx
Line 271 in 6ac6930
What do you think? I can open a PR if I'm not missing something important here.
The text was updated successfully, but these errors were encountered: