Component API #
In a component library's docs, the API tables take the most effort and go stale the fastest: a component gains a prop and the table is forgotten.
@kawapress/plugin-vue-component-meta reads component source at build time with vue-component-meta and renders props, events, slots and exposed members as tables. It works through the TypeScript type checker, so imported types, withDefaults values and JSDoc all come through. When the component changes, so do the tables.
Install #
npm install --save-dev @kawapress/plugin-vue-component-meta typescriptTypeScript 6 is a peer dependency. The native TypeScript 7 does not have the JavaScript API the checker needs yet.
import vueComponentMetaPlugin from '@kawapress/plugin-vue-component-meta'
import { nagi } from 'kawapress/nagi'
export default nagi({
plugins: [vueComponentMetaPlugin()],
})Render API tables #
Write a ComponentApi tag in Markdown, with src pointing to the component file:
<ComponentApi src="../../components/examples/KButton.vue" />src is relative to the current Markdown file, or a Vite alias such as @/components/Button.vue. Here are the tables of an example button on this site:
Props
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
label | string | — | Yes | 按钮文字。 |
size | "medium" | "small" | "large" | "medium" | — | 尺寸。Since 0.1.0 |
variant | "solid" | "outline" | "ghost" | "solid" | — | 外观。 |
disabled | boolean | false | — | 禁用后不响应点击。 |
count | number | 0 | — | 显示在文字后的计数,0 时不显示。 |
rounded | boolean | — | — | 旧的圆角开关,请改用主题 Token。Deprecated: 0.2.0 起移除 |
Events
| Name | Payload | Description |
|---|---|---|
press | [times: number] | 点击按钮时触发,参数是累计点击次数。 |
Slots
| Name | Bindings | Description |
|---|---|---|
icon | — | 文字前的图标。 |
Exposed
| Name | Type | Description |
|---|---|---|
focus | () => void | undefined | 让按钮获得焦点。 |
Add only to render one table: props, events, slots or exposed.
<ComponentApi src="../../components/examples/KButton.vue" only="events" />Empty tables are left out.
Describe with JSDoc #
Descriptions come from JSDoc on the component's types:
export interface KButtonProps {
/**
* Size.
* @since 0.1.0
*/
size?: 'small' | 'medium' | 'large'
/**
* The old rounded switch; use the theme tokens instead.
* @deprecated removed in 0.2.0
*/
rounded?: boolean
}@deprecated: strikes through the name and shows the note.@since: shows the version note.@ignore: keeps the prop out of the table and out of the demo controls.@control: picks the control in the demo controls, see Control inference.
Write event descriptions right above each event in the defineEmits type; both the property form and the call-signature form work:
const emit = defineEmits<{
/** Fired on click, with the number of presses so far. */
press: [times: number]
}>()Control inference #
Each prop gets a control from its type. The controls of Component Demo use it to build their inputs:
The JSDoc tags @control text, number, boolean, json or none override the inferred control.
Use the data elsewhere #
Other plugins and page components can import a component's description directly:
import type { ComponentDoc } from '@kawapress/plugin-vue-component-meta'
import doc from './Button.vue?component-doc'The default export of ?component-doc is a ComponentDoc with name, description, props, events, slots and exposed. It is generated as static JSON at build time, and the type checker never reaches the browser.
Output for AI tools #
In the page .md and in llms-full.txt, ComponentApi becomes Markdown tables with the same content.
Options #
Limits #
- The first component analysis loads TypeScript: about 0.3 seconds and 300 MB of memory. Only pages that use
ComponentApior demo controls pay for it. vuemust be resolvable from the component's folder, since the types ofdefinePropsand the other macros come from it. Otherwise the tables are empty and the build warns.- When the checker cannot read the props of a generic component (
<script setup generic="T">), the table shows a notice and the build warns. - A relative
srcis resolved against the page path afterrewrites.


Comments