From 12876f3a28fe998e7561ebb1e58486fbb7bc0837 Mon Sep 17 00:00:00 2001 From: sam-m-m Date: Mon, 5 Oct 2020 12:17:59 -0700 Subject: [PATCH] feat #92 - Refac preview.tsx to use react-jss --- .storybook/preview.tsx | 119 ++++++++++----------- src/components/assets/styles/baseStyles.ts | 14 ++- src/components/assets/styles/themes.ts | 15 ++- 3 files changed, 69 insertions(+), 79 deletions(-) diff --git a/.storybook/preview.tsx b/.storybook/preview.tsx index 3babfdc0..ccb33de0 100644 --- a/.storybook/preview.tsx +++ b/.storybook/preview.tsx @@ -1,23 +1,17 @@ import './index.css' import cn from 'classnames' -import { createUseStyles } from 'react-jss' 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 { ThemesType } from '../src/components/assets/styles/themes' -import { withCssResources } from '@storybook/addon-cssresources' import { - convert, - createReset, - Global, - styled, - Theme, - ThemeProvider, themes, - useTheme -} from '@storybook/theming' + Theme, + ThemesType +} 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' const { dark, light } = ThemesType @@ -31,47 +25,27 @@ const useStyles = createUseStyles({ } }) -const ThemeBlock = styled.div( - { +const useStylesWithTheme = createUseStyles({ + themeBlock: { + background: ({ theme }: { theme: Theme }) => theme.background, height: '100vh', - left: 0, + left: props => (props.side === 'left' ? 0 : '50vw'), overflow: 'auto', - right: '50vw', + right: props => (props.side === 'right' ? '50vw' : 0), width: '50vw' - }, - ({ theme }) => ({ - background: theme.background.app, - color: theme.color.defaultText - }), - // @ts-ignore - ({ side }) => - side === 'left' - ? { - left: 0, - right: '50vw' - } - : { - left: '50vw', - right: 0 - } -) - -const ThemedSetRoot = () => { + } +}) + +const ThemedCanvasBg = () => { const theme: Theme = useTheme() useEffect(() => { - document.body.style.background = theme.background.app - document.body.style.color = theme.defaultText + document.body.style.background = theme.background }) 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 @@ -90,7 +64,36 @@ const StoryWrapper: FC = ({ return
{children}
} -const ThemeWrapper = ( +interface StoryWrapperProps { + children: ReactNode + dark?: boolean +} + +/* This adds a wrapper to style the left and right blocks for side-by-side viewing of dark and light themes. */ +const ThemedBlock: FC = ({ + children, + ...props +}: ThemedBlockProps) => { + const theme = useTheme() + const classes = useStylesWithTheme({ ...props, theme }) + const { side } = props + + return ( +
+ + {children} + +
+ ) +} + +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 ThemeDecorator = ( ComponentStory: Story, { globals: { theme = light } }: StoryContext ) => { @@ -100,24 +103,15 @@ const ThemeWrapper = ( case 'side-by-side': { return (
- - - - - {/* @ts-ignore */} - - - - - + + + + - - {/* @ts-ignore */} - - - - - + + + +
) @@ -125,9 +119,8 @@ const ThemeWrapper = ( default: { return ( - - - + + @@ -161,4 +154,4 @@ export const globalTypes = { } } -export const decorators = [withCssResources, ThemeWrapper] +export const decorators = [withCssResources, ThemeDecorator] diff --git a/src/components/assets/styles/baseStyles.ts b/src/components/assets/styles/baseStyles.ts index d84aaf71..e4a20020 100644 --- a/src/components/assets/styles/baseStyles.ts +++ b/src/components/assets/styles/baseStyles.ts @@ -1,12 +1,8 @@ import { ColorManipulationTypes, manipulateColor } from '../../utils' import { dassanaBlues, dassanaGrays } from './colors' -import { getThemePalette, ThemePalette, ThemesType } from './themes' +import { Theme, themes, ThemesType } from './themes' -export const generateBaseStyles = ({ - background, - primary, - text -}: ThemePalette) => { +export const generateBaseStyles = ({ background, primary, text }: Theme) => { const base = { bgColor: background, borderColor: dassanaGrays[6], @@ -38,7 +34,9 @@ export const generateBaseStyles = ({ return { base, disabled, focus, hover, placeholder } } +const { dark, light } = ThemesType + export const generalColors = { - [ThemesType.dark]: generateBaseStyles(getThemePalette(ThemesType.dark)), - [ThemesType.light]: generateBaseStyles(getThemePalette(ThemesType.light)) + [ThemesType.dark]: generateBaseStyles(themes[dark]), + [ThemesType.light]: generateBaseStyles(themes[light]) } diff --git a/src/components/assets/styles/themes.ts b/src/components/assets/styles/themes.ts index 206c6af3..b1e319d7 100644 --- a/src/components/assets/styles/themes.ts +++ b/src/components/assets/styles/themes.ts @@ -1,13 +1,13 @@ import { dassanaBlue, dassanaGrays, dassanaReds, dassanaWhite } from './colors' -export interface ThemePalette { +export interface Theme { background: string primary: string error: string text: { disabled: string; primary: string } } -export const lightPalette: ThemePalette = { +export const lightPalette: Theme = { background: dassanaWhite, error: dassanaReds[6], primary: dassanaBlue, @@ -17,7 +17,7 @@ export const lightPalette: ThemePalette = { } } -export const darkPalette: ThemePalette = { +export const darkPalette: Theme = { background: dassanaGrays[9], error: dassanaReds[6], primary: dassanaBlue, @@ -32,8 +32,7 @@ export enum ThemesType { light = 'light' } -const { dark, light } = ThemesType - -const themes = { [dark]: darkPalette, [light]: lightPalette } - -export const getThemePalette = (theme: ThemesType) => themes[theme] +export const themes = { + [ThemesType.dark]: darkPalette, + [ThemesType.light]: lightPalette +}