File tree Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Expand file tree Collapse file tree 2 files changed +28
-5
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import type { Preview } from "@storybook/react"
6
6
import ThemeProvider from "@/components/ThemeProvider"
7
7
8
8
import i18n , { baseLocales } from "./i18next"
9
+ import { ThemeChanger } from "./ThemeChanger"
9
10
10
11
import "@docsearch/css"
11
12
import "../src/styles/global.css"
@@ -36,11 +37,14 @@ const preview: Preview = {
36
37
} ,
37
38
defaultTheme : "light" ,
38
39
} ) ,
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
+ } ,
44
48
] ,
45
49
parameters : {
46
50
i18n,
You can’t perform that action at this time.
0 commit comments