|
20 | 20 | import { addons } from 'storybook/internal/manager-api'; |
21 | 21 | import { themes } from 'storybook/internal/theming'; |
22 | 22 |
|
23 | | -addons.setConfig({ |
24 | | - theme: { |
25 | | - ...themes.dark, |
26 | | - appBg: '#000000', |
27 | | - barBg: '#000000', |
28 | | - brandTitle: 'WSO2 Oxygen UI', |
29 | | - brandUrl: 'https://github.com/wso2/oxygen-ui', |
30 | | - brandImage: './oxygen-ui-react-logo-inverted.svg' |
| 23 | +const getThemeMode = () => { |
| 24 | + return localStorage.getItem('mui-mode') === 'light' ? 'light' : 'dark'; |
| 25 | +}; |
| 26 | + |
| 27 | +const applyTheme = (mode) => { |
| 28 | + addons.setConfig({ |
| 29 | + theme: { |
| 30 | + ...(mode === 'light' ? themes.light : themes.dark), |
| 31 | + appBg: (mode === 'light' ? '#ffffff' : '#000000'), |
| 32 | + barBg: (mode === 'light' ? '#f0f0f0' : '#000000'), |
| 33 | + brandTitle: 'WSO2 Oxygen UI', |
| 34 | + brandUrl: 'https://github.com/wso2/oxygen-ui', |
| 35 | + brandImage: (mode === 'light' ? './oxygen-ui-react-logo.svg' : './oxygen-ui-react-logo-inverted.svg') |
| 36 | + } |
| 37 | + }); |
| 38 | +}; |
| 39 | + |
| 40 | +// Apply initial theme |
| 41 | +let currentMode = getThemeMode(); |
| 42 | +applyTheme(currentMode); |
| 43 | + |
| 44 | +// Poll for changes in localStorage (checks every 500ms) |
| 45 | +setInterval(() => { |
| 46 | + const newMode = getThemeMode(); |
| 47 | + if (newMode !== currentMode) { |
| 48 | + currentMode = newMode; |
| 49 | + applyTheme(currentMode); |
| 50 | + } |
| 51 | +}, 500); |
| 52 | + |
| 53 | +// Also listen for storage events (works across tabs) |
| 54 | +window.addEventListener('storage', (e) => { |
| 55 | + if (e.key === 'mui-mode') { |
| 56 | + const newMode = getThemeMode(); |
| 57 | + if (newMode !== currentMode) { |
| 58 | + currentMode = newMode; |
| 59 | + applyTheme(currentMode); |
| 60 | + } |
31 | 61 | } |
32 | 62 | }); |
0 commit comments