1414 * limitations under the License.
1515 */
1616
17- import { createContext , useCallback , useContext , useEffect , useMemo , useState } from 'react'
17+ import { useCallback , useEffect } from 'react'
1818import FocusTrap from 'focus-trap-react'
1919
20- import { noop } from '@Common/Helper'
2120import { ALLOW_ACTION_OUTSIDE_FOCUS_TRAP } from '@Shared/constants'
2221import { preventBodyScroll } from '@Shared/Helpers'
2322
2423import { DTFocusTrapType } from './types'
2524
26- const FocusTrapControlContext = createContext < {
27- disableFocusTrap : ( ) => void
28- resumeFocusTrap : ( ) => void
29- } > ( null )
30-
31- export const useFocusTrapControl = ( ) => {
32- const context = useContext ( FocusTrapControlContext )
33- if ( ! context ) {
34- return {
35- disableFocusTrap : noop ,
36- resumeFocusTrap : noop ,
37- }
38- }
39- return context
40- }
41-
4225const DTFocusTrap = ( {
4326 onEscape,
4427 deactivateFocusOnEscape = true ,
4528 children,
4629 initialFocus = undefined ,
4730 returnFocusOnDeactivate = true ,
31+ avoidFocusTrap = false ,
4832} : DTFocusTrapType ) => {
49- const [ isFocusEnabled , setIsFocusEnabled ] = useState ( true )
50-
5133 const handleEscape = useCallback (
5234 ( e ?: KeyboardEvent | MouseEvent ) => {
5335 onEscape ( e )
@@ -64,40 +46,30 @@ const DTFocusTrap = ({
6446 }
6547 } , [ ] )
6648
67- const focusContextValue = useMemo (
68- ( ) => ( {
69- disableFocusTrap : ( ) => setIsFocusEnabled ( false ) ,
70- resumeFocusTrap : ( ) => setIsFocusEnabled ( true ) ,
71- } ) ,
72- [ ] ,
73- )
74-
7549 return (
76- < FocusTrapControlContext . Provider value = { focusContextValue } >
77- < FocusTrap
78- active = { isFocusEnabled }
79- focusTrapOptions = { {
80- escapeDeactivates : handleEscape ,
81- initialFocus,
82- allowOutsideClick : ( event ) => {
83- // Allow up to 3 parent levels to check for the allowed class
84- let el = event . target as Element | null
85- let depth = 0
86- while ( el && depth < 4 ) {
87- if ( el . classList && el . classList . contains ( ALLOW_ACTION_OUTSIDE_FOCUS_TRAP ) ) {
88- return true
89- }
90- el = el . parentElement
91- depth += 1
50+ < FocusTrap
51+ active = { ! avoidFocusTrap }
52+ focusTrapOptions = { {
53+ escapeDeactivates : handleEscape ,
54+ initialFocus,
55+ allowOutsideClick : ( event ) => {
56+ // Allow up to 3 parent levels to check for the allowed class
57+ let el = event . target as Element | null
58+ let depth = 0
59+ while ( el && depth < 4 ) {
60+ if ( el . classList && el . classList . contains ( ALLOW_ACTION_OUTSIDE_FOCUS_TRAP ) ) {
61+ return true
9262 }
93- return false
94- } ,
95- returnFocusOnDeactivate,
96- } }
97- >
98- { children }
99- </ FocusTrap >
100- </ FocusTrapControlContext . Provider >
63+ el = el . parentElement
64+ depth += 1
65+ }
66+ return false
67+ } ,
68+ returnFocusOnDeactivate,
69+ } }
70+ >
71+ { children }
72+ </ FocusTrap >
10173 )
10274}
10375
0 commit comments