@@ -61,7 +61,7 @@ export interface UseFocusTrapReturn {
61
61
* @see https://vueuse.org/useFocusTrap
62
62
*/
63
63
export function useFocusTrap (
64
- target : Arrayable < MaybeRefOrGetter < string > | MaybeComputedElementRef > ,
64
+ target : MaybeRefOrGetter < Arrayable < MaybeRefOrGetter < string > | MaybeComputedElementRef > > ,
65
65
options : UseFocusTrapOptions = { } ,
66
66
) : UseFocusTrapReturn {
67
67
let trap : undefined | FocusTrap
@@ -102,28 +102,42 @@ export function useFocusTrap(
102
102
( els ) => {
103
103
if ( ! els . length )
104
104
return
105
-
106
- trap = createFocusTrap ( els , {
107
- ...focusTrapOptions ,
108
- onActivate ( ) {
109
- hasFocus . value = true
110
-
111
- // Apply if user provided onActivate option
112
- if ( options . onActivate )
113
- options . onActivate ( )
114
- } ,
115
- onDeactivate ( ) {
116
- hasFocus . value = false
117
-
118
- // Apply if user provided onDeactivate option
119
- if ( options . onDeactivate )
120
- options . onDeactivate ( )
121
- } ,
122
- } )
123
-
124
- // Focus if immediate is set to true
125
- if ( immediate )
126
- activate ( )
105
+ if ( ! trap ) {
106
+ // create the trap
107
+ trap = createFocusTrap ( els , {
108
+ ...focusTrapOptions ,
109
+ onActivate ( ) {
110
+ hasFocus . value = true
111
+
112
+ // Apply if user provided onActivate option
113
+ if ( options . onActivate )
114
+ options . onActivate ( )
115
+ } ,
116
+ onDeactivate ( ) {
117
+ hasFocus . value = false
118
+
119
+ // Apply if user provided onDeactivate option
120
+ if ( options . onDeactivate )
121
+ options . onDeactivate ( )
122
+ } ,
123
+ } )
124
+
125
+ // Focus if immediate is set to true
126
+ if ( immediate )
127
+ activate ( )
128
+ }
129
+ else {
130
+ // get the active state of the trap
131
+ const isActive = trap ?. active
132
+
133
+ // update the container elements
134
+ trap ?. updateContainerElements ( els )
135
+
136
+ // if the trap is not active and immediate is set to true, activate the trap
137
+ if ( ! isActive && immediate ) {
138
+ activate ( )
139
+ }
140
+ }
127
141
} ,
128
142
{ flush : 'post' } ,
129
143
)
0 commit comments