153 lines
3.6 KiB
Vue
153 lines
3.6 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref, type Component } from 'vue'
|
|
|
|
const props = defineProps<{
|
|
/** 示例路径,相对 docs/examples 且不含 .vue,例如 "button/basic" */
|
|
name: string
|
|
}>()
|
|
|
|
/**
|
|
* 收集所有示例。glob 只认相对路径与项目根绝对路径,这里用相对路径,
|
|
* key 归一化成 "button/basic" 形式。
|
|
*/
|
|
const modules = import.meta.glob('../../../examples/**/*.vue', { eager: true }) as Record<
|
|
string,
|
|
{ default: Component }
|
|
>
|
|
const sources = import.meta.glob('../../../examples/**/*.vue', {
|
|
query: '?raw',
|
|
import: 'default',
|
|
eager: true,
|
|
}) as Record<string, string>
|
|
|
|
function normalize(path: string): string {
|
|
return path.replace(/^.*\/examples\//, '').replace(/\.vue$/, '')
|
|
}
|
|
|
|
const registry: Record<string, Component> = {}
|
|
for (const [path, mod] of Object.entries(modules)) {
|
|
registry[normalize(path)] = mod.default
|
|
}
|
|
|
|
const sourceMap: Record<string, string> = {}
|
|
for (const [path, raw] of Object.entries(sources)) {
|
|
sourceMap[normalize(path)] = raw
|
|
}
|
|
|
|
const demo = computed(() => registry[props.name])
|
|
const source = computed(() => sourceMap[props.name] ?? '')
|
|
|
|
const showCode = ref(false)
|
|
const copied = ref(false)
|
|
|
|
async function copyCode() {
|
|
if (!source.value) return
|
|
try {
|
|
await navigator.clipboard.writeText(source.value)
|
|
copied.value = true
|
|
window.setTimeout(() => {
|
|
copied.value = false
|
|
}, 1500)
|
|
} catch {
|
|
// 非安全上下文(http)下剪贴板不可用,静默忽略
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="g3-demo">
|
|
<div class="g3-demo__preview">
|
|
<component :is="demo" v-if="demo" />
|
|
<p v-else class="g3-demo__missing">未找到示例 docs/examples/{{ name }}.vue</p>
|
|
</div>
|
|
|
|
<div class="g3-demo__toolbar">
|
|
<button type="button" class="g3-demo__action" @click="showCode = !showCode">
|
|
{{ showCode ? '收起代码' : '查看代码' }}
|
|
</button>
|
|
<button type="button" class="g3-demo__action" @click="copyCode">
|
|
{{ copied ? '已复制' : '复制代码' }}
|
|
</button>
|
|
</div>
|
|
|
|
<div v-show="showCode" class="g3-demo__code">
|
|
<pre><code>{{ source }}</code></pre>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<style>
|
|
/**
|
|
* 示例卡片样式随组件自身提供,不依赖文档站的全局样式。
|
|
* 颜色优先取 Nuxt UI 的设计令牌(--ui-*),未定义时退回中性值。
|
|
*/
|
|
.g3-demo {
|
|
margin: 16px 0;
|
|
border: 1px solid var(--ui-border, #e5e7eb);
|
|
border-radius: 10px;
|
|
background: var(--ui-bg, #fff);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.g3-demo__preview {
|
|
display: flex;
|
|
flex-wrap: wrap;
|
|
gap: 12px;
|
|
align-items: center;
|
|
padding: 28px 20px;
|
|
}
|
|
|
|
.g3-demo__missing {
|
|
margin: 0;
|
|
font-size: 13px;
|
|
color: #ef4444;
|
|
}
|
|
|
|
.g3-demo__toolbar {
|
|
display: flex;
|
|
gap: 4px;
|
|
justify-content: flex-end;
|
|
padding: 6px 12px;
|
|
border-top: 1px solid var(--ui-border, #e5e7eb);
|
|
background: var(--ui-bg-muted, #f9fafb);
|
|
}
|
|
|
|
.g3-demo__action {
|
|
padding: 4px 10px;
|
|
border: 0;
|
|
border-radius: 6px;
|
|
background: transparent;
|
|
color: var(--ui-text-muted, #6b7280);
|
|
font-family: inherit;
|
|
font-size: 12px;
|
|
cursor: pointer;
|
|
transition:
|
|
background 0.2s,
|
|
color 0.2s;
|
|
}
|
|
|
|
.g3-demo__action:hover {
|
|
background: var(--ui-bg-elevated, rgba(0, 0, 0, 0.04));
|
|
color: var(--ui-primary, #1677ff);
|
|
}
|
|
|
|
.g3-demo__code {
|
|
border-top: 1px solid var(--ui-border, #e5e7eb);
|
|
background: var(--ui-bg-muted, #f9fafb);
|
|
max-height: 420px;
|
|
overflow: auto;
|
|
}
|
|
|
|
.g3-demo__code pre {
|
|
margin: 0;
|
|
padding: 16px 20px;
|
|
}
|
|
|
|
.g3-demo__code code {
|
|
font-family: var(--g3-font-family-mono, ui-monospace, monospace);
|
|
font-size: 13px;
|
|
line-height: 1.7;
|
|
white-space: pre;
|
|
}
|
|
</style>
|