Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Vue support to @addons/background #3488

Merged
merged 9 commits into from
Apr 26, 2018
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions addons/background/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,28 @@ storiesOf("Button", module)
.add("with text", () => <button>Click me</button>);
```

In the case of Vue, use it similarly to React!

```js
import { storiesOf } from '@storybook/vue';
import backgrounds from '@storybook/addon-backgrounds';

storiesOf('Addon|Backgrounds', module)
.addDecorator(
backgrounds([
{ name: 'twitter', value: '#00aced' },
{ name: 'facebook', value: '#3b5998', default: true },
])
)
.add('story 1', () => {
const content = 'You should be able to switch backgrounds for this story';

return {
template: `<button>${content}</button>`,
};
})
```

> In the case of Mithril, use these imports:
>
> ```js
Expand Down
3 changes: 3 additions & 0 deletions addons/background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"prop-types": "^15.6.1",
"react-lifecycles-compat": "^3.0.2"
},
"devDependencies": {
"vue": "^2.5.16"
},
"peerDependencies": {
"react": "*"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should remove this peerDependency now (and suppress corresponding ESLint warnings)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the react or the vue dependency? or should I just remove both?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or should I move the react dep into devDependencies

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean react. In devDependencies, you can have anything that's used in tests.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good! Thanks for clarifying

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import { shallow } from 'enzyme';

import { BackgroundDecorator } from '../index';
import { BackgroundDecorator } from '../react';

const EventEmitter = require('events');

Expand Down
42 changes: 42 additions & 0 deletions addons/background/src/__tests__/vue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import Vue from 'vue';
import { vueHandler } from '../vue';

describe('Vue handler', () => {
it('Returns a component with a created function', () => {
const testChannel = { emit: jest.fn() };
const testStory = () => ({ template: '<div> testStory </div>' });
const testContext = {
kind: 'Foo',
story: 'bar baz',
};
const testBackground = [
{ name: 'twitter', value: '#00aced' },
{ name: 'facebook', value: '#3b5998', default: true },
];
const component = vueHandler(testChannel, testBackground)(testStory, testContext);

expect(component).toMatchObject({
created: expect.any(Function),
beforeDestroy: expect.any(Function),
render: expect.any(Function),
});
});

it('Subscribes to the channel on creation', () => {
const testChannel = { emit: jest.fn() };
const testStory = () => ({ render: h => h('div', ['testStory']) });
const testContext = {
kind: 'Foo',
story: 'bar baz',
};
const testBackground = [
{ name: 'twitter', value: '#00aced' },
{ name: 'facebook', value: '#3b5998', default: true },
];

new Vue(vueHandler(testChannel, testBackground)(testStory, testContext)).$mount();

expect(testChannel.emit).toHaveBeenCalledTimes(1);
expect(testChannel.emit).toHaveBeenCalledWith('background-set', expect.any(Array));
});
});
69 changes: 5 additions & 64 deletions addons/background/src/index.js
Original file line number Diff line number Diff line change
@@ -1,66 +1,7 @@
import React from 'react';
import { polyfill } from 'react-lifecycles-compat';
import PropTypes from 'prop-types';
import { window } from 'global';
import ReactBackground from './react';
import VueBackground from './vue';

import addons from '@storybook/addons';
const Background = window.STORYBOOK_ENV === 'vue' ? VueBackground : ReactBackground;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with this approach is that now you bring Vue to react storybook bundle when it's not actually needed. In knobs, we decided to have separate entry points for each framework:

import { withKnobs } from '@storybook/addon-knobs/react'

see #1832 for reference

Copy link
Member

@Hypnosphi Hypnosphi Apr 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But actually, there's a chance we come up with a generic framework-agnostic solution for addons like this, which only add subscriptions but don't render anything inside preview iframe:

See #3475 (comment)

In this case, there will be no need in having different decorators in different frameworks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I will follow this up with a PR to update @addons/centered since this is what it is currently doing.

generic framework agnostic addons would awesome!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I wait for subscriptionStore to be merged? or should I just do framework specific exports for now?

Copy link
Member

@Hypnosphi Hypnosphi Apr 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can add a temporary vue entry point without renaming index to react


export class BackgroundDecorator extends React.Component {
constructor(props) {
super(props);

const { channel } = props;

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

this.state = {};
}

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

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

render() {
return this.state.story;
}
}

BackgroundDecorator.getDerivedStateFromProps = ({ story }, { prevStory }) => {
if (story !== prevStory) {
return {
story: story(),
prevStory: story,
};
}
return null;
};

BackgroundDecorator.propTypes = {
backgrounds: PropTypes.arrayOf(PropTypes.object),
channel: PropTypes.shape({
emit: PropTypes.func,
on: PropTypes.func,
removeListener: PropTypes.func,
}),
// eslint-disable-next-line react/no-unused-prop-types
story: PropTypes.func.isRequired,
};

BackgroundDecorator.defaultProps = {
backgrounds: [],
channel: undefined,
};

polyfill(BackgroundDecorator);

export default backgrounds => story => (
<BackgroundDecorator story={story} backgrounds={backgrounds} />
);
export default Background;
66 changes: 66 additions & 0 deletions addons/background/src/react.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import React from 'react';
import { polyfill } from 'react-lifecycles-compat';
import PropTypes from 'prop-types';

import addons from '@storybook/addons';

export class BackgroundDecorator extends React.Component {
constructor(props) {
super(props);

const { channel } = props;

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

this.state = {};
}

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

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

render() {
return this.state.story;
}
}

BackgroundDecorator.getDerivedStateFromProps = ({ story }, { prevStory }) => {
if (story !== prevStory) {
return {
story: story(),
prevStory: story,
};
}
return null;
};

BackgroundDecorator.propTypes = {
backgrounds: PropTypes.arrayOf(PropTypes.object),
channel: PropTypes.shape({
emit: PropTypes.func,
on: PropTypes.func,
removeListener: PropTypes.func,
}),
// eslint-disable-next-line react/no-unused-prop-types
story: PropTypes.func.isRequired,
};

BackgroundDecorator.defaultProps = {
backgrounds: [],
channel: undefined,
};

polyfill(BackgroundDecorator);

export default backgrounds => story => (
<BackgroundDecorator story={story} backgrounds={backgrounds} />
);
28 changes: 28 additions & 0 deletions addons/background/src/vue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import addons from '@storybook/addons';

export const vueHandler = (channel, backgrounds) => (getStory, context) => ({
data() {
return {
context,
getStory,
story: getStory(context),
};
},

render(h) {
return h(this.story);
},

created() {
channel.emit('background-set', backgrounds);
},

beforeDestroy() {
channel.emit('background-unset');
},
});

export default function makeDecorator(backgrounds) {
const channel = addons.getChannel();
return vueHandler(channel, backgrounds);
}
1 change: 1 addition & 0 deletions examples/vue-kitchen-sink/.storybook/addons.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ import '@storybook/addon-notes/register';
import '@storybook/addon-knobs/register';
import '@storybook/addon-viewport/register';
import '@storybook/addon-options/register';
import '@storybook/addon-backgrounds/register';
1 change: 1 addition & 0 deletions examples/vue-kitchen-sink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"devDependencies": {
"@storybook/addon-actions": "4.0.0-alpha.3",
"@storybook/addon-backgrounds": "4.0.0-alpha.3",
"@storybook/addon-centered": "4.0.0-alpha.3",
"@storybook/addon-knobs": "4.0.0-alpha.3",
"@storybook/addon-links": "4.0.0-alpha.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Storyshots Addon|Backgrounds story 1 1`] = `
<button>
You should be able to switch backgrounds for this story
</button>
`;

exports[`Storyshots Addon|Backgrounds story 2 1`] = `
<button>
This one too!
</button>
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { storiesOf } from '@storybook/vue';
import backgrounds from '@storybook/addon-backgrounds';

storiesOf('Addon|Backgrounds', module)
.addDecorator(
backgrounds([
{ name: 'twitter', value: '#00aced' },
{ name: 'facebook', value: '#3b5998', default: true },
])
)
.add('story 1', () => {
const content = 'You should be able to switch backgrounds for this story';

return {
template: `<button>${content}</button>`,
};
})
.add('story 2', () => {
const content = 'This one too!';

return {
template: `<button>${content}</button>`,
};
});