Skip to content

Commit

Permalink
Merge pull request #3244 from sixmen/app-mithril-3.4.0-rc.3
Browse files Browse the repository at this point in the history
App for Mithril
  • Loading branch information
Hypnosphi authored Mar 28, 2018
2 parents 108af01 + 36379bb commit 239edfb
Show file tree
Hide file tree
Showing 89 changed files with 2,446 additions and 23 deletions.
11 changes: 11 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
- node_modules
- examples/angular-cli/node_modules
- examples/cra-kitchen-sink/node_modules
- examples/mithril-kitchen-sink/node_modules
- examples/official-storybook/node_modules
- examples/polymer-cli/node_modules
- examples/vue-kitchen-sink/node_modules
Expand Down Expand Up @@ -100,6 +101,11 @@ jobs:
command: |
cd examples/official-storybook
yarn build-storybook
- run:
name: "Build mithril kitchen-sink"
command: |
cd examples/mithril-kitchen-sink
yarn build-storybook
- run:
name: "Visually test storybook"
command: |
Expand Down Expand Up @@ -148,6 +154,11 @@ jobs:
command: |
cd examples/official-storybook
yarn storybook --smoke-test
- run:
name: "Run mithril kitchen-sink (smoke test)"
command: |
cd examples/mithril-kitchen-sink
yarn storybook --smoke-test
react-native:
<<: *defaults
steps:
Expand Down
1 change: 1 addition & 0 deletions .github/autolabeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
'app: react-native': ["app/react-native/**"]
'app: react': ["app/react/**"]
'app: vue': ["app/vue/**"]
'app: mithril': ["app/mithril/**"]
'babel / webpack': ["webpack", "babel"]
'cli': ["lib/cli/**"]
'compatibility with other tools': []
Expand Down
34 changes: 17 additions & 17 deletions ADDONS_SUPPORT.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
## Addon / Framework Support Table

| |[React](app/react)|[React Native](app/react-native)|[Vue](app/vue)|[Angular](app/angular)| [Polymer](app/polymer)|
| ----------- |:-------:|:-------:|:-------:|:-------:|:-------:|
|[a11y](addons/a11y) |+| | | | |
|[actions](addons/actions) |+|+|+|+|+|
|[background](addons/background) |+| | | | |
|[centered](addons/centered) |+| |+| | |
|[events](addons/events) |+| | | | |
|[graphql](addons/graphql) |+| | | | |
|[info](addons/info) |+| | | | |
|[jest](addons/jest) |+| | | | |
|[knobs](addons/knobs) |+|+|+|+|+|
|[links](addons/links) |+|+|+|+|+|
|[notes](addons/notes) |+| |+|+|+|
|[options](addons/options) |+|+|+|+|+|
|[storyshots](addons/storyshots) |+|+|+|+| |
|[storysource](addons/storysource)|+| |+|+|+|
|[viewport](addons/viewport) |+| |+|+|+|
| |[React](app/react)|[React Native](app/react-native)|[Vue](app/vue)|[Angular](app/angular)| [Polymer](app/polymer)| [Mithril](app/mithril)|
| ----------- |:-------:|:-------:|:-------:|:-------:|:-------:|:-------:|
|[a11y](addons/a11y) |+| | | | | |
|[actions](addons/actions) |+|+|+|+|+|+|
|[background](addons/background) |+| | | | |+|
|[centered](addons/centered) |+| |+| | |+|
|[events](addons/events) |+| | | | | |
|[graphql](addons/graphql) |+| | | | | |
|[info](addons/info) |+| | | | | |
|[jest](addons/jest) |+| | | | | |
|[knobs](addons/knobs) |+|+|+|+|+|+|
|[links](addons/links) |+|+|+|+|+|+|
|[notes](addons/notes) |+| |+|+|+|+|
|[options](addons/options) |+|+|+|+|+|+|
|[storyshots](addons/storyshots) |+|+|+|+| | |
|[storysource](addons/storysource)|+| |+|+|+|+|
|[viewport](addons/viewport) |+| |+|+|+|+|
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ For additional help, join us [in our Slack](https://now-examples-slackin-rrirkqo
- [Vue](app/vue)
- [Angular](app/angular)
- [Polymer](app/polymer) <sup>alpha</sup>
- [Mithril](app/mithril) <sup>alpha</sup>

### Sub Projects

Expand Down Expand Up @@ -107,6 +108,7 @@ See [Addon / Framework Support Table](ADDONS_SUPPORT.md)
- [Vue](https://storybooks-vue.netlify.com/)
- [Angular](https://storybooks-angular.netlify.com/)
- [Polymer](https://storybooks-polymer.netlify.com/)
- [Mithril](https://storybooks-mithril.netlify.com/)

### 3.4
- [React Official](https://release-3-4--storybooks-official.netlify.com)
Expand Down
7 changes: 7 additions & 0 deletions addons/background/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,10 @@ storiesOf("Button", module)
.addDecorator(backgrounds)
.add("with text", () => <button>Click me</button>);
```

> In the case of Mithril, use these imports:
>
> ```js
> import { storiesOf } from '@storybook/mithril';
> import backgrounds from "@storybook/addon-backgrounds/mithril";
> ```
1 change: 1 addition & 0 deletions addons/background/mithril.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/mithril');
39 changes: 39 additions & 0 deletions addons/background/src/mithril.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/** @jsx m */

// eslint-disable-next-line import/no-extraneous-dependencies
import m from 'mithril';

import addons from '@storybook/addons';

export class BackgroundDecorator {
constructor(vnode) {
this.props = vnode.attrs;

const { channel, story } = vnode.attrs;

// A channel is explicitly passed in for testing
if (channel) {
this.channel = channel;
} else {
this.channel = addons.getChannel();
}

this.story = story();
}

oncreate() {
this.channel.emit('background-set', this.props.backgrounds);
}

onremove() {
this.channel.emit('background-unset');
}

view() {
return m(this.story);
}
}

export default backgrounds => story => ({
view: () => <BackgroundDecorator story={story} backgrounds={backgrounds} />,
});
35 changes: 33 additions & 2 deletions addons/centered/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,29 @@ storiesOf('MyComponent', module)
.add('without props', () => ({
components: { MyComponent },
template: '<my-component />'
})
}))
.add('with some props', () => ({
components: { MyComponent },
template: '<my-component text="The Comp"/>'
});
}));
```

example for Mithril:

```js
import { storiesOf } from '@storybook/mithril';
import centered from '@storybook/addon-centered/mithril';

import MyComponent from '../Component';

storiesOf('MyComponent', module)
.addDecorator(centered)
.add('without props', () => ({
view: () => <MyComponent />
}))
.add('with some props', () => ({
view: () => <MyComponent text="The Comp"/>
}));
```

Also, you can also add this decorator globally
Expand Down Expand Up @@ -84,6 +102,19 @@ configure(function () {
}, module);
```

example for Mithril:

```js
import { configure, addDecorator } from '@storybook/mithril';
import centered from '@storybook/addon-centered/mithril';

addDecorator(centered);

configure(function () {
//...
}, module);
```

#### As an extension

##### 1 - Configure the extension
Expand Down
1 change: 1 addition & 0 deletions addons/centered/mithril.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/mithril');
30 changes: 30 additions & 0 deletions addons/centered/src/mithril.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/** @jsx m */

// eslint-disable-next-line import/no-extraneous-dependencies
import m from 'mithril';

const style = {
position: 'fixed',
top: 0,
left: 0,
bottom: 0,
right: 0,
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
overflow: 'auto',
};

const innerStyle = {
margin: 'auto',
};

export default function(storyFn) {
return {
view: () => (
<div style={style}>
<div style={innerStyle}>{m(storyFn())}</div>
</div>
),
};
}
7 changes: 7 additions & 0 deletions addons/knobs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ stories.add('as dynamic variables', () => {
> import { storiesOf } from '@storybook/angular';
> import { withKnobs, text, boolean, number } from '@storybook/addon-knobs/angular';
> ```
>
> In the case of Mithril, use these imports:
>
> ```js
> import { storiesOf } from '@storybook/mithril';
> import { withKnobs, text, boolean, number } from '@storybook/addon-knobs/mithril';
> ```
You can see your Knobs in a Storybook panel as shown below.
Expand Down
1 change: 1 addition & 0 deletions addons/knobs/mithril.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/mithril');
68 changes: 68 additions & 0 deletions addons/knobs/src/mithril/WrapStory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import m from 'mithril';

export default class WrapStory {
constructor(vnode) {
this.knobChanged = this.knobChanged.bind(this);
this.knobClicked = this.knobClicked.bind(this);
this.resetKnobs = this.resetKnobs.bind(this);
this.setPaneKnobs = this.setPaneKnobs.bind(this);
this.props = vnode.attrs;
this.storyContent = vnode.attrs.initialContent;
}

oncreate() {
// Watch for changes in knob editor.
this.props.channel.on('addon:knobs:knobChange', this.knobChanged);
// Watch for clicks in knob editor.
this.props.channel.on('addon:knobs:knobClick', this.knobClicked);
// Watch for the reset event and reset knobs.
this.props.channel.on('addon:knobs:reset', this.resetKnobs);
// Watch for any change in the knobStore and set the panel again for those
// changes.
this.props.knobStore.subscribe(this.setPaneKnobs);
// Set knobs in the panel for the first time.
this.setPaneKnobs();
}

onremove() {
this.props.channel.removeListener('addon:knobs:knobChange', this.knobChanged);
this.props.channel.removeListener('addon:knobs:knobClick', this.knobClicked);
this.props.channel.removeListener('addon:knobs:reset', this.resetKnobs);
this.props.knobStore.unsubscribe(this.setPaneKnobs);
}

setPaneKnobs(timestamp = +new Date()) {
const { channel, knobStore } = this.props;
channel.emit('addon:knobs:setKnobs', { knobs: knobStore.getAll(), timestamp });
}

knobChanged(change) {
const { name, value } = change;
const { knobStore, storyFn, context } = this.props;
// Update the related knob and it's value.
const knobOptions = knobStore.get(name);

knobOptions.value = value;
knobStore.markAllUnused();
this.storyContent = storyFn(context);
m.redraw();
}

knobClicked(clicked) {
const knobOptions = this.props.knobStore.get(clicked.name);
knobOptions.callback();
}

resetKnobs() {
const { knobStore, storyFn, context } = this.props;
knobStore.reset();
this.storyContent = storyFn(context);
m.redraw();
this.setPaneKnobs(false);
}

view() {
return m(this.storyContent);
}
}
49 changes: 49 additions & 0 deletions addons/knobs/src/mithril/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/** @jsx m */

// eslint-disable-next-line import/no-extraneous-dependencies
import m from 'mithril';
import addons from '@storybook/addons';

import WrapStory from './WrapStory';

import {
knob,
text,
boolean,
number,
color,
object,
array,
date,
select,
selectV2,
button,
manager,
} from '../base';

export { knob, text, boolean, number, color, object, array, date, select, selectV2, button };

export const mithrilHandler = (channel, knobStore) => getStory => context => {
const initialContent = getStory(context);
const props = { context, storyFn: getStory, channel, knobStore, initialContent };
return {
view: () => <WrapStory {...props} />,
};
};

function wrapperKnobs(options) {
const channel = addons.getChannel();
manager.setChannel(channel);

if (options) channel.emit('addon:knobs:setOptions', options);

return mithrilHandler(channel, manager.knobStore);
}

export function withKnobs(storyFn, context) {
return wrapperKnobs()(storyFn)(context);
}

export function withKnobsOptions(options = {}) {
return (storyFn, context) => wrapperKnobs(options)(storyFn)(context);
}
2 changes: 2 additions & 0 deletions app/mithril/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
docs
.babelrc
Loading

0 comments on commit 239edfb

Please sign in to comment.