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 #

Shell
npm install --save-dev @kawapress/plugin-vue-component-meta typescript

TypeScript 6 is a peer dependency. The native TypeScript 7 does not have the JavaScript API the checker needs yet.

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

Markdown
<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

NameTypeDefaultRequiredDescription
labelstring—Yes按钮文字。
size"medium" | "small" | "large""medium"—尺寸。Since 0.1.0
variant"solid" | "outline" | "ghost""solid"—外观。
disabledbooleanfalse—禁用后不响应点击。
countnumber0—显示在文字后的计数,0 时不显示。
roundedboolean——旧的圆角开关,请改用主题 Token。Deprecated: 0.2.0 起移除

Events

NamePayloadDescription
press[times: number]点击按钮时触发,参数是累计点击次数。

Slots

NameBindingsDescription
icon—文字前的图标。

Exposed

NameTypeDescription
focus() => void | undefined让按钮获得焦点。

Add only to render one table: props, events, slots or exposed.

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

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

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

TypeControl
booleanswitch
numbernumber input
stringtext input
literal union such as 'small' | 'large'options
object, array, string | number and similarJSON input
function, VNode, componentno control

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:

TypeScript
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 #

OptionDescription
tsconfigThe tsconfig used to check components, relative to the site root. By default, the nearest tsconfig.json above each component, or a built-in minimal config when there is none.
ignoreTypesType names that are not expanded, such as large DOM types; the table shows just the name.
tagThe tag name in Markdown. Default ComponentApi.
labels, localeLabelsOverride the table text. labels applies to every language, localeLabels per language.

Limits #

  • The first component analysis loads TypeScript: about 0.3 seconds and 300 MB of memory. Only pages that use ComponentApi or demo controls pay for it.
  • vue must be resolvable from the component's folder, since the types of defineProps and 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 src is resolved against the page path after rewrites.

Edit this pageLast updated:

Comments