提交正式官网
This commit is contained in:
@@ -0,0 +1,332 @@
|
||||
<script setup lang="ts">
|
||||
import type { SiteSearchResult } from '~/composables/useSiteSearch'
|
||||
|
||||
const open = defineModel<boolean>({ default: false })
|
||||
|
||||
const { t, tr } = useSiteLocale()
|
||||
const localePath = useLocalePath()
|
||||
const { searchSite } = useSiteSearch()
|
||||
|
||||
function categoryLabel(category: string) {
|
||||
const labels = tr<Record<string, string>>('searchCategories')
|
||||
return labels[category] ?? category
|
||||
}
|
||||
const query = ref('')
|
||||
const activeIndex = ref(0)
|
||||
const inputRef = ref<HTMLInputElement | null>(null)
|
||||
|
||||
const results = computed<SiteSearchResult[]>(() => searchSite(query.value))
|
||||
|
||||
watch(query, () => {
|
||||
activeIndex.value = 0
|
||||
})
|
||||
|
||||
watch(open, (isOpen) => {
|
||||
if (!import.meta.client) return
|
||||
document.body.style.overflow = isOpen ? 'hidden' : ''
|
||||
if (isOpen) {
|
||||
nextTick(() => inputRef.value?.focus())
|
||||
} else {
|
||||
query.value = ''
|
||||
activeIndex.value = 0
|
||||
}
|
||||
})
|
||||
|
||||
function close() {
|
||||
open.value = false
|
||||
}
|
||||
|
||||
function selectResult(result: SiteSearchResult) {
|
||||
close()
|
||||
navigateTo(localePath(result.url))
|
||||
}
|
||||
|
||||
function onKeydown(e: KeyboardEvent) {
|
||||
if (!open.value) return
|
||||
|
||||
if (e.key === 'Escape') {
|
||||
e.preventDefault()
|
||||
close()
|
||||
return
|
||||
}
|
||||
|
||||
if (e.key === 'ArrowDown') {
|
||||
e.preventDefault()
|
||||
if (!results.value.length) return
|
||||
activeIndex.value = (activeIndex.value + 1) % results.value.length
|
||||
return
|
||||
}
|
||||
|
||||
if (e.key === 'ArrowUp') {
|
||||
e.preventDefault()
|
||||
if (!results.value.length) return
|
||||
activeIndex.value =
|
||||
(activeIndex.value - 1 + results.value.length) % results.value.length
|
||||
return
|
||||
}
|
||||
|
||||
if (e.key === 'Enter') {
|
||||
e.preventDefault()
|
||||
const hit = results.value[activeIndex.value]
|
||||
if (hit) selectResult(hit)
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
document.addEventListener('keydown', onGlobalKeydown)
|
||||
document.addEventListener('keydown', onKeydown)
|
||||
})
|
||||
|
||||
onUnmounted(() => {
|
||||
document.removeEventListener('keydown', onGlobalKeydown)
|
||||
document.removeEventListener('keydown', onKeydown)
|
||||
if (import.meta.client) {
|
||||
document.body.style.overflow = ''
|
||||
}
|
||||
})
|
||||
|
||||
function onGlobalKeydown(e: KeyboardEvent) {
|
||||
if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === 'k') {
|
||||
e.preventDefault()
|
||||
open.value = !open.value
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Teleport to="body">
|
||||
<Transition name="site-search">
|
||||
<div
|
||||
v-if="open"
|
||||
class="site-search"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
:aria-label="t('search.siteSearch')"
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="site-search__backdrop"
|
||||
:aria-label="t('search.closeSearch')"
|
||||
@click="close"
|
||||
/>
|
||||
|
||||
<div class="site-search__panel">
|
||||
<div class="site-search__field">
|
||||
<svg
|
||||
class="site-search__icon"
|
||||
width="20"
|
||||
height="20"
|
||||
viewBox="0 0 24 24"
|
||||
aria-hidden="true"
|
||||
>
|
||||
<circle
|
||||
cx="11"
|
||||
cy="11"
|
||||
r="7"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
/>
|
||||
<path
|
||||
d="M16 16l5 5"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="2"
|
||||
stroke-linecap="round"
|
||||
/>
|
||||
</svg>
|
||||
<input
|
||||
ref="inputRef"
|
||||
v-model="query"
|
||||
type="search"
|
||||
class="site-search__input"
|
||||
:placeholder="t('search.placeholder')"
|
||||
autocomplete="off"
|
||||
:aria-label="t('search.searchSite')"
|
||||
/>
|
||||
<kbd class="site-search__hint" aria-hidden="true">Esc</kbd>
|
||||
</div>
|
||||
|
||||
<div v-if="query.trim()" class="site-search__results" role="listbox">
|
||||
<p v-if="!results.length" class="site-search__empty">
|
||||
{{ t('search.noResults', { query: query.trim() }) }}
|
||||
</p>
|
||||
|
||||
<button
|
||||
v-for="(item, index) in results"
|
||||
:key="item.id"
|
||||
type="button"
|
||||
class="site-search__result"
|
||||
:class="{ 'is-active': index === activeIndex }"
|
||||
role="option"
|
||||
:aria-selected="index === activeIndex"
|
||||
@mouseenter="activeIndex = index"
|
||||
@click="selectResult(item)"
|
||||
>
|
||||
<span class="site-search__result-head">
|
||||
<span class="site-search__result-title">{{ item.title }}</span>
|
||||
<span class="site-search__result-cat">{{ categoryLabel(item.category) }}</span>
|
||||
</span>
|
||||
<span class="site-search__result-excerpt">{{ item.excerpt }}</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p v-else class="site-search__tips">
|
||||
{{ t('search.tips') }}
|
||||
<kbd>Ctrl</kbd>+<kbd>K</kbd> {{ t('search.tipsShortcut') }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</Transition>
|
||||
</Teleport>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.site-search {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 1100;
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
justify-content: center;
|
||||
padding: min(12vh, 96px) 16px 24px;
|
||||
}
|
||||
.site-search__backdrop {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
border: none;
|
||||
background: rgba(15, 39, 64, 0.52);
|
||||
cursor: pointer;
|
||||
}
|
||||
.site-search__panel {
|
||||
position: relative;
|
||||
width: min(680px, 100%);
|
||||
background: #fff;
|
||||
border: 1px solid #e2eaf2;
|
||||
border-radius: 16px;
|
||||
box-shadow: 0 24px 64px rgba(15, 45, 85, 0.18);
|
||||
overflow: hidden;
|
||||
}
|
||||
.site-search__field {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 14px 16px;
|
||||
border-bottom: 1px solid #eef3f8;
|
||||
}
|
||||
.site-search__icon {
|
||||
flex-shrink: 0;
|
||||
color: #6b7f94;
|
||||
}
|
||||
.site-search__input {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
border: none;
|
||||
background: transparent;
|
||||
font-size: 16px;
|
||||
color: var(--ink);
|
||||
outline: none;
|
||||
}
|
||||
.site-search__input::placeholder {
|
||||
color: #8a9bb0;
|
||||
}
|
||||
.site-search__hint {
|
||||
flex-shrink: 0;
|
||||
font-size: 11px;
|
||||
font-family: inherit;
|
||||
color: #6b7f94;
|
||||
background: #f4f8fc;
|
||||
border: 1px solid #dde7f0;
|
||||
border-radius: 6px;
|
||||
padding: 2px 6px;
|
||||
}
|
||||
.site-search__results {
|
||||
max-height: min(52vh, 420px);
|
||||
overflow-y: auto;
|
||||
padding: 8px;
|
||||
}
|
||||
.site-search__empty,
|
||||
.site-search__tips {
|
||||
margin: 0;
|
||||
padding: 16px 18px 18px;
|
||||
font-size: 14px;
|
||||
line-height: 1.6;
|
||||
color: var(--muted);
|
||||
}
|
||||
.site-search__tips kbd {
|
||||
font-size: 11px;
|
||||
font-family: inherit;
|
||||
color: #4a6078;
|
||||
background: #f4f8fc;
|
||||
border: 1px solid #dde7f0;
|
||||
border-radius: 5px;
|
||||
padding: 1px 5px;
|
||||
margin: 0 2px;
|
||||
}
|
||||
.site-search__result {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 4px;
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
border: none;
|
||||
border-radius: 10px;
|
||||
background: transparent;
|
||||
text-align: left;
|
||||
cursor: pointer;
|
||||
font-family: inherit;
|
||||
transition: background 0.12s ease;
|
||||
}
|
||||
.site-search__result:hover,
|
||||
.site-search__result.is-active {
|
||||
background: #eef6ff;
|
||||
}
|
||||
.site-search__result-head {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
width: 100%;
|
||||
}
|
||||
.site-search__result-title {
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #0f2740;
|
||||
line-height: 1.35;
|
||||
}
|
||||
.site-search__result-cat {
|
||||
flex-shrink: 0;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.02em;
|
||||
text-transform: uppercase;
|
||||
color: var(--blue);
|
||||
background: rgba(72, 164, 252, 0.08);
|
||||
border-radius: 999px;
|
||||
padding: 2px 8px;
|
||||
}
|
||||
.site-search__result-excerpt {
|
||||
font-size: 13px;
|
||||
line-height: 1.45;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.site-search-enter-active,
|
||||
.site-search-leave-active {
|
||||
transition: opacity 0.18s ease;
|
||||
}
|
||||
.site-search-enter-active .site-search__panel,
|
||||
.site-search-leave-active .site-search__panel {
|
||||
transition: transform 0.18s ease, opacity 0.18s ease;
|
||||
}
|
||||
.site-search-enter-from,
|
||||
.site-search-leave-to {
|
||||
opacity: 0;
|
||||
}
|
||||
.site-search-enter-from .site-search__panel,
|
||||
.site-search-leave-to .site-search__panel {
|
||||
transform: translateY(-8px) scale(0.98);
|
||||
opacity: 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user