You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Implement the new addon API.
* Tested addon and decorator API with a real app.
* Add context support to decorators.
* Add kind to the api.
* Add extension documentation.
* Update extension docs.
* Fix a small typo.
React Storybook comes with an extensions API to customize the storybook experience. Let's have a look at them.
4
+
5
+
## TOC
6
+
7
+
*[API](#api)
8
+
*[Decorators](#decorators)
9
+
*[Addons](#addons)
10
+
*[Available Extensions](#available-extensions)
11
+
12
+
## API
13
+
14
+
### Decorators
15
+
16
+
A decorator is a way to wrap an story with a common set of component(s). Let's say you want to center all your stories. Then this is how we can do it with a decorator:
17
+
18
+
```js
19
+
importReactfrom'react';
20
+
import { storiesOf } from'@kadira/storybook';
21
+
importMyComponentfrom'../my_component';
22
+
23
+
storiesOf('MyComponent', module)
24
+
.addDecorator((story) => (
25
+
<div style={{textAlign:'center'}}>
26
+
{story()}
27
+
</div>
28
+
));
29
+
.add('without props', () => (<MyComponent />))
30
+
.add('with some props', () => (<MyComponent text="The Comp"/>));
31
+
```
32
+
33
+
Here we only add the decorator for the current set of stories for a given story kind.
34
+
35
+
But, you can add a decorator **globally** and it'll be applied to all the stories you create. This is how to add a decorator like that.
With an addon, you can introduce new methods to the story creation API. For an example, you can achieve the above centered component functionality with an addon like this:
Copy file name to clipboardExpand all lines: docs/writing_stories.md
+7-46Lines changed: 7 additions & 46 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,8 +4,8 @@
4
4
5
5
*[Basic API](#basic-api)
6
6
*[Creating Actions](#creating-actions)
7
-
*[Using Decorators](#using-decorators)
8
7
*[Linking Stories](#linking-stories)
8
+
*[Use Extensions](#use-extensions)
9
9
10
10
You need to write stories to show your components inside React Storybook. We've a set of APIs allows you to write stories and do more with them:
11
11
@@ -76,51 +76,6 @@ Here we can see the name we've mentioned when creating the action. After that, w
76
76
77
77
> For simplicity, React Storybook does not show the actual object. Instead it will show `[SyntheticEvent]`.
78
78
79
-
## Using Decorators
80
-
81
-
In some apps, we need to wrap our components with a given context. Most of the time, you have to do this when you are using Material UI or Radium.
82
-
83
-
So, you need to write your stories like this:
84
-
85
-
```js
86
-
importReactfrom'react';
87
-
import { storiesOf } from'@kadira/storybook';
88
-
importThemefrom'../theme';
89
-
importMyComponentfrom'../my_component';
90
-
91
-
storiesOf('MyComponent', modules)
92
-
.add('without props', () => (
93
-
<Theme>
94
-
<MyComponent />
95
-
</Theme>
96
-
))
97
-
.add('with some props', () => (
98
-
<Theme>
99
-
<MyComponent name="Arunoda"/>
100
-
</Theme>
101
-
));
102
-
```
103
-
104
-
As you can see, you always need to wrap your components with the `Theme` component. But, there's a much better way. See following example with a decorator:
105
-
106
-
```js
107
-
importReactfrom'react';
108
-
import { storiesOf } from'@kadira/storybook';
109
-
importThemefrom'../theme';
110
-
importMyComponentfrom'../my_component';
111
-
112
-
storiesOf('MyComponent', modules)
113
-
.addDecorator((story) => (
114
-
<Theme>
115
-
{story()}
116
-
</Theme>
117
-
))
118
-
.add('without props', () => (<MyComponent />))
119
-
.add('with some props', () => (<MyComponent name="Arunoda"/>));
120
-
```
121
-
122
-
You can add as many as decorators you want, but make sure to call `.addDecorator()` before you call `.add()`.
123
-
124
79
## Linking Stories
125
80
126
81
Sometimes, we may need to link stories. With that, we could use Storybook as a prototype builder. (like [InVision](https://www.invisionapp.com/), [Framer.js](http://framerjs.com/)). Here's how to do that.
@@ -153,3 +108,9 @@ With that, you can link an event prop to any story in the Storybook.
153
108
> You can also pass a function instead for any of above parameter. That function accepts arguments emitted by the event and it should return a string.
154
109
155
110
Have a look at [PR86](https://github.com/kadirahq/react-storybook/pull/86) for more information.
111
+
112
+
## Use Extensions
113
+
114
+
You can use [React Storybook Extensions](extensions.md) to group common functionalities and reduce the amount of code you need to write. You can also [re-use extensions](extensions.md#available-extensions) created by others.
115
+
116
+
Have a look at [React Storybook Extensions](extensions.md) for more information.
0 commit comments