Socket
Book a DemoInstallSign in
Socket

storybook-addon-selector

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

storybook-addon-selector

A storybook addon to have selector for your preview

5.3.9-4
latest
Source
npmnpm
Version published
Maintainers
1
Created
Source

Story Selector Addon

The Storybook selector addon can be used to select and share config between stories in Storybook.

Framework Support same as backgrounds addon

Installation

yarn add --dev storybook-addon-selector

or

npm i -D storybook-addon-selector

Configuration

Then create a file called main.js in your storybook config.

Add following content to it:

module.exports = {
  addons: ['storybook-addon-selector/register'],
};

Usage

Then write your stories like this:

import React from 'react';

export default {
  title: 'Button',
  parameters: {
    selector: [
      {
        name: 'unique-name1',
        icon: 'globe',
        title: 'title',
        options: [
          { value: 'value1', title: 'option1', default: true },
          { value: 'value2', title: 'option2' },
        ],
      },
      {
        name: 'unique-name2',
        icon: 'icon-name',
        title: 'title',
        options: [
          { value: 'value1', title: 'option1', default: true },
          { value: 'value2', title: 'option2' },
        ],
      },
    ],
  },
};

export const defaultView = () => <button>Click me</button>;

Then you can listen to addon channel for selected option

addons.getChannel().on('storybook/selector/unique-name/rendered', data => {
  console.log(data); // { value: 'value1', title: 'option1', default: true }
  forceReRender();
});
addons.getChannel().on('storybook/selector/unique-name/update', data => {
  console.log(data); // { value: 'value2', title: 'option2' }
});
addons.getChannel().on('storybook/selector/unique-name/destroying', () => {
  // do some codes
});

You can add the selectors to all stories with addParameters in .storybook/preview.js:

import { addParameters } from '@storybook/react'; // <- or your storybook framework

addParameters({
  selector: [
    {
      name: 'unique-name',
      icon: 'globe',
      title: 'title',
      options: [
        { value: 'value1', title: 'option1', default: true },
        { value: 'value2', title: 'option2' },
      ],
    },
  ],
});

If you want to override selectors for a single story or group of stories, pass the selector parameter:

import React from 'react';

export default {
  title: 'Button',
};

export const defaultView = () => <button>Click me</button>;
defaultView.story = {
  parameters: {
    selector: [
      {
        name: 'unique-name',
        icon: 'globe',
        title: 'title',
        options: [
          { value: 'value1', title: 'option1', default: true },
          { value: 'value2', title: 'option2' },
        ],
      },
    ],
  },
};

If you don't want to use selectors for a story, you can set the selector parameter to [] to skip the addon:

import React from 'react';

export default {
  title: 'Button',
};

export const noSelectors = () => <button>Click me</button>;
noSelectors.story = {
  parameters: {
    selector: [],
  },
};

FAQs

Package last updated on 27 Jan 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.