What's the best pattern for nested traps in the following scenario? #942
-
So I have nested Traps, both with the prop Is there a pattern I can use to close both traps if the click is outside of both, or just the inner trap if the click is outside the inner trap but within the outer one? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
Great question! When a second trap is activated, the first one gets paused, which means it stops listening to all the events necessary to detect things like outside clicks. So when the user clicks outside the second (now active) trap, the first trap doesn't know anything about it. But when the second trap gets deactivated from the outside click, the first one will automatically get unpaused. Unpausing a trap will effectively re-activate that trap, and the So what I would propose you try is to use the function form of the Then add an Admittedly, it would be nice if focus-trap tracked the outside click all the way back "up" the chain of traps and simply did not reactivate (but just popped of the internal stack) any traps before the deactivated one that are also "click outside deactivates" and for which the clicked node falls outside of. (Not the best English sentence formulation there, but I think you get the drift...) |
Beta Was this translation helpful? Give feedback.
Great question! When a second trap is activated, the first one gets paused, which means it stops listening to all the events necessary to detect things like outside clicks. So when the user clicks outside the second (now active) trap, the first trap doesn't know anything about it. But when the second trap gets deactivated from the outside click, the first one will automatically get unpaused. Unpausing a trap will effectively re-activate that trap, and the
onActivate()
andonPostActivate()
hooks (which you can tap into) should get called -- except you won't have a parameter passed to those telling you anything about why the trap is being (re)activated.So what I would propose you try is to…