Skip to content

Commit be84f16

Browse files
Antreesybackportbot[bot]
authored andcommitted
fix(focusTrap): allow to auto-resolve trap stack if was changed outside of controller
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
1 parent 13eb6e4 commit be84f16

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/utils/focusTrap.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export function getTrapStack() {
2626
*
2727
* When some component manually implements its own focus-trap without using global focus-trap stack,
2828
* it also needs to pause the global stack to avoid conflict.
29+
*
30+
* Note: if global focus-trap stack was modified outside TrapStackController (likely due to user actions),
31+
* trap queue should be correctly handled by focus-trap itself.
32+
* It is no longer safe to unpause cached `pausedStack`, as it can disrupt the current focus trap state.
33+
* We assume that the focus-trap stack is self-regulated and ignore unpause.
2934
*/
3035
export function createTrapStackController() {
3136
/**
@@ -45,10 +50,13 @@ export function createTrapStackController() {
4550
},
4651
/**
4752
* Unpause the paused focus trap stack
53+
* If the actual stack is different from the paused one, ignore unpause.
4854
*/
4955
unpause() {
50-
for (const trap of pausedStack) {
51-
trap.unpause()
56+
if (pausedStack.length === getTrapStack().length) {
57+
for (const trap of pausedStack) {
58+
trap.unpause()
59+
}
5260
}
5361
pausedStack = []
5462
},

0 commit comments

Comments
 (0)