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 #

Shell
npm install --save-dev @kawapress/plugin-vue-component-demo
TypeScript
import 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:

Vue
<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:

Markdown
<ComponentDemo
  src="../../components/examples/demos/ButtonSizes.vue"
  title="Sizes"
  description="Buttons come in `small`, `medium` and `large`."
/>

The result:

Sizes
Buttons come in small, medium and large.

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:

Counter

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:

Markdown
<ComponentDemo
  component="../../components/examples/KButton.vue"
  props='{"label":"Submit"}'
/>
Controls
按钮文字。
size
尺寸。
variant
外观。
禁用后不响应点击。
显示在文字后的计数,0 时不显示。
旧的圆角开关,请改用主题 Token。
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:xxx event, as with v-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 #

AttributeDescription
srcThe demo file, relative to the current Markdown file, or a Vite alias such as @/demos/Basic.vue.
componentA component file, shown as a playground with controls. Needs the Component API plugin; use either this or src.
propsWith component: the initial values, as a JSON object.
titleTitle, with inline Markdown.
descriptionDescription, with inline Markdown.
client-onlyRender the demo in the browser only, see below.

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.

Markdown
<ComponentDemo src="../../components/examples/demos/ViewportWidth.vue" client-only />

Loading demo…

Viewport width

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 #

OptionDescription
tagThe tag name in Markdown. Default ComponentDemo.
expandSourceStart with the source expanded. Default false.
labels, localeLabelsOverride the button text. labels applies to every language, localeLabels per language.

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 src is resolved from the page's source file.

Edit this pageLast updated:

Comments