Skip to content
webcomponents-previewPublic

About

A storybook alike preview application for web components.

Topics

Resources

Stars

6 stars

Watchers

2 watching

Forks

Latest commit

 

History

576 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WCP Logo Web Components Preview

Workflow status npm Release Deployed preview

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.

Usage

Generating a custom element manifest

We recommend to use the @custom-elements-manifest/analyzer package to generate a custom element manifest.

Generating readmes

Once you documented your components with jsdoc, readme files can be generated from it as well. See Component readmes below.

Configuration

Just add the following script tag to your html file:

via JsDelivr

<script type="module" src="https://cdn.jsdelivr.net/npm/@webcomponents-preview/client"></script>

via unpkg

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

Extras

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.

Examples from jsdoc

@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.

Component readmes

The wcp-stage-readme plugin shows the readme property of an element. There are two ways to fill it:

// 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.

Grouping

@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

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.

Plugins

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 as preview-tag-name attribute and their persisted state as data property. They can emit wcp-stage-plugin:data-change with a string to persist state in the url, and wcp-stage-plugin:availability-change with a boolean to show or hide their tab.
  • Preview plugins (PreviewPlugin) receive the preview-tag-name attribute and the element wrapping the preview as container property. They can emit wcp-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"]
}

Development

Use the Node version pinned in .node-version (e.g. via fnm or nvm), then:

npm ci
npm run dev

This 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.

Releases

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.

About

A storybook alike preview application for web components.

Topics

Resources

Stars

6 stars

Watchers

2 watching

Forks

Releases

Used by

Contributors

Languages