Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/components/404.astro
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,37 @@

<script>
import { track } from "~/util/zaraz";
import { openGlobalSearch } from "~/util/search";

const { pathname } = window.location;

const anchor = document.getElementById("404-search-link");
const pretty = decodeURIComponent(
pathname.replaceAll("/", " ").replaceAll("-", " ").trim(),
);

if (anchor) {
const pretty = pathname.replaceAll("/", " ").replaceAll("-", " ").trim();

anchor.setAttribute("href", `/search/?q=${encodeURIComponent(pretty)}`);
anchor.addEventListener("click", () => {
track("serp from location", { value: "404", query: pretty });
});
}

document.addEventListener(
"keydown",
(keyboardEvent) => {
if (
(keyboardEvent.metaKey || keyboardEvent.ctrlKey) &&
keyboardEvent.key === "k"
) {
keyboardEvent.preventDefault();
keyboardEvent.stopPropagation();

openGlobalSearch(pretty);
}
},
{
capture: true,
},
);
</script>
28 changes: 4 additions & 24 deletions src/components/overrides/Sidebar.astro
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
<Default><slot /></Default>

<script>
import { track } from "~/util/zaraz"
import { track } from "~/util/zaraz";
import { openGlobalSearch } from "~/util/search";

function initSidebarSearch() {
const searchInput = document.getElementById('sidebar-search') as HTMLInputElement;
const noResultsMessage = document.getElementById('sidebar-no-results') as HTMLElement;
Expand Down Expand Up @@ -230,29 +232,7 @@ const [product, module] = Astro.url.pathname.split("/").filter(Boolean);
globalSearchLink.addEventListener('click', () => {
const currentQuery = searchInput.value.trim();
if (currentQuery) {
// Try multiple selectors for DocSearch
const docSearchButton = document.querySelector('#docsearch button') as HTMLButtonElement ||
document.querySelector('.DocSearch-Button') as HTMLButtonElement ||
document.querySelector('[data-docsearch-button]') as HTMLButtonElement;

if (docSearchButton) {
// Click the DocSearch button to open the modal
docSearchButton.click();

// Wait for modal to open and set the search term
setTimeout(() => {
const searchInput = document.querySelector('.DocSearch-Input') as HTMLInputElement ||
document.querySelector('#docsearch-input') as HTMLInputElement ||
document.querySelector('[data-docsearch-input]') as HTMLInputElement;

if (searchInput) {
searchInput.value = currentQuery;
searchInput.focus();
// Trigger search
searchInput.dispatchEvent(new Event('input', { bubbles: true }));
}
}, 100);
}
openGlobalSearch(currentQuery);
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions src/util/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
export const openGlobalSearch = (searchTerm?: string) => {
// Try multiple selectors for DocSearch
const docSearchButton =
(document.querySelector("#docsearch button") as HTMLButtonElement) ||
(document.querySelector(".DocSearch-Button") as HTMLButtonElement) ||
(document.querySelector("[data-docsearch-button]") as HTMLButtonElement);

if (docSearchButton) {
// Click the DocSearch button to open the modal
docSearchButton.click();

if (searchTerm) {
// Wait for modal to open and set the search term
setTimeout(() => {
const searchInput =
(document.querySelector(".DocSearch-Input") as HTMLInputElement) ||
(document.querySelector("#docsearch-input") as HTMLInputElement) ||
(document.querySelector(
"[data-docsearch-input]",
) as HTMLInputElement);

if (searchInput) {
searchInput.value = searchTerm;
searchInput.focus();
// Trigger search
searchInput.dispatchEvent(new Event("input", { bubbles: true }));
}
}, 100);
}
}
};