Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat #92 - Theming #97

Merged
merged 24 commits into from Oct 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
9925b6f
feat #92 - Demo input custom styles
sam-dassana Sep 28, 2020
bcaf242
feat #92 - Update input default styles
sam-dassana Sep 29, 2020
c8f678e
feat #92 - WIP: sb bug canvas bg color
sam-dassana Oct 1, 2020
7841c8d
feat #92 - Add dark mode theming switcher to sb
sam-dassana Oct 1, 2020
f02e971
feat #92 - Update Tree.stories comment & input stories text
sam-dassana Oct 1, 2020
9fa064a
feat #92 - Add tests for color utils
sam-dassana Oct 1, 2020
aa4f422
feat #92 - Rebase with dev & update failing snapshot tests
sam-dassana Oct 1, 2020
4268f3b
feat #97 - Update failing snapshot and run npm audit
Oct 1, 2020
25ea1f0
feat #92 - Update to use sb native theming
sam-dassana Oct 2, 2020
43edc81
feat #92 - Add side-by-side theming views
sam-dassana Oct 2, 2020
15832ab
feat #92 - Remove package storybook-dark-mode & bump v to 0.4.1
sam-dassana Oct 2, 2020
3d93e9a
feat #92 - Fix bug input focused fadeColor
sam-dassana Oct 2, 2020
bea4293
feat #92 - Address PR comments
sam-dassana Oct 3, 2020
7a6d84f
feat #92 - Refac preview.tsx to use react-jss
sam-dassana Oct 5, 2020
2ceeaa4
feat #92 - Update colors
sam-dassana Oct 7, 2020
55cf427
feat #92 - Address PR comments
sam-dassana Oct 7, 2020
0a915ea
feat #92 - Address PR comments, update colors
sam-dassana Oct 7, 2020
57a6199
feat #92 - Add colors, refac, add story decorator
sam-dassana Oct 9, 2020
6058367
feat #92 - Fix bug: missing decorator class, update failing snapshots
sam-dassana Oct 9, 2020
d86d62f
feat #92 - Address PR comments
sam-dassana Oct 9, 2020
504682f
feat #92 - Update font to Inter
sam-dassana Oct 9, 2020
3874802
feat #92 - Fix Decorator test typo
sam-dassana Oct 9, 2020
d990d29
feat #92 - Fix bug: Add missing index file to assets/styles
sam-dassana Oct 9, 2020
e608fc9
feat #92 - Fix bug: exported types not working
sam-dassana Oct 9, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions .storybook/Decorator.tsx
@@ -0,0 +1,38 @@
import React, { FC, ReactNode } from 'react'
import { createUseStyles } from 'react-jss'
import { ThemeType, themes } from '../src/components/assets/styles/themes'
import cn from 'classnames'
const { dark } = ThemeType

const useStyles = createUseStyles({
'@global': {
[`.${dark} .decorator`]: {
backgroundColor: themes[dark].background.secondary
}
},
decorator: {
padding: '1rem'
}
})

export interface DecoratorProps {
children: ReactNode
classes?: string[]
}

const Decorator: FC<DecoratorProps> = ({
children,
classes = []
}: DecoratorProps) => {
const decoratorClasses = useStyles()

return (
<div
className={cn(decoratorClasses.decorator, 'decorator', ...classes)}
>
{children}
</div>
)
}

export default Decorator
13 changes: 13 additions & 0 deletions .storybook/__tests__/Decorator.test.tsx
@@ -0,0 +1,13 @@
import React from 'react'
import Decorator, { DecoratorProps } from '../Decorator'
import { shallow, ShallowWrapper } from 'enzyme'

let wrapper: ShallowWrapper<DecoratorProps>

describe('Decorator', () => {
it('renders', () => {
wrapper = shallow(<Decorator>Decorator</Decorator>)

expect(wrapper).toHaveLength(1)
})
})
18 changes: 14 additions & 4 deletions .storybook/index.css
@@ -1,9 +1,19 @@
/* This is the global dassana stylesheet. It should match the global stylesheet in web-orchestrator. */

@import '~normalize.css';
@import url('https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,300;0,400;0,700;0,900;1,300;1,400;1,700;1,900&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Inter&display=swap');

body {
font-family: 'Lato', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
Oxygen-Sans, Ubuntu, Cantarell, 'Helvetica Neue', sans-serif;
html,
body,
#root,
.App {
font-family: 'Inter', sans-serif;
height: 100%;
}

/* Storybook styles */

.sb-show-main {
/* For side-by-side theme view */
sam-dassana marked this conversation as resolved.
Show resolved Hide resolved
padding: 0 !important;
}
3 changes: 2 additions & 1 deletion .storybook/main.js
Expand Up @@ -4,6 +4,7 @@ module.exports = {
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/addon-actions',
'@storybook/preset-create-react-app'
'@storybook/preset-create-react-app',
'@storybook/addon-cssresources'
]
}
1 change: 0 additions & 1 deletion .storybook/manager.js
Expand Up @@ -8,7 +8,6 @@ addons.setConfig({
sidebarAnimations: true,
enableShortcuts: true,
isToolshown: true,
theme: undefined,
selectedPanel: 'Controls',
initialActive: 'sidebar',
showRoots: false
Expand Down
23 changes: 0 additions & 23 deletions .storybook/preview.ts

This file was deleted.

161 changes: 161 additions & 0 deletions .storybook/preview.tsx
@@ -0,0 +1,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({
sam-dassana marked this conversation as resolved.
Show resolved Hide resolved
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> = ({
sam-dassana marked this conversation as resolved.
Show resolved Hide resolved
sam-dassana marked this conversation as resolved.
Show resolved Hide resolved
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) {
sam-dassana marked this conversation as resolved.
Show resolved Hide resolved
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]