Skip to content

Commit

Permalink
playing around with theme switcher
Browse files Browse the repository at this point in the history
  • Loading branch information
josefie committed May 9, 2022
1 parent 8143086 commit 888129c
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 5 deletions.
25 changes: 20 additions & 5 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { withDesign } from "storybook-addon-designs";
import { withDesign } from 'storybook-addon-designs'
import { withThemeProvider, AVAILABLE_THEMES } from '../src/helpers/ThemeProvider'

export const parameters = {
actions: { argTypesRegex: "^on[A-Z].*" },
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
Expand All @@ -10,6 +11,20 @@ export const parameters = {
},
}

export const decorators = [
withDesign
];
const themeKeys = Object.keys(AVAILABLE_THEMES)

export const globalTypes = {
theme: {
name: 'Theme',
description: 'Switch theme for preview',
defaultValue: themeKeys[0],
toolbar: {
items: themeKeys.map((key) => {
return { value: key, title: AVAILABLE_THEMES[key] }
}),
showName: true,
},
},
}

export const decorators = [withDesign, withThemeProvider]
40 changes: 40 additions & 0 deletions src/helpers/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React, { ReactNode } from 'react'
import { Tokens } from '../../tokens'

export const AVAILABLE_THEMES = {
org: 'betterplace.org',
at: 'betterplace.at',
me: 'betterplace.me',
}

type Theme = keyof typeof AVAILABLE_THEMES

type ThemeProviderProps = {
theme: Theme
children: ReactNode
}

export const ThemeProvider = (props: ThemeProviderProps) => {
const tokens = {
globals: Tokens.globals,
[props.theme]: Tokens[props.theme],
}

console.log(tokens)

const ThemeContext = React.createContext(props.theme)

return (
<ThemeContext.Provider value={props.theme}>
<div className={'theme-' + props.theme}>{props.children}</div>
</ThemeContext.Provider>
)
}

export const withThemeProvider = (Story: any, context: { globals: { theme: Theme } }) => {
return (
<ThemeProvider theme={context.globals.theme}>
<Story {...context} />
</ThemeProvider>
)
}
7 changes: 7 additions & 0 deletions tokens/globals.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"colors": {
"colorRed100": "#ff0000",
"colorGreen100": "#00ff00",
"colorGreen300": "#00aa00"
}
}
11 changes: 11 additions & 0 deletions tokens/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import globals from './globals.json'
import org from './theme-org.json'
import at from './theme-at.json'
import me from './theme-me.json'

export const Tokens = {
globals,
org,
at,
me,
}
5 changes: 5 additions & 0 deletions tokens/theme-at.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"colors": {
"colorPrimary": "colorGreen300"
}
}
5 changes: 5 additions & 0 deletions tokens/theme-me.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"colors": {
"colorPrimary": "colorRed100"
}
}
5 changes: 5 additions & 0 deletions tokens/theme-org.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"colors": {
"colorPrimary": "colorGreen100"
}
}

0 comments on commit 888129c

Please sign in to comment.