|
1 | | -import { useSyncExternalStore, useCallback } from 'react'; |
| 1 | +import { useSyncExternalStore, useCallback, useContext } from 'react'; |
2 | 2 | import type { ThemeMode } from '../config-provider/config-context'; |
| 3 | +import { ConfigContext } from '../config-provider/config-context'; |
| 4 | +import { getSystemTheme, themeStore } from './theme-store'; |
3 | 5 |
|
4 | | -const STORAGE_KEY = 'ty-theme'; |
5 | | -const THEME_ATTR = 'data-tiny-theme'; |
6 | | - |
7 | | -function getSystemTheme(): 'light' | 'dark' { |
8 | | - if (typeof window === 'undefined') return 'light'; |
9 | | - return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; |
10 | | -} |
11 | | - |
12 | | -function applyTheme(mode: ThemeMode): void { |
13 | | - if (typeof document === 'undefined') return; |
14 | | - document.documentElement.setAttribute(THEME_ATTR, mode); |
15 | | -} |
16 | | - |
17 | | -function readDomTheme(): ThemeMode | null { |
18 | | - if (typeof document === 'undefined') return null; |
19 | | - const value = document.documentElement.getAttribute(THEME_ATTR); |
20 | | - return value === 'light' || value === 'dark' || value === 'system' ? value : null; |
21 | | -} |
22 | | - |
23 | | -function readStoredTheme(): ThemeMode { |
24 | | - if (typeof localStorage === 'undefined') return 'light'; |
25 | | - return (localStorage.getItem(STORAGE_KEY) as ThemeMode) || 'light'; |
26 | | -} |
27 | | - |
28 | | -function readInitialTheme(): ThemeMode { |
29 | | - return readDomTheme() ?? readStoredTheme(); |
30 | | -} |
31 | | - |
32 | | -// ---- Shared store ---- |
33 | | -let currentMode: ThemeMode = readInitialTheme(); |
34 | | -const listeners = new Set<() => void>(); |
35 | | - |
36 | | -function getSnapshot(): ThemeMode { |
37 | | - return readDomTheme() ?? currentMode; |
38 | | -} |
39 | | - |
40 | | -function getServerSnapshot(): ThemeMode { |
41 | | - return 'light'; |
42 | | -} |
43 | | - |
44 | | -function subscribe(cb: () => void): () => void { |
45 | | - const syncFromDom = () => { |
46 | | - const domTheme = readDomTheme(); |
47 | | - |
48 | | - if (domTheme && domTheme !== currentMode) { |
49 | | - currentMode = domTheme; |
50 | | - cb(); |
51 | | - } |
52 | | - }; |
53 | | - |
54 | | - listeners.add(cb); |
55 | | - syncFromDom(); |
56 | | - |
57 | | - const observer = |
58 | | - typeof MutationObserver !== 'undefined' && typeof document !== 'undefined' |
59 | | - ? new MutationObserver(() => { |
60 | | - syncFromDom(); |
61 | | - }) |
62 | | - : null; |
63 | | - |
64 | | - observer?.observe(document.documentElement, { |
65 | | - attributes: true, |
66 | | - attributeFilter: [THEME_ATTR], |
67 | | - }); |
68 | | - |
69 | | - return () => { |
70 | | - listeners.delete(cb); |
71 | | - observer?.disconnect(); |
72 | | - }; |
73 | | -} |
74 | | - |
75 | | -function setThemeMode(next: ThemeMode): void { |
76 | | - currentMode = next; |
77 | | - if (typeof localStorage !== 'undefined') { |
78 | | - localStorage.setItem(STORAGE_KEY, next); |
79 | | - } |
80 | | - applyTheme(next); |
81 | | - listeners.forEach((cb) => cb()); |
| 6 | +export interface UseThemeOptions { |
| 7 | + /** |
| 8 | + * Initial mode to hydrate the store with. Use on first mount for SSR to |
| 9 | + * align with the mode written to the document by a pre-hydration script. |
| 10 | + */ |
| 11 | + initialMode?: ThemeMode; |
82 | 12 | } |
83 | 13 |
|
84 | | -function emit(): void { |
85 | | - listeners.forEach((cb) => cb()); |
86 | | -} |
87 | | - |
88 | | -// Listen for system preference changes at module level |
89 | | -if (typeof document !== 'undefined') { |
90 | | - applyTheme(currentMode); |
91 | | -} |
92 | | - |
93 | | -if (typeof window !== 'undefined') { |
94 | | - if (typeof window.matchMedia === 'function') { |
95 | | - const mediaQueryList = window.matchMedia('(prefers-color-scheme: dark)'); |
96 | | - const handleSystemThemeChange = () => { |
97 | | - if (currentMode === 'system') { |
98 | | - emit(); |
99 | | - } |
100 | | - }; |
101 | | - |
102 | | - if (typeof mediaQueryList.addEventListener === 'function') { |
103 | | - mediaQueryList.addEventListener('change', handleSystemThemeChange); |
104 | | - } else if (typeof mediaQueryList.addListener === 'function') { |
105 | | - mediaQueryList.addListener(handleSystemThemeChange); |
106 | | - } |
| 14 | +export function useTheme(options?: UseThemeOptions) { |
| 15 | + if (options?.initialMode) { |
| 16 | + themeStore.hydrate(options.initialMode); |
107 | 17 | } |
108 | 18 |
|
109 | | - window.addEventListener('storage', (event) => { |
110 | | - if (event.key !== STORAGE_KEY) { |
111 | | - return; |
112 | | - } |
113 | | - |
114 | | - currentMode = event.newValue === 'light' || event.newValue === 'dark' || event.newValue === 'system' |
115 | | - ? event.newValue |
116 | | - : 'light'; |
117 | | - applyTheme(currentMode); |
118 | | - emit(); |
119 | | - }); |
120 | | -} |
121 | | - |
122 | | -// ---- Hook ---- |
123 | | -export function useTheme() { |
124 | | - const mode = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot); |
| 19 | + const contextMode = useContext(ConfigContext).theme; |
| 20 | + const storeMode = useSyncExternalStore( |
| 21 | + themeStore.subscribe, |
| 22 | + themeStore.getSnapshot, |
| 23 | + themeStore.getServerSnapshot |
| 24 | + ); |
| 25 | + const mode: ThemeMode = contextMode ?? storeMode; |
125 | 26 |
|
126 | 27 | const resolvedTheme: 'light' | 'dark' = mode === 'system' ? getSystemTheme() : mode; |
127 | 28 |
|
128 | | - const setMode = useCallback((newMode: ThemeMode) => { |
129 | | - setThemeMode(newMode); |
| 29 | + const setMode = useCallback((next: ThemeMode) => { |
| 30 | + themeStore.setMode(next); |
130 | 31 | }, []); |
131 | 32 |
|
132 | 33 | const toggle = useCallback(() => { |
133 | | - const resolved = currentMode === 'system' ? getSystemTheme() : currentMode; |
134 | | - setThemeMode(resolved === 'light' ? 'dark' : 'light'); |
| 34 | + const active = themeStore.getSnapshot(); |
| 35 | + const resolved = active === 'system' ? getSystemTheme() : active; |
| 36 | + themeStore.setMode(resolved === 'light' ? 'dark' : 'light'); |
135 | 37 | }, []); |
136 | 38 |
|
137 | 39 | return { mode, resolvedTheme, setMode, toggle }; |
|
0 commit comments