UNPKG

4.71 kBMarkdownView Raw
1# Storybook Centered Decorator
2
3Storybook Centered Decorator can be used to center components inside the preview in [Storybook](https://storybook.js.org).
4
5[Framework Support](https://github.com/storybookjs/storybook/blob/master/ADDONS_SUPPORT.md)
6
7⚠️ This addon applies styling to the view in order to center the component. This may impact the look and feel of story.
8
9### Usage
10
11```sh
12yarn add @storybook/addon-centered --dev
13```
14
15You can set the decorator locally.
16
17example for React:
18
19```js
20import { storiesOf } from '@storybook/react';
21import centered from '@storybook/addon-centered/react';
22
23import MyComponent from '../Component';
24
25storiesOf('MyComponent', module)
26 .addDecorator(centered)
27 .add('without props', () => (<MyComponent />))
28 .add('with some props', () => (<MyComponent text="The Comp"/>));
29```
30
31example for Vue:
32
33```js
34import { storiesOf } from '@storybook/vue';
35import centered from '@storybook/addon-centered/vue';
36
37import MyComponent from '../Component.vue';
38storiesOf('MyComponent', module)
39 .addDecorator(centered)
40 .add('without props', () => ({
41 components: { MyComponent },
42 template: '<my-component />'
43 }))
44 .add('with some props', () => ({
45 components: { MyComponent },
46 template: '<my-component text="The Comp"/>'
47 }));
48```
49
50example for Preact:
51
52```js
53import { storiesOf } from '@storybook/preact';
54import centered from '@storybook/addon-centered/preact';
55
56import MyComponent from '../Component';
57
58storiesOf('MyComponent', module)
59 .addDecorator(centered)
60 .add('without props', () => (<MyComponent />))
61 .add('with some props', () => (<MyComponent text="The Comp"/>));
62```
63
64example for Svelte:
65
66```js
67import { storiesOf } from '@storybook/svelte';
68import Centered from '@storybook/addon-centered/svelte';
69
70import Component from '../Component.svelte';
71
72storiesOf('Addon|Centered', module)
73 .addDecorator(Centered)
74 .add('rounded', () => ({
75 Component,
76 data: {
77 rounded: true,
78 text: "Look, I'm centered!",
79 },
80 }))
81```
82
83example for Mithril:
84
85```js
86import { storiesOf } from '@storybook/mithril';
87import centered from '@storybook/addon-centered/mithril';
88
89import MyComponent from '../Component';
90
91storiesOf('MyComponent', module)
92 .addDecorator(centered)
93 .add('without props', () => ({
94 view: () => <MyComponent />
95 }))
96 .add('with some props', () => ({
97 view: () => <MyComponent text="The Comp"/>
98 }));
99```
100
101example for Angular with component:
102
103```ts
104import { storiesOf } from '@storybook/angular';
105import { centered } from '@storybook/addon-centered/angular';
106
107import { AppComponent } from '../app/app.component';
108
109storiesOf('Addon|Centered', module)
110 .addDecorator(centered)
111 .add('centered component', () => ({
112 component: AppComponent,
113 props: {},
114 }));
115
116```
117
118example for Angular with template:
119
120```ts
121import { moduleMetadata, storiesOf } from '@storybook/angular';
122import { centered } from '@storybook/addon-centered/angular';
123
124import { AppComponent } from '../app/app.component';
125
126storiesOf('Addon|Centered', module)
127 .addDecorator(
128 moduleMetadata({
129 declarations: [Button],
130 })
131 )
132 .addDecorator(centered)
133 .add('centered template', () => ({
134 template: `<storybook-button-component
135 [text]="text" (onClick)="onClick($event)">
136 </storybook-button-component>`,
137 props: {
138 text: 'Hello Button',
139 onClick: event => {
140 console.log('some bindings work');
141 console.log(event);
142 },
143 },
144 }));
145```
146
147Also, you can also add this decorator globally
148
149example for React:
150
151```js
152import { configure, addDecorator } from '@storybook/react';
153import centered from '@storybook/addon-centered/react';
154
155addDecorator(centered);
156
157configure(function () {
158 //...
159}, module);
160```
161
162example for Vue:
163
164```js
165import { configure, addDecorator } from '@storybook/vue';
166import centered from '@storybook/addon-centered/vue';
167
168addDecorator(centered);
169
170configure(function () {
171 //...
172}, module);
173```
174
175example for Svelte:
176
177```js
178import { configure, addDecorator } from '@storybook/svelte';
179import Centered from '@storybook/addon-centered/svelte';
180
181addDecorator(Centered);
182
183configure(function () {
184 //...
185}, module);
186```
187
188example for Mithril:
189
190```js
191import { configure, addDecorator } from '@storybook/mithril';
192import centered from '@storybook/addon-centered/mithril';
193
194addDecorator(centered);
195
196configure(function () {
197 //...
198}, module);
199```
200
201If you don't want to use centered for a story, you can disable it by using `{ disable: true }` to skip the addon:
202
203```js
204import React from 'react';
205import { storiesOf } from '@storybook/react';
206
207storiesOf('Button', module)
208 .add('example', () => <button>Click me</button>, {
209 centered: { disable: true },
210 });
211```