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

[@mantine/hooks] Focus trap identity #4118

Merged
merged 4 commits into from
Apr 25, 2023

Conversation

Ben-Kincaid
Copy link
Contributor

Fixes #4079

This addresses an edge case with useFocusTrap that can cause the cleanup function returned by createAriaHider to run after another useFocusTrap instance has operated on the same set of root nodes. This is due to the createAriaHider function being called when the callback ref returned by useFocusTrap is invoked, which will run before the cleanup function if two elements that utilize useFocusRoot are toggled simultaneously. An example of this behavior can be found in the original issue with Menu.Item/Modal: https://codesandbox.io/s/angry-rgb-x46ub2

The approach taken was to set a data-focus-id attribute to every root node that has it's aria-hidden value mutated by createAriaHider. In the returned restore function, we are then able to check if the nodes current data-focus-id attribute matches the original assigned ID. If not, we know that another useFocusTrap instance was rendered & we should not move forward with restoring the aria-hidden attributes for that given run. Additionally, a data-hidden attribute will also be added to these root nodes so that we are able to keep track of the elements initial aria-hidden state between usages of useFocusTrap.

Tests for use-focus-trap were also added, including a test case for the above scenario.

@Ben-Kincaid Ben-Kincaid changed the title Focus trap identity [@mantine/hooks] Focus trap identity Apr 22, 2023
@Ben-Kincaid
Copy link
Contributor Author

Ugh... Even though this should be totally safe in this context, need to figure out a way to suppress these warnings in the tests.

image

These tests must use the document body as the container element since createAriaHider uses body > :not(script) for getting the root nodes. Appending via portal also appears to throw the same warnings.

Looks like i can reassign global.console for each of the tests to avoid output, but that doesn't feel safe.

@rtivital
Copy link
Member

https://github.com/mantinedev/mantine/blob/master/src/mantine-tests/src/patch-console-error.ts#L13

@Ben-Kincaid
Copy link
Contributor Author

Thanks @rtivital! Just updated with the fix

@rtivital rtivital changed the base branch from dev to master April 25, 2023 06:45
@rtivital rtivital merged commit e922847 into mantinedev:master Apr 25, 2023
1 check passed
@rtivital
Copy link
Member

Thanks!

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.

FocusTrap doesn't remove aria-hidden=true attribute
2 participants