Skip to content

Commit 28d125e

Browse files
committed
Autofill Cmd+K on 404 pages
1 parent fec384b commit 28d125e

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

src/components/404.astro

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,37 @@
1010

1111
<script>
1212
import { track } from "~/util/zaraz";
13+
import { openGlobalSearch } from "~/util/search";
1314

1415
const { pathname } = window.location;
1516

1617
const anchor = document.getElementById("404-search-link");
18+
const pretty = decodeURIComponent(
19+
pathname.replaceAll("/", " ").replaceAll("-", " ").trim(),
20+
);
1721

1822
if (anchor) {
19-
const pretty = pathname.replaceAll("/", " ").replaceAll("-", " ").trim();
20-
2123
anchor.setAttribute("href", `/search/?q=${encodeURIComponent(pretty)}`);
2224
anchor.addEventListener("click", () => {
2325
track("serp from location", { value: "404", query: pretty });
2426
});
2527
}
28+
29+
document.addEventListener(
30+
"keydown",
31+
(keyboardEvent) => {
32+
if (
33+
(keyboardEvent.metaKey || keyboardEvent.ctrlKey) &&
34+
keyboardEvent.key === "k"
35+
) {
36+
keyboardEvent.preventDefault();
37+
keyboardEvent.stopPropagation();
38+
39+
openGlobalSearch(pretty);
40+
}
41+
},
42+
{
43+
capture: true,
44+
},
45+
);
2646
</script>

src/components/overrides/Sidebar.astro

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
3737
<Default><slot /></Default>
3838

3939
<script>
40-
import { track } from "~/util/zaraz"
40+
import { track } from "~/util/zaraz";
41+
import { openGlobalSearch } from "~/util/search";
42+
4143
function initSidebarSearch() {
4244
const searchInput = document.getElementById('sidebar-search') as HTMLInputElement;
4345
const noResultsMessage = document.getElementById('sidebar-no-results') as HTMLElement;
@@ -230,29 +232,7 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
230232
globalSearchLink.addEventListener('click', () => {
231233
const currentQuery = searchInput.value.trim();
232234
if (currentQuery) {
233-
// Try multiple selectors for DocSearch
234-
const docSearchButton = document.querySelector('#docsearch button') as HTMLButtonElement ||
235-
document.querySelector('.DocSearch-Button') as HTMLButtonElement ||
236-
document.querySelector('[data-docsearch-button]') as HTMLButtonElement;
237-
238-
if (docSearchButton) {
239-
// Click the DocSearch button to open the modal
240-
docSearchButton.click();
241-
242-
// Wait for modal to open and set the search term
243-
setTimeout(() => {
244-
const searchInput = document.querySelector('.DocSearch-Input') as HTMLInputElement ||
245-
document.querySelector('#docsearch-input') as HTMLInputElement ||
246-
document.querySelector('[data-docsearch-input]') as HTMLInputElement;
247-
248-
if (searchInput) {
249-
searchInput.value = currentQuery;
250-
searchInput.focus();
251-
// Trigger search
252-
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
253-
}
254-
}, 100);
255-
}
235+
openGlobalSearch(currentQuery);
256236
}
257237
});
258238

src/util/search.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export const openGlobalSearch = (searchTerm?: string) => {
2+
// Try multiple selectors for DocSearch
3+
const docSearchButton =
4+
(document.querySelector("#docsearch button") as HTMLButtonElement) ||
5+
(document.querySelector(".DocSearch-Button") as HTMLButtonElement) ||
6+
(document.querySelector("[data-docsearch-button]") as HTMLButtonElement);
7+
8+
if (docSearchButton) {
9+
// Click the DocSearch button to open the modal
10+
docSearchButton.click();
11+
12+
if (searchTerm) {
13+
// Wait for modal to open and set the search term
14+
setTimeout(() => {
15+
const searchInput =
16+
(document.querySelector(".DocSearch-Input") as HTMLInputElement) ||
17+
(document.querySelector("#docsearch-input") as HTMLInputElement) ||
18+
(document.querySelector(
19+
"[data-docsearch-input]",
20+
) as HTMLInputElement);
21+
22+
if (searchInput) {
23+
searchInput.value = searchTerm;
24+
searchInput.focus();
25+
// Trigger search
26+
searchInput.dispatchEvent(new Event("input", { bubbles: true }));
27+
}
28+
}, 100);
29+
}
30+
}
31+
};

0 commit comments

Comments
 (0)