npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@storybook/addon-devkit

v1.4.2

Published

Storybook Addon Development Kit

Downloads

108,272

Readme

npm version npm

Storybook ADK

Some of features originally introduced in this package are already available via Storybook API. Please consider https://github.com/storybookjs/addon-kit first, which is a simple Github repo template that uses the latest addon APIs.

This kit provides additional middleware for Storybook API and can be used for creating addons based on this.

Simplifies the addons creation. Keeps in sync addon's data through the channel. Provides intelligent blocks for creating addon UI. Offer simple API for registering addons and creating decorators. It's a base to quickly build your custom brand new awesome addon

Features

  • Hides under the hood all the complex issues of communication through the channel and data synchronization while switching stories.
  • Connects your addon components to your addon store via HOCs and updates it only when data changes
  • Divides addon store data to global and local. Tracks story surfing in order to switch appropriate local data both on manager and preview sides simultaneously
  • Keeps immutable init data and overridable data which you mutate via actions
  • Provides redux like approach to deal with your addon store via selectors and actions (but don't worry, the default action just simply override your data)
  • Allows to connect any amount of pannels, buttons and any other addon types to the single addon store
  • Offers UI container which automatically reflects the aspect ratio of addon panel. Extremely useful to create addon UI responsive for vertical and horizontal panel positions
  • Includes Typescript definitions

Usage

npm i --save @storybook/addon-devkit
import {
  register,
  createDecorator,
  setParameters,
  setConfig,
  Layout,
  Block,
} from '@storybook/addon-devkit'

API

Register manager side Addon panel

HOC to register addon UI and connect it to the addon store.

// in your addon `register.js`
import { register } from '@storybook/addon-devkit'

register(
  {
    ...selectors,
  },
  ({ global, local }) => ({
    ...globalActions,
    ...localActions,
  })
)(AddonPanelUI);

where selectors is an object with functions like:

{
  deepData: store => store.path.to.deep.store.data,
}

and actions could be "global" and "local". Global actions affects on the global part of store, while local only on the data related to the current story.


({ global, local }) => ({
    // action to manipulate with common data
    increase: global(store => ({
      ...store,
      index: store.index + 1,
    })),
    // action to manipulate with current story data
    // usage: setBackground('#ff66cc')
    setBackground: local((store, color) => ({
      ...store,
      backgroundColor: color,
    })),
    // action to override data
    // usage: update({...newData})
    update: global(),
  })

AddonPanelUI - is your component which appears on addon panel when you select appropriate tab

Note: the HOC automatically track the active state of addon and shows it only when it's necessary

register HOC will pass the follow props to the AddonPanelUI component:

<AddonPanelUI
  {...actions} // generated actions
  {...selectors} // selected pieces of store
  api={api} // storybook API object
  active={active} // you don't need to do anything with it
  store={store} // entire store. prefer to use selectors
  kind={kind} // current story kind
  story={story} // current story
  ADDON_ID={ADDON_ID}
  PANEL_ID={PANEL_ID}
  PANEL_Title={PANEL_Title} // Title on the addon panel
  rect={rect} // dimensions of panel area
/>

As soon as you change the store via actions both the AddonPanelUI and storyDecorator will be re-rendered with the new data.

Same if the data will come from the story - it will be updated

After initialization HOC will wait for init data from story and only after it will render UI

Create stories side decorator

HOC to create decorator and connect it to the addon store.

// in your addon `decorator.js`
import { createDecorator } from '@storybook/addon-devkit'

export const withMyAddon = createDecorator({
    ...selectors,
  },
  ({ global, local }) => ({
    ...globalActions,
    ...localActions,
  })
)(DecoratorUI, { isGlobal });

so then you can use your decorator this way:

// stories.js

import React from 'react';
import { storiesOf, addDecorator, addParameters } from '@storybook/react';
import { withMyAddon, myAddonParams } from 'my-addon';

// add decorator globally
addDecorator(withMyAddon({ ...initData }))
addParameters(myAddonParams({ ...globalParams }))

storiesOf('My UI Kit', module)
  // ...or add decorator locally
  .addDecorator(withMyAddon({ ...initData }))
  .add(
    'Awesome',
    () => <Button>Make Awesome</Button>,
    myAddonParams({ ...localParams })
  )

DecoratorUI could look like this:


const DecoratorUI = ({ context, getStory, selectedData }) => (
  <div>
    <h1>Title: {selectedData}</h1>
    {getStory(context)}
  </div>
);

When isGlobal = true decorator will consider all passing data as global

Note: addon parameters will be merged with init data and available both for decorator and panel selectors

Pass parameters to addon

Creates functions for passing parameters to your addon

See usage above

import { setParameters } from '@storybook/addon-devkit'

export const myAddonParams = setParameters()

Addon config

In order to create addon you need to specify some unique parameters like event name, addon title, parameters key and others. They should be same on manager and preview sides. If you don't specify them addon-devkit will use the default ones. To specify your own use setConfig:

import { setConfig } from '@storybook/addon-devkit';

setConfig({
  addId: 'dev_adk',
  panelTitle: 'ADK DEV'
});

You should run it before using register, setParameters and createDecorator

Note: don't forget to use setConfig both in on manager and preview sides with the same parameters

Addon panel UI components

Components to organize UI in a row when panel in bottom position and in column when it on the right side

import { Layout, Block, register } from '@storybook/addon-devkit';
import { styled } from '@storybook/theming';
import './config'

const LayoutBlock = styled(Layout)`
  ...styles
`

const AddonBlock = styled(Block)`
  ...styles
`

const AddonPanel = () => (
  <LayoutBlock>
    <AddonBlock size={200}>
      {UI1}
    </AddonBlock>
    <AddonBlock>
      {UI2}
    </AddonBlock>
    <AddonBlock>
      {UI3}
    </AddonBlock>
  </LayoutBlock>
)

register()(AddonPanel)

<Layout> has display: flex with flex-direction: row when bottom and flex-direction: column in right side.

You can specify the size of <Block>. In case of horizontal layout it will be the width, in case of vertical - height of element.

Otherwise it will have flex-grow: 1

Credits