1 | # Storybook Centered Decorator
|
2 |
|
3 | Storybook 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
|
12 | yarn add @storybook/addon-centered --dev
|
13 | ```
|
14 |
|
15 | You can set the decorator locally.
|
16 |
|
17 | example for React:
|
18 |
|
19 | ```js
|
20 | import { storiesOf } from '@storybook/react';
|
21 | import centered from '@storybook/addon-centered/react';
|
22 |
|
23 | import MyComponent from '../Component';
|
24 |
|
25 | storiesOf('MyComponent', module)
|
26 | .addDecorator(centered)
|
27 | .add('without props', () => (<MyComponent />))
|
28 | .add('with some props', () => (<MyComponent text="The Comp"/>));
|
29 | ```
|
30 |
|
31 | example for Vue:
|
32 |
|
33 | ```js
|
34 | import { storiesOf } from '@storybook/vue';
|
35 | import centered from '@storybook/addon-centered/vue';
|
36 |
|
37 | import MyComponent from '../Component.vue';
|
38 | storiesOf('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 |
|
50 | example for Preact:
|
51 |
|
52 | ```js
|
53 | import { storiesOf } from '@storybook/preact';
|
54 | import centered from '@storybook/addon-centered/preact';
|
55 |
|
56 | import MyComponent from '../Component';
|
57 |
|
58 | storiesOf('MyComponent', module)
|
59 | .addDecorator(centered)
|
60 | .add('without props', () => (<MyComponent />))
|
61 | .add('with some props', () => (<MyComponent text="The Comp"/>));
|
62 | ```
|
63 |
|
64 | example for Svelte:
|
65 |
|
66 | ```js
|
67 | import { storiesOf } from '@storybook/svelte';
|
68 | import Centered from '@storybook/addon-centered/svelte';
|
69 |
|
70 | import Component from '../Component.svelte';
|
71 |
|
72 | storiesOf('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 |
|
83 | example for Mithril:
|
84 |
|
85 | ```js
|
86 | import { storiesOf } from '@storybook/mithril';
|
87 | import centered from '@storybook/addon-centered/mithril';
|
88 |
|
89 | import MyComponent from '../Component';
|
90 |
|
91 | storiesOf('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 |
|
101 | example for Angular with component:
|
102 |
|
103 | ```ts
|
104 | import { storiesOf } from '@storybook/angular';
|
105 | import { centered } from '@storybook/addon-centered/angular';
|
106 |
|
107 | import { AppComponent } from '../app/app.component';
|
108 |
|
109 | storiesOf('Addon|Centered', module)
|
110 | .addDecorator(centered)
|
111 | .add('centered component', () => ({
|
112 | component: AppComponent,
|
113 | props: {},
|
114 | }));
|
115 |
|
116 | ```
|
117 |
|
118 | example for Angular with template:
|
119 |
|
120 | ```ts
|
121 | import { moduleMetadata, storiesOf } from '@storybook/angular';
|
122 | import { centered } from '@storybook/addon-centered/angular';
|
123 |
|
124 | import { AppComponent } from '../app/app.component';
|
125 |
|
126 | storiesOf('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 |
|
147 | Also, you can also add this decorator globally
|
148 |
|
149 | example for React:
|
150 |
|
151 | ```js
|
152 | import { configure, addDecorator } from '@storybook/react';
|
153 | import centered from '@storybook/addon-centered/react';
|
154 |
|
155 | addDecorator(centered);
|
156 |
|
157 | configure(function () {
|
158 | //...
|
159 | }, module);
|
160 | ```
|
161 |
|
162 | example for Vue:
|
163 |
|
164 | ```js
|
165 | import { configure, addDecorator } from '@storybook/vue';
|
166 | import centered from '@storybook/addon-centered/vue';
|
167 |
|
168 | addDecorator(centered);
|
169 |
|
170 | configure(function () {
|
171 | //...
|
172 | }, module);
|
173 | ```
|
174 |
|
175 | example for Svelte:
|
176 |
|
177 | ```js
|
178 | import { configure, addDecorator } from '@storybook/svelte';
|
179 | import Centered from '@storybook/addon-centered/svelte';
|
180 |
|
181 | addDecorator(Centered);
|
182 |
|
183 | configure(function () {
|
184 | //...
|
185 | }, module);
|
186 | ```
|
187 |
|
188 | example for Mithril:
|
189 |
|
190 | ```js
|
191 | import { configure, addDecorator } from '@storybook/mithril';
|
192 | import centered from '@storybook/addon-centered/mithril';
|
193 |
|
194 | addDecorator(centered);
|
195 |
|
196 | configure(function () {
|
197 | //...
|
198 | }, module);
|
199 | ```
|
200 |
|
201 | If 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
|
204 | import React from 'react';
|
205 | import { storiesOf } from '@storybook/react';
|
206 |
|
207 | storiesOf('Button', module)
|
208 | .add('example', () => <button>Click me</button>, {
|
209 | centered: { disable: true },
|
210 | });
|
211 | ```
|