Skip to content

Commit 8d90f7e

Browse files
committed
Update README
1 parent 1583db7 commit 8d90f7e

File tree

5 files changed

+224
-43
lines changed

5 files changed

+224
-43
lines changed

README.md

Lines changed: 216 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,252 @@
11
# Storybook Addon Development Kit
22

3-
Keeps in sync addon's data through the channel
3+
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
4+
5+
## Features
6+
7+
- Hides under the hood all the complex issues of communication through the channel and data synchronization while switching stories.
8+
- Connects your addon components to your addon store via HOCs and updates it only when data changes
9+
- 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
10+
- Keeps immutable init data and overridable data which you mutate via actions
11+
- 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)
12+
- Allows to connect any amount of pannels, buttons and any other addon types to the single addon store
13+
- 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
14+
15+
## Usage
16+
17+
```shell
18+
npm i --save @storybook/addon-devkit
19+
```
20+
21+
```js
22+
import {
23+
register,
24+
createDecorator,
25+
setParameters,
26+
setConfig,
27+
Layout,
28+
Block,
29+
} from '@storybook/addon-devkit'
30+
31+
```
432

533
## API
634

7-
### Addons Panel (Manager)
35+
### Register manager side Addon panel
36+
37+
HOC to register addon UI and connect it to the addon store.
38+
39+
```js
40+
// in your addon `register.js`
41+
import { register } from '@storybook/addon-devkit'
42+
43+
register(
44+
{
45+
...selectors,
46+
},
47+
({ global, local }) => ({
48+
...globalActions,
49+
...localActions,
50+
})
51+
)(AddonPanelUI);
52+
53+
54+
```
55+
56+
where `selectors` is an object on functions like:
57+
58+
```js
59+
{
60+
deepData: store => store.path.to.deep.store.data,
61+
}
62+
63+
```
64+
65+
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.
66+
67+
```js
68+
69+
({ global, local }) => ({
70+
// action to manipulate with common data
71+
increase: global(store => ({
72+
...store,
73+
index: store.index + 1,
74+
})),
75+
// action to manipulate with current story data
76+
// usage: setBackground('#ff66cc')
77+
setBackground: local((store, color) => ({
78+
...store,
79+
backgroundColor: color,
80+
})),
81+
// action to override data
82+
// usage: update({...newData})
83+
update: global(),
84+
})
85+
86+
```
87+
88+
AddonPanelUI - is your component which appears on addon panel when you select appropriate tab
89+
> Note: the HOC automatically track the `active` state of addon and shows it only when it's necessary
90+
91+
register HOC will pass the follow props to the `AddonPanelUI` component:
892

9-
**register(PanelComponent)**
93+
```js
94+
<AddonPanelUI
95+
{...actions} // generated actions
96+
{...selectors} // selected pieces of store
97+
api={api} // storybook API object
98+
active={active} // you don't need to do anything with it
99+
store={store} // entire store. prefer to use selectors
100+
kind={kind} // current story kind
101+
story={story} // current story
102+
ADDON_ID={ADDON_ID}
103+
PANEL_ID={PANEL_ID}
104+
PANEL_Title={PANEL_Title} // Title on the addon panel
105+
rect={rect} // dimensions of panel area
106+
/>
107+
108+
```
109+
110+
As soon as you change the store via actions both the `AddonPanelUI` and `storyDecorator` will be re-rendered with the new data.
10111

11-
HOC that adds your PanelComponent to the addons panel. It will register it and provides follow props:
112+
Same if the data will come from the story - it will be updated
12113

13-
- **kind**, a selected story kind name
14-
- **story**, a selected story name
15-
- **api**, manager API
16-
- **data**, current channel data (associated with selected story)
17-
- **setData**, callback to set data
18-
- **rect**, geometric dimensions of the addon area,
19-
- **Layout**, Helper Component with `display: flex` to make addon's markup to fit in portrait or landscape mode by switching `flex-direction`. Could be used alone or together with Blocks.
20-
- **Block**, Helper Block to use inside Layout and provides flex items behavior in portrait and landscape mode
114+
After initialization HOC will wait for init data from story and only after it will render UI
21115

22-
>see how works Layout and Blocks in the Live Demo
23116

24-
example:
117+
### Create stories side decorator
118+
119+
HOC to create decorator and connect it to the addon store.
25120

26121
```js
27-
// your addon register.js
122+
// in your addon `decorator.js`
123+
import { createDecorator } from '@storybook/addon-devkit'
124+
125+
export const withMyAddon = createDecorator({
126+
...selectors,
127+
},
128+
({ global, local }) => ({
129+
...globalActions,
130+
...localActions,
131+
})
132+
)(DecoratorUI, { isGlobal });
133+
134+
```
135+
136+
so then you can use your decorator this way:
137+
138+
```js
139+
// stories.js
28140

29141
import React from 'react';
30-
import { register } from '../dist/register';
142+
import { storiesOf, addDecorator, addParameters } from '@storybook/react';
143+
import { withMyAddon, myAddonParams } from 'my-addon';
144+
145+
// add decorator globally
146+
addDecorator(withMyAddon({ ...initData }))
147+
addParameters(myAddonParams({ ...globalParams }))
148+
149+
storiesOf('My UI Kit', module)
150+
// ...or add decorator locally
151+
.addDecorator(withMyAddon({ ...initData }))
152+
.add(
153+
'Awesome',
154+
() => <Button>Make Awesome</Button>,
155+
myAddonParams({ ...localParams })
156+
)
157+
158+
```
159+
160+
`DecoratorUI` could look like this:
161+
162+
```js
31163

32-
const AddonPanel = ({ api, data, setData, kind, story }) => (
164+
const DecoratorUI = ({ context, getStory, selectedData }) => (
33165
<div>
34-
<p>kind: {kind}</p>
35-
<p>story: {story}</p>
36-
<p>data ({JSON.stringify(data)})</p>
37-
<button onClick={() => setData({ foo: 'bar' })}>setData</button>
166+
<h1>Title: {selectedData}</h1>
167+
{getStory(context)}
38168
</div>
39169
);
40-
41-
register(AddonPanel);
42170
```
43171

