-
Notifications
You must be signed in to change notification settings - Fork 1
/
preview.tsx
161 lines (144 loc) · 3.78 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
import './index.css'
import cn from 'classnames'
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 {
themes,
Theme,
ThemeType
} from '../src/components/assets/styles/themes'
import { createUseStyles, ThemeProvider, useTheme } from 'react-jss'
import { withCssResources } from '@storybook/addon-cssresources'
import React, { FC, ReactNode, useEffect } from 'react'
enum LayoutTypes {
sideBySide = 'side-by-side',
left = 'left',
right = 'right'
}
const { sideBySide, left, right } = LayoutTypes
const { dark, light } = ThemeType
const useStyles = createUseStyles({
storyContainer: {
display: 'flex'
},
storyWrapper: {
padding: '1rem'
}
})
const useStylesWithTheme = createUseStyles({
themeBlock: {
background: ({ theme }: { theme: Theme }) => theme.background.primary,
height: '100vh',
left: props => (props.side === left ? 0 : '50vw'),
overflow: 'auto',
right: props => (props.side === right ? '50vw' : 0),
width: '50vw'
}
})
const ThemedCanvasBg = () => {
const theme: Theme = useTheme()
useEffect(() => {
document.body.style.background = theme.background.primary
})
return null
}
interface StoryWrapperProps {
children: ReactNode
dark?: boolean
}
/*
This wrapper does two things:
1. Adds padding to the story since it was removed from .sb-show-main in ./index.css
2. Toggles 'dark' theme class
*/
const StoryWrapper: FC<StoryWrapperProps> = ({
children,
dark = false
}: StoryWrapperProps) => {
const classes = useStyles()
const wrapperClasses = cn({
dark,
[classes.storyWrapper]: true
})
return <div className={wrapperClasses}>{children}</div>
}
interface ThemedBlockProps {
children: ReactNode
side: LayoutTypes.left | LayoutTypes.right
}
/* 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 })
const { side } = props
return (
<div className={classes.themeBlock}>
<StoryWrapper dark={side === right}>{children}</StoryWrapper>
</div>
)
}
/* This is the decorator that wraps the stories with a theme provider and a wrapper div for side-by-side view. */
const ThemeDecorator = (
ComponentStory: Story,
{ globals: { theme = light } }: StoryContext
) => {
const classes = useStyles()
switch (theme) {
case sideBySide: {
return (
<div className={classes.storyContainer}>
<ThemeProvider theme={themes[light]}>
<ThemedBlock side={left}>
<ComponentStory />
</ThemedBlock>
</ThemeProvider>
<ThemeProvider theme={themes[dark]}>
<ThemedBlock side={right}>
<ComponentStory />
</ThemedBlock>
</ThemeProvider>
</div>
)
}
default: {
return (
<ThemeProvider theme={themes[theme]}>
<ThemedCanvasBg />
<StoryWrapper dark={theme === dark}>
<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() ? sideBySide : 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: sideBySide
}
]
}
},
viewport: {
viewports: INITIAL_VIEWPORTS
}
}
export const decorators = [withCssResources, ThemeDecorator]