Skip to content

Instantly share code, notes, and snippets.

@tmeasday
Last active January 29, 2019 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmeasday/1ff87d0c76f85e645cbfced60bcb6011 to your computer and use it in GitHub Desktop.
Save tmeasday/1ff87d0c76f85e645cbfced60bcb6011 to your computer and use it in GitHub Desktop.
SB v5 API
// The idea is to maintain the `api.X` contract that allows us to change the internal store format without releasing a breaking change
import React from 'react';
import { addons, types } from '@storybook/addons';
import { ADDON_ID, PANEL_ID } from './constants';
addons.register(ADDON_ID, api => {
addons.add(PANEL_ID, {
type: types.PANEL,
title: 'My Addon',
// A function from api calls to props for render. This allows us to be reactive on state changes
// but maintain performance by only considering props that we care about (ala mapStateToProps)
renderProps: () => ({
const storyId = api.storyId();
return {
storyId,
parameters: api.getParameters(storyId),
currentVersion: api.getCurrentVersion(),
};
}),
render: ({ active, storyId, parameters, currentVersion }) => (
<h1>Current story = {storyId}</h1>
),
});
// If an addon needs to directly access state (for instance to use our persistence layer),
// it can register a module
addons.addModule(({ store }) => ({
// This is the same API as core modules use
return ({
api: {
myAddonSomething() {
/* ... */
},
},
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment