docs/.vitepress/theme/components/Search.vue
1211 lines · 30090 bytes
1<script lang="ts" setup>
2import MiniSearch, { type SearchResult } from 'minisearch'
3import { dataSymbol, inBrowser, useRouter } from 'vitepress'
4import {
5 computed,
6 createApp,
7 markRaw,
8 nextTick,
9 onBeforeUnmount,
10 onMounted,
11 type Ref,
12 ref,
13 shallowRef,
14 watch,
15 watchEffect
16} from 'vue'
17// @ts-ignore
18import { pathToFile } from 'vitepress/dist/client/app/utils'
19// @ts-ignore
20import { escapeRegExp } from 'vitepress/dist/client/shared'
21
22import {
23 computedAsync,
24 onKeyStroke,
25 useEventListener,
26 useLocalStorage,
27 useScrollLock,
28 useSessionStorage,
29 watchDebounced
30} from '@vueuse/core'
31import { useFocusTrap } from '@vueuse/integrations/useFocusTrap'
32import { AnimatePresence, motion } from 'motion-v'
33
34// @ts-ignore
35import localSearchIndex from '@localSearchIndex'
36
37import {
38 ArrowRight,
39 Delete,
40 File,
41 Hash,
42 Regex,
43 TextAlignStart
44} from 'lucide-vue-next'
45import Mark from 'mark.js/dist/mark.es6.js'
46import { enhanceAppWithTabs } from 'vitepress-plugin-tabs/client'
47import { LRUCache } from '../composables/search/lru-cache'
48import { createSearchTranslate } from '../composables/search/translation'
49import { useData } from '../composables/search/use-data'
50
51export interface FooterTranslations {
52 selectText?: string
53 selectKeyAriaLabel?: string
54 navigateText?: string
55 navigateUpKeyAriaLabel?: string
56 navigateDownKeyAriaLabel?: string
57 closeText?: string
58 closeKeyAriaLabel?: string
59}
60
61export interface ModalTranslations {
62 displayDetails?: string
63 resetButtonTitle?: string
64 backButtonTitle?: string
65 noResultsText?: string
66 footer?: FooterTranslations
67}
68
69const showSearch = defineModel<boolean>()
70
71const el = shallowRef<HTMLElement>()
72const resultsEl = shallowRef<HTMLElement>()
73
74/* Search */
75const searchIndexData = shallowRef(localSearchIndex)
76
77// @ts-ignore
78if (import.meta.hot) {
79 // @ts-ignore
80 import.meta.hot.accept('@localSearchIndex', (m) => {
81 if (m) {
82 searchIndexData.value = m.default
83 }
84 })
85}
86
87interface Result {
88 title: string
89 titles: string[]
90 text?: string
91}
92
93const vitePressData = useData()
94const { activate } = useFocusTrap(el, {
95 immediate: true,
96 allowOutsideClick: true,
97 clickOutsideDeactivates: true,
98 escapeDeactivates: true,
99 returnFocusOnDeactivate: false,
100 initialFocus: () => {
101 return searchInput.value
102 }
103})
104const { localeIndex, theme } = vitePressData
105const searchIndex = computedAsync(async () =>
106 markRaw(
107 MiniSearch.loadJSON<Result>(
108 (await searchIndexData.value[localeIndex.value]?.())?.default,
109 {
110 fields: ['title', 'titles', 'text'],
111 storeFields: ['title', 'titles'],
112 searchOptions: {
113 // Conditional fuzzy search based on matchExact value
114 fuzzy: matchExact.value ? false : 0.07,
115 // Conditional prefix search based on matchExact value
116 prefix: matchExact.value ? false : (term) => term.length > 3,
117 // Disable max fuzzy if match exact is enabled
118 maxFuzzy: matchExact.value ? 0 : 1,
119 boost: { title: 4, text: 2, titles: 1 },
120 ...(theme.value.search?.provider === 'local' &&
121 theme.value.search.options?.miniSearch?.searchOptions)
122 },
123 ...(theme.value.search?.provider === 'local' &&
124 theme.value.search.options?.miniSearch?.options)
125 }
126 )
127 )
128)
129
130const disableQueryPersistence = computed(() => {
131 return (
132 theme.value.search?.provider === 'local' &&
133 theme.value.search.options?.disableQueryPersistence === true
134 )
135})
136
137const filterText = disableQueryPersistence.value
138 ? ref('')
139 : useSessionStorage('vitepress:local-search-filter', '')
140
141const showDetailedList = useLocalStorage(
142 'vitepress:local-search-detailed-list',
143 theme.value.search?.provider === 'local' &&
144 theme.value.search.options?.detailedView === true
145)
146
147const matchExact = useLocalStorage(
148 'vitepress:local-search-match-exact',
149 false // disabled by default
150)
151
152const disableDetailedView = computed(() => {
153 return (
154 theme.value.search?.provider === 'local' &&
155 theme.value.search.options?.detailedView === false
156 )
157})
158
159const buttonText = computed(() => {
160 const options = theme.value.search?.options
161
162 return (
163 options?.locales?.[localeIndex.value]?.translations?.button?.buttonText ||
164 options?.translations?.button?.buttonText ||
165 'Search'
166 )
167})
168
169watchEffect(() => {
170 if (disableDetailedView.value) {
171 showDetailedList.value = false
172 }
173})
174
175const results: Ref<(SearchResult & Result)[]> = shallowRef([])
176
177const enableNoResults = ref(false)
178
179watch(filterText, () => {
180 enableNoResults.value = false
181})
182
183const mark = computedAsync(async () => {
184 if (!resultsEl.value) return
185 return markRaw(new Mark(resultsEl.value))
186}, null)
187
188const cache = new LRUCache<string, Map<string, string>>(16) // 16 files
189
190watchDebounced(
191 () => [searchIndex.value, filterText.value, showDetailedList.value] as const,
192 async ([index, filterTextValue, showDetailedListValue], old, onCleanup) => {
193 if (old?.[0] !== index) {
194 // in case of hmr
195 cache.clear()
196 }
197
198 let canceled = false
199 onCleanup(() => {
200 canceled = true
201 })
202
203 if (!index) return
204
205 // Search
206 const _result = index
207 .search(filterTextValue)
208 .slice(0, 16) as (SearchResult & Result)[]
209 enableNoResults.value = true
210
211 if (!_result) {
212 results.value = []
213 return
214 }
215
216 // Highlighting
217 const mods = showDetailedListValue
218 ? await Promise.all(_result.map((r) => fetchExcerpt(r.id)))
219 : []
220 if (canceled) return
221 for (const { id, mod } of mods) {
222 const mapId = id.slice(0, id.indexOf('#'))
223 const cacheId = `${mapId}\n${filterTextValue}`
224 let map = cache.get(cacheId)
225 if (map) continue
226 map = new Map()
227 cache.set(cacheId, map)
228 const comp = mod.default ?? mod
229 if (comp?.render || comp?.setup) {
230 const app = createApp(comp)
231 // Silence warnings about missing components
232 app.config.warnHandler = () => {}
233 enhanceAppWithTabs(app)
234 app.provide(dataSymbol, vitePressData)
235 Object.defineProperties(app.config.globalProperties, {
236 $frontmatter: {
237 get() {
238 return vitePressData.frontmatter.value
239 }
240 },
241 $params: {
242 get() {
243 return vitePressData.page.value.params
244 }
245 }
246 })
247 const div = document.createElement('div')
248 app.mount(div)
249 await selectExcerptTabs(div, filterTextValue)
250 const headings = div.querySelectorAll('h1, h2, h3, h4, h5, h6')
251 headings.forEach((el) => {
252 const href = el.querySelector('a')?.getAttribute('href')
253 const anchor = href?.startsWith('#') && href.slice(1)
254 if (!anchor) return
255 let html = ''
256 while (
257 (el = el.nextElementSibling!) &&
258 !/^h[1-6]$/i.test(el.tagName)
259 ) {
260 html += el.outerHTML
261 }
262 map!.set(anchor, html)
263 })
264 app.unmount()
265 }
266 if (canceled) return
267 }
268
269 const terms = new Set<string>()
270
271 results.value = _result.map((r) => {
272 const [id, anchor] = r.id.split('#')
273 const map = cache.get(`${id}\n${filterTextValue}`)
274 const text = map?.get(anchor) ?? ''
275 for (const term in r.match) {
276 terms.add(term)
277 }
278 return { ...r, text }
279 })
280
281 await nextTick()
282 if (canceled) return
283
284 await new Promise((r) => {
285 mark.value?.unmark({
286 done: () => {
287 mark.value?.markRegExp(formMarkRegex(terms), { done: r })
288 }
289 })
290 })
291
292 const excerpts = el.value?.querySelectorAll('.result .excerpt') ?? []
293 const scrollMutations: { element: HTMLElement; scrollTop: number }[] = []
294 for (let i = 0; i < excerpts.length; i++) {
295 const excerptElement = excerpts[i] as HTMLElement
296
297 const markNode = excerptElement.querySelector(
298 'mark[data-markjs="true"]'
299 ) as HTMLElement | null
300 if (markNode) {
301 const markRect = markNode.getBoundingClientRect()
302 const excerptRect = excerptElement.getBoundingClientRect()
303
304 const relativeTop = (markRect.top - excerptRect.top) +
305 excerptElement.scrollTop
306 const targetScrollTop = relativeTop -
307 (excerptElement.clientHeight / 2) + (markRect.height / 2)
308
309 scrollMutations.push({
310 element: excerptElement,
311 scrollTop: targetScrollTop
312 })
313 }
314 }
315 for (let i = 0; i < scrollMutations.length; i++) {
316 scrollMutations[i].element.scrollTop = scrollMutations[i].scrollTop
317 }
318 if (resultsEl.value) {
319 resultsEl.value.scrollTop = 0
320 }
321 },
322 { debounce: 180, immediate: true }
323)
324
325const excerptCache = new LRUCache<string, unknown>(60) // 60 excerpts
326
327async function fetchExcerpt(id: string) {
328 const cache = excerptCache.get(id)
329 if (cache) return { id, mod: cache }
330
331 const file = pathToFile(id.slice(0, id.indexOf('#')))
332 try {
333 if (!file) throw new Error(`Cannot find file for id: ${id}`)
334 const mod = await import(/*@vite-ignore*/ file)
335
336 excerptCache.set(id, mod)
337
338 return { id, mod }
339 } catch (e) {
340 console.error(e)
341 return { id, mod: {} }
342 }
343}
344
345const nextFrame = () =>
346 new Promise<void>((resolve) => requestAnimationFrame(() => resolve()))
347
348const getSearchTerms = (query: string) =>
349 query
350 .toLowerCase()
351 .split(/\s+/)
352 .map((term) => term.trim())
353 .filter(Boolean)
354
355async function selectExcerptTabs(root: HTMLElement, query: string) {
356 const terms = getSearchTerms(query)
357 if (!terms.length) return
358
359 const tabs = root.querySelectorAll<HTMLElement>('.plugin-tabs')
360
361 for (let i = 0; i < tabs.length; i++) {
362 const tab = tabs[i]
363 const selectedTab = tab.querySelector<HTMLButtonElement>(
364 '.plugin-tabs--tab[aria-selected="true"]'
365 )
366 const buttons = tab.querySelectorAll<HTMLButtonElement>(
367 '.plugin-tabs--tab[role="tab"]'
368 )
369
370 let found = false
371
372 for (let j = 0; j < buttons.length; j++) {
373 const button = buttons[j]
374 button.click()
375 await nextTick()
376 await nextFrame()
377
378 const panelId = button.getAttribute('aria-controls')
379 const panel = panelId
380 ? root.querySelector<HTMLElement>(`#${CSS.escape(panelId)}`)
381 : tab.querySelector<HTMLElement>('.plugin-tabs--content')
382 const text = panel?.textContent?.toLowerCase() ?? ''
383
384 if (terms.every((term) => text.includes(term))) {
385 found = true
386 break
387 }
388 }
389
390 if (!found) {
391 selectedTab?.click()
392 await nextTick()
393 }
394 }
395}
396
397/* Search input focus */
398
399const searchInput = ref<HTMLInputElement>()
400const disableReset = computed(() => {
401 return filterText.value?.length <= 0
402})
403function focusSearchInput(select = true) {
404 searchInput.value?.focus()
405 select && searchInput.value?.select()
406}
407
408onMounted(() => {
409 focusSearchInput()
410})
411
412function onSearchBarClick(event: PointerEvent) {
413 if (event.pointerType === 'mouse') {
414 focusSearchInput()
415 }
416}
417
418/* Search keyboard selection */
419
420const selectedIndex = ref(-1)
421const disableMouseOver = ref(true)
422
423watch(results, (r) => {
424 selectedIndex.value = r.length ? 0 : -1
425 scrollToSelectedResult()
426})
427
428function scrollToSelectedResult() {
429 nextTick(() => {
430 const selectedEl = document.querySelector('.result.selected')
431 selectedEl?.scrollIntoView({ block: 'nearest' })
432 })
433}
434
435onKeyStroke('ArrowUp', (event) => {
436 event.preventDefault()
437 selectedIndex.value--
438 if (selectedIndex.value < 0) {
439 selectedIndex.value = results.value.length - 1
440 }
441 disableMouseOver.value = true
442 scrollToSelectedResult()
443})
444
445onKeyStroke('ArrowDown', (event) => {
446 event.preventDefault()
447 selectedIndex.value++
448 if (selectedIndex.value >= results.value.length + 1) {
449 selectedIndex.value = 0
450 }
451 disableMouseOver.value = true
452 scrollToSelectedResult()
453})
454
455const router = useRouter()
456
457onKeyStroke('Enter', (e) => {
458 if (e.isComposing) return
459
460 if (e.target instanceof HTMLButtonElement && e.target.type !== 'submit') {
461 return
462 }
463
464 const index = selectedIndex.value - 1
465
466 if (index === -1) {
467 // @ts-ignore
468 window.toggleAI({ value: filterText.value })
469 showSearch.value = false
470 return
471 }
472
473 const selectedPackage = results.value[index]
474 if (e.target instanceof HTMLInputElement && !selectedPackage) {
475 e.preventDefault()
476 return
477 }
478
479 if (selectedPackage) {
480 window.dispatchEvent(
481 new CustomEvent('search-nav', {
482 detail: { query: filterText.value }
483 })
484 )
485 router.go(selectedPackage.id)
486 showSearch.value = false
487 }
488})
489
490function onResultClick() {
491 window.dispatchEvent(
492 new CustomEvent('search-nav', {
493 detail: { query: filterText.value }
494 })
495 )
496 showSearch.value = false
497}
498
499onKeyStroke('Escape', () => {
500 showSearch.value = false
501})
502
503// Translations
504const defaultTranslations = {
505 modal: {
506 displayDetails: 'Display detailed list',
507 exactMatchTitle: 'Match exact phrases',
508 resetButtonTitle: 'Reset search',
509 backButtonTitle: 'Close search',
510 noResultsText: 'No results for',
511 footer: {
512 selectText: 'to select',
513 selectKeyAriaLabel: 'enter',
514 navigateText: 'to navigate',
515 navigateUpKeyAriaLabel: 'up arrow',
516 navigateDownKeyAriaLabel: 'down arrow',
517 closeText: 'to close',
518 closeKeyAriaLabel: 'escape'
519 }
520 }
521}
522
523const translate = createSearchTranslate(defaultTranslations)
524
525// Back
526
527onMounted(() => {
528 // Prevents going to previous site
529 window.history.pushState(null, '', null)
530})
531
532useEventListener('popstate', (event) => {
533 event.preventDefault()
534 showSearch.value = false
535})
536
537/** Lock body */
538const isLocked = useScrollLock(inBrowser ? document.body : null)
539
540watch(
541 () => showSearch.value,
542 (val) => {
543 if (!val) {
544 isLocked.value = false
545 return
546 }
547
548 nextTick(() => {
549 isLocked.value = true
550 nextTick().then(() => activate())
551 })
552 }
553)
554
555function formMarkRegex(terms: Set<string>) {
556 return new RegExp(
557 [...terms]
558 .sort((a, b) => b.length - a.length)
559 .map((term) => `(${escapeRegExp(term)})`)
560 .join('|'),
561 'gi'
562 )
563}
564
565function resetSearch() {
566 filterText.value = ''
567 nextTick().then(() => focusSearchInput(false))
568}
569
570function onMouseMove(e: MouseEvent) {
571 if (!disableMouseOver.value) return
572 const el = (e.target as HTMLElement)?.closest<HTMLAnchorElement>('.result')
573 const index = Number.parseInt(el?.dataset.index!)
574 if (index >= 0 && index !== selectedIndex.value) {
575 selectedIndex.value = index
576 }
577 disableMouseOver.value = false
578}
579</script>
580
581<template>
582 <Teleport to="body">
583 <div
584 ref="el"
585 role="button"
586 :aria-owns="results?.length ? 'localsearch-list' : undefined"
587 aria-expanded="true"
588 aria-haspopup="listbox"
589 aria-labelledby="localsearch-label"
590 class="VPLocalSearchBox"
591 :class="{ 'pointer-events-none': !showSearch }"
592 >
593 <AnimatePresence>
594 <motion.div
595 v-if="showSearch"
596 class="backdrop"
597 @click="showSearch = false"
598 :initial="{
599 opacity: 0
600 }"
601 :animate="{
602 opacity: 1
603 }"
604 :exit="{
605 opacity: 0
606 }"
607 :transition="{
608 duration: 0.55,
609 ease: [0.16, 1, 0.3, 1]
610 }"
611 />
612 </AnimatePresence>
613
614 <AnimatePresence>
615 <motion.div
616 v-if="showSearch"
617 layout="size"
618 class="shell"
619 :initial="{
620 opacity: 0,
621 scale: 0.9375
622 }"
623 :animate="{
624 opacity: 1,
625 scale: 1
626 }"
627 :exit="{
628 opacity: 0,
629 scale: 0.9375
630 }"
631 :transition="{
632 duration: 0.55,
633 ease: [0.16, 1, 0.3, 1]
634 }"
635 >
636 <motion.form
637 layout="position"
638 class="search-bar"
639 @pointerup="onSearchBarClick($event)"
640 @submit.prevent=""
641 >
642 <label
643 :title="buttonText"
644 id="localsearch-label"
645 for="localsearch-input"
646 >
647 <span
648 aria-hidden="true"
649 class="vpi-search search-icon local-search-icon"
650 />
651 </label>
652 <div class="search-actions before">
653 <button
654 class="back-button"
655 :title="translate('modal.backButtonTitle')"
656 @click="showSearch = false"
657 >
658 <span class="vpi-arrow-left local-search-icon" />
659 </button>
660 </div>
661 <input
662 ref="searchInput"
663 v-model="filterText"
664 :aria-activedescendant="selectedIndex > -1
665 ? 'localsearch-item-' + selectedIndex
666 : undefined"
667 aria-autocomplete="both"
668 :aria-controls="results?.length ? 'localsearch-list' : undefined"
669 aria-labelledby="localsearch-label"
670 autocapitalize="off"
671 autocomplete="off"
672 autocorrect="off"
673 class="search-input"
674 id="localsearch-input"
675 enterkeyhint="go"
676 maxlength="64"
677 :placeholder="buttonText"
678 spellcheck="false"
679 type="search"
680 />
681 <div class="search-actions">
682 <button
683 v-if="!disableDetailedView"
684 class="toggle-layout-button"
685 type="button"
686 :class="{ 'detailed-list': showDetailedList }"
687 :title="translate('modal.displayDetails')"
688 @click="selectedIndex > -1 &&
689 (showDetailedList = !showDetailedList)"
690 >
691 <TextAlignStart :size="19" stroke-width="1.25" />
692 </button>
693 <button
694 class="exact-match-button"
695 type="button"
696 :class="{ 'exact-match-active': matchExact }"
697 :title="translate('modal.exactMatchTitle')"
698 @click="matchExact = !matchExact"
699 >
700 <Regex :size="19" stroke-width="1.25" />
701 </button>
702 <button
703 class="clear-button"
704 type="reset"
705 :disabled="disableReset"
706 :title="translate('modal.resetButtonTitle')"
707 @click="resetSearch"
708 >
709 <Delete :size="19" stroke-width="1.25" />
710 </button>
711 </div>
712 </motion.form>
713
714 <ul
715 ref="resultsEl"
716 :id="results?.length ? 'localsearch-list' : undefined"
717 :role="results?.length ? 'listbox' : undefined"
718 :aria-labelledby="results?.length ? 'localsearch-label' : undefined"
719 class="results"
720 @mousemove="onMouseMove"
721 >
722 <AnimatePresence>
723 <motion.div
724 class="flex flex-col justify-center items-center h-47.5 gap-2 font-medium text-sm text-gray-500 dark:text-gray-300 m-auto md:mt-10 md:mb-6 opacity-90"
725 v-if="!filterText || (filterText && !results.length)"
726 :initial="{
727 opacity: 0
728 }"
729 :animate="{
730 opacity: 1,
731 transition: {
732 delay: 0.1
733 }
734 }"
735 :exit="{
736 opacity: 0,
737 height: 0,
738 transition: {
739 duration: 0.2
740 }
741 }"
742 :transition="{
743 duration: 0.7,
744 ease: [0.16, 1, 0.3, 1]
745 }"
746 >
747 <img
748 class="h-40 object-contain object-center -translate-x-2"
749 src="/asset/smolame.png"
750 alt="Smol ame"
751 />
752 <h1 v-if="filterText && !results.length">
753 Couldn't find anything, try again?
754 </h1>
755 <h1 v-else>Looking for something?</h1>
756 </motion.div>
757 </AnimatePresence>
758
759 <AnimatePresence>
760 <motion.li
761 v-for="(p, index) in results"
762 :key="p.id"
763 layout
764 class="result-layout"
765 :id="'localsearch-item-' + (index + 1)"
766 :aria-selected="selectedIndex === index + 1 ? 'true' : 'false'"
767 role="option"
768 :initial="{
769 opacity: 0,
770 scale: 0.9375
771 }"
772 :animate="{
773 opacity: 1,
774 scale: 1
775 }"
776 :exit="{
777 opacity: 0,
778 scale: 0.9375,
779 height: 0,
780 transition: {
781 duration: 0.25
782 }
783 }"
784 :transition="{
785 duration: 0.55,
786 ease: [0.16, 1, 0.3, 1],
787 delay: index * 0.02
788 }"
789 >
790 <a
791 :href="p.id"
792 class="result"
793 :class="{
794 selected: selectedIndex === index + 1
795 }"
796 :aria-label="[...p.titles, p.title].join(' > ')"
797 @mouseenter="!disableMouseOver &&
798 (selectedIndex = index + 1)"
799 @focusin="selectedIndex = index + 1"
800 @click="onResultClick"
801 :data-index="index + 1"
802 >
803 <div>
804 <div class="titles">
805 <Hash
806 v-if="p.titles.length > 0"
807 stroke-width="1.25"
808 :size="18"
809 />
810 <File v-else stroke-width="1.25" :size="18" />
811
812 <span
813 v-for="(t, index) in p.titles"
814 :key="index"
815 class="title"
816 >
817 <span class="text" v-html="t" />
818 <ArrowRight
819 stroke-width="1.25"
820 :size="18"
821 class="mx-0.5"
822 />
823 </span>
824 <span class="title main">
825 <span class="text" v-html="p.title" />
826 </span>
827 </div>
828
829 <div v-if="showDetailedList" class="excerpt-wrapper">
830 <motion.div
831 v-if="p.text"
832 layout
833 class="excerpt"
834 inert
835 :initial="{
836 height: 0
837 }"
838 :animate="{
839 height: '84px'
840 }"
841 :exit="{
842 height: 0
843 }"
844 :transition="{
845 duration: 0.4,
846 ease: [0.16, 1, 0.3, 1]
847 }"
848 >
849 <div class="vp-doc" v-html="p.text" />
850 </motion.div>
851 <div class="excerpt-gradient-bottom" />
852 <div class="excerpt-gradient-top" />
853 </div>
854 </div>
855 </a>
856 </motion.li>
857 </AnimatePresence>
858 </ul>
859
860 <motion.div layout class="search-keyboard-shortcuts">
861 <span>
862 <kbd
863 :aria-label="translate(
864 'modal.footer.navigateUpKeyAriaLabel'
865 )"
866 >
867 <span class="vpi-arrow-up navigate-icon" />
868 </kbd>
869 <kbd
870 :aria-label="translate(
871 'modal.footer.navigateDownKeyAriaLabel'
872 )"
873 >
874 <span class="vpi-arrow-down navigate-icon" />
875 </kbd>
876 {{ translate('modal.footer.navigateText') }}
877 </span>
878 <span>
879 <kbd :aria-label="translate('modal.footer.selectKeyAriaLabel')">
880 <span class="vpi-corner-down-left navigate-icon" />
881 </kbd>
882 {{ translate('modal.footer.selectText') }}
883 </span>
884 <span>
885 <kbd :aria-label="translate('modal.footer.closeKeyAriaLabel')">
886 esc
887 </kbd>
888 {{ translate('modal.footer.closeText') }}
889 </span>
890 </motion.div>
891 </motion.div>
892 </AnimatePresence>
893
894 <button v-if="!showSearch" />
895 </div>
896 </Teleport>
897</template>
898
899<style scoped>
900.VPLocalSearchBox {
901 position: fixed;
902 z-index: 100;
903 inset: 0;
904 display: flex;
905}
906
907.backdrop {
908 position: absolute;
909 inset: 0;
910 background: var(--vp-backdrop-bg-color);
911 transition: opacity 0.5s;
912 will-change: opacity;
913}
914
915.shell {
916 position: relative;
917 padding: 12px;
918 margin: 64px auto;
919 display: flex;
920 flex-direction: column;
921 gap: 16px;
922 background: var(--vp-local-search-bg);
923 width: min(100vw - 60px, 900px);
924 height: min-content;
925 max-height: min(100vh - 128px, 900px);
926 border-radius: 6px;
927 will-change: transform, opacity, height;
928}
929
930@media (max-width: 767px) {
931 .shell {
932 margin: 0;
933 width: 100vw;
934 height: 100vh;
935 max-height: none;
936 border-radius: 0;
937 }
938}
939
940.search-bar {
941 border: 1px solid var(--vp-c-divider);
942 border-radius: 4px;
943 display: flex;
944 align-items: center;
945 padding: 0 12px;
946 cursor: text;
947 will-change: height;
948}
949
950@media (max-width: 767px) {
951 .search-bar {
952 padding: 0 8px;
953 }
954}
955
956.search-bar:focus-within {
957 border-color: var(--vp-c-brand-1);
958}
959
960.local-search-icon {
961 display: block;
962 font-size: 18px;
963}
964
965.navigate-icon {
966 display: block;
967 font-size: 14px;
968}
969
970.search-icon {
971 margin: 8px;
972}
973
974@media (max-width: 767px) {
975 .search-icon {
976 display: none;
977 }
978}
979
980.search-input {
981 padding: 6px 12px;
982 font-size: inherit;
983 width: 100%;
984}
985
986.search-input::-webkit-search-cancel-button {
987 display: none;
988}
989
990@media (max-width: 767px) {
991 .search-input {
992 padding: 6px 4px;
993 }
994}
995
996.search-actions {
997 display: flex;
998 gap: 4px;
999}
1000
1001@media (any-pointer: coarse) {
1002 .search-actions {
1003 gap: 8px;
1004 }
1005}
1006
1007@media (min-width: 769px) {
1008 .search-actions.before {
1009 display: none;
1010 }
1011}
1012
1013.search-actions button {
1014 padding: 8px;
1015}
1016
1017.search-actions button:not([disabled]):hover,
1018.toggle-layout-button.detailed-list {
1019 color: var(--vp-c-brand-1);
1020}
1021
1022.search-actions button.clear-button:disabled {
1023 opacity: 0.37;
1024}
1025
1026.search-keyboard-shortcuts {
1027 font-size: 0.8rem;
1028 opacity: 75%;
1029 display: flex;
1030 flex-wrap: wrap;
1031 gap: 16px;
1032 line-height: 14px;
1033}
1034
1035.search-keyboard-shortcuts span {
1036 display: flex;
1037 align-items: center;
1038 gap: 4px;
1039}
1040
1041@media (max-width: 767px) {
1042 .search-keyboard-shortcuts {
1043 display: none;
1044 }
1045}
1046
1047.search-keyboard-shortcuts kbd {
1048 background: rgba(128, 128, 128, 0.1);
1049 border-radius: 4px;
1050 padding: 3px 6px;
1051 min-width: 24px;
1052 display: inline-block;
1053 text-align: center;
1054 vertical-align: middle;
1055 border: 1px solid rgba(128, 128, 128, 0.15);
1056 box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.1);
1057}
1058
1059.results {
1060 display: flex;
1061 flex-direction: column;
1062 gap: 6px;
1063 overflow-x: hidden;
1064 overflow-y: auto;
1065 overscroll-behavior: contain;
1066}
1067
1068.result-layout {
1069 will-change: transform, opacity, height;
1070}
1071
1072.result {
1073 display: flex;
1074 align-items: center;
1075 gap: 8px;
1076 border-radius: 4px;
1077 transition: none;
1078 line-height: 1rem;
1079 border: solid 2px var(--vp-local-search-result-border);
1080 outline: none;
1081}
1082
1083.result > div {
1084 margin: 12px;
1085 width: 100%;
1086 overflow: hidden;
1087}
1088
1089@media (max-width: 767px) {
1090 .result > div {
1091 margin: 8px;
1092 }
1093}
1094
1095.titles {
1096 display: flex;
1097 flex-wrap: wrap;
1098 gap: 4px;
1099 position: relative;
1100 z-index: 1001;
1101 padding: 2px 0;
1102}
1103
1104.title {
1105 display: flex;
1106 align-items: center;
1107 gap: 4px;
1108}
1109
1110.title.main {
1111 font-weight: 500;
1112}
1113
1114.title-icon {
1115 opacity: 1;
1116 font-weight: 500;
1117}
1118
1119.title svg {
1120 opacity: 0.5;
1121}
1122
1123.result.selected {
1124 --vp-local-search-result-bg: var(--vp-local-search-result-selected-bg);
1125 border-color: var(--vp-local-search-result-selected-border);
1126}
1127
1128.excerpt-wrapper {
1129 position: relative;
1130}
1131
1132.excerpt {
1133 opacity: 50%;
1134 pointer-events: none;
1135 max-height: 84px;
1136 overflow: hidden;
1137 position: relative;
1138 margin-top: 4px;
1139 will-change: height;
1140}
1141
1142.result.selected .excerpt {
1143 opacity: 1;
1144}
1145
1146.excerpt :deep(*) {
1147 font-size: 0.8rem !important;
1148 line-height: 130% !important;
1149}
1150
1151.titles :deep(mark),
1152.excerpt :deep(mark) {
1153 background-color: var(--vp-local-search-highlight-bg);
1154 color: var(--vp-local-search-highlight-text);
1155 border-radius: 2px;
1156 padding: 0 2px;
1157}
1158
1159.excerpt :deep(.vp-code-group) .tabs {
1160 display: none;
1161}
1162
1163.excerpt :deep(.vp-code-group) div[class*="language-"] {
1164 border-radius: 8px !important;
1165}
1166
1167.excerpt > .vp-doc > *:first-child {
1168 margin-top: 6px !important;
1169}
1170
1171.excerpt-gradient-bottom {
1172 position: absolute;
1173 bottom: -1px;
1174 left: 0;
1175 width: 100%;
1176 height: 8px;
1177 background: linear-gradient(transparent, var(--vp-local-search-result-bg));
1178 z-index: 1000;
1179}
1180
1181.excerpt-gradient-top {
1182 position: absolute;
1183 top: -1px;
1184 left: 0;
1185 width: 100%;
1186 height: 8px;
1187 background: linear-gradient(var(--vp-local-search-result-bg), transparent);
1188 z-index: 1000;
1189}
1190
1191.result.selected .titles,
1192.result.selected .title-icon {
1193 color: var(--vp-c-brand-1) !important;
1194}
1195
1196.no-results {
1197 font-size: 0.9rem;
1198 text-align: center;
1199 padding: 12px;
1200}
1201
1202svg {
1203 flex: none;
1204}
1205
1206.exact-match-button.exact-match-active {
1207 color: var(--vp-c-brand-1);
1208 background-color: rgba(var(--vp-c-brand-1), 0.1);
1209 border-radius: 4px;
1210}
1211</style>