44-
Then users can add your addon to Storybook like this:
172+
When `isGlobal = true` decorator will consider all passing data as global
173+
174+
>Note: addon parameters will be merged with init data and available both for decorator and panel selectors
175+
176+
177+
### Pass parameters to addon
178+
179+
Creates functions for passing parameters to your addon
180+
See usage above
45181

46182
```js
47-
// user's .storybook/addons.js
183+
import { setParameters } from '@storybook/addon-devkit'
184+
185+
export const myAddonParams = setParameters()
48186

49-
import 'your-addon/register';
50187
```
51188

52-
## Develop
189+
### Addon config
190+
191+
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.
192+
To specify your own use `setConfig`:
53193

54-
scripts:
194+
```js
195+
import { setConfig } from '@storybook/addon-devkit';
196+
197+
setConfig({
198+
addId: 'dev_adk',
199+
panelTitle: 'ADK DEV'
200+
});
201+
202+
```
203+
You should run it **before** using `register`, `setParameters` and `createDecorator`
55204

56-
`yarn start` - compiles everything, starts Storybook and watches for changes
205+
>Note: don't remember to use setConfig both in on manager and preview sides with the same parameters
57206
58-
`yarn prepare` - compiles `src` to the `dist` folder by babel
59207

60-
`yarn prepare-dev` - compiles `dev` to the `dev-dist` folder by babel
208+
### Addon panel UI components
61209

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

63-
folders:
212+
```js
213+
import { Layout, Block, register } from '@storybook/addon-devkit';
214+
import { styled } from '@storybook/theming';
215+
import './config'
216+
217+
const LayoutBlock = styled(Layout)`
218+
...styles
219+
`
220+
221+
const AddonBlock = styled(Block)`
222+
...styles
223+
`
224+
225+
const AddonPanel = () => (
226+
<LayoutBlock>
227+
<AddonBlock size={200}>
228+
{UI1}
229+
</AddonBlock>
230+
<AddonBlock>
231+
{UI2}
232+
</AddonBlock>
233+
<AddonBlock>
234+
{UI3}
235+
</AddonBlock>
236+
</LayoutBlock>
237+
)
238+
239+
register()(AddonPanel)
240+
241+
```
64242

65-
`src` - source of the addon
243+
<Layout> has `display: flex` with `flex-direction: row` when bottom and `flex-direction: column` in right side.
66244

67-
`dev` - addon usage
245+
You can specify the size of <Block>. In case of horizontal layout it will be the width, in case of vertical - height of element.
68246

247+
Otherwise it will have `flex-grow: 1`
69248

70-
how to develop:
249+
## Credits
71250

72-
`yarn start` and edit files in `src` folder. To test API see `dev` folder.
251+
<div align="left" style="height: 16px;">Created with ❤︎ to <b>React</b> and <b>Storybook</b> by <a href="https://twitter.com/UsulPro">@usulpro</a> [<a href="https://github.com/react-theming">React Theming</a>]
252+
</div>

dev/withAdk.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from 'react';
22
import { createDecorator, setParameters } from '../src/decorator';
3-
import './config'
3+
import './config';
44

5-
const DecoratorUI = ({ context, getStory, data, parameters, theme }) => (
5+
const DecoratorUI = ({ context, getStory, theme }) => (
66
<div>
77
Theme: {theme} <br />
88
{getStory(context)}
99
</div>
1010
);
1111

1212
export const withAdk = createDecorator({
13-
theme: store => store.themes[store.currentTheme]
14-
})(DecoratorUI, {isGlobal: true});
15-
export const adkParams = setParameters()
13+
theme: store => store.themes[store.currentTheme],
14+
})(DecoratorUI, { isGlobal: true });
15+
export const adkParams = setParameters();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@storybook/addon-devkit",
3-
"version": "1.0.0-alpha.1",
3+
"version": "1.0.0",
44
"description": "Storybook Addon Development Kit",
55
"author": {
66
"name": "Oleg Proskurin",

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './config';
12
export * from './register';
23
export * from './decorator';
34
export * from './Layout';

src/register.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class PanelHOC extends React.Component {
9494
{...this.props.selectors}
9595
api={api}
9696
active={active}
97-
data={data}
97+
store={data}
9898
setData={setData}
9999
kind={kind}
100100
story={story}

0 commit comments

Comments
 (0)