2023-02-08 20:24:09 +00:00
|
|
|
import { DecoratorFunction, useGlobals } from '@storybook/addons'
|
|
|
|
import React, { useEffect } from 'react'
|
2023-04-13 22:04:15 +00:00
|
|
|
import { ThemeProvider } from '../src'
|
|
|
|
import { storybookThemes } 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-04-13 22:04:15 +00:00
|
|
|
const theme = storybookThemes.getTheme(context)
|
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(
|
2023-04-13 22:04:15 +00:00
|
|
|
(value) => theme.name.startsWith(value.name),
|
2023-02-13 09:43:50 +00:00
|
|
|
)?.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>
|
|
|
|
)
|
|
|
|
}
|