|
1 | 1 | import * as React from 'react';
|
2 |
| -import { Preview } from '@storybook/react'; |
3 |
| -import { useDarkMode } from 'storybook-dark-mode'; |
| 2 | +import { Preview } from '@storybook/react-vite'; |
| 3 | +import { useDarkMode } from '@vueless/storybook-dark-mode'; |
4 | 4 | import { parse } from 'qs';
|
5 | 5 |
|
6 | 6 | import { DesignSystemProvider, Box, darkTheme, lightTheme, type BoxProps } from '@strapi/design-system';
|
7 | 7 |
|
8 |
| -import { DocsContainer, Unstyled } from '@storybook/blocks'; |
| 8 | +import { DocsContainer, Unstyled } from '@storybook/addon-docs/blocks'; |
9 | 9 | import { styled, DefaultTheme } from 'styled-components';
|
10 | 10 | import { MARKDOWN_OVERRIDES } from '../components/Markdown';
|
11 | 11 |
|
@@ -45,15 +45,87 @@ const createCustomTheme = (theme: DefaultTheme, base: 'light' | 'dark' = 'light'
|
45 | 45 |
|
46 | 46 | const themeQueryURL = parse(document.location.search).theme;
|
47 | 47 |
|
48 |
| -const Theme = ({ children, ...props }: BoxProps) => { |
49 |
| - const isDarkAddon = useDarkMode(); |
50 |
| - const [isDark, setIsDark] = React.useState(themeQueryURL || isDarkAddon); |
| 48 | +// Reusable hook to get dark mode state from localStorage |
| 49 | +export const useLocalStorageDarkMode = () => { |
| 50 | + const [isDark, setIsDark] = React.useState(() => { |
| 51 | + if (themeQueryURL) return themeQueryURL; |
| 52 | + |
| 53 | + const themeParameters = localStorage.getItem('sb-addon-themes-3'); |
| 54 | + let theme = 'light'; |
| 55 | + try { |
| 56 | + theme = JSON.parse(themeParameters || '{}').current; |
| 57 | + } catch (error) { |
| 58 | + console.error(error); |
| 59 | + } |
| 60 | + return theme === 'dark'; |
| 61 | + }); |
51 | 62 |
|
52 | 63 | React.useEffect(() => {
|
53 |
| - if (!themeQueryURL && isDarkAddon !== isDark) { |
54 |
| - setIsDark(isDarkAddon); |
| 64 | + const handleStorageChange = () => { |
| 65 | + const themeParameters = localStorage.getItem('sb-addon-themes-3'); |
| 66 | + let theme = 'light'; |
| 67 | + try { |
| 68 | + theme = JSON.parse(themeParameters || '{}').current; |
| 69 | + } catch (error) { |
| 70 | + console.error(error); |
| 71 | + } |
| 72 | + setIsDark(theme === 'dark'); |
| 73 | + }; |
| 74 | + |
| 75 | + window.addEventListener('storage', handleStorageChange); |
| 76 | + return () => window.removeEventListener('storage', handleStorageChange); |
| 77 | + }, []); |
| 78 | + |
| 79 | + return isDark; |
| 80 | +}; |
| 81 | + |
| 82 | +// Safe hook that tries useDarkMode first, then falls back to localStorage |
| 83 | +export const useSafeDarkMode = () => { |
| 84 | + try { |
| 85 | + return useDarkMode(); |
| 86 | + } catch (error) { |
| 87 | + return useLocalStorageDarkMode(); |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +const Theme = ({ children, isDarkMode, ...props }: BoxProps & { isDarkMode?: boolean }) => { |
| 92 | + const [isDark, setIsDark] = React.useState(() => { |
| 93 | + if (themeQueryURL) return themeQueryURL; |
| 94 | + if (isDarkMode !== undefined) return isDarkMode; |
| 95 | + |
| 96 | + const themeParameters = localStorage.getItem('sb-addon-themes-3'); |
| 97 | + let theme = 'light'; |
| 98 | + try { |
| 99 | + theme = JSON.parse(themeParameters || '{}').current; |
| 100 | + } catch (error) { |
| 101 | + console.error(error); |
55 | 102 | }
|
56 |
| - }, [isDarkAddon, isDark]); |
| 103 | + return theme === 'dark'; |
| 104 | + }); |
| 105 | + |
| 106 | + React.useEffect(() => { |
| 107 | + if (!themeQueryURL && isDarkMode !== undefined && isDarkMode !== isDark) { |
| 108 | + setIsDark(isDarkMode); |
| 109 | + } |
| 110 | + }, [isDarkMode, isDark]); |
| 111 | + |
| 112 | + // Listen for theme changes in localStorage for docs |
| 113 | + React.useEffect(() => { |
| 114 | + if (isDarkMode === undefined) { |
| 115 | + const handleStorageChange = () => { |
| 116 | + const themeParameters = localStorage.getItem('sb-addon-themes-3'); |
| 117 | + let theme = 'light'; |
| 118 | + try { |
| 119 | + theme = JSON.parse(themeParameters || '{}').current; |
| 120 | + } catch (error) { |
| 121 | + console.error(error); |
| 122 | + } |
| 123 | + setIsDark(theme === 'dark'); |
| 124 | + }; |
| 125 | + window.addEventListener('storage', handleStorageChange); |
| 126 | + return () => window.removeEventListener('storage', handleStorageChange); |
| 127 | + } |
| 128 | + }, [isDarkMode]); |
57 | 129 |
|
58 | 130 | return (
|
59 | 131 | <DesignSystemProvider locale="en" theme={isDark ? darkTheme : lightTheme}>
|
@@ -109,11 +181,14 @@ const Main = styled(Box)`
|
109 | 181 |
|
110 | 182 | const preview: Preview = {
|
111 | 183 | decorators: [
|
112 |
| - (Story) => ( |
113 |
| - <Theme> |
114 |
| - <Story /> |
115 |
| - </Theme> |
116 |
| - ), |
| 184 | + (Story) => { |
| 185 | + const isDarkMode = useDarkMode(); |
| 186 | + return ( |
| 187 | + <Theme isDarkMode={isDarkMode}> |
| 188 | + <Story /> |
| 189 | + </Theme> |
| 190 | + ); |
| 191 | + }, |
117 | 192 | ],
|
118 | 193 |
|
119 | 194 | parameters: {
|
|
0 commit comments