Storybook Centered Decorator can be used to center components inside the preview in Storybook.
This addon works with Storybook for: React.
npm install @storybook/addon-centered --save-dev
You can set the decorator locally:
import { storiesOf } from '@storybook/react';
import centered from '@storybook/addon-centered';
import MyComponent from '../Component';
storiesOf('MyComponent', module)
.addDecorator(centered)
.add('without props', () => (<MyComponent />))
.add('with some props', () => (<MyComponent text="The Comp"/>));
Or you can also add this decorator globally:
import { configure, addDecorator } from '@storybook/react';
import centered from '@storybook/react-storybook-decorator-centered';
addDecorator(centered);
configure(function () {
//...
}, module);
1 - Configure the extension
import { configure, setAddon } from '@storybook/react';
import centered from '@storybook/addon-centered';
setAddon({
addCentered(storyName, storyFn) {
this.add(storyName, (context) => (
centered.call(context, storyFn)
));
},
});
configure(function () {
//...
}, module);
2 - Use it in your story
import { storiesOf } from '@storybook/react';
import Component from '../Component';
storiesOf('Component', module)
.addCentered('without props', () => (<Component />))