Skip to content

Commit 5e8eb11

Browse files
authored
Fix S key in Ask AI modal triggering the MeiliSearch modal (#2786)
* Fix S key triggering MeiliSearch modal * Allow some keys to be captured
1 parent 85ab647 commit 5e8eb11

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

docusaurus/src/theme/SearchBar/index.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,37 @@ function SearchBarContent() {
1616
return;
1717
}
1818

19-
// Clean up previous instance and clear container
19+
const handleKeyDown = (e) => {
20+
const kapaContainer = document.getElementById('kapa-widget-container');
21+
22+
if (!kapaContainer || !kapaContainer.shadowRoot) {
23+
return;
24+
}
25+
26+
const shadowActiveElement = kapaContainer.shadowRoot.activeElement;
27+
28+
if (shadowActiveElement &&
29+
(shadowActiveElement.tagName === 'INPUT' ||
30+
shadowActiveElement.tagName === 'TEXTAREA' ||
31+
shadowActiveElement.isContentEditable)) {
32+
33+
const allowedKeys = ['Enter', 'Tab', 'Escape', 'ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'];
34+
35+
if (!allowedKeys.includes(e.key)) {
36+
e.stopImmediatePropagation();
37+
}
38+
}
39+
};
40+
41+
document.addEventListener('keydown', handleKeyDown, true);
42+
2043
if (searchInstanceRef.current) {
2144
searchInstanceRef.current.destroy?.();
2245
searchInstanceRef.current = null;
2346
}
2447

25-
// Clear the container content
2648
searchButtonRef.current.innerHTML = '';
2749

28-
// Dynamic import to avoid SSR issues with solid-js
2950
Promise.all([
3051
import('meilisearch-docsearch'),
3152
import('meilisearch-docsearch/css')
@@ -129,6 +150,7 @@ function SearchBarContent() {
129150
});
130151

131152
return () => {
153+
document.removeEventListener('keydown', handleKeyDown, true);
132154
if (searchInstanceRef.current) {
133155
searchInstanceRef.current.destroy?.();
134156
searchInstanceRef.current = null;

0 commit comments

Comments
 (0)