ComponentExample
Usage
ComponentExample is used to embed interactive component examples in your documentation. It automatically loads the component and shows a live preview alongside the source code.
Basic usage
Use the :component-example directive in your Markdown documentation:
<script setup lang="ts">
import type { AccordionItem } from '@nuxt/ui'
const items = [
{
label: 'Icons',
icon: 'i-lucide-smile',
content: 'You have nothing to do, @nuxt/icon will handle it automatically.'
},
{
label: 'Colors',
icon: 'i-lucide-swatch-book',
slot: 'colors' as const,
content: 'Choose a primary and a neutral color from your Tailwind CSS theme.'
},
{
label: 'Components',
icon: 'i-lucide-box',
content: 'You can customize components by using the `class` / `ui` props or in your app.config.ts.'
}
] satisfies AccordionItem[]
</script>
<template>
<UAccordion :items="items">
<template #colors="{ item }">
<p class="text-sm pb-3.5 text-primary">
{{ item.content }}
</p>
</template>
</UAccordion>
</template>
:component-example{name="AccordionExample"}
Advanced configuration
::component-example
---
name: AccordionExample
highlights: [10, 15, 20]
collapse: true
---
::
Client-only rendering
If the example uses browser APIs such as window, document, or setInterval directly, you can add client-only="true" so the preview component renders only on the client, avoiding warnings during the SSR phase:
:component-example{name="AccordionExample" client-only="true"}
API
Props
| Prop | Default | Type |
|---|---|---|
name | string | |
iframe | false | boolean | { [key: string]: any; }是否在 iframe 中渲染组件 |
props | { [key: string]: any; } | |
collapse | false | boolean | { icon?: string; name?: string; openText?: string; closeText?: string; open?: boolean; }是否折叠代码块 |
options | { type?: string; alias?: string; name: string; label: string; items?: any[]; default: any; multiple?: boolean; }[]链接到组件的可变属性列表 | |
highlights | number[]代码块中需要高亮的行号列表 | |
lang | 'vue' | string |
filename | string覆盖用于代码块的文件名 | |
iframeMobile | false | boolean是否在移动端尺寸的 iframe 视口中显示组件 |
prettier | false | boolean是否使用 Prettier 格式化代码 |
preview | true | boolean是否显示预览
当设置为 |
source | true | boolean是否显示源代码 |
overflowHidden | boolean是否在包装器上添加 overflow-hidden | |
clientOnly | false | boolean是否只在客户端渲染预览组件,适用于依赖 window / setInterval 等浏览器 API 的示例 |
elevated | boolean是否添加 background-elevated 到 wrapper |
Slots
| Slot | Type |
|---|---|
options | {} |
code | {} |
ComponentExampleExtras
Create app/components/content/ComponentExampleExtras.vue in the consumer repository to overlay additional UI such as ThemeVisualizer or a Playground button inside the example container, without forking ComponentExample. The layer provides an empty implementation by default.
Available props:
| Prop | Type | Description |
|---|---|---|
name | string | The raw value of the MDC name="...", i.e. the example filename |
camelName | string | The camelCase form of the example name |
pascalName | string | The PascalCase form of the example name, from useFetchComponentExample |
effectiveProps | Record<string, any> | The currently effective merged props (including options) |
options | Array<{ name, label, ... }> | undefined | The options configuration, passed through as-is |
wrapperContainer | HTMLElement | null | The outermost wrapper, used for popover / highlight box positioning |
componentContainer | HTMLElement | null | The actual render container of the example component, used for DOM scanning such as querySelector('[data-slot]') |
Example: overlaying ThemeVisualizer
Following Nuxt UI's ComponentThemeVisualizer.vue, implement a LazyComponentThemeVisualizer, then wire it up in the override file.
<script setup lang="ts">
import { camelCase } from 'scule'
defineProps<{
wrapperContainer: HTMLElement | null
componentContainer: HTMLElement | null
}>()
const route = useRoute()
const slug = computed(() => camelCase(route.path.split('/').pop() ?? ''))
</script>
<template>
<LazyComponentThemeVisualizer
:slug="slug"
:container="componentContainer"
:position-container="wrapperContainer"
/>
</template>
Changelog
ComponentEmits
ComponentEmits automatically extracts a Vue component's defineEmits definitions via nuxt-component-meta, generating an Emits documentation table in your Markdown that includes event names, parameter types, and descriptions, with no need to write event API documentation by hand. Use it together with ComponentProps and ComponentSlots.
ComponentCode
ComponentCode lets authors drive the live rendering of any registered Vue component from a declarative property matrix in Markdown, while synthesizing a copy-paste Vue snippet in sync.