2023-10-06 15:26:08 +00:00
|
|
|
import { useGlobals } from '@storybook/manager-api'
|
|
|
|
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-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>
|
|
|
|
)
|
|
|
|
}
|