|
| 1 | +--- |
| 2 | +import SearchComponent from "astro-pagefind/components/Search"; |
| 3 | +import Button from "@ui/Button.astro"; |
| 4 | +import Modal from "@components/Modal.astro"; |
| 5 | +
|
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +<Modal id="modal-search" closeOnOutsideClick=true> |
| 10 | + <h2 class="text-xl font-bold mb-2 py-2">Search</h2> |
| 11 | + <SearchComponent |
| 12 | + id="search" |
| 13 | + className="pagefind-ui" |
| 14 | + uiOptions={{ |
| 15 | + showImages: false, |
| 16 | + translations: { |
| 17 | + zero_results: "Couldn't find [SEARCH_TERM]", |
| 18 | + }, |
| 19 | + }} |
| 20 | + /> |
| 21 | + </div> |
| 22 | +</div> |
| 23 | +</Modal> |
| 24 | + |
| 25 | + |
| 26 | +<script> |
| 27 | +document.addEventListener("DOMContentLoaded", function () { |
| 28 | + const searchContainer = document.querySelector(".pagefind-ui") as HTMLElement | null; |
| 29 | + const searchInput = searchContainer?.querySelector("input") as HTMLInputElement | null; |
| 30 | + const searchButton = document.getElementById("searchButton") as HTMLElement | null; |
| 31 | + const openModal = document.querySelector('[data-open-modal="modal-search"]') as HTMLElement; |
| 32 | + const closeButton = document.querySelector('#search-close') as HTMLElement | null; |
| 33 | + |
| 34 | + let selectedIndex = -1; |
| 35 | + |
| 36 | + function openSearch() { |
| 37 | + console.log("open"); |
| 38 | + openModal.click(); |
| 39 | + if (searchInput) { |
| 40 | + searchInput.value = "Tips"; |
| 41 | + const inputEvent = new Event("input", { bubbles: true }); |
| 42 | + searchInput.dispatchEvent(inputEvent); |
| 43 | + setTimeout(() => { |
| 44 | + searchInput.value = ""; |
| 45 | + searchInput.placeholder = "Tips for You"; |
| 46 | + }, 100); |
| 47 | + searchInput.focus(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | +function closeSearch() { |
| 52 | + closeButton?.click(); |
| 53 | +} |
| 54 | + |
| 55 | + function updateSelection() { |
| 56 | + const results = searchContainer?.querySelectorAll(".pagefind-ui__result"); |
| 57 | + if (!results) return; |
| 58 | + |
| 59 | + results.forEach((result, index) => { |
| 60 | + if (result instanceof HTMLElement) { |
| 61 | + if (index === selectedIndex) { |
| 62 | + result.classList.add("selected"); |
| 63 | + result.scrollIntoView({ block: "nearest", behavior: "smooth" }); |
| 64 | + } else { |
| 65 | + result.classList.remove("selected"); |
| 66 | + } |
| 67 | + } |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + function makeResultsClickable() { |
| 72 | + const results = searchContainer?.querySelectorAll(".pagefind-ui__result"); |
| 73 | + if (!results) return; |
| 74 | + |
| 75 | + results.forEach((result) => { |
| 76 | + const link = result.querySelector(".pagefind-ui__result-link") as HTMLAnchorElement | null; |
| 77 | + if (link && !result.hasAttribute("data-clickable")) { |
| 78 | + if (result instanceof HTMLElement) { |
| 79 | + result.style.cursor = "pointer"; |
| 80 | + result.addEventListener("click", (e) => { |
| 81 | + if (!(e.target instanceof HTMLElement) || !e.target.closest("a")) { |
| 82 | + link.click(); |
| 83 | + } |
| 84 | + }); |
| 85 | + result.setAttribute("data-clickable", "true"); |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + if (searchButton) { |
| 92 | + searchButton.addEventListener("click", openSearch); |
| 93 | + } |
| 94 | + |
| 95 | + document.addEventListener("keydown", function (event) { |
| 96 | + if (!searchContainer || !searchInput) return; |
| 97 | + |
| 98 | + const results = searchContainer.querySelectorAll(".pagefind-ui__result"); |
| 99 | + |
| 100 | + if (event.ctrlKey && event.key === "k") { |
| 101 | + event.preventDefault(); |
| 102 | + openSearch(); |
| 103 | + } else if (event.key === "Escape") { |
| 104 | + closeSearch(); |
| 105 | + } else if (document.activeElement === searchInput) { |
| 106 | + if (event.key === "ArrowDown") { |
| 107 | + event.preventDefault(); |
| 108 | + selectedIndex = (selectedIndex + 1) % results.length; |
| 109 | + updateSelection(); |
| 110 | + } else if (event.key === "ArrowUp") { |
| 111 | + event.preventDefault(); |
| 112 | + selectedIndex = (selectedIndex - 1 + results.length) % results.length; |
| 113 | + updateSelection(); |
| 114 | + } else if (event.key === "Enter") { |
| 115 | + event.preventDefault(); |
| 116 | + if (results.length > 0) { |
| 117 | + const indexToOpen = selectedIndex === -1 ? 0 : selectedIndex; |
| 118 | + const link = results[indexToOpen]?.querySelector("a") as HTMLAnchorElement | null; |
| 119 | + link?.click(); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + }); |
| 124 | + |
| 125 | + const resultsObserver = new MutationObserver(() => { |
| 126 | + const results = searchContainer?.querySelectorAll(".pagefind-ui__result"); |
| 127 | + if (results && results.length > 0) { |
| 128 | + selectedIndex = 0; |
| 129 | + updateSelection(); |
| 130 | + makeResultsClickable(); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + if (searchContainer) { |
| 135 | + resultsObserver.observe(searchContainer, { |
| 136 | + childList: true, |
| 137 | + subtree: true, |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + closeButton?.addEventListener("click", closeSearch); |
| 142 | +}); |
| 143 | +</script> |
0 commit comments