[@mantine/hooks] Focus trap identity #4118
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #4079
This addresses an edge case with
useFocusTrap
that can cause the cleanup function returned bycreateAriaHider
to run after anotheruseFocusTrap
instance has operated on the same set of root nodes. This is due to thecreateAriaHider
function being called when the callback ref returned byuseFocusTrap
is invoked, which will run before the cleanup function if two elements that utilizeuseFocusRoot
are toggled simultaneously. An example of this behavior can be found in the original issue withMenu.Item
/Modal
: https://codesandbox.io/s/angry-rgb-x46ub2The approach taken was to set a
data-focus-id
attribute to every root node that has it'saria-hidden
value mutated bycreateAriaHider
. In the returned restore function, we are then able to check if the nodes currentdata-focus-id
attribute matches the original assigned ID. If not, we know that anotheruseFocusTrap
instance was rendered & we should not move forward with restoring thearia-hidden
attributes for that given run. Additionally, adata-hidden
attribute will also be added to these root nodes so that we are able to keep track of the elements initialaria-hidden
state between usages ofuseFocusTrap
.Tests for
use-focus-trap
were also added, including a test case for the above scenario.