lsd/packages/lsd-react/.storybook/withTheme.decorator.tsx

36 lines
917 B
TypeScript
Raw Permalink Normal View History

import { DecoratorFunction, useGlobals } from '@storybook/addons'
import React, { useEffect } from 'react'
import { ThemeProvider } from '../src'
import { storybookThemes } from './themes'
export const withTheme: DecoratorFunction = (Story, context) => {
const StoryComponent = Story as any as React.ComponentType
const theme = storybookThemes.getTheme(context)
const [globals, setGlobals] = useGlobals()
useEffect(() => {
2023-02-13 09:43:50 +00:00
const background = (context.parameters.backgrounds?.values ?? []).find(
(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,
},
})
}, [theme])
return (
<div>
<ThemeProvider theme={theme}>
<StoryComponent />
</ThemeProvider>
</div>
)
}