Skip to content

Commit 48b319d

Browse files
committed
feat(storybook): add dark mode toolbar toggle with synced shell theme
storybook-dark-mode addon's preset format is incompatible with Storybook v10 + rsbuild. Replaced with native globalTypes toolbar toggle (sun/moon icons) that toggles .dark class on html element and syncs the Storybook manager theme via channel events.
1 parent 3a01a80 commit 48b319d

5 files changed

Lines changed: 52 additions & 37 deletions

File tree

apps/storybook/.storybook/main.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const config: StorybookConfig = {
1010
'@storybook/addon-a11y',
1111
'@storybook/addon-interactions',
1212
'@storybook/addon-links',
13-
'storybook-dark-mode',
1413
'storybook-addon-pseudo-states',
1514
'storybook-addon-tag-badges',
1615
],

apps/storybook/.storybook/manager.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { addons } from 'storybook/manager-api'
2-
import { datumLight } from './theme'
2+
import { datumDark, datumLight } from './theme'
3+
4+
addons.register('datum-theme-switcher', (_api) => {
5+
const channel = addons.getChannel()
6+
7+
channel.on('updateGlobals', ({ globals }: { globals: Record<string, unknown> }) => {
8+
if ('theme' in globals) {
9+
addons.setConfig({
10+
theme: globals.theme === 'dark' ? datumDark : datumLight,
11+
})
12+
}
13+
})
14+
})
315

416
addons.setConfig({
517
theme: datumLight,

apps/storybook/.storybook/preview.tsx

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,54 @@
11
import type { Preview } from 'storybook-react-rsbuild'
22
import { ThemeProvider } from '@datum-cloud/datum-ui/theme'
3-
import { datumDark, datumLight } from './theme'
3+
import { useEffect } from 'react'
44
import { datumViewports } from './viewport'
55
import '../stories/storybook.css'
66

77
const preview: Preview = {
8+
globalTypes: {
9+
theme: {
10+
description: 'Toggle dark mode',
11+
toolbar: {
12+
title: 'Theme',
13+
icon: 'moon',
14+
items: [
15+
{ value: 'light', icon: 'sun', title: 'Light' },
16+
{ value: 'dark', icon: 'moon', title: 'Dark' },
17+
],
18+
dynamicTitle: true,
19+
},
20+
},
21+
},
22+
initialGlobals: {
23+
theme: 'light',
24+
},
825
decorators: [
9-
Story => (
10-
<ThemeProvider attribute="class" defaultTheme="light" enableSystem>
11-
<div style={{ padding: '1rem' }}>
12-
<Story />
13-
</div>
14-
</ThemeProvider>
15-
),
26+
(Story, context) => {
27+
const theme = context.globals.theme || 'light'
28+
29+
useEffect(() => {
30+
const html = document.documentElement
31+
if (theme === 'dark') {
32+
html.classList.add('dark')
33+
}
34+
else {
35+
html.classList.remove('dark')
36+
}
37+
}, [theme])
38+
39+
return (
40+
<ThemeProvider attribute="class" defaultTheme={theme} enableSystem={false} forcedTheme={theme}>
41+
<div style={{ padding: '1rem' }}>
42+
<Story />
43+
</div>
44+
</ThemeProvider>
45+
)
46+
},
1647
],
1748
parameters: {
1849
viewport: {
1950
viewports: datumViewports,
2051
},
21-
darkMode: {
22-
dark: datumDark,
23-
light: datumLight,
24-
classTarget: 'html',
25-
darkClass: 'dark',
26-
lightClass: '',
27-
stylePreview: true,
28-
},
2952
a11y: {
3053
config: {
3154
rules: [

apps/storybook/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"storybook": "^10",
3838
"storybook-addon-pseudo-states": "^10.3.4",
3939
"storybook-addon-tag-badges": "^3.1.0",
40-
"storybook-dark-mode": "^5.0.0",
4140
"storybook-react-rsbuild": "^2",
4241
"tailwind-scrollbar-hide": "^4.0.0",
4342
"tailwindcss": "^4",

pnpm-lock.yaml

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)