Skip to content

Commit 5c4ffce

Browse files
committed
docs: Clarify Knobs and Stories API
1 parent 0a69163 commit 5c4ffce

File tree

1 file changed

+103
-23
lines changed

1 file changed

+103
-23
lines changed

README.md

Lines changed: 103 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# bs-storybook
22

3-
BuckleScript bindings for Storybook.js! The goal of this project is to provide bindings for the main Storybook API, as well as the official add-ons. Currently it supports:
3+
BuckleScript bindings for **[Storybook](https://storybook.js.org/)**.
4+
5+
The goal of this project is to provide bindings for the main Storybook API, as well as the official add-ons. Currently it supports:
46

57
* [actions](https://github.com/storybooks/storybook/tree/master/addons/actions)
68
* [knobs](https://github.com/storybooks/storybook/tree/master/addons/knobs)
@@ -16,9 +18,12 @@ npm install bs-storybook
1618

1719
Next, you'll need to add `bs-storybook` to your `bsconfig.json` as a dependency.
1820

19-
Then, get Storybook up and running according to [their docs](https://storybook.js.org/basics/quick-start-guide/). (_Note:_ This library does not attempt to provide a way to configure storybook in Reason - just use the standard JS configs.)
21+
Then, get Storybook up and running according to [their docs](https://storybook.js.org/basics/quick-start-guide/)
22+
23+
> **Note:** This library does not attempt to provide a way to configure storybook in Reason - just use the standard JS configs.
2024
21-
In your `/.storybook/config.js`, import your stories from wherever your compiled Reason modules end up. For example, if you're writing your stories inside a `__stories__` directory, and `bsb` is configured for a standard build, you might do something like:
25+
In your `/.storybook/config.js`, import your stories from wherever your compiled Reason modules end up.
26+
For example, if you're writing your stories inside a `__stories__` directory, and `bsb` is configured for a standard build, you might do something like:
2227

2328
```javascript
2429
const req = require.context('../lib/js', true, /\__stories__\/.*.js$/);
@@ -29,7 +34,17 @@ configure(() => {
2934
}, module);
3035
```
3136

32-
Note that in the above example, we're assuming the convention of each module containing a function as the `default` export. We'll account for that when writing our stories in the next section.
37+
or if you are using Storybook v6.
38+
39+
```javascript
40+
/* .storybook/main.js */
41+
module.exports = {
42+
stories: ['../stories/**/*.js'],
43+
addons: ['@storybook/addon-actions', '@storybook/addon-links', '@storybook/addon-knobs/register'],
44+
};
45+
```
46+
47+
Note that in the above example, we're assuming the convention of each module containing a function as the `default` export. bs-storybook is accountable for that while writting your stories:
3348

3449
## Writing a story
3550

@@ -41,32 +56,66 @@ open BsStorybook.Story;
4156
let _module = [%bs.raw "module"];
4257
4358
storiesOf("My First Reason Story", _module)
44-
|. addDecorator()
45-
|. add("first chapter", () =>
46-
<span> (ReasonReact.stringToElement("Hello bs-storybook!")) </span>
47-
);
59+
->add("Chapter I", () => <span> {React.string("Hello bs-storybook!")} </span>);
4860
```
4961

5062
Storybook uses a reference to the `module` global provided by webpack to facilitate hot-reloading. We'll access that via the `[%bs.raw]` decorator.
5163

5264
## The Actions Addon
5365

5466
The action addon's API is essentially unchanged from its JS implementation:
67+
> Make sure that you have @storybook/addon-actions in the config
5568
5669
```reason
5770
let clickAction = Action.action("I Clicked The Button!");
71+
72+
<div onClick={clickAction} />
5873
```
5974

6075
## The Knobs Addon
6176

62-
To use knobs, be sure to add the decorator to your story definition:
77+
To use [knobs](https://github.com/storybooks/storybook/tree/master/addons/knobs) you have twoo ways:
78+
> Make sure that you have @storybook/addon-knobs/register in the config
79+
80+
#### As a decorator
81+
82+
```reason
83+
open Storybook;
84+
open Story;
85+
86+
let _module = [%bs.raw "module"];
87+
88+
storiesOf("My First Reason Story", _module)
89+
->addDecorator(Knobs.withKnobs)
90+
->add("Chaper with Knobs", () => {
91+
let name = Knobs.text(~label="Name", ~defaultValue="Patrick", ());
92+
<span> {React.string(name)} </span>;
93+
})
94+
```
95+
96+
#### Creating the story
6397

6498
```reason
99+
open Storybook;
100+
open Story;
101+
102+
let _module = [%bs.raw "module"];
103+
65104
let knobsStory =
66-
createStory(~title="Hey look, knobs!", ~decorators=[Knobs.withKnobs], ~_module, ());
105+
Main.createStory(
106+
~title="Hey look, knobs!",
107+
~decorators=[Knobs.withKnobs],
108+
~_module,
109+
(),
110+
);
111+
112+
knobsStory.add("Chaper with Knobs", () => {
113+
let name = Knobs.text(~label="Name", ~defaultValue="Patrick", ());
114+
<span> {React.string(name)} </span>;
115+
})
67116
```
68117

69-
Each knob type is invoked using a function with labeled arguments, and each requires passing `unit` as the final argument. They all share a `~label` argument, and a `~defaultValue` argument (where appropriate);
118+
Each knob type is invoked using a function with labeled arguments, and each requires passing `unit` as the final argument. They all share a `~label` argument, and a `~defaultValue` argument (where appropriate):
70119

71120
### Text
72121

@@ -95,11 +144,11 @@ The number type works with floats. If no `defaultValue` is provided, it will pas
95144
```reason
96145
let num1 = Knobs.number(~label="Number 1", ());
97146
let num2 =
98-
Knobs.number(
147+
Knobs.number(
99148
~label="Number 2",
100-
~rangeConfiguration={min: 0., max: 10., step: 1.},
149+
~rangeConfiguration={range: true, min: 0., max: 10., step: 1.},
101150
()
102-
);
151+
);
103152
```
104153

105154
### Select
@@ -108,13 +157,13 @@ To use the select knob, first define a record type that contains the shape of th
108157

109158
```reason
110159
type selectOptions = {
111-
one: string,
112-
two: string
160+
one: string,
161+
two: string
113162
};
114163
115164
let options : Knobs.selectConfig(selectOptions) = {
116-
one: "Hello",
117-
two: "Hi"
165+
one: "Hello",
166+
two: "Hi"
118167
};
119168
```
120169

@@ -128,16 +177,47 @@ let greeting = Knobs.select(~label="Greeting", ~options, ~defaultValue=options.o
128177

129178
```reason
130179
Knobs.button(
131-
~label="Knob Button",
132-
~handler=Action.action("Clicked the knob button"),
133-
()
180+
~label="Knob Button",
181+
~handler=Action.action("Clicked the knob button"),
182+
()
134183
)
135184
```
136185

137186
### Object
138187

139-
Not yet implemented.
188+
```reason
189+
let obj = Knobs.object_(~label="User", ~defaultValue={"color": "grey"}, ());
190+
```
191+
192+
### Js.Dict
193+
> https://bucklescript.github.io/bucklescript/api/Js.Dict.html
194+
195+
```reason
196+
let options =
197+
Js.Dict.fromArray([|
198+
("Red", "red"),
199+
("Blue", "blue"),
200+
("Yellow", "yellow"),
201+
("None", ""),
202+
|]);
203+
204+
let color =
205+
Knobs.selectFromDict(
206+
~label="MySelection",
207+
~options,
208+
~defaultValue="red",
209+
(),
210+
);
211+
```
140212

141213
### Array
142214

143-
Not yet implemented.
215+
```reason
216+
let color =
217+
Knobs.selectFromArray(
218+
~label="MySelection",
219+
~options=[|"red", "blue", "yellow"|],
220+
~defaultValue="red",
221+
(),
222+
);
223+
```

0 commit comments

Comments
 (0)