-
Notifications
You must be signed in to change notification settings - Fork 1
/
preview.tsx
138 lines (125 loc) · 3.76 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
import './index.css';
import '../src/styles/index.css';
import document from 'global/document';
import { INITIAL_VIEWPORTS } from '@storybook/addon-viewport';
import isChromatic from 'chromatic/isChromatic';
import { Story } from '@storybook/react/types-6-0';
import { StoryContext } from '@storybook/addons';
import { withCssResources } from '@storybook/addon-cssresources';
import { theme as appTheme, Theme } from '../src/theming';
import { createUseStyles, ThemeProvider, useTheme } from 'react-jss';
import React, { FC, ReactNode, useEffect } from 'react';
const useStyles = createUseStyles({
storyContainer: {
display: 'flex',
},
storyWrapper: {
padding: '1rem',
},
});
const useStylesWithTheme = createUseStyles({
themeBlock: {
background: ({ theme }: { theme: Theme }) => theme.background,
height: '100vh',
left: (props) => (props.side === 'left' ? 0 : '50vw'),
overflow: 'auto',
right: (props) => (props.side === 'right' ? '50vw' : 0),
width: '50vw',
},
});
/* This sets the appropriate background color to the storybook canvas. */
const ThemedSetRoot = () => {
const theme: Theme = useTheme();
useEffect(() => {
document.body.style.background = theme.background;
});
return null;
};
/* This wrapper adds padding to the story since it was removed from .sb-show-main in ./index.css */
const StoryWrapper: FC<StoryWrapperProps> = ({
children,
}: StoryWrapperProps) => {
const classes = useStyles();
return <div className={classes.storyWrapper}>{children}</div>;
};
interface StoryWrapperProps {
children: ReactNode;
}
/* This adds a wrapper to style the left and right blocks for side-by-side viewing of dark and light themes. */
const ThemedBlock: FC<ThemedBlockProps> = ({
children,
...props
}: ThemedBlockProps) => {
const theme = useTheme();
const classes = useStylesWithTheme({ ...props, theme });
return (
<div className={classes.themeBlock}>
<StoryWrapper>{children}</StoryWrapper>
</div>
);
};
interface ThemedBlockProps {
children: ReactNode;
side: 'left' | 'right';
}
/* This is the decorator that wraps the stories with a theme provider and a wrapper div for side-by-side view. */
const ThemeWrapper = (
ComponentStory: Story,
{ globals: { theme = 'light' } }: StoryContext
) => {
const classes = useStyles();
switch (theme) {
case 'side-by-side': {
return (
<div className={classes.storyContainer}>
<ThemeProvider theme={appTheme.light}>
<ThemedBlock side="left">
<ComponentStory />
</ThemedBlock>
</ThemeProvider>
<ThemeProvider theme={appTheme.dark}>
<ThemedBlock side="right">
<ComponentStory />
</ThemedBlock>
</ThemeProvider>
</div>
);
}
default: {
return (
<ThemeProvider
theme={theme === 'light' ? appTheme.light : appTheme.dark}
>
<ThemedSetRoot />
<StoryWrapper>
<ComponentStory />
</StoryWrapper>
</ThemeProvider>
);
}
}
};
export const globalTypes = {
theme: {
/* Setting side-by-side as default for chromatic allows for visual regression testing on both dark and light themed stories. */
defaultValue: isChromatic() ? 'side-by-side' : 'light',
description: 'Global theme for components',
name: 'Theme',
toolbar: {
icon: 'circlehollow',
items: [
{ icon: 'circlehollow', title: 'light', value: 'light' },
{ icon: 'circle', title: 'dark', value: 'dark' },
{
icon: 'sidebar',
title: 'side by side',
value: 'side-by-side',
},
],
},
},
viewport: {
viewports: INITIAL_VIEWPORTS,
},
};
export const decorators = [withCssResources, ThemeWrapper];