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

53 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-10-13 11:58:37 +00:00
import { Global, css } from '@emotion/react'
2023-10-06 15:39:56 +00:00
import { useGlobals } from '@storybook/preview-api'
2023-10-06 15:26:08 +00:00
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
2023-10-13 11:58:37 +00:00
const isDoc = context.viewMode === 'docs'
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>
2023-10-23 06:59:39 +00:00
<ThemeProvider theme={theme} injectCssVars={false}>
<div className="story-wrapper">
<StoryComponent />
</div>
{
2023-10-13 11:58:37 +00:00
<Global
styles={css`
2023-10-23 06:59:39 +00:00
.story-wrapper,
#lsd-presentation {
2023-10-13 11:58:37 +00:00
${theme.cssVars}
2023-10-23 06:59:39 +00:00
}
.docs-story {
background: rgb(${theme.palette.surface.primary});
2023-10-13 11:58:37 +00:00
}
`}
/>
2023-10-23 06:59:39 +00:00
}
</ThemeProvider>
</div>
)
}