Component Demo #
Almost every page of a component library's docs is "result + source": first how the component looks, then how to write it. Demo code written straight into Markdown gets no type hints in the editor and cannot be reused elsewhere.
@kawapress/plugin-vue-component-demo lets you write each demo as a standalone .vue file, renders it for real on the page, and shows its source below, highlighted like the site's code blocks. Demos go through server rendering like any page content; they are not iframes and not an online editor.
Install #
npm install --save-dev @kawapress/plugin-vue-component-demoimport vueComponentDemoPlugin from '@kawapress/plugin-vue-component-demo'
import { nagi } from 'kawapress/nagi'
export default nagi({
plugins: [vueComponentDemoPlugin()],
})Show a demo #
A demo is an ordinary Vue single-file component:
<script setup lang="ts">
import KButton from '../KButton.vue'
</script>
<template>
<div class="row">
<KButton label="Small" size="small" />
<KButton label="Medium" />
<KButton label="Large" size="large" />
</div>
</template>Reference it from Markdown with the ComponentDemo tag:
<ComponentDemo
src="../../components/examples/demos/ButtonSizes.vue"
title="Sizes"
description="Buttons come in `small`, `medium` and `large`."
/>The result:
small, medium and large.<script setup lang="ts">
import KButton from '../KButton.vue'
</script>
<template>
<div class="row">
<KButton label="小" size="small" />
<KButton label="中" />
<KButton label="大" size="large" />
</div>
</template>
<style scoped>
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 0.75rem;
}
</style>The source starts collapsed: Show source reveals it and Copy source copies the whole file. A demo is a real component, so state and events work as usual:
<script setup lang="ts">
import { ref } from 'vue'
import KButton from '../KButton.vue'
const presses = ref(0)
</script>
<template>
<KButton label="点我" variant="outline" :count="presses" @press="presses = $event" />
</template>Props controls #
With the Component API plugin also installed, swap src for component and point it at the component itself to get a playground with controls:
<ComponentDemo
component="../../components/examples/KButton.vue"
props='{"label":"Submit"}'
/>Events0No events yet
<KButton label="Submit" />- Each control is inferred by the Component API plugin from the prop's type: a switch for booleans, a number input for numbers, options for literal unions, a JSON input for objects and arrays. Props that cannot be edited, such as functions, are left out.
- The source snippet below the playground follows the controls, lists only props that differ from their defaults, and can be copied.
- Events the component emits go to the event log, which keeps the latest 20.
- When the component changes its own prop through an
update:xxxevent, as withv-model, the controls follow. - Reset restores the initial values.
Initial values come from the props attribute, then from the prop defaults; otherwise the prop is not passed. Give required props without a default a value in props, which is a JSON object.
The playground shows the component itself, so the snippet always matches the result. To show a particular combination, write a demo file and use src.
Attributes #
Render in the browser only #
Demos are also rendered on the server. A demo that uses browser APIs such as window or document needs client-only: the server outputs a placeholder and the demo renders after the page loads.
<ComponentDemo src="../../components/examples/demos/ViewportWidth.vue" client-only />Loading demo…
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue'
// 读取 window:只能在浏览器里渲染,所以文档里加了 client-only。
const width = ref(window.innerWidth)
function update(): void {
width.value = window.innerWidth
}
window.addEventListener('resize', update)
onBeforeUnmount(() => window.removeEventListener('resize', update))
</script>
<template>
<p>当前窗口宽度:{{ width }}px</p>
</template>While editing #
In the dev server, editing a demo file hot-updates both the rendered demo and the expanded source, without a page reload and keeping the demo's state.
Output for AI tools #
In the page .md and in llms-full.txt, each demo becomes its title, description and full source as a code block, without the rendered result.
Options #
Notes #
- A demo is an ordinary component on the page with no style isolation. Keep a demo's own styles
scoped. - The tag must be a paragraph of its own, not in the middle of text.
- A relative
srcis resolved from the page's source file.


Comments