Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@wc-toolkit/storybook-helpers

wc-toolkit14.2kMIT9.0.1TypeScript support: included

Helpers designed to make integrating Web Components with Storybook easier.

custom-elements, custom-elements-manifest, web-components, web components, components, cem, storybook, @wc-toolkit/storybook-helpers, storybook web components

readme

workbench with tools, html, css, javascript, and storybook logo

WC Toolkit Storybook Helpers

These helpers are designed to make integrating Web Components with Storybook easier.

There are a number of things that this helper library does to provide developers a better experience with Storybook and Web Components:

  1. Uses types to provide better controls
  2. Prevents name collisions when attributes, properties, slots, and CSS shadow parts share the same name
  3. Provides a template with bindings for attributes, properties, CSS custom properties, and CSS shadow parts.
  4. Provides two-way binding for controls and attributes in the template to help keep control values in sync with the component

Be sure to check out the official docs for more information on how to configure and use this.

Before You Install

  1. If you don't already have it installed, follow the installation steps in the Storybook docs for web components
npm create storybook@latest
  1. Load your custom elements manifest into Storybook in the .storybook/preview.js file:
// .storybook/preview.js
import { setCustomElementsManifest } from "@storybook/web-components-vite";
import manifest from "./path/to/custom-elements.json" with { type: "json" };

setCustomElementsManifest(manifest);
  1. Add the expanded controls to your config in the .storybook/preview.js file:
export const parameters = {
  ...
  controls: {
    expanded: true,
    ...
  },
}

Installation

Now that you have Storybook installed, you can install the helpers:

npm i -D @wc-toolkit/storybook-helpers

Next, if you have global configurations, set those in your Storybook config in the .storybook/preview.js file:

// preview.js
import { setStorybookHelpersConfig, type Options } from "@wc-toolkit/storybook-helpers";

const options: Options = {...}

setStorybookHelpersConfig(options);

Setup

Import the storybook helpers into your story and get the appropriate helpers by passing your element's tag name into the Storybook helper function. the function will return the helper data you can assign to the Storybook meta object.

// my-element.stories.ts
import type { Meta, StoryObj } from "@storybook/web-components-vite";
import { getStorybookHelpers } from "@wc-toolkit/storybook-helpers";

const { events, args, argTypes, template } =
  getStorybookHelpers<MyElement>("my-element");

const meta: Meta<MyElement> = {
  title: "Components/My Element",
  component: "my-element",
  args,
  argTypes,
  render: (args) => template(args),
  parameters: {
    actions: {
      handles: events,
    },
  },
};
export default meta;

The function returns the following:

  • events: an array of events that will be captured in the actions tab
  • args: this provides the default values for the component args
  • argTypes: an object tha configures the controls for the component based on the data from the Custom Elements Manifest
  • template: a function that will return a template for the component as well as provide two-way data binding for the component's API and the Storybook controls

Be sure to check out the official docs for more information on how to configure and use this.

changelog

@wc-toolkit/storybook-helpers

9.0.1

Patch Changes

  • c733b62: Resolved a slot template issue preventing non-text slots from rendering

9.0.0

Major Changes

  • 70578b4: Upgrade package to support Storybook v9

1.1.5

Patch Changes

  • 02c3c29: Fixed issue with querySelector when using component variable

1.1.4

Patch Changes

  • 89a26ed: Adjusts ArgTypes export to source from Storybook

1.1.3

Patch Changes

  • e8c4db0: Resolves an issue where non properties were throwing type errors (slots, cssprops, etc)
  • f7ac995: Fixes an incorrect import path in the documentation

1.1.2

Patch Changes

  • 00494f5: added readonly support for properties

1.1.1

Patch Changes

  • 1e0f97a: Add type parameter to getStorybookHelpers so that types match Meta<T> from Storybook.

1.1.0

Minor Changes

  • 30a24ef: Added ability to set component variable at the component level using setComponentVariable

Patch Changes

  • 30a24ef: Added parser and formatter for properties with an object default value

1.0.4

Patch Changes

  • b3d876f: Fexed error where hidden categories still display the default values in the args
  • b3d876f: Fixed error when omitting CSS Props from story
  • b3d876f: Fixed error where empty content is slotted when the slots category is hidden

1.0.3

Patch Changes

  • eb08aa0: Fixed issue where attributes are not removed when the default value is used

1.0.2

Patch Changes

  • 52ce9a7: Removed duplicate event entries in the control table

1.0.1

Patch Changes

  • 1eaa523: Fixed links in README to doc site
  • 1eaa523: Fixed CSS block rendering when content comes from controls
  • 1eaa523: Fixed slot rendering when content comes from controls

1.0.0

Major Changes

  • c76a7ca: Initial commit