2023-02-08 20:24:09 +00:00
|
|
|
import { DecoratorFunction, useGlobals } from '@storybook/addons'
|
|
|
|
import React, { useEffect } from 'react'
|
2023-03-20 23:48:44 +00:00
|
|
|
import { Theme, ThemeProvider } from '../src'
|
|
|
|
import { storybookDefaultThemeKey, themes } from './themes'
|
2023-02-08 20:24:09 +00:00
|
|
|
|
|
|
|
export const withTheme: DecoratorFunction = (Story, context) => {
|
|
|
|
const StoryComponent = Story as any as React.ComponentType
|
|
|
|
|
2023-03-20 23:48:44 +00:00
|
|
|
const themeName = context.globals?.theme ?? storybookDefaultThemeKey
|
|
|
|
const theme = themes[themeName] as Theme
|
2023-02-08 20:24:09 +00:00
|
|
|
|
|
|
|
const [globals, setGlobals] = useGlobals()
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-02-13 09:43:50 +00:00
|
|
|
const background = (context.parameters.backgrounds?.values ?? []).find(
|
|
|
|
(value) => value.name === themeName,
|
|
|
|
)?.value
|
|
|
|
|
|
|
|
globals.backgrounds?.value !== background &&
|
|
|
|
setGlobals({
|
|
|
|
...globals,
|
|
|
|
backgrounds: {
|
|
|
|
...(globals.background ?? {}),
|
|
|
|
value: background,
|
|
|
|
},
|
|
|
|
})
|
2023-02-08 20:24:09 +00:00
|
|
|
}, [theme])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<ThemeProvider theme={theme}>
|
|
|
|
<StoryComponent />
|
|
|
|
</ThemeProvider>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|