Skip to content

Commit 4622dd9

Browse files
committed
ensure stories have dark bg in dark mode
1 parent d5f4d02 commit 4622dd9

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

.storybook/preview.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import type { Theme } from '@mui/material'
22
import {
3-
createTheme,
4-
Stack, ThemeProvider, useColorScheme, useTheme
3+
createTheme, CssBaseline,
4+
ThemeProvider, useColorScheme, useTheme
55
} from '@mui/material'
66
import type { Decorator } from '@storybook/react-vite'
77
import {
88
DataismTheme, XyLabsTheme, XyosTheme, XyoTheme,
99
} from '@xylabs/react-theme'
10-
import React, { useEffect } from 'react'
10+
import React, { useEffect, useLayoutEffect } from 'react'
1111

1212
const themeNames = ['None', 'XYO', 'Dataism', 'XYLabs', 'xyOS'] as const
1313
type ThemeName = typeof themeNames[number]
@@ -79,21 +79,28 @@ const withThemeProvider: Decorator = (Story, context) => {
7979
const themeOptions = getTheme(context.globals.theme)
8080
const theme = themeOptions
8181

82+
// SB does not adjust the background color based off the color scheme, so we need to do it manually
83+
useLayoutEffect(() => {
84+
const body = document.getElementsByTagName('body')[0]
85+
if (body) {
86+
body.style.backgroundColor = themeOptions.vars?.palette?.background?.paper || 'transparent'
87+
}
88+
}, [themeOptions])
89+
8290
return (
83-
<ThemeProvider theme={theme}>
84-
<Stack alignItems="stretch">
91+
<ThemeProvider theme={theme} defaultMode={'dark'}>
92+
<CssBaseline enableColorScheme />
8593
<Story {...context} />
86-
</Stack>
8794
</ThemeProvider>
8895
)
8996
}
9097

9198
const withModeSelector: Decorator = (Story, context) => {
9299
const { mode } = context.globals
93-
const { setMode: setMuiMode } = useColorScheme()
100+
const { setMode } = useColorScheme()
94101

95102
useEffect(() => {
96-
setMuiMode(mode)
103+
setMode(mode)
97104
}, [mode])
98105

99106
return <Story {...context} />

0 commit comments

Comments
 (0)