@@ -11,7 +11,7 @@ import {
11
11
} from '@/hooks' ;
12
12
import { useTrapFocus } from '@/hooks/useTrapFocus/useTrapFocus' ;
13
13
import { ErrorBoundary , FallbackComponent } from '@/provider/errorBoundary' ;
14
- import { focusFirstDescendant , isKeyTabPressed } from '@/utils' ;
14
+ import { focusFirstDescendant } from '@/utils' ;
15
15
import { convertDurationToNumber } from '@/utils/stringUtility/string.utility' ;
16
16
17
17
import { PopoverStandAlone } from './popoverStandAlone' ;
@@ -34,6 +34,7 @@ const PopoverControlledComponent = React.forwardRef(
34
34
clickOverlayClose = true ,
35
35
pressEscapeClose = true ,
36
36
preventScrollOnCloseFocus = false ,
37
+ trapFocusInsideModal = true ,
37
38
ctv,
38
39
...props
39
40
} : IPopoverControlled ,
@@ -43,6 +44,7 @@ const PopoverControlledComponent = React.forwardRef(
43
44
const currentFocus = React . useRef < HTMLElement | null > ( null ) ;
44
45
const innerRef = React . useRef < HTMLDivElement | null > ( null ) ;
45
46
const forwardedRef = React . useRef < HTMLDivElement | null > ( null ) ;
47
+ const [ forwardedElementInitialized , setForwardedElementInitialized ] = React . useState ( false ) ;
46
48
const setForwareddRef = React . useCallback (
47
49
node => {
48
50
if ( node && focusFirstDescendantAutomatically ) {
@@ -57,6 +59,7 @@ const PopoverControlledComponent = React.forwardRef(
57
59
if ( ! node && blockBack ) {
58
60
allowScroll ( ) ;
59
61
}
62
+ setForwardedElementInitialized ( ! ! node ) ;
60
63
forwardedRef . current = node ;
61
64
} ,
62
65
[ props . forwardedRef , focusFirstDescendantAutomatically ]
@@ -120,7 +123,12 @@ const PopoverControlledComponent = React.forwardRef(
120
123
} ;
121
124
122
125
useClickOutside ( forwardedRef , handleClickOutside , props . preventCloseOnClickElements ) ;
123
- useEscPressedV2 ( { ref : innerRef , onEscPress : handlePressScape } ) ;
126
+ useEscPressedV2 ( {
127
+ ref : innerRef ,
128
+ onEscPress : handlePressScape ,
129
+ disablePreventDefault : ! pressEscapeClose ,
130
+ disableStopPropagation : ! pressEscapeClose ,
131
+ } ) ;
124
132
125
133
const beforeModalFocus = ( ) => {
126
134
currentFocus . current = document . activeElement as HTMLElement ;
@@ -137,17 +145,10 @@ const PopoverControlledComponent = React.forwardRef(
137
145
}
138
146
} ;
139
147
140
- // to force rerender to update ref in useTrapFocus
141
- const [ tabPressed , setTabPressed ] = React . useState ( false ) ;
142
-
143
- const handleKeyDown : React . KeyboardEventHandler < HTMLElement > = event => {
144
- if ( isKeyTabPressed ( event . key ) && forwardedRef . current ) {
145
- setTabPressed ( true ) ;
146
- props . onKeyDown ?.( event ) ;
147
- }
148
- } ;
149
-
150
- useTrapFocus ( { element : forwardedRef , hasFocusTrap : props . trapFocusInsideModal , tabPressed } ) ;
148
+ useTrapFocus ( {
149
+ ref : forwardedRef ,
150
+ trapFocus : forwardedElementInitialized && trapFocusInsideModal ,
151
+ } ) ;
151
152
152
153
React . useEffect ( ( ) => {
153
154
if ( ! props . open && openAnimationRef . current ) {
@@ -207,7 +208,6 @@ const PopoverControlledComponent = React.forwardRef(
207
208
forwardedRef = { setForwareddRef }
208
209
open = { openAnimation }
209
210
styles = { styles }
210
- onKeyDown = { handleKeyDown }
211
211
/>
212
212
) ;
213
213
}
0 commit comments