|
| 1 | +--- |
| 2 | +// Heavy inspiration taken from Astro Starlight -> https://github.com/withastro/starlight/blob/main/packages/starlight/components/Search.astro |
| 3 | +
|
| 4 | +import "@pagefind/default-ui/css/ui.css"; |
| 5 | +--- |
| 6 | + |
| 7 | +<site-search id="search" class="ms-auto"> |
| 8 | + <button |
| 9 | + data-open-modal |
| 10 | + disabled |
| 11 | + class="flex h-9 w-9 items-center justify-center rounded-md ring-zinc-400 transition-all hover:ring-2" |
| 12 | + > |
| 13 | + <svg |
| 14 | + aria-label="search" |
| 15 | + class="h-6 w-6" |
| 16 | + xmlns="http://www.w3.org/2000/svg" |
| 17 | + width="16" |
| 18 | + height="16" |
| 19 | + viewBox="0 0 24 24" |
| 20 | + fill="none" |
| 21 | + stroke="currentColor" |
| 22 | + stroke-linecap="round" |
| 23 | + stroke-linejoin="round" |
| 24 | + stroke-width="1.5" |
| 25 | + > |
| 26 | + <path stroke="none" d="M0 0h24v24H0z"></path> |
| 27 | + <path d="M3 10a7 7 0 1 0 14 0 7 7 0 1 0-14 0M21 21l-6-6"></path> |
| 28 | + </svg> |
| 29 | + </button> |
| 30 | + <dialog |
| 31 | + aria-label="search" |
| 32 | + class="h-full max-h-full w-full max-w-full border border-zinc-400 bg-[--theme-bg] shadow backdrop:bg-[--theme-menu-bg] backdrop:backdrop-blur sm:mx-auto sm:mb-auto sm:mt-16 sm:h-max sm:max-h-[calc(100%-8rem)] sm:min-h-[15rem] sm:w-5/6 sm:max-w-[48rem] sm:rounded-md" |
| 33 | + > |
| 34 | + <div class="dialog-frame flex flex-col gap-4 p-6 pt-12 sm:pt-6"> |
| 35 | + <button |
| 36 | + data-close-modal |
| 37 | + class="ms-auto cursor-pointer rounded-md bg-zinc-200 p-2 font-semibold dark:bg-zinc-700" |
| 38 | + >Close</button |
| 39 | + > |
| 40 | + { |
| 41 | + import.meta.env.DEV ? ( |
| 42 | + <div class="mx-auto text-center"> |
| 43 | + <p> |
| 44 | + Search is only available in production builds. <br /> |
| 45 | + Try building and previewing the site to test it out locally. |
| 46 | + </p> |
| 47 | + </div> |
| 48 | + ) : ( |
| 49 | + <div class="search-container"> |
| 50 | + <div id="cactus__search" /> |
| 51 | + </div> |
| 52 | + ) |
| 53 | + } |
| 54 | + </div> |
| 55 | + </dialog> |
| 56 | +</site-search> |
| 57 | + |
| 58 | +<script> |
| 59 | + class SiteSearch extends HTMLElement { |
| 60 | + constructor() { |
| 61 | + super(); |
| 62 | + const openBtn = this.querySelector<HTMLButtonElement>("button[data-open-modal]")!; |
| 63 | + const closeBtn = this.querySelector<HTMLButtonElement>("button[data-close-modal]")!; |
| 64 | + const dialog = this.querySelector("dialog")!; |
| 65 | + const dialogFrame = this.querySelector(".dialog-frame")!; |
| 66 | + |
| 67 | + const onWindowClick = (event: MouseEvent) => { |
| 68 | + // make sure the click is outside the of the dialog |
| 69 | + if ( |
| 70 | + document.body.contains(event.target as Node) && |
| 71 | + !dialogFrame.contains(event.target as Node) |
| 72 | + ) |
| 73 | + closeModal(); |
| 74 | + }; |
| 75 | + |
| 76 | + const openModal = (event?: MouseEvent) => { |
| 77 | + dialog.showModal(); |
| 78 | + this.querySelector("input")?.focus(); |
| 79 | + event?.stopPropagation(); |
| 80 | + window.addEventListener("click", onWindowClick); |
| 81 | + }; |
| 82 | + |
| 83 | + const closeModal = () => { |
| 84 | + dialog.close(); |
| 85 | + window.removeEventListener("click", onWindowClick); |
| 86 | + }; |
| 87 | + |
| 88 | + openBtn.addEventListener("click", openModal); |
| 89 | + openBtn.disabled = false; |
| 90 | + closeBtn.addEventListener("click", closeModal); |
| 91 | + |
| 92 | + // Listen for `/` keyboard shortcut |
| 93 | + window.addEventListener("keydown", (e) => { |
| 94 | + if (e.key === "/" && !dialog.open) { |
| 95 | + openModal(); |
| 96 | + e.preventDefault(); |
| 97 | + } |
| 98 | + }); |
| 99 | + |
| 100 | + window.addEventListener("DOMContentLoaded", () => { |
| 101 | + if (import.meta.env.DEV) return; |
| 102 | + const onIdle = window.requestIdleCallback || ((cb) => setTimeout(cb, 1)); |
| 103 | + onIdle(async () => { |
| 104 | + const { PagefindUI } = await import("@pagefind/default-ui"); |
| 105 | + new PagefindUI({ |
| 106 | + element: "#cactus__search", |
| 107 | + baseUrl: import.meta.env.BASE_URL, |
| 108 | + bundlePath: import.meta.env.BASE_URL.replace(/\/$/, "") + "/_pagefind/", |
| 109 | + showImages: false, |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + customElements.define("site-search", SiteSearch); |
| 117 | +</script> |
| 118 | + |
| 119 | +<style is:global> |
| 120 | + :root { |
| 121 | + --pagefind-ui-font: inherit; |
| 122 | + } |
| 123 | + |
| 124 | + #cactus__search .pagefind-ui__search-clear { |
| 125 | + width: calc(60px * var(--pagefind-ui-scale)); |
| 126 | + padding: 0; |
| 127 | + background-color: transparent; |
| 128 | + overflow: hidden; |
| 129 | + } |
| 130 | + #cactus__search .pagefind-ui__search-clear:focus { |
| 131 | + outline: 1px solid var(--theme-accent-2); |
| 132 | + } |
| 133 | + #cactus__search .pagefind-ui__search-clear::before { |
| 134 | + content: ""; |
| 135 | + -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke-width='1.5' stroke='currentColor' %3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M6 18L18 6M6 6l12 12'%3E%3C/path%3E%3C/svg%3E") |
| 136 | + center / 60% no-repeat; |
| 137 | + mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke-width='1.5' stroke='currentColor' %3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M6 18L18 6M6 6l12 12'%3E%3C/path%3E%3C/svg%3E") |
| 138 | + center / 60% no-repeat; |
| 139 | + background-color: var(--theme-accent); |
| 140 | + display: block; |
| 141 | + width: 100%; |
| 142 | + height: 100%; |
| 143 | + } |
| 144 | + |
| 145 | + #cactus__search .pagefind-ui__result { |
| 146 | + border: 0; |
| 147 | + } |
| 148 | + |
| 149 | + #cactus__search .pagefind-ui__result-link { |
| 150 | + background-size: 100% 6px; |
| 151 | + background-position: bottom; |
| 152 | + background-repeat: repeat-x; |
| 153 | + background-image: linear-gradient( |
| 154 | + transparent, |
| 155 | + transparent 5px, |
| 156 | + var(--theme-text) 5px, |
| 157 | + var(--theme-text) |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + #cactus__search .pagefind-ui__result-link:hover { |
| 162 | + text-decoration: none; |
| 163 | + background-image: linear-gradient( |
| 164 | + transparent, |
| 165 | + transparent 4px, |
| 166 | + var(--theme-link) 4px, |
| 167 | + var(--theme-link) |
| 168 | + ); |
| 169 | + } |
| 170 | + |
| 171 | + #cactus__search mark { |
| 172 | + color: var(--theme-quote); |
| 173 | + background-color: transparent; |
| 174 | + font-weight: 600; |
| 175 | + } |
| 176 | +</style> |
| 177 | + |
| 178 | +<style> |
| 179 | + #cactus__search { |
| 180 | + --pagefind-ui-primary: var(--theme-accent); |
| 181 | + --pagefind-ui-text: var(--theme-text); |
| 182 | + --pagefind-ui-background: var(--theme-bg); |
| 183 | + --pagefind-ui-border: #a1a1aa; |
| 184 | + --pagefind-ui-border-width: 1px; |
| 185 | + } |
| 186 | +</style> |
0 commit comments