Skip to content
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

Ensure focus trap, Tabs and Dialog play well together #1231

Merged
merged 6 commits into from
Mar 10, 2022

Conversation

RobinMalfait
Copy link
Collaborator

When you render Tabs inside a Dialog then you will receive a warning like this: There are no focusable elements inside the <FocusTrap />
This is because the Dialog uses a FocusTrap, and the selected Tab registers itself in a next tick so it is not immediately in the DOM.

This PR fixes that so that they all play well together.

Fixes: #771

This component will allow you to catch the focus and forward it to a new
element. The catch is that it will retry to do that because sometimes
components won't be available yet.

E.g.: We want to focus the first Tab component if it is rendered inside
the Dialog. However, a Tab will register itself in the next tick,
triggering a re-render and only then will it be `selected`. This is a
bit too late for the FocusTrap component.

The FocusSentinel should fix this by catching the focus, and forwarding
it to the correct component. Once that is done, it will remove itself
from the DOM tree so that you can't ever focus that element anymore.
This should fix potential `<tab>` and `<shift+tab>` behaviour.
Because we are firing `setState` calls within the component, React is
yelling at us for not using `act(() => { ... })`. Welp, not going to add
those calls inside the component just for tests...
@vercel
Copy link

vercel bot commented Mar 10, 2022

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployments, click below or on the icon next to each commit.

headlessui-vue – ./packages/playground-vue

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-vue/DmUtwet6YbAxr6h3oL8oCBHvq4Wd
✅ Preview: https://headlessui-vue-git-tabs-and-focus-trap-tailwindlabs.vercel.app

headlessui-react – ./packages/playground-react

🔍 Inspect: https://vercel.com/tailwindlabs/headlessui-react/8kdqPqm9EzFuVAipuTUGxfDjWo2i
✅ Preview: https://headlessui-react-git-tabs-and-focus-trap-tailwindlabs.vercel.app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: <FocusTrap /> does not recognize <Tab />s as focusable elements on first mount
1 participant