A storybook alike preview application for web components. It processes a given custom element manifest and renders a preview for each element with its properties and events.
Configuration is done by providing a schema file of your custom elements. The schema is a JSON file that describes the custom elements and their properties and should comply to the draft Custom element manifest specification.
We recommend to use the @custom-elements-manifest/analyzer package to generate a custom element manifest.
Once you documented your components with jsdoc, readme files can be generated from it as well. See Component readmes below.
Just add the following script tag to your html file:
<script type="module" src="https://cdn.jsdelivr.net/npm/@webcomponents-preview/client"></script><script type="module" src="https://unpkg.com/@webcomponents-preview/client"></script>Then add the following markup to your html and pass in the manifest url:
<wcp-root manifest-url="/custom-elements.json"></wcp-root>The preview reads a few additional properties from the manifest. They are added by plugins for the @custom-elements-manifest/analyzer, published from webcomponents-preview/cem-plugins.
@webcomponents-preview/cem-plugin-examples collects every @example jsdoc tag of a component class, plus an EXAMPLES.md file next to the component, into an examples property. The wcp-stage-examples plugin renders each of them with a code and a preview tab.
// custom-elements-manifest.config.js
import { customElementExamplesPlugin } from '@webcomponents-preview/cem-plugin-examples';
export default {
globs: ['src/**/*.component.ts'],
plugins: [customElementExamplesPlugin()],
};/**
* A button.
*
* @example
* ## Primary
*
* ```html
* <my-button variant="primary">Save</my-button>
* ```
*/
@customElement('my-button')
export class MyButton extends LitElement {}The examples file name can be changed with the exampleFileName option, either as a string or as a callback (componentFileName, componentClassName) => string.
The wcp-stage-readme plugin shows the readme property of an element. There are two ways to fill it:
- Generated by
@webcomponents-preview/cem-plugin-generate-readmes: writes aREADME.mdper component from its code, using theweb-component-analyzer(transformer: 'wca') or@custom-elements-manifest/to-markdown(transformer: 'cem'). Everything above an<!-- Auto Generated Below -->comment in an existing readme is kept, so a handwritten intro survives. WithaddInlineReadme: truethe result is also added to the manifest. - Existing readmes are added by
@webcomponents-preview/cem-plugin-inline-readme, which puts whateverloadReadmereturns into the manifest.
// custom-elements-manifest.config.js
import { existsSync, readFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { customElementGenerateReadmesPlugin } from '@webcomponents-preview/cem-plugin-generate-readmes';
import { customElementInlineReadmePlugin } from '@webcomponents-preview/cem-plugin-inline-readme';
// either generate the readmes ...
customElementGenerateReadmesPlugin({
transformer: 'wca',
addInlineReadme: true,
outputPath: (path = '') => resolve(dirname(path), 'README.md'),
});
// ... or inline existing ones
customElementInlineReadmePlugin({
loadReadme(path = '') {
const readme = resolve(dirname(path), 'README.md');
return existsSync(readme) ? readFileSync(readme, 'utf-8') : '';
},
});Readmes not belonging to an element, e.g. guides, are listed in the navigation by the additionalReadmes setting (see below), grouped under labels.additionalReadmeGroupName.
@webcomponents-preview/cem-plugin-grouping assigns elements to navigation groups. Its addGroups(componentPath) callback returns the group names, e.g. derived from the folder structure. Elements without a group are listed under labels.fallbackGroupName.
Settings are read from a JSON file passed as config-url (defaults to config.json):
<wcp-root config-url="config.json" manifest-url="custom-elements.json"></wcp-root>{
"$schema": "./node_modules/@webcomponents-preview/client/dist/config.schema.json",
"excludeElements": ["my-internal-element"],
"statePersistence": "local",
"labels": { "title": "My Components" },
"additionalReadmes": [{ "name": "Getting started", "url": "docs/getting-started.md" }]
}All settings are optional; the schema documents each of them and its default.
The preview is extended by plugins. Each plugin is a custom element, listed by its tag name in one of three settings:
| Setting | Placement | Defaults |
|---|---|---|
stagePlugins |
tabs on the page of an element | wcp-stage-examples, wcp-stage-readme, wcp-stage-editor |
previewPlugins |
toolbar of each preview | wcp-preview-simulate-viewports, wcp-preview-editor-link, wcp-preview-maximize |
topbarPlugins |
top bar of the application | wcp-toggle-sidebar, wcp-toggle-color-scheme, wcp-topbar-preview-editor-link-toggle |
A setting replaces its defaults, so list the built-in plugins you want to keep along with your own.
To add a plugin, define a custom element before the preview renders and implement the Plugin interface: a name, a label and an available flag. Depending on the placement, the preview passes in more:
- Stage plugins (
StagePlugin) receive the tag name of the shown element aspreview-tag-nameattribute and their persisted state asdataproperty. They can emitwcp-stage-plugin:data-changewith a string to persist state in the url, andwcp-stage-plugin:availability-changewith a boolean to show or hide their tab. - Preview plugins (
PreviewPlugin) receive thepreview-tag-nameattribute and the element wrapping the preview ascontainerproperty. They can emitwcp-preview-plugin:availability-change(bubbling, composed). - Topbar plugins (
TopbarPlugin) receive nothing.
import type { PreviewPlugin } from '@webcomponents-preview/client';
import { html, LitElement } from 'lit';
import { customElement } from 'lit/decorators.js';
@customElement('my-preview-outline')
export class PreviewOutline extends LitElement implements PreviewPlugin {
readonly name = 'outline';
readonly label = 'Outline';
readonly available = true;
container?: HTMLElement;
protected override render() {
return html` <button @click="${() => this.container?.classList.toggle('outline')}">Outline</button> `;
}
}{
"previewPlugins": ["wcp-preview-simulate-viewports", "wcp-preview-maximize", "my-preview-outline"]
}Use the Node version pinned in .node-version (e.g. via fnm or nvm), then:
npm ci
npm run devThis generates the sources, analyzes the components and serves the preview of this repository at http://127.0.0.1:8087/, rebuilding and reloading on changes.
| Script | Purpose |
|---|---|
npm run dev |
Generate, then build, analyze and serve in watch mode. |
npm run build |
Clean, generate and bundle the production build into dist. |
npm run generate |
Barrel src/index.ts, src/config.schema.json from the Config type, manifest and component readmes. |
npm test |
Unit tests with the web test runner in headless Chrome; npm run test:watch to keep it running. |
npm run lint |
ESLint with @enke.dev/lint. |
src/index.ts, src/config.schema.json and the component readmes below <!-- Auto Generated Below --> are generated, so changes there are overwritten.
Commits follow conventional commits. Each push to main runs the checks and lets release-it bump the version, update the changelog and tag the release. The tag publishes the package to npm and deploys the preview to GitHub Pages. Pull requests get a preview of their own.