Skip to content

Commit 3ab8f0c

Browse files
authored
Merge pull request #8 from draftbit/davesnx/Update-readme
2 parents 0a69163 + cb639f6 commit 3ab8f0c

File tree

13 files changed

+247
-59
lines changed

13 files changed

+247
-59
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = space
6+
end_of_line = lf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
indent_size = 2

README.md

Lines changed: 105 additions & 25 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(
99-
~label="Number 2",
100-
~rangeConfiguration={min: 0., max: 10., step: 1.},
101-
()
102-
);
147+
Knobs.number(
148+
~label="Number 2",
149+
~rangeConfiguration={range: true, min: 0., max: 10., step: 1.},
150+
()
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+
```

bsconfig.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
{
22
"name": "bs-storybook",
33
"namespace": true,
4-
"bsc-flags": ["-bs-super-errors"],
4+
"bsc-flags": [
5+
"-bs-super-errors"
6+
],
57
"version": "0.1.0",
6-
"sources": ["src"],
7-
"bs-dependencies": ["reason-react"],
8-
"reason": { "react-jsx": 2 },
8+
"sources": [
9+
"src"
10+
],
11+
"bs-dependencies": [
12+
"reason-react"
13+
],
14+
"reason": {
15+
"react-jsx": 2
16+
},
917
"refmt": 3
1018
}

example/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
src/*.js
3+
lib
4+
yarn.lock

example/bsconfig.json

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
{
22
"name": "storybook-test",
33
"namespace": false,
4-
"reason": { "react-jsx": 3 },
4+
"reason": {
5+
"react-jsx": 3
6+
},
57
"refmt": 3,
6-
"bs-dependencies": ["reason-react"],
8+
"bs-dependencies": [
9+
"reason-react"
10+
],
711
"ppx-flags": [],
812
"sources": [
9-
{ "dir": "src", "subdirs": true },
10-
{ "dir": "bindings", "subdirs": true },
11-
{ "dir": "stories", "subdirs": true }
13+
{
14+
"dir": "src",
15+
"subdirs": true
16+
},
17+
{
18+
"dir": "bindings",
19+
"subdirs": true
20+
},
21+
{
22+
"dir": "stories",
23+
"subdirs": true
24+
}
1225
],
1326
"package-specs": {
1427
"module": "es6",
1528
"in-source": true
1629
},
1730
"suffix": ".bs.js",
18-
"bsc-flags": ["-bs-no-version-header", "-bs-super-errors", "-bs-g"]
31+
"bsc-flags": [
32+
"-bs-no-version-header",
33+
"-bs-super-errors",
34+
"-bs-g"
35+
]
1936
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"author": "",
2121
"license": "MIT",
2222
"devDependencies": {
23-
"bs-platform": "^7.0.1"
23+
"bs-platform": "^7.0.1",
24+
"reason-react": "^0.7.1"
2425
},
2526
"peerDependencies": {
2627
"reason-react": "^0.7.1",

src/action.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
type actionHandler('a) = 'a => unit;
22

33
[@bs.val] [@bs.module "@storybook/addon-actions"]
4-
external action : string => actionHandler('a) = "";
4+
external action: string => actionHandler('a);

src/addons.re

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ type channelListener = string => unit;
1111
type panelConfig = {
1212
.
1313
"title": string,
14-
"render": unit => ReasonReact.reactElement
14+
"render": unit => ReasonReact.reactElement,
1515
};
1616

17-
[@bs.module "@storybook/addons"] external addons : t = "default";
17+
[@bs.module "@storybook/addons"] external addons: t = "default";
1818

19-
[@bs.send] external register : (t, string, callback) => unit = "register";
19+
[@bs.send] external register: (t, string, callback) => unit = "register";
2020

21-
[@bs.send] external addPanel : (t, string, panelConfig) => unit = "addPanel";
21+
[@bs.send] external addPanel: (t, string, panelConfig) => unit = "addPanel";
2222

23-
[@bs.send] external getChannel : (t, unit) => channel = "getChannel";
23+
[@bs.send] external getChannel: (t, unit) => channel = "getChannel";
2424

25-
[@bs.send] external emitChannel : (channel, string, string) => unit = "emit";
25+
[@bs.send] external emitChannel: (channel, string, string) => unit = "emit";
2626

2727
[@bs.send]
28-
external onChannel : (channel, string, channelListener) => unit = "on";
28+
external onChannel: (channel, string, channelListener) => unit = "on";
2929

3030
[@bs.send]
31-
external removeChannelListener : (channel, string, channelListener) => unit =
31+
external removeChannelListener: (channel, string, channelListener) => unit =
3232
"removeListener";
3333

34-
[@bs.send] external onStory : (api, unit => unit) => unit = "onStory";
34+
[@bs.send] external onStory: (api, unit => unit) => unit = "onStory";

src/knobs.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[@bs.val] [@bs.module "@storybook/addon-knobs/react"]
2-
external withKnobs: Main.decorator = "";
2+
external withKnobs: Main.decorator;
33

44
[@bs.val] [@bs.module "@storybook/addon-knobs/react"]
55
external extText: (string, Js.null_undefined(string)) => string = "text";

src/main.re

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ type chapter = unit => ReasonReact.reactElement;
33
type section;
44

55
[@bs.val] [@bs.module "@storybook/react"]
6-
external storiesOf : (string, 'a) => section =
7-
"";
6+
external storiesOf: (string, 'a) => section;
87

9-
[@bs.send] external extAdd : (section, string, chapter) => unit = "add";
8+
[@bs.send] external extAdd: (section, string, chapter) => unit = "add";
109

1110
type decorator = chapter => ReasonReact.reactElement;
1211

13-
[@bs.send] external addDecorator : (section, decorator) => unit = "";
12+
[@bs.send] external addDecorator: (section, decorator) => unit;
1413

1514
type webpackModule;
1615

@@ -19,9 +18,14 @@ type chapterAdd = (string, chapter) => unit;
1918
type story = {add: chapterAdd};
2019

2120
let createStory =
22-
(~title: string, ~decorators: list(decorator), ~_module: webpackModule, ())
21+
(
22+
~title: string,
23+
~decorators: list(decorator),
24+
~_module: webpackModule,
25+
(),
26+
)
2327
: story => {
2428
let story = storiesOf(title, _module);
25-
List.iter((dec) => addDecorator(story, dec), decorators);
26-
{add: (name: string, c: chapter) => extAdd(story, name, c)}
29+
List.iter(dec => addDecorator(story, dec), decorators);
30+
{add: (name: string, c: chapter) => extAdd(story, name, c)};
2731
};

0 commit comments

Comments
 (0)