feat: initialize Vue component library and aligned documentation
This commit is contained in:
@@ -0,0 +1,300 @@
|
||||
<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: "组件",
|
||||
items: [
|
||||
{ title: "Button 按钮", url: "/components/button" },
|
||||
{ title: "Card 卡片", url: "/components/card" },
|
||||
{ title: "Input 输入框", url: "/components/input" },
|
||||
{ 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>
|
||||
Reference in New Issue
Block a user