Skip to content

Commit 928b9ca

Browse files
fix(docsearch): allow a single instance to open
If a website uses two DocSearch instances (e.g., one for mobile and one for desktop), it resulted in a double modal being show when hitting `Ctrl + K`. This is fixed by checking that the active DocSearch CSS class is not applied on `body` before opening the modal.
1 parent 9c1b040 commit 928b9ca

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

src/useDocSearchKeyboardEvents.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ export function useDocSearchKeyboardEvents({
2929
}: UseDocSearchKeyboardEventsProps) {
3030
React.useEffect(() => {
3131
function onKeyDown(event: KeyboardEvent) {
32+
function open() {
33+
// We check that no other DocSearch modal is showing before opening
34+
// another one.
35+
if (!document.body.classList.contains('DocSearch--active')) {
36+
onOpen();
37+
}
38+
}
3239
if (
3340
(event.keyCode === 27 && isOpen) ||
3441
// The `Cmd+K` shortcut both opens and closes the modal.
@@ -41,8 +48,8 @@ export function useDocSearchKeyboardEvents({
4148

4249
if (isOpen) {
4350
onClose();
44-
} else {
45-
onOpen();
51+
} else if (!document.body.classList.contains('DocSearch--active')) {
52+
open();
4653
}
4754
}
4855

0 commit comments

Comments
 (0)