docs/.vitepress/theme/components/Search.vue

d0ba6f42e987ab03c113091fcc54013406e3a983
wotaku/docs/.vitepress/theme/components/Search.vue

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