Skip to content

Commit 1b65874

Browse files
fix(.storybook/preview): sync theme from global with storage key
1 parent a6c9086 commit 1b65874

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

.storybook/ThemeChanger.tsx

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useEffect } from "react"
2+
import { useTheme } from "next-themes"
3+
4+
/**
5+
* Forces update of color mode to `next-theme` provider when
6+
* changing the mode from Storybook.
7+
*
8+
* This will keep color mode synced with the `theme` key in local storage.
9+
*/
10+
export const ThemeChanger = ({ theme }: { theme: "light" | "dark" }) => {
11+
const { setTheme } = useTheme()
12+
13+
useEffect(() => {
14+
console.log("Syncing color mode for SB and next-themes to: ", theme)
15+
setTheme(theme)
16+
}, [setTheme, theme])
17+
18+
return null
19+
}

.storybook/preview.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Preview } from "@storybook/react"
66
import ThemeProvider from "@/components/ThemeProvider"
77

88
import i18n, { baseLocales } from "./i18next"
9+
import { ThemeChanger } from "./ThemeChanger"
910

1011
import "@docsearch/css"
1112
import "../src/styles/global.css"
@@ -36,11 +37,14 @@ const preview: Preview = {
3637
},
3738
defaultTheme: "light",
3839
}),
39-
(Story) => (
40-
<ThemeProvider>
41-
<Story />
42-
</ThemeProvider>
43-
),
40+
(Story, context) => {
41+
return (
42+
<ThemeProvider>
43+
<ThemeChanger theme={context.globals.theme} />
44+
<Story />
45+
</ThemeProvider>
46+
)
47+
},
4448
],
4549
parameters: {
4650
i18n,

0 commit comments

Comments
 (0)