@@ -17,6 +17,7 @@ import { useEffect } from 'react';
17
17
import { getGoogleAnalyticsUserIdFromBrowserCookie } from '../../lib/google/google'
18
18
19
19
let DocSearchModal = null ;
20
+ let searchContainer = null ;
20
21
21
22
function Hit ( { hit, children } ) {
22
23
const handleClick = ( ) => {
@@ -69,7 +70,6 @@ function DocSearch({ contextualSearch, externalUrlRegex, ...props }) {
69
70
clickAnalytics : true ,
70
71
} ;
71
72
const history = useHistory ( ) ;
72
- const searchContainer = useRef ( null ) ;
73
73
const searchButtonRef = useRef ( null ) ;
74
74
const [ isOpen , setIsOpen ] = useState ( false ) ;
75
75
const [ initialQuery , setInitialQuery ] = useState ( undefined ) ;
@@ -100,18 +100,26 @@ function DocSearch({ contextualSearch, externalUrlRegex, ...props }) {
100
100
101
101
const onOpen = useCallback ( ( ) => {
102
102
importDocSearchModalIfNeeded ( ) . then ( ( ) => {
103
- searchContainer . current = document . createElement ( 'div' ) ;
103
+ // searchContainer is not null here when the modal is already open
104
+ // this check is needed because ctrl + k shortcut was handled by another instance of SearchBar component
105
+ if ( searchContainer ) {
106
+ return ;
107
+ }
108
+
109
+ searchContainer = document . createElement ( 'div' ) ;
104
110
document . body . insertBefore (
105
- searchContainer . current ,
111
+ searchContainer ,
106
112
document . body . firstChild ,
107
113
) ;
114
+
108
115
setIsOpen ( true ) ;
109
116
} ) ;
110
117
} , [ importDocSearchModalIfNeeded , setIsOpen ] ) ;
111
118
112
119
const onClose = useCallback ( ( ) => {
113
120
setIsOpen ( false ) ;
114
- searchContainer . current ?. remove ( ) ;
121
+ searchContainer ?. remove ( ) ;
122
+ searchContainer = null ; ;
115
123
} , [ setIsOpen ] ) ;
116
124
117
125
const onInput = useCallback (
@@ -204,7 +212,7 @@ function DocSearch({ contextualSearch, externalUrlRegex, ...props }) {
204
212
205
213
{ isOpen &&
206
214
DocSearchModal &&
207
- searchContainer . current &&
215
+ searchContainer &&
208
216
createPortal (
209
217
< DocSearchModal
210
218
onClose = { onClose }
@@ -223,7 +231,7 @@ function DocSearch({ contextualSearch, externalUrlRegex, ...props }) {
223
231
placeholder = { translations . placeholder }
224
232
translations = { translations . modal }
225
233
/> ,
226
- searchContainer . current ,
234
+ searchContainer ,
227
235
) }
228
236
</ >
229
237
) ;
0 commit comments