docs/.vitepress/theme/components/search/SearchSettings.vue

838 lines · 22976 bytes

  1<script setup lang="ts">
  2import { onClickOutside, useElementBounding } from '@vueuse/core'
  3import {
  4  Clock,
  5  Globe,
  6  Highlighter,
  7  Layers,
  8  LayoutList,
  9  List,
 10  LocateOff,
 11  Menu,
 12  Regex,
 13  Search,
 14  TextAlignStart
 15} from 'lucide-vue-next'
 16import { computed, nextTick, ref, watch } from 'vue'
 17import {
 18  excerptPreload,
 19  ribbonStyle,
 20  saveHistoryEnabled,
 21  searchMode,
 22  searchResultHighlightMode,
 23  showDetailedList
 24} from '../../searchState'
 25import type { SearchSortMode } from '../../searchState'
 26
 27const props = defineProps<{
 28  showSettingsPopup: boolean
 29  settingsButtonRef: HTMLElement | undefined
 30}>()
 31
 32const emit = defineEmits<{
 33  'update:showSettingsPopup': [value: boolean]
 34}>()
 35
 36const settingsPopupRef = ref<HTMLDivElement>()
 37const settingsBounding = useElementBounding(() => props.settingsButtonRef)
 38const settingsPopupBounding = useElementBounding(settingsPopupRef)
 39const showFade = ref(true)
 40
 41const settingsFadeStyle = computed(() => ({
 42  top: `${settingsPopupBounding.bottom.value - 56}px`,
 43  left: `${settingsPopupBounding.left.value}px`,
 44  width: `${settingsPopupBounding.width.value}px`
 45}))
 46
 47const settingsFadeReady = computed(() => settingsPopupBounding.width.value > 0)
 48
 49function onSettingsScroll() {
 50  const el = settingsPopupRef.value
 51  if (!el) return
 52  const { scrollTop, scrollHeight, clientHeight } = el
 53  showFade.value = scrollTop + clientHeight < scrollHeight - 4
 54}
 55
 56type HelpSection =
 57  | 'search'
 58  | 'view'
 59  | 'ribbon'
 60  | 'preload'
 61  | 'highlight'
 62  | 'history'
 63const activeHelpSection = ref<HelpSection | null>(null)
 64const helpPopupEl = ref<HTMLDivElement>()
 65const helpPopupPos = ref({ top: -9999, left: -9999 })
 66
 67watch(() => props.showSettingsPopup, async (val) => {
 68  if (!val) {
 69    activeHelpSection.value = null
 70    helpPopupPos.value = { top: -9999, left: -9999 }
 71    return
 72  }
 73  await nextTick()
 74  onSettingsScroll()
 75})
 76
 77const canHoverHelp = () =>
 78  typeof window !== 'undefined' && window.matchMedia('(hover: hover)').matches
 79
 80function positionHelpPopup(btn: HTMLElement) {
 81  helpPopupPos.value = { top: -9999, left: -9999 }
 82  const rect = btn.getBoundingClientRect()
 83  const menuRect = settingsPopupRef.value?.getBoundingClientRect()
 84  nextTick(() => {
 85    const popupW = helpPopupEl.value?.offsetWidth || 260
 86    const popupH = helpPopupEl.value?.offsetHeight || 200
 87    const vw = window.innerWidth
 88    const margin = 8
 89    const isMobile = vw < 768
 90    if (isMobile) {
 91      const aboveTop = rect.top - popupH - margin
 92      const belowTop = rect.bottom + margin
 93      const top = aboveTop >= margin ? aboveTop : belowTop
 94      const left = Math.max(
 95        margin,
 96        Math.min((vw - popupW) / 2, vw - popupW - margin)
 97      )
 98      helpPopupPos.value = { top, left }
 99    } else {
100      const anchorLeft = menuRect ? menuRect.left : rect.left
101      helpPopupPos.value = {
102        left: Math.max(margin, anchorLeft - popupW - 16),
103        top: rect.top
104      }
105    }
106  })
107}
108
109function closeHelpSection() {
110  activeHelpSection.value = null
111  helpPopupPos.value = { top: -9999, left: -9999 }
112}
113
114function toggleHelpSection(section: HelpSection, e: MouseEvent) {
115  if (canHoverHelp()) {
116    activeHelpSection.value = section
117    positionHelpPopup(e.currentTarget as HTMLElement)
118    return
119  }
120  if (activeHelpSection.value === section) {
121    closeHelpSection()
122    return
123  }
124  activeHelpSection.value = section
125  positionHelpPopup(e.currentTarget as HTMLElement)
126}
127
128function onHelpEnter(section: HelpSection, e: MouseEvent) {
129  if (!canHoverHelp()) return
130  activeHelpSection.value = section
131  positionHelpPopup(e.currentTarget as HTMLElement)
132}
133
134function onHelpLeave() {
135  if (!canHoverHelp()) return
136  closeHelpSection()
137}
138
139const settingsPopupStyle = computed(() => ({
140  top: `${settingsBounding.bottom.value + 8}px`,
141  right: `calc(100vw - ${settingsBounding.right.value}px)`
142}))
143
144const settingsBtnEl = computed(() => props.settingsButtonRef)
145onClickOutside(settingsPopupRef, () => {
146  emit('update:showSettingsPopup', false)
147}, { ignore: [settingsBtnEl] })
148
149function setSearchMode(mode: string) {
150  searchMode.value = mode as SearchSortMode
151}
152</script>
153
154<template>
155  <Transition name="settings-popup">
156    <div
157      v-if="showSettingsPopup"
158      ref="settingsPopupRef"
159      class="search-settings-popup"
160      :style="settingsPopupStyle"
161      @scroll.passive="onSettingsScroll"
162      @click="closeHelpSection"
163    >
164      <!-- Search Mode -->
165      <div class="settings-section">
166        <div class="settings-section-header">
167          <div class="settings-section-label">
168            <Search :size="14" stroke-width="1.75" />
169            Search
170          </div>
171          <button
172            type="button"
173            class="settings-help-btn"
174            :class="{ active: activeHelpSection === 'search' }"
175            aria-label="Search mode help"
176            @click.stop="toggleHelpSection('search', $event)"
177            @mouseenter="onHelpEnter('search', $event)"
178            @mouseleave="onHelpLeave"
179          >
180            <span class="i-carbon:help-filled settings-help-icon" />
181          </button>
182        </div>
183        <div
184          class="settings-options"
185          :class="{ 'is-highlighted': activeHelpSection === 'search' }"
186        >
187          <button
188            type="button"
189            class="settings-option"
190            :class="{ active: searchMode === 'exact' }"
191            @click="setSearchMode('exact')"
192          >
193            <span>Exact</span>
194          </button>
195          <button
196            type="button"
197            class="settings-option"
198            :class="{ active: searchMode === 'fuzzy' }"
199            @click="setSearchMode('fuzzy')"
200          >
201            <span>Fuzzy</span>
202          </button>
203          <button
204            type="button"
205            class="settings-option"
206            :class="{ active: searchMode === 'url' }"
207            @click="setSearchMode('url')"
208          >
209            <span>URL</span>
210          </button>
211        </div>
212      </div>
213
214      <!-- View -->
215      <div
216        v-if="searchMode !== 'url'"
217        class="settings-section"
218      >
219        <div class="settings-section-header">
220          <div class="settings-section-label">
221            <LayoutList :size="14" stroke-width="1.75" />
222            View
223          </div>
224          <button
225            type="button"
226            class="settings-help-btn"
227            :class="{ active: activeHelpSection === 'view' }"
228            aria-label="Result view help"
229            @click.stop="toggleHelpSection('view', $event)"
230            @mouseenter="onHelpEnter('view', $event)"
231            @mouseleave="onHelpLeave"
232          >
233            <span class="i-carbon:help-filled settings-help-icon" />
234          </button>
235        </div>
236        <div
237          class="settings-options"
238          :class="{ 'is-highlighted': activeHelpSection === 'view' }"
239        >
240          <button
241            type="button"
242            class="settings-option"
243            :class="{ active: showDetailedList }"
244            @click="showDetailedList = true"
245          >
246            <span>Detail</span>
247          </button>
248          <button
249            type="button"
250            class="settings-option"
251            :class="{ active: !showDetailedList }"
252            @click="showDetailedList = false"
253          >
254            <span>List</span>
255          </button>
256        </div>
257      </div>
258
259      <!-- Ribbon -->
260      <div class="settings-section">
261        <div class="settings-section-header">
262          <div class="settings-section-label">
263            <Menu :size="14" stroke-width="1.75" />
264            Ribbon
265          </div>
266          <button
267            type="button"
268            class="settings-help-btn"
269            :class="{ active: activeHelpSection === 'ribbon' }"
270            aria-label="Ribbon style help"
271            @click.stop="toggleHelpSection('ribbon', $event)"
272            @mouseenter="onHelpEnter('ribbon', $event)"
273            @mouseleave="onHelpLeave"
274          >
275            <span class="i-carbon:help-filled settings-help-icon" />
276          </button>
277        </div>
278        <div
279          class="settings-options"
280          :class="{ 'is-highlighted': activeHelpSection === 'ribbon' }"
281        >
282          <button
283            type="button"
284            class="settings-option"
285            :class="{ active: ribbonStyle === 'tabs' }"
286            @click="ribbonStyle = 'tabs'"
287          >
288            <span>Tabs</span>
289          </button>
290          <button
291            type="button"
292            class="settings-option"
293            :class="{ active: ribbonStyle === 'list' }"
294            @click="ribbonStyle = 'list'"
295          >
296            <span>List</span>
297          </button>
298        </div>
299      </div>
300
301      <!-- Preload -->
302      <div
303        v-if="searchMode !== 'url' && showDetailedList"
304        class="settings-section"
305      >
306        <div class="settings-section-header">
307          <div class="settings-section-label">
308            <Layers :size="14" stroke-width="1.75" />
309            Preload
310          </div>
311          <button
312            type="button"
313            class="settings-help-btn"
314            :class="{ active: activeHelpSection === 'preload' }"
315            aria-label="Excerpt preload help"
316            @click.stop="toggleHelpSection('preload', $event)"
317            @mouseenter="onHelpEnter('preload', $event)"
318            @mouseleave="onHelpLeave"
319          >
320            <span class="i-carbon:help-filled settings-help-icon" />
321          </button>
322        </div>
323        <div
324          class="settings-options"
325          :class="{ 'is-highlighted': activeHelpSection === 'preload' }"
326        >
327          <button
328            type="button"
329            class="settings-option"
330            :class="{ active: excerptPreload === 'all' }"
331            @click="excerptPreload = 'all'"
332          >
333            <span>All</span>
334          </button>
335          <button
336            type="button"
337            class="settings-option"
338            :class="{ active: excerptPreload === 'next' }"
339            @click="excerptPreload = 'next'"
340          >
341            <span>Next</span>
342          </button>
343          <button
344            type="button"
345            class="settings-option"
346            :class="{ active: excerptPreload === 'off' }"
347            @click="excerptPreload = 'off'"
348          >
349            <span>Off</span>
350          </button>
351        </div>
352      </div>
353
354      <!-- Highlight -->
355      <div class="settings-section">
356        <div class="settings-section-header">
357          <div class="settings-section-label">
358            <Highlighter :size="14" stroke-width="1.75" />
359            Highlight
360          </div>
361          <button
362            type="button"
363            class="settings-help-btn"
364            :class="{ active: activeHelpSection === 'highlight' }"
365            aria-label="Search result highlight help"
366            @click.stop="toggleHelpSection('highlight', $event)"
367            @mouseenter="onHelpEnter('highlight', $event)"
368            @mouseleave="onHelpLeave"
369          >
370            <span class="i-carbon:help-filled settings-help-icon" />
371          </button>
372        </div>
373        <div
374          class="settings-options"
375          :class="{ 'is-highlighted': activeHelpSection === 'highlight' }"
376        >
377          <button
378            type="button"
379            class="settings-option"
380            :class="{ active: searchResultHighlightMode === 'soft' }"
381            @click="searchResultHighlightMode = 'soft'"
382          >
383            <span>Soft</span>
384          </button>
385          <button
386            type="button"
387            class="settings-option"
388            :class="{ active: searchResultHighlightMode === 'solid' }"
389            @click="searchResultHighlightMode = 'solid'"
390          >
391            <span>Solid</span>
392          </button>
393          <button
394            type="button"
395            class="settings-option"
396            :class="{ active: searchResultHighlightMode === 'off' }"
397            @click="searchResultHighlightMode = 'off'"
398          >
399            <span>Off</span>
400          </button>
401        </div>
402      </div>
403
404      <!-- History -->
405      <div class="settings-section">
406        <div class="settings-section-header">
407          <div class="settings-section-label">
408            <Clock :size="14" stroke-width="1.75" />
409            History
410          </div>
411          <button
412            type="button"
413            class="settings-help-btn"
414            :class="{ active: activeHelpSection === 'history' }"
415            aria-label="Search history help"
416            @click.stop="toggleHelpSection('history', $event)"
417            @mouseenter="onHelpEnter('history', $event)"
418            @mouseleave="onHelpLeave"
419          >
420            <span class="i-carbon:help-filled settings-help-icon" />
421          </button>
422        </div>
423        <div
424          class="settings-options"
425          :class="{ 'is-highlighted': activeHelpSection === 'history' }"
426        >
427          <button
428            type="button"
429            class="settings-option"
430            :class="{ active: saveHistoryEnabled }"
431            @click="saveHistoryEnabled = true"
432          >
433            <span>On</span>
434          </button>
435          <button
436            type="button"
437            class="settings-option"
438            :class="{ active: !saveHistoryEnabled }"
439            @click="saveHistoryEnabled = false"
440          >
441            <span>Off</span>
442          </button>
443        </div>
444      </div>
445    </div>
446  </Transition>
447
448  <Transition name="settings-fade">
449    <div
450      v-if="showSettingsPopup && showFade && settingsFadeReady"
451      class="search-settings-fade-overlay"
452      :style="settingsFadeStyle"
453    />
454  </Transition>
455
456  <!-- Help popups -->
457  <Transition name="help-popup-fade">
458    <div
459      v-if="activeHelpSection && showSettingsPopup"
460      ref="helpPopupEl"
461      class="search-help-popup"
462      :style="{ top: helpPopupPos.top + 'px', left: helpPopupPos.left + 'px' }"
463    >
464      <template v-if="activeHelpSection === 'search'">
465        <h4 class="sh-title">
466          <Search :size="16" stroke-width="1.75" class="sh-title-icon" />
467          Search Mode
468        </h4>
469        <p class="sh-desc">Choose how queries are matched against content.</p>
470        <div class="sh-options">
471          <div class="sh-option">
472            <strong><Regex :size="14" stroke-width="1.5" /> Exact</strong>
473            <span>Matches the exact phrase you type</span>
474          </div>
475          <div class="sh-option">
476            <strong><LocateOff :size="14" stroke-width="1.5" /> Fuzzy</strong>
477            <span>Approximate matching that tolerates typos</span>
478          </div>
479          <div class="sh-option">
480            <strong><Globe :size="14" stroke-width="1.5" /> URL</strong>
481            <span>Searches through hyperlinks</span>
482          </div>
483        </div>
484      </template>
485      <template v-else-if="activeHelpSection === 'view'">
486        <h4 class="sh-title">
487          <LayoutList :size="16" stroke-width="1.75" class="sh-title-icon" />
488          Result View
489        </h4>
490        <p class="sh-desc">Controls how search results are displayed.</p>
491        <div class="sh-options">
492          <div class="sh-option">
493            <strong><TextAlignStart :size="14" stroke-width="1.5" />
494              Detail</strong>
495            <span>Shows a content excerpt below each result. Uses more
496              memory.</span>
497          </div>
498          <div class="sh-option">
499            <strong><List :size="14" stroke-width="1.5" /> List</strong>
500            <span>Compact list of titles only. Uses less memory.</span>
501          </div>
502        </div>
503      </template>
504      <template v-else-if="activeHelpSection === 'preload'">
505        <h4 class="sh-title">
506          <Layers :size="16" stroke-width="1.75" class="sh-title-icon" />
507          Excerpt Preload
508        </h4>
509        <p class="sh-desc">
510          Renders excerpts for upcoming result pages ahead of time in Detail
511          view.
512        </p>
513        <div class="sh-options">
514          <div class="sh-option">
515            <strong>All</strong>
516            <span>Prepares every result page. Uses more memory.</span>
517          </div>
518          <div class="sh-option">
519            <strong>Next</strong>
520            <span>Also prepares the next page so flipping forward is
521              instant</span>
522          </div>
523          <div class="sh-option">
524            <strong>Off</strong>
525            <span>Only the visible page. Uses the least memory.</span>
526          </div>
527        </div>
528      </template>
529      <template v-else-if="activeHelpSection === 'highlight'">
530        <h4 class="sh-title">
531          <Highlighter :size="16" stroke-width="1.75" class="sh-title-icon" />
532          Search Result Highlight
533        </h4>
534        <p class="sh-desc">Highlights the exact result you open.</p>
535        <div class="sh-options">
536          <div class="sh-option">
537            <strong>Soft</strong>
538            <span>Soft accent background with a solid border.</span>
539          </div>
540          <div class="sh-option">
541            <strong>Solid</strong>
542            <span>Solid accent fill with no border.</span>
543          </div>
544        </div>
545      </template>
546      <template v-else-if="activeHelpSection === 'ribbon'">
547        <h4 class="sh-title">
548          <Menu :size="16" stroke-width="1.75" class="sh-title-icon" />
549          Page Ribbon
550        </h4>
551        <p class="sh-desc">How result pages are displayed in the search bar.</p>
552        <div class="sh-options">
553          <div class="sh-option">
554            <strong>Tabs</strong>
555            <span>Horizontal page list.</span>
556          </div>
557          <div class="sh-option">
558            <strong>List</strong>
559            <span>Vertical page list.</span>
560          </div>
561        </div>
562      </template>
563      <template v-else-if="activeHelpSection === 'history'">
564        <h4 class="sh-title">
565          <Clock :size="16" stroke-width="1.75" class="sh-title-icon" />
566          Search History
567        </h4>
568        <p class="sh-desc">
569          Saves recent searches and shows them when the search bar is empty.
570        </p>
571      </template>
572    </div>
573  </Transition>
574</template>
575
576<style>
577.search-settings-popup {
578  --seg-track: var(--wk-c-menu-bg);
579  --seg-pill-bg: var(--vp-c-bg);
580  --seg-pill-text: var(--vp-c-text-1);
581  --seg-pill-shadow: 0 2px 4px 0 #bababa8c;
582  position: fixed;
583  z-index: 200;
584  background: var(--vp-c-bg-elv);
585  border: 1px solid var(--vp-c-divider);
586  border-radius: 12px;
587  padding: 12px;
588  box-shadow: var(--vp-shadow-3);
589  display: flex;
590  flex-direction: column;
591  min-width: 196px;
592  max-height: min(320px, 60vh);
593  overflow-y: auto;
594  overflow-x: hidden;
595  overscroll-behavior: contain;
596  scrollbar-width: none;
597  -ms-overflow-style: none;
598}
599
600.search-settings-popup::-webkit-scrollbar {
601  display: none;
602}
603
604.dark .search-settings-popup {
605  --seg-track: var(--wk-c-menu-bg);
606  --seg-pill-bg: var(--vp-c-text-1);
607  --seg-pill-text: var(--vp-c-bg-elv);
608  --seg-pill-shadow: 0 2px 4px 0 #535353db;
609}
610
611.settings-section {
612  display: flex;
613  flex-direction: column;
614  gap: 0;
615}
616
617.settings-section + .settings-section {
618  border-top: 1px solid var(--vp-c-divider);
619  margin-top: 8px;
620  padding-top: 8px;
621}
622
623.settings-section-header {
624  display: flex;
625  align-items: center;
626  justify-content: space-between;
627  margin-bottom: 6px;
628}
629
630.settings-section-label {
631  display: flex;
632  align-items: center;
633  gap: 6px;
634  font-size: 14px;
635  font-weight: 500;
636  color: var(--vp-c-text-1);
637}
638
639.settings-help-btn {
640  display: flex;
641  align-items: center;
642  justify-content: center;
643  padding: 3px;
644  background: transparent;
645  border: none;
646  cursor: pointer;
647  color: var(--vp-c-text-3);
648  opacity: 0.5;
649  transition: opacity 0.15s, color 0.15s;
650  border-radius: 3px;
651  line-height: 1;
652}
653
654.settings-help-btn:hover,
655.settings-help-btn.active {
656  opacity: 1;
657  color: var(--vp-c-text-1);
658}
659
660.settings-help-icon {
661  display: block;
662  width: 16px;
663  height: 16px;
664}
665
666.settings-options {
667  display: flex;
668  gap: 4px;
669  background: var(--seg-track);
670  border-radius: 8px;
671  padding: 4px;
672  outline: 2px dashed transparent;
673  outline-offset: 4px;
674  transition: outline-color 0.2s ease;
675}
676
677.settings-options.is-highlighted {
678  outline-color: var(--vp-c-brand-1);
679}
680
681.settings-option {
682  display: flex;
683  align-items: center;
684  justify-content: center;
685  gap: 5px;
686  padding: 5px 6px;
687  background: transparent;
688  border: none;
689  border-radius: 6px;
690  cursor: pointer;
691  color: var(--vp-c-text-1);
692  font-size: 14px;
693  font-weight: 500;
694  transition: background-color 0.15s, color 0.15s, box-shadow 0.15s;
695  flex: 1;
696}
697
698.settings-option:hover,
699.settings-option.active {
700  color: var(--seg-pill-text);
701  background: var(--seg-pill-bg);
702  box-shadow: var(--seg-pill-shadow);
703}
704
705.settings-option.active {
706  font-weight: 700;
707}
708
709.settings-popup-enter-active,
710.settings-popup-leave-active {
711  transition: opacity 0.15s ease, transform 0.15s ease;
712  transform-origin: top right;
713}
714
715.settings-popup-enter-from,
716.settings-popup-leave-to {
717  opacity: 0;
718  transform: scale(0.92) translateY(-4px);
719}
720
721.search-settings-fade-overlay {
722  position: fixed;
723  z-index: 200;
724  height: 56px;
725  pointer-events: none;
726  background: linear-gradient(
727    to bottom,
728    transparent,
729    var(--vp-c-bg-elv)
730  );
731  border-radius: 0 0 12px 12px;
732}
733
734.settings-fade-enter-active {
735  transition: opacity 0.2s;
736}
737
738.settings-fade-leave-active {
739  transition: none;
740}
741
742.settings-fade-enter-from,
743.settings-fade-leave-to {
744  opacity: 0;
745}
746
747.search-help-popup {
748  position: fixed;
749  z-index: 9999;
750  width: 280px;
751  background: var(--vp-c-bg-elv);
752  border: 1px solid var(--vp-c-divider);
753  border-radius: 12px;
754  padding: 16px;
755  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
756  pointer-events: none;
757}
758
759.help-popup-fade-enter-active,
760.help-popup-fade-leave-active {
761  transition: opacity 0.15s ease;
762}
763
764.help-popup-fade-enter-from,
765.help-popup-fade-leave-to {
766  opacity: 0;
767}
768
769.sh-title {
770  display: flex;
771  align-items: center;
772  gap: 6px;
773  font-size: 1rem;
774  font-weight: 600;
775  color: var(--vp-c-text-1);
776  margin: 0 0 4px;
777}
778
779.sh-title-icon {
780  flex-shrink: 0;
781  opacity: 0.8;
782  width: 16px;
783  height: 16px;
784}
785
786.sh-desc {
787  font-size: 0.875rem;
788  color: var(--vp-c-text-2);
789  margin: 0 0 6px;
790  line-height: 1.5;
791}
792
793.sh-options {
794  display: flex;
795  flex-direction: column;
796  gap: 5px;
797}
798
799.sh-option {
800  display: flex;
801  flex-direction: column;
802  gap: 4px;
803  background: var(--wk-c-menu-bg);
804  border-radius: 12px;
805  padding: 10px 12px;
806}
807
808.dark .sh-option {
809  background: var(--wk-c-menu-bg);
810}
811
812.sh-option strong {
813  display: flex;
814  align-items: center;
815  gap: 4px;
816  font-size: 0.875rem;
817  font-weight: 600;
818  color: var(--vp-c-text-1);
819}
820
821.sh-option span {
822  font-size: 0.875rem;
823  color: var(--vp-c-text-2);
824  line-height: 1.4;
825}
826
827@media (max-width: 767px) {
828  .search-settings-popup {
829    max-width: 280px;
830  }
831
832  .search-help-popup {
833    position: fixed !important;
834    width: calc(100vw - 16px);
835    max-width: 300px;
836  }
837}
838</style>