|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { HTMLAttributes } from "vue"; |
| 3 | +import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue"; |
| 4 | +import { Search } from "@lucide/vue"; |
| 5 | +import { cn } from "@/lib/utils"; |
| 6 | +
|
| 7 | +export interface DocsSearchItem { |
| 8 | + title: string; |
| 9 | + href: string; |
| 10 | + description?: string; |
| 11 | + section?: string; |
| 12 | + content?: string; |
| 13 | +} |
| 14 | +
|
| 15 | +export interface DocsSearchModalProps { |
| 16 | + /** Searchable items supplied by the consuming app */ |
| 17 | + items?: DocsSearchItem[]; |
| 18 | + /** Search input placeholder */ |
| 19 | + placeholder?: string; |
| 20 | + /** Label used for the dialog and search input */ |
| 21 | + searchLabel?: string; |
| 22 | + /** Text shown when no query is entered and no items are available */ |
| 23 | + emptyText?: string; |
| 24 | + /** Text shown when a query has no matches */ |
| 25 | + noResultsText?: string; |
| 26 | + /** Enable Cmd/Ctrl+K keyboard shortcut */ |
| 27 | + shortcut?: boolean; |
| 28 | + /** Additional CSS classes for the modal panel */ |
| 29 | + class?: HTMLAttributes["class"]; |
| 30 | +} |
| 31 | +
|
| 32 | +const props = withDefaults(defineProps<DocsSearchModalProps>(), { |
| 33 | + items: () => [], |
| 34 | + placeholder: "Search documentation...", |
| 35 | + searchLabel: "Search documentation", |
| 36 | + emptyText: "No pages available.", |
| 37 | + noResultsText: "No results found.", |
| 38 | + shortcut: true, |
| 39 | +}); |
| 40 | +
|
| 41 | +const open = defineModel<boolean>("open", { default: false }); |
| 42 | +const query = ref(""); |
| 43 | +const inputRef = ref<HTMLInputElement>(); |
| 44 | +const panelRef = ref<HTMLElement>(); |
| 45 | +
|
| 46 | +const trimmedQuery = computed(() => query.value.trim().toLowerCase()); |
| 47 | +
|
| 48 | +const results = computed(() => { |
| 49 | + if (!trimmedQuery.value) return props.items; |
| 50 | +
|
| 51 | + return props.items.filter((item) => { |
| 52 | + const haystack = [ |
| 53 | + item.title, |
| 54 | + item.description, |
| 55 | + item.section, |
| 56 | + item.content, |
| 57 | + ].filter(Boolean).join(" ").toLowerCase(); |
| 58 | +
|
| 59 | + return haystack.includes(trimmedQuery.value); |
| 60 | + }); |
| 61 | +}); |
| 62 | +
|
| 63 | +function closeSearch() { |
| 64 | + open.value = false; |
| 65 | + query.value = ""; |
| 66 | +} |
| 67 | +
|
| 68 | +function onKeydown(event: KeyboardEvent) { |
| 69 | + if (props.shortcut && (event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") { |
| 70 | + event.preventDefault(); |
| 71 | + open.value = true; |
| 72 | + } |
| 73 | +
|
| 74 | + if (event.key === "Escape" && open.value) { |
| 75 | + closeSearch(); |
| 76 | + } |
| 77 | +} |
| 78 | +
|
| 79 | +function onDialogKeydown(event: KeyboardEvent) { |
| 80 | + if (event.key !== "Tab") return; |
| 81 | +
|
| 82 | + const focusable = panelRef.value?.querySelectorAll<HTMLElement>( |
| 83 | + "a[href], button:not([disabled]), input:not([disabled]), textarea:not([disabled]), select:not([disabled]), [tabindex]:not([tabindex='-1'])", |
| 84 | + ); |
| 85 | + if (!focusable?.length) return; |
| 86 | +
|
| 87 | + const first = focusable[0]; |
| 88 | + const last = focusable[focusable.length - 1]; |
| 89 | + if (!first || !last) return; |
| 90 | +
|
| 91 | + if (event.shiftKey && document.activeElement === first) { |
| 92 | + event.preventDefault(); |
| 93 | + last.focus(); |
| 94 | + } |
| 95 | + else if (!event.shiftKey && document.activeElement === last) { |
| 96 | + event.preventDefault(); |
| 97 | + first.focus(); |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +onMounted(() => { |
| 102 | + window.addEventListener("keydown", onKeydown); |
| 103 | +}); |
| 104 | +
|
| 105 | +onBeforeUnmount(() => { |
| 106 | + window.removeEventListener("keydown", onKeydown); |
| 107 | +}); |
| 108 | +
|
| 109 | +watch(open, async (value) => { |
| 110 | + if (value) { |
| 111 | + await nextTick(); |
| 112 | + inputRef.value?.focus(); |
| 113 | + } |
| 114 | + else { |
| 115 | + query.value = ""; |
| 116 | + } |
| 117 | +}); |
| 118 | +</script> |
| 119 | + |
| 120 | +<template> |
| 121 | + <Teleport to="body"> |
| 122 | + <div |
| 123 | + v-if="open" |
| 124 | + class="fixed inset-0 z-50" |
| 125 | + role="dialog" |
| 126 | + aria-modal="true" |
| 127 | + :aria-label="searchLabel" |
| 128 | + data-slot="docs-search-modal" |
| 129 | + @keydown="onDialogKeydown" |
| 130 | + > |
| 131 | + <button |
| 132 | + class="absolute inset-0 bg-background/80 backdrop-blur-sm" |
| 133 | + type="button" |
| 134 | + aria-label="Close search" |
| 135 | + @click="closeSearch" |
| 136 | + /> |
| 137 | + |
| 138 | + <div |
| 139 | + ref="panelRef" |
| 140 | + :class="cn('relative mx-auto mt-20 w-[calc(100%-2rem)] max-w-2xl overflow-hidden rounded-xl border bg-popover text-popover-foreground shadow-xl', props.class)" |
| 141 | + > |
| 142 | + <div class="flex items-center gap-3 border-b px-4 py-3"> |
| 143 | + <Search |
| 144 | + class="size-4.5 text-muted-foreground" |
| 145 | + aria-hidden="true" |
| 146 | + /> |
| 147 | + <input |
| 148 | + ref="inputRef" |
| 149 | + v-model="query" |
| 150 | + type="search" |
| 151 | + :aria-label="searchLabel" |
| 152 | + :placeholder="placeholder" |
| 153 | + class="h-10 flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground" |
| 154 | + > |
| 155 | + <kbd class="hidden h-6 select-none items-center rounded border bg-muted px-2 font-mono text-xs text-muted-foreground sm:inline-flex">Esc</kbd> |
| 156 | + </div> |
| 157 | + |
| 158 | + <div class="max-h-[min(28rem,calc(100vh-10rem))] overflow-y-auto p-2"> |
| 159 | + <a |
| 160 | + v-for="item in results" |
| 161 | + :key="item.href" |
| 162 | + :href="item.href" |
| 163 | + class="block rounded-lg p-3 hover:bg-accent hover:text-accent-foreground" |
| 164 | + @click="closeSearch" |
| 165 | + > |
| 166 | + <slot |
| 167 | + name="item" |
| 168 | + :item="item" |
| 169 | + :query="query" |
| 170 | + > |
| 171 | + <div class="flex items-center justify-between gap-3"> |
| 172 | + <p class="font-medium"> |
| 173 | + {{ item.title }} |
| 174 | + </p> |
| 175 | + <span |
| 176 | + v-if="item.section" |
| 177 | + class="text-xs text-muted-foreground" |
| 178 | + >{{ item.section }}</span> |
| 179 | + </div> |
| 180 | + <p |
| 181 | + v-if="item.description" |
| 182 | + class="mt-1 line-clamp-2 text-sm text-muted-foreground" |
| 183 | + > |
| 184 | + {{ item.description }} |
| 185 | + </p> |
| 186 | + </slot> |
| 187 | + </a> |
| 188 | + |
| 189 | + <p |
| 190 | + v-if="!results.length" |
| 191 | + class="px-3 py-8 text-center text-sm text-muted-foreground" |
| 192 | + > |
| 193 | + {{ trimmedQuery ? noResultsText : emptyText }} |
| 194 | + </p> |
| 195 | + </div> |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + </Teleport> |
| 199 | +</template> |
0 commit comments