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'
|
2023-02-08 20:24:09 +00:00
|
|
|
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
|
|
|
|
2023-10-06 15:26:08 +00:00
|
|
|
export const withTheme: Decorator = (Story, context) => {
|
2023-02-08 20:24:09 +00:00
|
|
|
const StoryComponent = Story as any as React.ComponentType
|
2023-10-13 11:58:37 +00:00
|
|
|
const isDoc = context.viewMode === 'docs'
|
2023-02-08 20:24:09 +00:00
|
|
|
|
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>
|
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
|
|
|
}
|
2023-02-08 20:24:09 +00:00
|
|
|
</ThemeProvider>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|