Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modal: Fix infinite focus loop (#649)
The TrapFocusBehavior component listens to all document focus events. If a target of a FocusEvent does not match any children of the trapped component, the event is blocked and a new one is dispatched for the first child of the trapped component. This works well for a single trap. The infinite loop occurs when there is more than one TrapFocusBehaviour trying to trap the focus for its first child. Calling .focus() for it triggers a new FocusEvent, which is then captured by all Trap components, calling .focus() again and filling all call stack up. The solution is to check whether the event has been triggered by a trap component or not. If it has, then let the event run its course. Since the first child is actually the event target, and not the trap component, Element.closest is used to identify if the target has a trap ancestor. In short, * 'trap-focus' name added to identify TrapFocusBehavior components * Element.closest() used to identify trapped components * class component migrated to hook Impact: both Modal and Sheet use TrapFocusBehavior.
- Loading branch information
1 parent
1ab8fd0
commit 024b979
Showing
3 changed files
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters