feat(docs): align documentation shell with HeroUI baseline
This commit is contained in:
@@ -10,15 +10,21 @@ import {
|
||||
} from "vue";
|
||||
import { withBase, useRouter } from "vitepress";
|
||||
import {
|
||||
DialogRoot,
|
||||
DialogTrigger,
|
||||
DialogPortal,
|
||||
DialogOverlay,
|
||||
DialogContent,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
DialogClose,
|
||||
} from "reka-ui";
|
||||
Button,
|
||||
Link,
|
||||
Kbd,
|
||||
Modal,
|
||||
ModalTrigger,
|
||||
ModalBackdrop,
|
||||
ModalContainer,
|
||||
ModalDialog,
|
||||
ModalHeading,
|
||||
SearchField,
|
||||
SearchFieldGroup,
|
||||
SearchFieldInput,
|
||||
SearchFieldSearchIcon,
|
||||
EmptyState,
|
||||
} from "@heroui-vue/components";
|
||||
import { searchIndex } from "virtual:docs-data";
|
||||
import { searchDocs, searchExcerpt } from "./search";
|
||||
const router = useRouter();
|
||||
@@ -26,7 +32,7 @@ const navigating = ref(false);
|
||||
const open = ref(false),
|
||||
query = ref(""),
|
||||
active = ref(0),
|
||||
field = ref<HTMLInputElement>();
|
||||
field = ref<{ focus: () => void }>();
|
||||
const results = computed(() => searchDocs(searchIndex, query.value));
|
||||
watch(query, () => {
|
||||
active.value = 0;
|
||||
@@ -36,11 +42,31 @@ watch(open, (value) => {
|
||||
else navigating.value = false;
|
||||
});
|
||||
function shortcut(event: KeyboardEvent) {
|
||||
if ((event.metaKey || event.ctrlKey) && event.key.toLowerCase() === "k") {
|
||||
if (
|
||||
!event.defaultPrevented &&
|
||||
!event.isComposing &&
|
||||
!event.repeat &&
|
||||
(event.metaKey || event.ctrlKey) &&
|
||||
event.key.toLowerCase() === "k"
|
||||
) {
|
||||
event.preventDefault();
|
||||
open.value = !open.value;
|
||||
}
|
||||
}
|
||||
function keydown(event: KeyboardEvent) {
|
||||
if (event.defaultPrevented || event.isComposing || event.keyCode === 229)
|
||||
return;
|
||||
if (event.key === "ArrowDown" || event.key === "ArrowUp") {
|
||||
event.preventDefault();
|
||||
move(event.key === "ArrowDown" ? 1 : -1);
|
||||
} else if (event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
navigate();
|
||||
} else if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
open.value = false;
|
||||
}
|
||||
}
|
||||
function move(delta: number) {
|
||||
if (!results.value.length) return;
|
||||
active.value =
|
||||
@@ -67,6 +93,9 @@ function navigate() {
|
||||
const entry = results.value[active.value];
|
||||
if (entry) void navigateTo(entry);
|
||||
}
|
||||
function guardEscape(event: KeyboardEvent) {
|
||||
if (event.isComposing) event.preventDefault();
|
||||
}
|
||||
function closeAutoFocus(event: Event) {
|
||||
if (navigating.value) event.preventDefault();
|
||||
}
|
||||
@@ -74,83 +103,113 @@ onMounted(() => window.addEventListener("keydown", shortcut));
|
||||
onBeforeUnmount(() => window.removeEventListener("keydown", shortcut));
|
||||
</script>
|
||||
<template>
|
||||
<DialogRoot v-model:open="open">
|
||||
<DialogTrigger class="search-trigger" aria-label="搜索文档">
|
||||
<IconSearch aria-hidden="true" :size="17" />
|
||||
<span>搜索文档…</span>
|
||||
<kbd>⌘ / Ctrl K</kbd>
|
||||
</DialogTrigger>
|
||||
<DialogPortal>
|
||||
<DialogOverlay class="search-overlay" />
|
||||
<DialogContent
|
||||
class="search-dialog"
|
||||
@open-auto-focus.prevent="field?.focus()"
|
||||
@close-auto-focus="closeAutoFocus"
|
||||
>
|
||||
<DialogTitle class="sr-only">搜索文档</DialogTitle>
|
||||
<DialogDescription class="sr-only">
|
||||
搜索组件、参数和用法;使用方向键选择,回车打开,Esc 关闭。
|
||||
</DialogDescription>
|
||||
<div class="search-field">
|
||||
<input
|
||||
ref="field"
|
||||
v-model="query"
|
||||
placeholder="搜索组件、参数或用法…"
|
||||
aria-label="搜索组件、参数或用法"
|
||||
role="combobox"
|
||||
aria-autocomplete="list"
|
||||
aria-controls="doc-search-results"
|
||||
:aria-expanded="Boolean(query.trim())"
|
||||
:aria-activedescendant="
|
||||
results.length ? 'search-result-' + active : undefined
|
||||
"
|
||||
@keydown.down.prevent="move(1)"
|
||||
@keydown.up.prevent="move(-1)"
|
||||
@keydown.enter.prevent="navigate"
|
||||
/>
|
||||
<DialogClose class="search-close" aria-label="关闭搜索">
|
||||
Esc
|
||||
</DialogClose>
|
||||
</div>
|
||||
<div
|
||||
class="search-results"
|
||||
id="doc-search-results"
|
||||
role="listbox"
|
||||
aria-label="搜索结果"
|
||||
<Modal v-model:open="open">
|
||||
<ModalTrigger as-child>
|
||||
<Button class="search-trigger" variant="secondary" aria-label="搜索文档">
|
||||
<IconSearch aria-hidden="true" :size="17" />
|
||||
<span>搜索文档…</span>
|
||||
<Kbd>⌘ / Ctrl K</Kbd>
|
||||
</Button>
|
||||
</ModalTrigger>
|
||||
<ModalBackdrop class="docs-search-backdrop" variant="blur">
|
||||
<ModalContainer placement="top" size="lg" class="docs-search-container">
|
||||
<ModalDialog
|
||||
class="search-dialog"
|
||||
@open-auto-focus.prevent="field?.focus()"
|
||||
@close-auto-focus="closeAutoFocus"
|
||||
@escape-key-down="guardEscape"
|
||||
>
|
||||
<p v-if="!query.trim()" class="search-empty">
|
||||
试试搜索「isPending」「主题」或「受控」
|
||||
<ModalHeading class="sr-only">搜索文档</ModalHeading>
|
||||
<p class="sr-only" id="docs-search-instructions">
|
||||
搜索组件、参数和用法;使用方向键选择,回车打开,Esc 关闭。
|
||||
</p>
|
||||
<p v-else-if="!results.length" class="search-empty" role="status">
|
||||
没有找到“{{ query }}”,试试组件名或参数名。
|
||||
</p>
|
||||
<a
|
||||
v-for="(entry, index) in results"
|
||||
:id="'search-result-' + index"
|
||||
:key="entry.url"
|
||||
:href="withBase(entry.url)"
|
||||
role="option"
|
||||
:aria-selected="index === active"
|
||||
:tabindex="-1"
|
||||
@mousemove="active = index"
|
||||
@click.prevent="navigateTo(entry)"
|
||||
<div class="search-field">
|
||||
<SearchField
|
||||
v-model="query"
|
||||
class="docs-search-input"
|
||||
aria-label="搜索组件、参数或用法"
|
||||
>
|
||||
<SearchFieldGroup>
|
||||
<SearchFieldSearchIcon />
|
||||
<SearchFieldInput
|
||||
ref="field"
|
||||
placeholder="搜索组件、参数或用法…"
|
||||
aria-label="搜索组件、参数或用法"
|
||||
role="combobox"
|
||||
aria-autocomplete="list"
|
||||
aria-describedby="docs-search-instructions"
|
||||
aria-controls="doc-search-results"
|
||||
:aria-expanded="Boolean(query.trim())"
|
||||
:aria-activedescendant="
|
||||
query.trim() && results.length
|
||||
? 'search-result-' + active
|
||||
: undefined
|
||||
"
|
||||
@keydown="keydown"
|
||||
/>
|
||||
</SearchFieldGroup>
|
||||
</SearchField>
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
class="search-close"
|
||||
aria-label="关闭搜索"
|
||||
@click="open = false"
|
||||
>
|
||||
<Kbd>Esc</Kbd>
|
||||
</Button>
|
||||
</div>
|
||||
<div
|
||||
class="search-results"
|
||||
id="doc-search-results"
|
||||
role="listbox"
|
||||
aria-label="搜索结果"
|
||||
>
|
||||
<span class="result-title">
|
||||
{{ entry.title }}
|
||||
<span>{{ entry.section }}</span>
|
||||
<EmptyState v-if="!query.trim()" class="search-empty">
|
||||
<IconSearch :size="24" aria-hidden="true" />
|
||||
<p>试试搜索「isPending」「主题」或「受控」</p>
|
||||
</EmptyState>
|
||||
<EmptyState
|
||||
v-else-if="!results.length"
|
||||
class="search-empty"
|
||||
role="status"
|
||||
>
|
||||
<p>没有找到“{{ query }}”,试试组件名或参数名。</p>
|
||||
</EmptyState>
|
||||
<Link
|
||||
v-for="(entry, index) in results"
|
||||
:id="'search-result-' + index"
|
||||
:key="entry.url"
|
||||
:href="withBase(entry.url)"
|
||||
role="option"
|
||||
:aria-selected="index === active"
|
||||
:tabindex="-1"
|
||||
@mousemove="active = index"
|
||||
@click.prevent="navigateTo(entry)"
|
||||
>
|
||||
<span class="result-title">
|
||||
{{ entry.title }}
|
||||
<span>{{ entry.section }}</span>
|
||||
</span>
|
||||
<span class="result-excerpt">
|
||||
{{ searchExcerpt(entry, query) || "查看文档" }}
|
||||
</span>
|
||||
</Link>
|
||||
</div>
|
||||
<div class="search-footer" aria-live="polite">
|
||||
<span>
|
||||
{{ query.trim() ? results.length + " 条结果" : "本地文档搜索" }}
|
||||
</span>
|
||||
<span class="result-excerpt">
|
||||
{{ searchExcerpt(entry, query) || "查看文档" }}
|
||||
<span>
|
||||
<Kbd>↑</Kbd>
|
||||
<Kbd>↓</Kbd>
|
||||
选择
|
||||
<Kbd>↵</Kbd>
|
||||
打开
|
||||
</span>
|
||||
</a>
|
||||
</div>
|
||||
<div class="search-footer" aria-live="polite">
|
||||
<span>
|
||||
{{ query.trim() ? results.length + " 条结果" : "本地文档搜索" }}
|
||||
</span>
|
||||
<span>↑ ↓ 选择 ↵ 打开</span>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</DialogPortal>
|
||||
</DialogRoot>
|
||||
</div>
|
||||
</ModalDialog>
|
||||
</ModalContainer>
|
||||
</ModalBackdrop>
|
||||
</Modal>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user