-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathpreview.tsx
326 lines (309 loc) · 9.29 KB
/
preview.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { global } from '@storybook/global';
import React, { Fragment, useEffect } from 'react';
import isChromatic from 'chromatic/isChromatic';
import {
Global,
ThemeProvider,
themes,
createReset,
convert,
styled,
useTheme,
} from '@storybook/theming';
import { useArgs, DocsContext as DocsContextProps } from '@storybook/preview-api';
import { Symbols } from '@storybook/components';
import type { PreviewWeb } from '@storybook/preview-api';
import type { ReactRenderer } from '@storybook/react';
import type { Channel } from '@storybook/channels';
import { DocsContext } from '@storybook/blocks';
import { DocsPageWrapper } from '../blocks/src/components';
const { document } = global;
const ThemeBlock = styled.div<{ side: 'left' | 'right' }>(
{
position: 'absolute',
top: 0,
left: 0,
right: '50vw',
width: '50vw',
height: '100vh',
bottom: 0,
overflow: 'auto',
padding: 10,
},
({ theme }) => ({
background: theme.background.content,
color: theme.color.defaultText,
}),
({ side }) =>
side === 'left'
? {
left: 0,
right: '50vw',
}
: {
right: 0,
left: '50vw',
}
);
const ThemeStack = styled.div(
{
position: 'relative',
minHeight: 'calc(50vh - 15px)',
},
({ theme }) => ({
background: theme.background.content,
color: theme.color.defaultText,
})
);
const PlayFnNotice = styled.div(
{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
borderBottom: '1px solid #ccc',
padding: '3px 8px',
fontSize: '10px',
fontWeight: 'bold',
'> *': {
display: 'block',
},
},
({ theme }) => ({
background: '#fffbd9',
color: theme.color.defaultText,
})
);
const ThemedSetRoot = () => {
const theme = useTheme();
useEffect(() => {
document.body.style.background = theme.background.content;
document.body.style.color = theme.color.defaultText;
});
return null;
};
// eslint-disable-next-line no-underscore-dangle
const preview = (window as any).__STORYBOOK_PREVIEW__ as PreviewWeb<ReactRenderer>;
const channel = (window as any).__STORYBOOK_ADDONS_CHANNEL__ as Channel;
export const loaders = [
/**
* This loader adds a DocsContext to the story, which is required for the most Blocks to work.
* A story will specify which stories they need in the index with:
* parameters: {
* relativeCsfPaths: ['../stories/MyStory.stories.tsx'], // relative to the story
* }
* The DocsContext will then be added via the decorator below.
*/
async ({ parameters: { relativeCsfPaths, attached = true } }) => {
if (!relativeCsfPaths) return {};
const csfFiles = await Promise.all(
(relativeCsfPaths as string[]).map(async (blocksRelativePath) => {
const projectRelativePath = `./ui/blocks/src/${blocksRelativePath.replace(
/^..\//,
''
)}.tsx`;
const entry = preview.storyStore.storyIndex?.importPathToEntry(projectRelativePath);
if (!entry) {
throw new Error(
`Couldn't find story file at ${projectRelativePath} (passed as ${blocksRelativePath})`
);
}
return preview.storyStore.loadCSFFileByStoryId(entry.id);
})
);
const docsContext = new DocsContextProps(
channel,
preview.storyStore,
preview.renderStoryToElement.bind(preview),
csfFiles
);
if (attached && csfFiles[0]) {
docsContext.attachCSFFile(csfFiles[0]);
}
return { docsContext };
},
];
export const decorators = [
// This decorator adds the DocsContext created in the loader above
(Story, { loaded: { docsContext } }) =>
docsContext ? (
<DocsContext.Provider value={docsContext}>
<Story />
</DocsContext.Provider>
) : (
<Story />
),
/**
* This decorator adds wrappers that contains global styles for stories to be targeted by.
* Activated with parameters.docsStyles = true
*/ (Story, { parameters: { docsStyles } }) =>
docsStyles ? (
<DocsPageWrapper>
<Story />
</DocsPageWrapper>
) : (
<Story />
),
/**
* This decorator adds Symbols that the sidebar icons references.
* Any sidebar story that uses the icons must set the parameter withSymbols: true .
*/
(Story, { parameters: { withSymbols } }) => (
<>
{withSymbols && <Symbols icons={['folder', 'component', 'document', 'bookmarkhollow']} />}
<Story />
</>
),
/**
* This decorator renders the stories side-by-side, stacked or default based on the theme switcher in the toolbar
*/
(StoryFn, { globals, parameters, playFunction }) => {
const defaultTheme = isChromatic() && !playFunction ? 'stacked' : 'light';
const theme = globals.theme || parameters.theme || defaultTheme;
switch (theme) {
case 'side-by-side': {
return (
<Fragment>
<ThemeProvider theme={convert(themes.light)}>
<Global styles={createReset} />
</ThemeProvider>
<ThemeProvider theme={convert(themes.light)}>
<ThemeBlock side="left" data-side="left">
<StoryFn />
</ThemeBlock>
</ThemeProvider>
<ThemeProvider theme={convert(themes.dark)}>
<ThemeBlock side="right" data-side="right">
<StoryFn />
</ThemeBlock>
</ThemeProvider>
</Fragment>
);
}
case 'stacked': {
return (
<Fragment>
<ThemeProvider theme={convert(themes.light)}>
<Global styles={createReset} />
</ThemeProvider>
<ThemeProvider theme={convert(themes.light)}>
<ThemeStack data-side="left">
<StoryFn />
</ThemeStack>
</ThemeProvider>
<ThemeProvider theme={convert(themes.dark)}>
<ThemeStack data-side="right">
<StoryFn />
</ThemeStack>
</ThemeProvider>
</Fragment>
);
}
case 'default':
default: {
return (
<ThemeProvider theme={convert(themes[theme])}>
<Global styles={createReset} />
<ThemedSetRoot />
{!parameters.theme && isChromatic() && playFunction && (
<>
<PlayFnNotice>
<span>
Detected play function in Chromatic. Rendering only light theme to avoid
multiple play functions in the same story.
</span>
</PlayFnNotice>
<div style={{ marginBottom: 20 }} />
</>
)}
<StoryFn />
</ThemeProvider>
);
}
}
},
/**
* This decorator shows the current state of the arg named in the
* parameters.withRawArg property, by updating the arg in the onChange function
* this also means that the arg will sync with the control panel
*
* If parameters.withRawArg is not set, this decorator will do nothing
*/
(StoryFn, { parameters, args, hooks }) => {
const [, updateArgs] = useArgs();
if (!parameters.withRawArg) {
return <StoryFn />;
}
return (
<>
<StoryFn
args={{
...args,
onChange: (newValue) => {
updateArgs({ [parameters.withRawArg]: newValue });
args.onChange?.(newValue);
},
}}
/>
<div style={{ marginTop: '1rem' }}>
Current <code>{parameters.withRawArg}</code>:{' '}
<pre>{JSON.stringify(args[parameters.withRawArg], null, 2) || 'undefined'}</pre>
</div>
</>
);
},
];
export const parameters = {
actions: { argTypesRegex: '^on.*' },
options: {
storySort: (a, b) =>
a.title === b.title ? 0 : a.id.localeCompare(b.id, undefined, { numeric: true }),
},
docs: {
theme: themes.light,
toc: {},
},
controls: {
presetColors: [
{ color: '#ff4785', title: 'Coral' },
{ color: '#1EA7FD', title: 'Ocean' },
{ color: 'rgb(252, 82, 31)', title: 'Orange' },
{ color: 'RGBA(255, 174, 0, 0.5)', title: 'Gold' },
{ color: 'hsl(101, 52%, 49%)', title: 'Green' },
{ color: 'HSLA(179,65%,53%,0.5)', title: 'Seafoam' },
{ color: '#6F2CAC', title: 'Purple' },
{ color: '#2A0481', title: 'Ultraviolet' },
{ color: 'black' },
{ color: '#333', title: 'Darkest' },
{ color: '#444', title: 'Darker' },
{ color: '#666', title: 'Dark' },
{ color: '#999', title: 'Mediumdark' },
{ color: '#ddd', title: 'Medium' },
{ color: '#EEE', title: 'Mediumlight' },
{ color: '#F3F3F3', title: 'Light' },
{ color: '#F8F8F8', title: 'Lighter' },
{ color: '#FFFFFF', title: 'Lightest' },
'#fe4a49',
'#FED766',
'rgba(0, 159, 183, 1)',
'HSLA(240,11%,91%,0.5)',
'slategray',
],
},
};
export const globalTypes = {
theme: {
name: 'Theme',
description: 'Global theme for components',
toolbar: {
icon: 'circlehollow',
title: 'Theme',
items: [
{ value: 'light', icon: 'circlehollow', title: 'light' },
{ value: 'dark', icon: 'circle', title: 'dark' },
{ value: 'side-by-side', icon: 'sidebar', title: 'side by side' },
{ value: 'stacked', icon: 'bottombar', title: 'stacked' },
],
},
},
};