|
| 1 | +<script lang="ts"> |
| 2 | + import Dialog from './Dialog.svelte'; |
| 3 | + import SearchIcon from 'lucide-svelte/icons/search'; |
| 4 | + import LoaderIcon from 'lucide-svelte/icons/loader'; |
| 5 | + import { page } from '$app/stores'; |
| 6 | +
|
| 7 | + let open = $state(false); |
| 8 | + let query = $state(''); |
| 9 | +
|
| 10 | + const searchPromise = $derived.by(async () => { |
| 11 | + if (query === '') { |
| 12 | + return []; |
| 13 | + } |
| 14 | +
|
| 15 | + // FIXME: https://github.com/sveltejs/eslint-plugin-svelte/issues/652 |
| 16 | + // eslint-disable-next-line svelte/valid-compile |
| 17 | + const result = await $page.data.pagefind.debouncedSearch(query, {}, 250); |
| 18 | +
|
| 19 | + if (result === null) { |
| 20 | + return []; |
| 21 | + } |
| 22 | +
|
| 23 | + return await Promise.all(result.results.map((result) => result.data())); |
| 24 | + }); |
| 25 | +
|
| 26 | + $effect(() => { |
| 27 | + function onKeydown(event: KeyboardEvent) { |
| 28 | + if (event.key === 'k' && (event.ctrlKey || event.metaKey)) { |
| 29 | + event.preventDefault(); |
| 30 | + open = true; |
| 31 | + } |
| 32 | + } |
| 33 | +
|
| 34 | + document.addEventListener('keydown', onKeydown); |
| 35 | +
|
| 36 | + return () => { |
| 37 | + document.removeEventListener('keydown', onKeydown); |
| 38 | + }; |
| 39 | + }); |
| 40 | +</script> |
| 41 | + |
| 42 | +<button |
| 43 | + class="flex gap-4 items-center border border-surface-600 px-4 py-1 rounded-md" |
| 44 | + onclick={() => (open = true)} |
| 45 | +> |
| 46 | + <p class="hidden md:block text-lg">Search</p> |
| 47 | + <SearchIcon size={20} /> |
| 48 | +</button> |
| 49 | + |
| 50 | +<Dialog bind:open> |
| 51 | + <div class="divide-y-4 divide-surface-600"> |
| 52 | + <div class="relative p-4"> |
| 53 | + <input |
| 54 | + class="bg-transparent text-2xl outline-none pl-10 w-full" |
| 55 | + placeholder="Search the docs..." |
| 56 | + bind:value={query} |
| 57 | + /> |
| 58 | + <SearchIcon class="absolute left-4 top-1/2 -translate-y-1/2" /> |
| 59 | + </div> |
| 60 | + <div class="flex flex-col gap-1 p-4" tabindex="-1"> |
| 61 | + {#if query === ''} |
| 62 | + <p class="text-lg text-center py-16">What can we help you find?</p> |
| 63 | + {:else} |
| 64 | + {#await searchPromise} |
| 65 | + <p class="text-lg text-center py-16"> |
| 66 | + <LoaderIcon class="inline animate-spin" size={16} /> |
| 67 | + </p> |
| 68 | + {:then results} |
| 69 | + {#if results.length > 0} |
| 70 | + <nav> |
| 71 | + <ul class="space-y-4"> |
| 72 | + {#each results as result} |
| 73 | + <li> |
| 74 | + <a |
| 75 | + href={result.url.replace('.html', '')} |
| 76 | + class="block text-xl font-semibold hover:bg-surface-500 focus:bg-surface-500 transition p-1 rounded-md" |
| 77 | + > |
| 78 | + {result.meta.title} |
| 79 | + |
| 80 | + <!-- eslint-disable-next-line svelte/no-at-html-tags--> |
| 81 | + <p class="text-sm line-clamp-2">{@html result.excerpt}</p> |
| 82 | + </a> |
| 83 | + </li> |
| 84 | + {/each} |
| 85 | + </ul> |
| 86 | + </nav> |
| 87 | + {:else if query !== ''} |
| 88 | + <p class="text-lg text-center py-16">No results found for "{query}"</p> |
| 89 | + {/if} |
| 90 | + {/await} |
| 91 | + {/if} |
| 92 | + </div> |
| 93 | + </div> |
| 94 | +</Dialog> |
| 95 | + |
| 96 | +<style lang="postcss"> |
| 97 | + :global(mark) { |
| 98 | + @apply bg-rose-400 rounded-md px-0.5; |
| 99 | + } |
| 100 | +</style> |
0 commit comments