feat(docs): align documentation shell with HeroUI baseline

This commit is contained in:
maoyingjie
2026-10-24 07:18:36 +08:00
parent 406497a20f
commit b2246f0058
33 changed files with 2499 additions and 1540 deletions
+378 -282
View File
@@ -1,11 +1,4 @@
<script setup lang="ts">
import {
IconSun,
IconMoon,
IconMenu2,
IconX,
IconArrowUpRight,
} from "@tabler/icons-vue";
import {
computed,
ref,
@@ -14,173 +7,186 @@ import {
onMounted,
onBeforeUnmount,
} from "vue";
import { useData, useRoute, withBase, onContentUpdated } from "vitepress";
import {
DialogRoot,
DialogTrigger,
DialogPortal,
DialogOverlay,
DialogContent,
DialogTitle,
DialogClose,
} from "reka-ui";
useData,
useRoute,
useRouter,
withBase,
onContentUpdated,
} from "vitepress";
import {
Button,
Link,
Chip,
Separator,
Breadcrumbs,
BreadcrumbsItem,
Card,
Drawer,
DrawerTrigger,
DrawerBackdrop,
DrawerContent,
DrawerDialog,
DrawerHeader,
DrawerHeading,
DrawerBody,
DrawerCloseTrigger,
Popover,
PopoverTrigger,
PopoverContent,
PopoverDialog,
PopoverHeading,
ScrollShadow,
} from "@heroui-vue/components";
import {
IconMenu2,
IconArrowUpRight,
IconBook2,
IconComponents,
IconList,
IconChevronDown,
IconBrandVue,
IconBrandGithub,
IconArrowLeft,
IconArrowRight,
} from "@tabler/icons-vue";
import DocSearch from "./DocSearch.vue";
const { frontmatter, page, isDark } = useData();
import DocsSidebar from "./DocsSidebar.vue";
import DocsToc from "./DocsToc.vue";
import DocsThemeSwitch from "./DocsThemeSwitch.vue";
import {
guideGroups,
componentGroups,
componentItems,
docsPath,
docsSection,
adjacentPages,
} from "./navigation";
const { frontmatter, page, site } = useData();
const route = useRoute();
const mobile = ref(false),
activeSlug = ref("");
const copyMessage = ref("");
let copyTimer: ReturnType<typeof setTimeout> | undefined;
onBeforeUnmount(() => clearTimeout(copyTimer));
const groups = [
{
title: "开始使用",
items: [
{ title: "介绍", url: "/introduction" },
{ title: "安装", url: "/guide" },
{ title: "主题", url: "/themes" },
{ title: "字段与表单基础", url: "/fields" },
],
},
{
title: "组件",
items: [
{ title: "Accordion 手风琴", url: "/components/accordion" },
{ title: "Alert 警告", url: "/components/alert" },
{ title: "AlertDialog 警告对话框", url: "/components/alert-dialog" },
{ title: "Avatar 头像", url: "/components/avatar" },
{ title: "AvatarGroup 头像组", url: "/components/avatar-group" },
{ title: "Badge 徽章", url: "/components/badge" },
{ title: "Breadcrumbs 面包屑", url: "/components/breadcrumbs" },
{ title: "Button 按钮", url: "/components/button" },
{
title: "ButtonGroup 按钮组",
url: "/components/button-group",
},
{ title: "Card 卡片", url: "/components/card" },
{ title: "Checkbox 复选框", url: "/components/checkbox" },
{ title: "CheckboxGroup 复选框组", url: "/components/checkbox-group" },
{ title: "Chip 标签", url: "/components/chip" },
{ title: "CloseButton 关闭按钮", url: "/components/close-button" },
{ title: "DateField 日期字段", url: "/components/date-field" },
{ title: "Description 描述", url: "/components/description" },
{ title: "Disclosure 折叠面板", url: "/components/disclosure" },
{
title: "DisclosureGroup 折叠面板组",
url: "/components/disclosure-group",
},
{ title: "Drawer 抽屉", url: "/components/drawer" },
{ title: "EmptyState 空状态", url: "/components/empty-state" },
{ title: "FieldError 字段错误", url: "/components/field-error" },
{ title: "Fieldset 字段组", url: "/components/fieldset" },
{ title: "Header 标题", url: "/components/header" },
{ title: "Input 输入框", url: "/components/input" },
{ title: "InputOTP 验证码输入", url: "/components/input-otp" },
{ title: "InputGroup 输入框组", url: "/components/input-group" },
{ title: "Kbd 键盘按键", url: "/components/kbd" },
{ title: "Label 标签", url: "/components/label" },
{ title: "Link 链接", url: "/components/link" },
{ title: "Meter 计量条", url: "/components/meter" },
{ title: "Modal 模态框", url: "/components/modal" },
{ title: "Pagination 分页", url: "/components/pagination" },
{ title: "Popover 弹出框", url: "/components/popover" },
{ title: "ProgressBar 进度条", url: "/components/progress-bar" },
{
title: "ProgressCircle 环形进度条",
url: "/components/progress-circle",
},
{ title: "RadioGroup 单选框组", url: "/components/radio-group" },
{ title: "ScrollShadow 滚动阴影", url: "/components/scroll-shadow" },
{ title: "SearchField 搜索框", url: "/components/search-field" },
{ title: "Separator 分隔符", url: "/components/separator" },
{ title: "Skeleton 骨架屏", url: "/components/skeleton" },
{ title: "Spinner 加载指示器", url: "/components/spinner" },
{ title: "Surface 表面", url: "/components/surface" },
{ title: "Switch 开关", url: "/components/switch" },
{ title: "Tabs 标签页", url: "/components/tabs" },
{ title: "TextArea 多行文本框", url: "/components/textarea" },
{ title: "TextField 文本输入框", url: "/components/text-field" },
{ title: "Toolbar 工具栏", url: "/components/toolbar" },
{ title: "NumberField 数字输入框", url: "/components/number-field" },
{ title: "Slider 滑块", url: "/components/slider" },
{ title: "ListBox 列表框", url: "/components/list-box" },
{ title: "Select 选择器", url: "/components/select" },
{ title: "Tooltip 工具提示", url: "/components/tooltip" },
{
title: "ToggleButton 切换按钮",
url: "/components/toggle-button",
},
{
title: "ToggleButtonGroup 切换按钮组",
url: "/components/toggle-button-group",
},
{ title: "Typography 排版", url: "/components/typography" },
],
},
];
const items = groups.flatMap((g) => g.items);
const currentIndex = computed(() =>
items.findIndex((item) => selected(item.url)),
);
const previous = computed(() => items[currentIndex.value - 1]);
const next = computed(() => items[currentIndex.value + 1]);
function selected(url: string) {
return (
route.path === withBase(url) ||
route.path === withBase(url + ".html") ||
(url === "/components/button" && route.path === withBase("/"))
);
const router = useRouter();
let pendingToc: { slug: string; path: string } | undefined;
function navigateToc(slug: string, event: MouseEvent) {
event.preventDefault();
pendingToc = { slug, path: current.value };
tocOpen.value = false;
}
async function finishTocNavigation(event: Event) {
const target = pendingToc;
pendingToc = undefined;
if (!target || current.value !== target.path) return;
event.preventDefault();
await router.go(
withBase(target.path + "#" + encodeURIComponent(target.slug)),
);
await nextTick();
if (current.value !== target.path) return;
const heading = document.getElementById(target.slug);
heading?.setAttribute("tabindex", "-1");
heading?.focus({ preventScroll: true });
heading?.scrollIntoView({ block: "start" });
}
const current = computed(() => docsPath(route.path, site.value.base));
const section = computed(() => docsSection(route.path, site.value.base));
const groups = computed(() =>
section.value === "components" ? componentGroups : guideGroups,
);
const adjacent = computed(() => adjacentPages(route.path, site.value.base));
const componentSlug = computed(() =>
componentItems.some((item) => item.url === current.value)
? current.value.split("/").pop()
: undefined,
);
const headers = computed(() => page.value.headers ?? []);
const flatHeaders = computed(() =>
headers.value.flatMap((h) => [h, ...(h.children ?? [])]),
headers.value.flatMap((heading) => [heading, ...(heading.children ?? [])]),
);
let ticking = false;
const mobile = ref(false),
tocOpen = ref(false),
activeSlug = ref("");
const sidebar = ref<{ $el?: HTMLElement }>();
const copyMessage = ref("");
let copyTimer: ReturnType<typeof setTimeout> | undefined;
let frame = 0;
function updateActive() {
ticking = false;
const headings = flatHeaders.value
.map((h) => document.getElementById(h.slug))
.filter((el): el is HTMLElement => Boolean(el));
let active = headings[0]?.id ?? "";
for (const heading of headings) {
if (heading.getBoundingClientRect().top <= 120) active = heading.id;
else break;
frame = 0;
let active = flatHeaders.value[0]?.slug ?? "";
for (const heading of flatHeaders.value) {
const element = document.getElementById(heading.slug);
if (element && element.getBoundingClientRect().top <= 160)
active = heading.slug;
else if (element) break;
}
if (
window.innerHeight + window.scrollY >=
document.documentElement.scrollHeight - 2
)
active = flatHeaders.value.at(-1)?.slug ?? active;
activeSlug.value = active;
}
function onScroll() {
if (!ticking) {
ticking = true;
requestAnimationFrame(updateActive);
}
if (!frame) frame = requestAnimationFrame(updateActive);
}
function revealCurrent() {
const element = sidebar.value?.$el?.querySelector<HTMLElement>(
'[aria-current="page"]',
);
const viewport = sidebar.value?.$el;
if (!element || !viewport || !viewport.clientHeight) return;
const top = element.getBoundingClientRect().top,
bounds = viewport.getBoundingClientRect();
if (
top < bounds.top ||
element.getBoundingClientRect().bottom > bounds.bottom
)
element.scrollIntoView({ block: "nearest" });
}
onMounted(() => {
window.addEventListener("scroll", onScroll, { passive: true });
updateActive();
revealCurrent();
});
onBeforeUnmount(() => {
window.removeEventListener("scroll", onScroll);
cancelAnimationFrame(frame);
clearTimeout(copyTimer);
});
onBeforeUnmount(() => window.removeEventListener("scroll", onScroll));
onContentUpdated(() => {
void nextTick(updateActive);
void nextTick(() => {
updateActive();
revealCurrent();
});
});
watch(
() => route.path,
() => {
mobile.value = false;
tocOpen.value = false;
void nextTick(() => {
if (!window.location.hash)
window.scrollTo({ top: 0, behavior: "instant" });
updateActive();
revealCurrent();
});
},
);
function focusPage(event: Event) {
if (!navigationFromDrawer) return;
event.preventDefault();
navigationFromDrawer = false;
document
.querySelector<HTMLElement>(".docs-main h1")
?.focus({ preventScroll: true });
}
let navigationFromDrawer = false;
function leaveDrawer() {
navigationFromDrawer = true;
mobile.value = false;
}
async function copyMarkdownCode(event: MouseEvent) {
const target = (event.target as HTMLElement).closest<HTMLButtonElement>(
"button.copy",
);
if (!target) return;
const code = target.parentElement?.querySelector("pre code")?.textContent;
if (!code) return;
const code = target?.parentElement?.querySelector("pre code")?.textContent;
if (!target || !code) return;
try {
await navigator.clipboard.writeText(code);
target.classList.add("copied");
@@ -200,167 +206,257 @@ async function copyMarkdownCode(event: MouseEvent) {
</script>
<template>
<div class="docs-layout">
<a class="skip" href="#main">跳至内容</a>
<Link class="skip" href="#main">跳至内容</Link>
<p class="sr-only" role="status">{{ copyMessage }}</p>
<header class="docs-nav">
<a class="brand" :href="withBase('/components/button')">
heroui
<span>Vue</span>
</a>
<span class="version">v0.0.1</span>
<nav aria-label="主导航">
<a
:href="withBase('/guide')"
:class="{
active:
!frontmatter.component && route.path !== withBase('/themes'),
}"
>
文档
</a>
<a
:href="withBase('/components/button')"
:class="{ active: Boolean(frontmatter.component) }"
>
组件
</a>
<a
:href="withBase('/themes')"
:class="{ active: route.path === withBase('/themes') }"
>
主题
</a>
</nav>
<div class="nav-tools">
<div class="docs-nav-main">
<div class="docs-brand-group">
<Link
class="brand"
:href="withBase('/')"
aria-label="HeroUI Vue 文档首页"
>
heroui
<Chip size="sm" variant="soft">Vue</Chip>
</Link>
<Popover>
<PopoverTrigger as-child>
<Button
class="version"
variant="ghost"
size="sm"
aria-label="版本信息"
>
v0.0.1
<IconChevronDown :size="12" aria-hidden="true" />
</Button>
</PopoverTrigger>
<PopoverContent placement="bottom start">
<PopoverDialog class="version-panel">
<PopoverHeading>HeroUI Vue</PopoverHeading>
<p>社区独立实现 · v0.0.1</p>
<Separator />
<Link
href="https://github.com/heroui-inc/heroui/tree/ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960"
target="_blank"
rel="noreferrer noopener"
>
设计基线 HeroUI v3.2.6
<IconArrowUpRight :size="13" aria-hidden="true" />
</Link>
</PopoverDialog>
</PopoverContent>
</Popover>
</div>
<DocSearch />
<button
class="theme-toggle"
:aria-label="isDark ? '切换浅色主题' : '切换深色主题'"
@click="isDark = !isDark"
>
<IconSun v-if="isDark" aria-hidden="true" :size="18" />
<IconMoon v-else aria-hidden="true" :size="18" />
</button>
<DialogRoot v-model:open="mobile">
<DialogTrigger class="mobile-menu" aria-label="打开文档目录">
<IconMenu2 aria-hidden="true" :size="20" />
</DialogTrigger>
<DialogPortal>
<DialogOverlay class="mobile-nav-overlay" />
<DialogContent class="mobile-nav-dialog">
<div class="mobile-nav-heading">
<DialogTitle>文档目录</DialogTitle>
<DialogClose aria-label="关闭文档目录">
<IconX aria-hidden="true" :size="20" />
</DialogClose>
</div>
<nav aria-label="移动端文档导航">
<section v-for="group in groups" :key="group.title">
<h2>{{ group.title }}</h2>
<a
v-for="item in group.items"
:key="item.url"
:href="withBase(item.url)"
:aria-current="selected(item.url) ? 'page' : undefined"
@click="mobile = false"
>
{{ item.title }}
</a>
</section>
</nav>
</DialogContent>
</DialogPortal>
</DialogRoot>
<div class="nav-tools">
<Link
class="docs-github"
href="https://git.myjdemo.cn/maoyingjie/heroui-vue"
target="_blank"
rel="noreferrer noopener"
aria-label="项目源码"
>
<IconBrandGithub :size="19" aria-hidden="true" />
</Link>
<DocsThemeSwitch />
<Drawer v-model:open="mobile">
<DrawerTrigger as-child>
<Button
class="mobile-menu"
is-icon-only
variant="ghost"
size="sm"
aria-label="打开文档目录"
>
<IconMenu2 :size="20" aria-hidden="true" />
</Button>
</DrawerTrigger>
<DrawerBackdrop>
<DrawerContent placement="left" class="docs-drawer-content">
<DrawerDialog
class="docs-drawer-dialog"
@close-auto-focus="focusPage"
>
<DrawerHeader class="docs-drawer-heading">
<DrawerHeading>文档目录</DrawerHeading>
<DrawerCloseTrigger aria-label="关闭文档目录" />
</DrawerHeader>
<DrawerBody>
<nav class="mobile-section-links" aria-label="文档栏目">
<Link
:href="withBase('/introduction')"
:aria-current="section === 'guide' ? 'page' : undefined"
@click="leaveDrawer"
>
快速开始
</Link>
<Link
:href="withBase('/components')"
:aria-current="
section === 'components' ? 'page' : undefined
"
@click="leaveDrawer"
>
组件
</Link>
</nav>
<Separator />
<DocsSidebar
:groups="groups"
:current="current"
label="移动端文档导航"
@navigate="leaveDrawer"
/>
</DrawerBody>
</DrawerDialog>
</DrawerContent>
</DrawerBackdrop>
</Drawer>
</div>
</div>
<div class="docs-nav-sections">
<nav aria-label="主导航">
<Link
:href="withBase('/introduction')"
:aria-current="section === 'guide' ? 'page' : undefined"
>
<IconBook2 :size="16" aria-hidden="true" />
快速开始
</Link>
<Link
:href="withBase('/components')"
:aria-current="section === 'components' ? 'page' : undefined"
>
<IconComponents :size="16" aria-hidden="true" />
组件
</Link>
</nav>
<Chip class="framework" size="sm" variant="soft">
<IconBrandVue :size="14" aria-hidden="true" />
Vue 3
</Chip>
</div>
</header>
<div class="docs-body">
<aside class="docs-sidebar" aria-label="文档导航">
<div class="framework">
<span class="vue-mark">V</span>
Vue
<span class="framework-note">组件库</span>
</div>
<section v-for="group in groups" :key="group.title">
<h2>{{ group.title }}</h2>
<a
v-for="item in group.items"
:key="item.url"
:href="withBase(item.url)"
:class="{ selected: selected(item.url) }"
:aria-current="selected(item.url) ? 'page' : undefined"
>
{{ item.title }}
</a>
</section>
<p class="community">
独立社区实现
<br />
设计基线 HeroUI v3.2.6
</p>
<aside class="docs-sidebar" aria-label="侧栏">
<ScrollShadow
ref="sidebar"
class="docs-sidebar-scroll"
:hide-scroll-bar="false"
>
<DocsSidebar :groups="groups" :current="current" />
<p class="community">
独立社区实现
<br />
设计基线 HeroUI v3.2.6
</p>
</ScrollShadow>
</aside>
<main id="main" class="docs-main">
<div class="breadcrumb">
{{ frontmatter.group ?? "文档" }}
<span>›</span>
{{ frontmatter.title }}
</div>
<h1>{{ frontmatter.title }}</h1>
<p class="page-description">{{ frontmatter.description }}</p>
<div v-if="frontmatter.component" class="source-links">
<a
:href="`https://github.com/heroui-inc/heroui/tree/ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960/packages/react/src/components/${frontmatter.component}`"
<div v-if="headers.length" class="mobile-toc">
<Popover v-model:open="tocOpen" is-non-modal>
<PopoverTrigger as-child>
<Button variant="ghost" size="sm">
<IconList :size="16" aria-hidden="true" />
本页内容
<IconChevronDown :size="14" aria-hidden="true" />
</Button>
</PopoverTrigger>
<PopoverContent
placement="bottom start"
class="mobile-toc-content"
@close-auto-focus="finishTocNavigation"
>
<PopoverDialog>
<PopoverHeading class="sr-only">本页内容</PopoverHeading>
<DocsToc
:headers="headers"
:active="activeSlug"
@navigate="navigateToc"
/>
</PopoverDialog>
</PopoverContent>
</Popover>
</div>
<main id="main" class="docs-main" tabindex="-1">
<nav aria-label="面包屑">
<Breadcrumbs class="breadcrumb" aria-label="页面路径">
<BreadcrumbsItem
:href="
withBase(
section === 'components' ? '/components' : '/introduction',
)
"
>
{{ section === "components" ? "组件" : "快速开始" }}
</BreadcrumbsItem>
<BreadcrumbsItem>{{ frontmatter.title }}</BreadcrumbsItem>
</Breadcrumbs>
</nav>
<h1 tabindex="-1">{{ frontmatter.title }}</h1>
<p v-if="frontmatter.description" class="page-description">
{{ frontmatter.description }}
</p>
<div v-if="componentSlug" class="source-links">
<Link
:href="`https://github.com/heroui-inc/heroui/tree/ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960/packages/react/src/components/${componentSlug}`"
target="_blank"
rel="noreferrer"
rel="noreferrer noopener"
>
React 参考源码
<IconArrowUpRight aria-hidden="true" :size="12" />
</a>
<a
:href="`https://github.com/heroui-inc/heroui/blob/ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960/packages/styles/components/${frontmatter.component}.css`"
<IconArrowUpRight :size="12" aria-hidden="true" />
</Link>
<Link
:href="`https://github.com/heroui-inc/heroui/blob/ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960/packages/styles/components/${componentSlug}.css`"
target="_blank"
rel="noreferrer"
rel="noreferrer noopener"
>
样式源码
<IconArrowUpRight aria-hidden="true" :size="12" />
</a>
<IconArrowUpRight :size="12" aria-hidden="true" />
</Link>
</div>
<div @click="copyMarkdownCode"><Content class="docs-content" /></div>
<nav class="page-navigation" aria-label="相邻页面">
<a v-if="previous" :href="withBase(previous.url)">
<span>上一页</span>
<strong>{{ previous.title }}</strong>
</a>
<a v-if="next" class="page-next" :href="withBase(next.url)">
<span>下一页</span>
<strong>{{ next.title }}</strong>
</a>
<Card
v-if="adjacent.previous"
as="a"
:href="withBase(adjacent.previous.url)"
class="page-prev"
variant="transparent"
>
<IconArrowLeft :size="18" aria-hidden="true" />
<span>
上一页
<strong>{{ adjacent.previous.title }}</strong>
</span>
</Card>
<Card
v-if="adjacent.next"
as="a"
:href="withBase(adjacent.next.url)"
class="page-next"
variant="transparent"
>
<span>
下一页
<strong>{{ adjacent.next.title }}</strong>
</span>
<IconArrowRight :size="18" aria-hidden="true" />
</Card>
</nav>
<footer class="docs-footer">
基于 HeroUI v3 设计的 Vue 实现 ·
<a :href="withBase('/introduction')">实现范围与差异</a>
<Link :href="withBase('/introduction')">实现范围与差异</Link>
</footer>
</main>
<aside v-if="headers.length" class="docs-toc" aria-label="本页目录">
<p>本页内容</p>
<template v-for="h in headers" :key="h.slug">
<a
:href="'#' + h.slug"
:class="{ current: activeSlug === h.slug }"
:aria-current="activeSlug === h.slug ? 'location' : undefined"
>
{{ h.title }}
</a>
<a
v-for="child in h.children"
:key="child.slug"
class="nested"
:class="{ current: activeSlug === child.slug }"
:aria-current="activeSlug === child.slug ? 'location' : undefined"
:href="'#' + child.slug"
>
{{ child.title }}
</a>
</template>
<aside v-if="headers.length" class="docs-toc" aria-label="本页目录区域">
<p>
<IconList :size="16" aria-hidden="true" />
本页内容
</p>
<ScrollShadow class="docs-toc-scroll">
<DocsToc :headers="headers" :active="activeSlug" />
</ScrollShadow>
</aside>
</div>
</div>