feat(docs): align documentation shell with HeroUI baseline
This commit is contained in:
@@ -1,16 +1,31 @@
|
||||
<script setup lang="ts">
|
||||
import { IconCode, IconCopy } from "@tabler/icons-vue";
|
||||
import { computed, ref, watch, onBeforeUnmount, useId } from "vue";
|
||||
import { IconCode, IconCopy, IconCheck } from "@tabler/icons-vue";
|
||||
import {
|
||||
computed,
|
||||
ref,
|
||||
watch,
|
||||
onBeforeUnmount,
|
||||
onMounted,
|
||||
useId,
|
||||
defineAsyncComponent,
|
||||
} from "vue";
|
||||
import { Button } from "@heroui-vue/components";
|
||||
import { highlights } from "virtual:docs-data";
|
||||
const props = defineProps<{
|
||||
name: string;
|
||||
align?: "center" | "start" | "end";
|
||||
isBgSolid?: boolean;
|
||||
hideCode?: boolean;
|
||||
minHeight?: string;
|
||||
description?: string;
|
||||
}>();
|
||||
const demos = import.meta.glob("../../demos/*.vue", {
|
||||
eager: true,
|
||||
import: "default",
|
||||
});
|
||||
const loaders = import.meta.glob("../../demos/*.vue", { import: "default" });
|
||||
const demos = Object.fromEntries(
|
||||
Object.entries(loaders).map(([name, loader]) => [
|
||||
name,
|
||||
defineAsyncComponent(loader as () => Promise<import("vue").Component>),
|
||||
]),
|
||||
);
|
||||
const sources = import.meta.glob("../../demos/*.vue", {
|
||||
eager: true,
|
||||
query: "?raw",
|
||||
@@ -24,20 +39,44 @@ const expanded = ref(false),
|
||||
copyStatus = ref("复制代码");
|
||||
const id = useId();
|
||||
const highlightedCode = ref("");
|
||||
watch([expanded, () => props.name], async ([open, name], _, onCleanup) => {
|
||||
let current = true;
|
||||
onCleanup(() => {
|
||||
current = false;
|
||||
});
|
||||
highlightedCode.value = "";
|
||||
if (open) {
|
||||
try {
|
||||
const result = await highlights[name]?.();
|
||||
if (current) highlightedCode.value = result?.default ?? "";
|
||||
} catch {
|
||||
// The plain source remains available if a highlight chunk cannot load.
|
||||
}
|
||||
const preview = ref<HTMLElement>();
|
||||
let generation = 0;
|
||||
let observer: IntersectionObserver | undefined;
|
||||
async function highlight() {
|
||||
const current = ++generation;
|
||||
try {
|
||||
const result = await highlights[props.name]?.();
|
||||
if (current === generation) highlightedCode.value = result?.default ?? "";
|
||||
} catch {
|
||||
/* Plain same-SFC source remains available. */
|
||||
}
|
||||
}
|
||||
watch(
|
||||
() => props.name,
|
||||
() => {
|
||||
highlightedCode.value = "";
|
||||
void highlight();
|
||||
},
|
||||
);
|
||||
watch(expanded, (value) => {
|
||||
if (value && !highlightedCode.value) void highlight();
|
||||
});
|
||||
onMounted(() => {
|
||||
if (!preview.value || props.hideCode) return;
|
||||
if (typeof IntersectionObserver === "undefined") {
|
||||
void highlight();
|
||||
return;
|
||||
}
|
||||
observer = new IntersectionObserver(
|
||||
(entries) => {
|
||||
if (entries.some((entry) => entry.isIntersecting)) {
|
||||
void highlight();
|
||||
observer?.disconnect();
|
||||
}
|
||||
},
|
||||
{ rootMargin: "200px" },
|
||||
);
|
||||
observer.observe(preview.value);
|
||||
});
|
||||
let timer: ReturnType<typeof setTimeout> | undefined;
|
||||
async function copy() {
|
||||
@@ -53,12 +92,18 @@ async function copy() {
|
||||
copyStatus.value = "复制失败,请手动选择代码";
|
||||
}
|
||||
}
|
||||
onBeforeUnmount(() => clearTimeout(timer));
|
||||
onBeforeUnmount(() => {
|
||||
clearTimeout(timer);
|
||||
generation++;
|
||||
observer?.disconnect();
|
||||
});
|
||||
</script>
|
||||
<template>
|
||||
<div class="component-preview-container" :data-name="name">
|
||||
<p v-if="description" class="preview-description">{{ description }}</p>
|
||||
<div
|
||||
class="preview not-prose"
|
||||
:class="{ 'preview-only': hideCode }"
|
||||
:style="{
|
||||
alignItems:
|
||||
align === 'start'
|
||||
@@ -69,50 +114,59 @@ onBeforeUnmount(() => clearTimeout(timer));
|
||||
background: isBgSolid ? 'var(--background)' : undefined,
|
||||
}"
|
||||
>
|
||||
<div class="demo-row">
|
||||
<div class="demo-row" :style="{ minHeight }">
|
||||
<component :is="component" v-if="component" />
|
||||
<p v-else role="alert">示例不存在:{{ name }}</p>
|
||||
</div>
|
||||
</div>
|
||||
<div class="code-section">
|
||||
<div class="code-toolbar">
|
||||
<span class="code-language">
|
||||
<span class="code-dot" aria-hidden="true" />
|
||||
Vue
|
||||
</span>
|
||||
<div>
|
||||
<button
|
||||
:aria-label="(expanded ? '收起' : '展开') + name + ' 示例代码'"
|
||||
:aria-controls="id"
|
||||
:aria-expanded="expanded"
|
||||
@click="expanded = !expanded"
|
||||
>
|
||||
<IconCode aria-hidden="true" :size="14" />
|
||||
{{ expanded ? "收起代码" : "展开代码" }}
|
||||
</button>
|
||||
<button :aria-label="'复制' + name + ' 示例代码'" @click="copy">
|
||||
<IconCopy aria-hidden="true" :size="14" />
|
||||
复制代码
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<p
|
||||
class="copy-feedback"
|
||||
:class="{ 'sr-only': copyStatus === '复制代码' }"
|
||||
role="status"
|
||||
<div
|
||||
v-if="!hideCode"
|
||||
ref="preview"
|
||||
class="code-section not-prose"
|
||||
:data-expanded="expanded"
|
||||
>
|
||||
<Button
|
||||
class="preview-copy"
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
is-icon-only
|
||||
:aria-label="'复制' + name + ' 示例代码'"
|
||||
@click="copy"
|
||||
>
|
||||
{{ copyStatus === "复制代码" ? "" : copyStatus }}
|
||||
</p>
|
||||
<IconCheck
|
||||
v-if="copyStatus === '已复制'"
|
||||
aria-hidden="true"
|
||||
:size="16"
|
||||
/>
|
||||
<IconCopy v-else aria-hidden="true" :size="16" />
|
||||
</Button>
|
||||
<div
|
||||
v-if="expanded"
|
||||
:id="id"
|
||||
class="highlighted-source"
|
||||
tabindex="0"
|
||||
:aria-hidden="!expanded || undefined"
|
||||
:tabindex="expanded ? 0 : undefined"
|
||||
:aria-label="name + ' 示例源码'"
|
||||
>
|
||||
<div v-if="highlightedCode" v-html="highlightedCode" />
|
||||
<pre v-else><code>{{ code }}</code></pre>
|
||||
<pre
|
||||
v-else
|
||||
><code>{{expanded ? code : code.split('\n').slice(0,5).join('\n')}}</code></pre>
|
||||
</div>
|
||||
<Button
|
||||
class="preview-expand"
|
||||
size="sm"
|
||||
variant="tertiary"
|
||||
:aria-label="(expanded ? '收起' : '展开') + name + ' 示例代码'"
|
||||
:aria-controls="id"
|
||||
:aria-expanded="expanded"
|
||||
@click="expanded = !expanded"
|
||||
>
|
||||
<IconCode :size="14" aria-hidden="true" />
|
||||
{{ expanded ? "收起代码" : "展开代码" }}
|
||||
</Button>
|
||||
<p class="sr-only" role="status">
|
||||
{{ copyStatus === "复制代码" ? "" : copyStatus }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user