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

37 lines
940 B
TypeScript
Raw Normal View History

2023-10-06 15:26:08 +00:00
import { useGlobals } from '@storybook/manager-api'
import { Decorator } from '@storybook/react'
import React, { useEffect } from 'react'
import { ThemeProvider } from '../src'
import { storybookThemes } from './themes'
2023-10-06 15:26:08 +00:00
export const withTheme: Decorator = (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>
)
}