Files
heroui-vue/apps/docs/.vitepress/theme/Layout.vue
T

315 lines
10 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import {
IconSun,
IconMoon,
IconMenu2,
IconX,
IconArrowUpRight,
} from "@tabler/icons-vue";
import {
computed,
ref,
watch,
nextTick,
onMounted,
onBeforeUnmount,
} from "vue";
import { useData, useRoute, withBase, onContentUpdated } from "vitepress";
import {
DialogRoot,
DialogTrigger,
DialogPortal,
DialogOverlay,
DialogContent,
DialogTitle,
DialogClose,
} from "reka-ui";
import DocSearch from "./DocSearch.vue";
const { frontmatter, page, isDark } = 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: "Alert 警告", url: "/components/alert" },
{ title: "Avatar 头像", url: "/components/avatar" },
{ title: "AvatarGroup 头像组", url: "/components/avatar-group" },
{ title: "Badge 徽章", url: "/components/badge" },
{ title: "Button 按钮", url: "/components/button" },
{ title: "Card 卡片", url: "/components/card" },
{ title: "Chip 标签", url: "/components/chip" },
{ title: "CloseButton 关闭按钮", url: "/components/close-button" },
{ title: "Input 输入框", url: "/components/input" },
{ title: "InputGroup 输入框组", url: "/components/input-group" },
{ title: "Kbd 键盘按键", url: "/components/kbd" },
{ title: "Link 链接", url: "/components/link" },
{ 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" },
],
},
];
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 headers = computed(() => page.value.headers ?? []);
const flatHeaders = computed(() =>
headers.value.flatMap((h) => [h, ...(h.children ?? [])]),
);
let ticking = false;
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;
}
activeSlug.value = active;
}
function onScroll() {
if (!ticking) {
ticking = true;
requestAnimationFrame(updateActive);
}
}
onMounted(() => {
window.addEventListener("scroll", onScroll, { passive: true });
updateActive();
});
onBeforeUnmount(() => window.removeEventListener("scroll", onScroll));
onContentUpdated(() => {
void nextTick(updateActive);
});
watch(
() => route.path,
() => {
mobile.value = false;
void nextTick(() => {
if (!window.location.hash)
window.scrollTo({ top: 0, behavior: "instant" });
updateActive();
});
},
);
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;
try {
await navigator.clipboard.writeText(code);
target.classList.add("copied");
target.setAttribute("aria-label", "已复制");
copyMessage.value = "代码已复制";
clearTimeout(copyTimer);
copyTimer = setTimeout(() => {
target.classList.remove("copied");
target.setAttribute("aria-label", "复制代码");
copyMessage.value = "";
}, 2000);
} catch {
target.setAttribute("aria-label", "复制失败,请手动选择代码");
copyMessage.value = "复制失败,请手动选择代码";
}
}
</script>
<template>
<div class="docs-layout">
<a class="skip" href="#main">跳至内容</a>
<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">
<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>
</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>
<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}`"
target="_blank"
rel="noreferrer"
>
React 参考源码
<IconArrowUpRight aria-hidden="true" :size="12" />
</a>
<a
:href="`https://github.com/heroui-inc/heroui/blob/ce90ea3ab49d58f6c428f71bfd6fe8ae2a87b960/packages/styles/components/${frontmatter.component}.css`"
target="_blank"
rel="noreferrer"
>
样式源码
<IconArrowUpRight aria-hidden="true" :size="12" />
</a>
</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>
</nav>
<footer class="docs-footer">
基于 HeroUI v3 设计的 Vue 实现 ·
<a :href="withBase('/introduction')">实现范围与差异</a>
</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>
</div>
</div>
</template>