@thangnqdev/ui 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-DUPPPGXY.js → chunk-2ANJFOIU.js} +34 -32
- package/dist/chunk-2ANJFOIU.js.map +1 -0
- package/dist/core/index.cjs +31 -29
- package/dist/core/index.cjs.map +1 -1
- package/dist/core/index.js +1 -1
- package/dist/index.cjs +31 -29
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1 -1
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/src/core/ThemeProvider.tsx +75 -57
- package/ui-contract.json +1 -1
- package/dist/chunk-DUPPPGXY.js.map +0 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __export, cn } from './chunk-4S6L5V7X.js';
|
|
2
|
-
import { createContext, useState, useCallback, useEffect, useMemo, useContext } from 'react';
|
|
2
|
+
import { createContext, useState, useRef, useCallback, useEffect, useMemo, useContext, useLayoutEffect } from 'react';
|
|
3
3
|
import { jsx } from 'react/jsx-runtime';
|
|
4
4
|
|
|
5
5
|
// src/core/index.ts
|
|
@@ -10,6 +10,10 @@ __export(core_exports, {
|
|
|
10
10
|
useTheme: () => useTheme
|
|
11
11
|
});
|
|
12
12
|
var ThemeContext = createContext(void 0);
|
|
13
|
+
var useIsomorphicLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
14
|
+
function isTheme(value) {
|
|
15
|
+
return value === "dark" || value === "light" || value === "system";
|
|
16
|
+
}
|
|
13
17
|
function ThemeProvider({
|
|
14
18
|
children,
|
|
15
19
|
defaultTheme = "light",
|
|
@@ -21,6 +25,7 @@ function ThemeProvider({
|
|
|
21
25
|
const [resolvedTheme, setResolvedTheme] = useState(
|
|
22
26
|
defaultTheme === "dark" ? "dark" : "light"
|
|
23
27
|
);
|
|
28
|
+
const pendingStoredThemeRef = useRef(null);
|
|
24
29
|
const computeResolved = useCallback(
|
|
25
30
|
(t) => {
|
|
26
31
|
if (t === "system" && enableSystem && typeof window !== "undefined" && window.matchMedia) {
|
|
@@ -30,56 +35,53 @@ function ThemeProvider({
|
|
|
30
35
|
},
|
|
31
36
|
[enableSystem]
|
|
32
37
|
);
|
|
33
|
-
|
|
38
|
+
const applyResolvedTheme = useCallback(
|
|
39
|
+
(nextResolvedTheme) => {
|
|
40
|
+
setResolvedTheme(nextResolvedTheme);
|
|
41
|
+
if (typeof document === "undefined") return;
|
|
42
|
+
const root = document.documentElement;
|
|
43
|
+
root.setAttribute(attribute, nextResolvedTheme);
|
|
44
|
+
root.classList.toggle("dark", nextResolvedTheme === "dark");
|
|
45
|
+
root.classList.toggle("light", nextResolvedTheme === "light");
|
|
46
|
+
},
|
|
47
|
+
[attribute]
|
|
48
|
+
);
|
|
49
|
+
useIsomorphicLayoutEffect(() => {
|
|
34
50
|
if (typeof window === "undefined") return;
|
|
51
|
+
let initialTheme = defaultTheme;
|
|
35
52
|
try {
|
|
36
53
|
const stored = window.localStorage?.getItem(storageKey);
|
|
37
|
-
if (stored
|
|
38
|
-
setThemeState(stored);
|
|
39
|
-
}
|
|
54
|
+
if (isTheme(stored)) initialTheme = stored;
|
|
40
55
|
} catch {
|
|
41
56
|
}
|
|
57
|
+
pendingStoredThemeRef.current = { storageKey, theme: initialTheme };
|
|
58
|
+
setThemeState((currentTheme) => currentTheme === initialTheme ? currentTheme : initialTheme);
|
|
42
59
|
}, [storageKey]);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
root.setAttribute(attribute, currentResolved);
|
|
49
|
-
if (currentResolved === "dark") {
|
|
50
|
-
root.classList.add("dark");
|
|
51
|
-
root.classList.remove("light");
|
|
52
|
-
} else {
|
|
53
|
-
root.classList.add("light");
|
|
54
|
-
root.classList.remove("dark");
|
|
60
|
+
useIsomorphicLayoutEffect(() => {
|
|
61
|
+
const pendingStoredTheme = pendingStoredThemeRef.current;
|
|
62
|
+
if (pendingStoredTheme?.storageKey === storageKey) {
|
|
63
|
+
if (pendingStoredTheme.theme !== theme) return;
|
|
64
|
+
pendingStoredThemeRef.current = null;
|
|
55
65
|
}
|
|
66
|
+
const currentResolved = computeResolved(theme);
|
|
67
|
+
applyResolvedTheme(currentResolved);
|
|
56
68
|
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
57
69
|
try {
|
|
58
70
|
window.localStorage.setItem(storageKey, theme);
|
|
59
71
|
} catch {
|
|
60
72
|
}
|
|
61
73
|
}
|
|
62
|
-
}, [theme,
|
|
74
|
+
}, [theme, computeResolved, applyResolvedTheme, storageKey]);
|
|
63
75
|
useEffect(() => {
|
|
64
76
|
if (theme !== "system" || !enableSystem || typeof window === "undefined" || !window.matchMedia) return;
|
|
65
77
|
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
66
78
|
const handleChange = () => {
|
|
67
79
|
const newResolved = mediaQuery.matches ? "dark" : "light";
|
|
68
|
-
|
|
69
|
-
if (typeof document !== "undefined") {
|
|
70
|
-
document.documentElement.setAttribute(attribute, newResolved);
|
|
71
|
-
if (newResolved === "dark") {
|
|
72
|
-
document.documentElement.classList.add("dark");
|
|
73
|
-
document.documentElement.classList.remove("light");
|
|
74
|
-
} else {
|
|
75
|
-
document.documentElement.classList.add("light");
|
|
76
|
-
document.documentElement.classList.remove("dark");
|
|
77
|
-
}
|
|
78
|
-
}
|
|
80
|
+
applyResolvedTheme(newResolved);
|
|
79
81
|
};
|
|
80
82
|
mediaQuery.addEventListener("change", handleChange);
|
|
81
83
|
return () => mediaQuery.removeEventListener("change", handleChange);
|
|
82
|
-
}, [theme, enableSystem,
|
|
84
|
+
}, [theme, enableSystem, applyResolvedTheme]);
|
|
83
85
|
const toggleTheme = () => {
|
|
84
86
|
setThemeState((prev) => {
|
|
85
87
|
if (prev === "dark") return "light";
|
|
@@ -106,5 +108,5 @@ function useTheme() {
|
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
export { ThemeProvider, core_exports, useTheme };
|
|
109
|
-
//# sourceMappingURL=chunk-
|
|
110
|
-
//# sourceMappingURL=chunk-
|
|
111
|
+
//# sourceMappingURL=chunk-2ANJFOIU.js.map
|
|
112
|
+
//# sourceMappingURL=chunk-2ANJFOIU.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/core/index.ts","../src/core/ThemeProvider.tsx"],"names":[],"mappings":";;;;;AAAA,IAAA,YAAA,GAAA;AAAA,QAAA,CAAA,YAAA,EAAA;AAAA,EAAA,aAAA,EAAA,MAAA,aAAA;AAAA,EAAA,EAAA,EAAA,MAAA,EAAA;AAAA,EAAA,QAAA,EAAA,MAAA;AAAA,CAAA,CAAA;AC4BA,IAAM,YAAA,GAAe,cAA4C,MAAS,CAAA;AAE1E,IAAM,yBAAA,GAA4B,OAAO,MAAA,KAAW,WAAA,GAAc,SAAA,GAAY,eAAA;AAE9E,SAAS,QAAQ,KAAA,EAAsC;AACrD,EAAA,OAAO,KAAA,KAAU,MAAA,IAAU,KAAA,KAAU,OAAA,IAAW,KAAA,KAAU,QAAA;AAC5D;AAEO,SAAS,aAAA,CAAc;AAAA,EAC5B,QAAA;AAAA,EACA,YAAA,GAAe,OAAA;AAAA,EACf,UAAA,GAAa,UAAA;AAAA,EACb,SAAA,GAAY,YAAA;AAAA,EACZ,YAAA,GAAe;AACjB,CAAA,EAAuB;AAErB,EAAA,MAAM,CAAC,KAAA,EAAO,aAAa,CAAA,GAAI,SAAgB,YAAY,CAAA;AAC3D,EAAA,MAAM,CAAC,aAAA,EAAe,gBAAgB,CAAA,GAAI,QAAA;AAAA,IACxC,YAAA,KAAiB,SAAS,MAAA,GAAS;AAAA,GACrC;AACA,EAAA,MAAM,qBAAA,GAAwB,OAAoD,IAAI,CAAA;AAEtF,EAAA,MAAM,eAAA,GAAkB,WAAA;AAAA,IACtB,CAAC,CAAA,KAA+B;AAC9B,MAAA,IAAI,MAAM,QAAA,IAAY,YAAA,IAAgB,OAAO,M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c,kBAAkB,CAAC,CAAA;AAE5C,EAAA,MAAM,cAAc,MAAM;AACxB,IAAA,aAAA,CAAc,CAAC,IAAA,KAAS;AACtB,MAAA,IAAI,IAAA,KAAS,QAAQ,OAAO,OAAA;AAC5B,MAAA,OAAO,MAAA;AAAA,IACT,CAAC,CAAA;AAAA,EACH,CAAA;AAEA,EAAA,MAAM,KAAA,GAAQ,OAAA;AAAA,IACZ,OAAO;AAAA,MACL,KAAA;AAAA,MACA,aAAA;AAAA,MACA,QAAA,EAAU,aAAA;AAAA,MACV;AAAA,KACF,CAAA;AAAA,IACA,CAAC,OAAO,aAAa;AAAA,GACvB;AAEA,EAAA,uBAAO,GAAA,CAAC,YAAA,CAAa,QAAA,EAAb,EAAsB,OAAe,QAAA,EAAS,CAAA;AACxD;AAEO,SAAS,QAAA,GAA6B;AAC3C,EAAA,MAAM,OAAA,GAAU,WAAW,YAAY,CAAA;AACvC,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,8CAA8C,CAAA;AAAA,EAChE;AACA,EAAA,OAAO,OAAA;AACT","file":"chunk-2ANJFOIU.js","sourcesContent":["export * from './ThemeProvider';\r\nexport * from '../utils/cn';\r\n","import React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useLayoutEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\n\r\nexport type Theme = 'dark' | 'light' | 'system';\r\n\r\nexport interface ThemeProviderProps {\r\n children: React.ReactNode;\r\n defaultTheme?: Theme;\r\n storageKey?: string;\r\n attribute?: string;\r\n enableSystem?: boolean;\r\n}\r\n\r\ninterface ThemeContextType {\r\n theme: Theme;\r\n resolvedTheme: 'dark' | 'light';\r\n setTheme: (theme: Theme) => void;\r\n toggleTheme: () => void;\r\n}\r\n\r\nconst ThemeContext = createContext<ThemeContextType | undefined>(undefined);\n\nconst useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;\n\nfunction isTheme(value: string | null): value is Theme {\n return value === 'dark' || value === 'light' || value === 'system';\n}\n\r\nexport function ThemeProvider({\r\n children,\r\n defaultTheme = 'light',\n storageKey = 'ui-theme',\r\n attribute = 'data-theme',\r\n enableSystem = true,\r\n}: ThemeProviderProps) {\r\n // Always initialize identically on SSR and initial client pass to avoid hydration mismatches\r\n const [theme, setThemeState] = useState<Theme>(defaultTheme);\r\n const [resolvedTheme, setResolvedTheme] = useState<'dark' | 'light'>(\n defaultTheme === 'dark' ? 'dark' : 'light'\n );\n const pendingStoredThemeRef = useRef<{ storageKey: string; theme: Theme } | null>(null);\n\r\n const computeResolved = useCallback(\r\n (t: Theme): 'dark' | 'light' => {\r\n if (t === 'system' && enableSystem && typeof window !== 'undefined' && window.matchMedia) {\r\n return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';\r\n }\r\n return t === 'dark' ? 'dark' : 'light';\r\n },\r\n [enableSystem]\r\n );\r\n\r\n const applyResolvedTheme = useCallback(\n (nextResolvedTheme: 'dark' | 'light') => {\n setResolvedTheme(nextResolvedTheme);\n\n if (typeof document === 'undefined') return;\n\n const root = document.documentElement;\n root.setAttribute(attribute, nextResolvedTheme);\n root.classList.toggle('dark', nextResolvedTheme === 'dark');\n root.classList.toggle('light', nextResolvedTheme === 'light');\n },\n [attribute]\n );\n\n // Read the persisted value before paint. The pending ref prevents the DOM/storage\n // sync below from writing defaultTheme over a different persisted theme first.\n useIsomorphicLayoutEffect(() => {\n if (typeof window === 'undefined') return;\n\n let initialTheme = defaultTheme;\n try {\n const stored = window.localStorage?.getItem(storageKey);\n if (isTheme(stored)) initialTheme = stored;\n } catch {\n // Ignore storage access errors in restricted iframe/incognito environments\n }\n\n pendingStoredThemeRef.current = { storageKey, theme: initialTheme };\n setThemeState((currentTheme) => (currentTheme === initialTheme ? currentTheme : initialTheme));\n }, [storageKey]);\n\n // Apply user changes before paint. Storage initialization and user updates now\n // have a single write direction, so a click cannot oscillate light <-> dark.\n useIsomorphicLayoutEffect(() => {\n const pendingStoredTheme = pendingStoredThemeRef.current;\n if (pendingStoredTheme?.storageKey === storageKey) {\n if (pendingStoredTheme.theme !== theme) return;\n pendingStoredThemeRef.current = null;\n }\n\n const currentResolved = computeResolved(theme);\n applyResolvedTheme(currentResolved);\n\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.setItem(storageKey, theme);\r\n } catch {\r\n // Ignore storage access errors\n }\n }\n }, [theme, computeResolved, applyResolvedTheme, storageKey]);\n\r\n // System media query listener\r\n useEffect(() => {\r\n if (theme !== 'system' || !enableSystem || typeof window === 'undefined' || !window.matchMedia) return;\r\n\r\n const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');\n const handleChange = () => {\n const newResolved = mediaQuery.matches ? 'dark' : 'light';\n applyResolvedTheme(newResolved);\n };\n\r\n mediaQuery.addEventListener('change', handleChange);\n return () => mediaQuery.removeEventListener('change', handleChange);\n }, [theme, enableSystem, applyResolvedTheme]);\n\r\n const toggleTheme = () => {\r\n setThemeState((prev) => {\r\n if (prev === 'dark') return 'light';\r\n return 'dark';\r\n });\r\n };\r\n\r\n const value = useMemo(\r\n () => ({\r\n theme,\r\n resolvedTheme,\r\n setTheme: setThemeState,\r\n toggleTheme,\r\n }),\r\n [theme, resolvedTheme]\r\n );\r\n\r\n return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;\r\n}\r\n\r\nexport function useTheme(): ThemeContextType {\r\n const context = useContext(ThemeContext);\r\n if (!context) {\r\n throw new Error('useTheme must be used within a ThemeProvider');\r\n }\r\n return context;\r\n}\r\n"]}
|
package/dist/core/index.cjs
CHANGED
|
@@ -7,6 +7,10 @@ var tailwindMerge = require('tailwind-merge');
|
|
|
7
7
|
|
|
8
8
|
// src/core/ThemeProvider.tsx
|
|
9
9
|
var ThemeContext = react.createContext(void 0);
|
|
10
|
+
var useIsomorphicLayoutEffect = typeof window === "undefined" ? react.useEffect : react.useLayoutEffect;
|
|
11
|
+
function isTheme(value) {
|
|
12
|
+
return value === "dark" || value === "light" || value === "system";
|
|
13
|
+
}
|
|
10
14
|
function ThemeProvider({
|
|
11
15
|
children,
|
|
12
16
|
defaultTheme = "light",
|
|
@@ -18,6 +22,7 @@ function ThemeProvider({
|
|
|
18
22
|
const [resolvedTheme, setResolvedTheme] = react.useState(
|
|
19
23
|
defaultTheme === "dark" ? "dark" : "light"
|
|
20
24
|
);
|
|
25
|
+
const pendingStoredThemeRef = react.useRef(null);
|
|
21
26
|
const computeResolved = react.useCallback(
|
|
22
27
|
(t) => {
|
|
23
28
|
if (t === "system" && enableSystem && typeof window !== "undefined" && window.matchMedia) {
|
|
@@ -27,56 +32,53 @@ function ThemeProvider({
|
|
|
27
32
|
},
|
|
28
33
|
[enableSystem]
|
|
29
34
|
);
|
|
30
|
-
react.
|
|
35
|
+
const applyResolvedTheme = react.useCallback(
|
|
36
|
+
(nextResolvedTheme) => {
|
|
37
|
+
setResolvedTheme(nextResolvedTheme);
|
|
38
|
+
if (typeof document === "undefined") return;
|
|
39
|
+
const root = document.documentElement;
|
|
40
|
+
root.setAttribute(attribute, nextResolvedTheme);
|
|
41
|
+
root.classList.toggle("dark", nextResolvedTheme === "dark");
|
|
42
|
+
root.classList.toggle("light", nextResolvedTheme === "light");
|
|
43
|
+
},
|
|
44
|
+
[attribute]
|
|
45
|
+
);
|
|
46
|
+
useIsomorphicLayoutEffect(() => {
|
|
31
47
|
if (typeof window === "undefined") return;
|
|
48
|
+
let initialTheme = defaultTheme;
|
|
32
49
|
try {
|
|
33
50
|
const stored = window.localStorage?.getItem(storageKey);
|
|
34
|
-
if (stored
|
|
35
|
-
setThemeState(stored);
|
|
36
|
-
}
|
|
51
|
+
if (isTheme(stored)) initialTheme = stored;
|
|
37
52
|
} catch {
|
|
38
53
|
}
|
|
54
|
+
pendingStoredThemeRef.current = { storageKey, theme: initialTheme };
|
|
55
|
+
setThemeState((currentTheme) => currentTheme === initialTheme ? currentTheme : initialTheme);
|
|
39
56
|
}, [storageKey]);
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
root.setAttribute(attribute, currentResolved);
|
|
46
|
-
if (currentResolved === "dark") {
|
|
47
|
-
root.classList.add("dark");
|
|
48
|
-
root.classList.remove("light");
|
|
49
|
-
} else {
|
|
50
|
-
root.classList.add("light");
|
|
51
|
-
root.classList.remove("dark");
|
|
57
|
+
useIsomorphicLayoutEffect(() => {
|
|
58
|
+
const pendingStoredTheme = pendingStoredThemeRef.current;
|
|
59
|
+
if (pendingStoredTheme?.storageKey === storageKey) {
|
|
60
|
+
if (pendingStoredTheme.theme !== theme) return;
|
|
61
|
+
pendingStoredThemeRef.current = null;
|
|
52
62
|
}
|
|
63
|
+
const currentResolved = computeResolved(theme);
|
|
64
|
+
applyResolvedTheme(currentResolved);
|
|
53
65
|
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
54
66
|
try {
|
|
55
67
|
window.localStorage.setItem(storageKey, theme);
|
|
56
68
|
} catch {
|
|
57
69
|
}
|
|
58
70
|
}
|
|
59
|
-
}, [theme,
|
|
71
|
+
}, [theme, computeResolved, applyResolvedTheme, storageKey]);
|
|
60
72
|
react.useEffect(() => {
|
|
61
73
|
if (theme !== "system" || !enableSystem || typeof window === "undefined" || !window.matchMedia) return;
|
|
62
74
|
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
63
75
|
const handleChange = () => {
|
|
64
76
|
const newResolved = mediaQuery.matches ? "dark" : "light";
|
|
65
|
-
|
|
66
|
-
if (typeof document !== "undefined") {
|
|
67
|
-
document.documentElement.setAttribute(attribute, newResolved);
|
|
68
|
-
if (newResolved === "dark") {
|
|
69
|
-
document.documentElement.classList.add("dark");
|
|
70
|
-
document.documentElement.classList.remove("light");
|
|
71
|
-
} else {
|
|
72
|
-
document.documentElement.classList.add("light");
|
|
73
|
-
document.documentElement.classList.remove("dark");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
77
|
+
applyResolvedTheme(newResolved);
|
|
76
78
|
};
|
|
77
79
|
mediaQuery.addEventListener("change", handleChange);
|
|
78
80
|
return () => mediaQuery.removeEventListener("change", handleChange);
|
|
79
|
-
}, [theme, enableSystem,
|
|
81
|
+
}, [theme, enableSystem, applyResolvedTheme]);
|
|
80
82
|
const toggleTheme = () => {
|
|
81
83
|
setThemeState((prev) => {
|
|
82
84
|
if (prev === "dark") return "light";
|
package/dist/core/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/core/ThemeProvider.tsx","../../src/utils/cn.ts"],"names":["createContext","useState","
|
|
1
|
+
{"version":3,"sources":["../../src/core/ThemeProvider.tsx","../../src/utils/cn.ts"],"names":["createContext","useEffect","useLayoutEffect","useState","useRef","useCallback","useMemo","jsx","useContext","twMerge","clsx"],"mappings":";;;;;;;;AA4BA,IAAM,YAAA,GAAeA,oBAA4C,MAAS,CAAA;AAE1E,IAAM,yBAAA,GAA4B,OAAO,MAAA,KAAW,WAAA,GAAcC,eAAA,GAAYC,qBAAA;AAE9E,SAAS,QAAQ,KAAA,EAAsC;AACrD,EAAA,OAAO,KAAA,KAAU,MAAA,IAAU,KAAA,KAAU,OAAA,IAAW,KAAA,KAAU,QAAA;AAC5D;AAEO,SAAS,aAAA,CAAc;AAAA,EAC5B,QAAA;AAAA,EACA,YAAA,GAAe,OAAA;AAAA,EACf,UAAA,GAAa,UAAA;AAAA,EACb,SAAA,GAAY,YAAA;AAAA,EACZ,YAAA,GAAe;AACjB,CAAA,EAAuB;AAErB,EAAA,MAAM,CAAC,KAAA,EAAO,aAAa,CAAA,GAAIC,eAAgB,YAAY,CAAA;AAC3D,EAAA,MAAM,CAAC,aAAA,EAAe,gBAAgB,CAAA,GAAIA,cAAA;AAAA,IACxC,YAAA,KAAiB,SAAS,MAAA,GAAS;AAAA,GACrC;AACA,EAAA,MAAM,qBAAA,GAAwBC,aAAoD,IAAI,CAAA;AAEtF,EAAA,MAAM,eAAA,GAAkBC,iBAAA;AA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,mBAAA,CAAoB,QAAA,EAAU,YAAY,CAAA;AAAA,EACpE,CAAA,EAAG,CAAC,KAAA,EAAO,YAAA,EAAc,kBAAkB,CAAC,CAAA;AAE5C,EAAA,MAAM,cAAc,MAAM;AACxB,IAAA,aAAA,CAAc,CAAC,IAAA,KAAS;AACtB,MAAA,IAAI,IAAA,KAAS,QAAQ,OAAO,OAAA;AAC5B,MAAA,OAAO,MAAA;AAAA,IACT,CAAC,CAAA;AAAA,EACH,CAAA;AAEA,EAAA,MAAM,KAAA,GAAQK,aAAA;AAAA,IACZ,OAAO;AAAA,MACL,KAAA;AAAA,MACA,aAAA;AAAA,MACA,QAAA,EAAU,aAAA;AAAA,MACV;AAAA,KACF,CAAA;AAAA,IACA,CAAC,OAAO,aAAa;AAAA,GACvB;AAEA,EAAA,uBAAOC,cAAA,CAAC,YAAA,CAAa,QAAA,EAAb,EAAsB,OAAe,QAAA,EAAS,CAAA;AACxD;AAEO,SAAS,QAAA,GAA6B;AAC3C,EAAA,MAAM,OAAA,GAAUC,iBAAW,YAAY,CAAA;AACvC,EAAA,IAAI,CAAC,OAAA,EAAS;AACZ,IAAA,MAAM,IAAI,MAAM,8CAA8C,CAAA;AAAA,EAChE;AACA,EAAA,OAAO,OAAA;AACT;ACrJO,SAAS,MAAM,MAAA,EAAsB;AAC1C,EAAA,OAAOC,qBAAA,CAAQC,SAAA,CAAK,MAAM,CAAC,CAAA;AAC7B","file":"index.cjs","sourcesContent":["import React, {\n createContext,\n useCallback,\n useContext,\n useEffect,\n useLayoutEffect,\n useMemo,\n useRef,\n useState,\n} from 'react';\n\r\nexport type Theme = 'dark' | 'light' | 'system';\r\n\r\nexport interface ThemeProviderProps {\r\n children: React.ReactNode;\r\n defaultTheme?: Theme;\r\n storageKey?: string;\r\n attribute?: string;\r\n enableSystem?: boolean;\r\n}\r\n\r\ninterface ThemeContextType {\r\n theme: Theme;\r\n resolvedTheme: 'dark' | 'light';\r\n setTheme: (theme: Theme) => void;\r\n toggleTheme: () => void;\r\n}\r\n\r\nconst ThemeContext = createContext<ThemeContextType | undefined>(undefined);\n\nconst useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;\n\nfunction isTheme(value: string | null): value is Theme {\n return value === 'dark' || value === 'light' || value === 'system';\n}\n\r\nexport function ThemeProvider({\r\n children,\r\n defaultTheme = 'light',\n storageKey = 'ui-theme',\r\n attribute = 'data-theme',\r\n enableSystem = true,\r\n}: ThemeProviderProps) {\r\n // Always initialize identically on SSR and initial client pass to avoid hydration mismatches\r\n const [theme, setThemeState] = useState<Theme>(defaultTheme);\r\n const [resolvedTheme, setResolvedTheme] = useState<'dark' | 'light'>(\n defaultTheme === 'dark' ? 'dark' : 'light'\n );\n const pendingStoredThemeRef = useRef<{ storageKey: string; theme: Theme } | null>(null);\n\r\n const computeResolved = useCallback(\r\n (t: Theme): 'dark' | 'light' => {\r\n if (t === 'system' && enableSystem && typeof window !== 'undefined' && window.matchMedia) {\r\n return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';\r\n }\r\n return t === 'dark' ? 'dark' : 'light';\r\n },\r\n [enableSystem]\r\n );\r\n\r\n const applyResolvedTheme = useCallback(\n (nextResolvedTheme: 'dark' | 'light') => {\n setResolvedTheme(nextResolvedTheme);\n\n if (typeof document === 'undefined') return;\n\n const root = document.documentElement;\n root.setAttribute(attribute, nextResolvedTheme);\n root.classList.toggle('dark', nextResolvedTheme === 'dark');\n root.classList.toggle('light', nextResolvedTheme === 'light');\n },\n [attribute]\n );\n\n // Read the persisted value before paint. The pending ref prevents the DOM/storage\n // sync below from writing defaultTheme over a different persisted theme first.\n useIsomorphicLayoutEffect(() => {\n if (typeof window === 'undefined') return;\n\n let initialTheme = defaultTheme;\n try {\n const stored = window.localStorage?.getItem(storageKey);\n if (isTheme(stored)) initialTheme = stored;\n } catch {\n // Ignore storage access errors in restricted iframe/incognito environments\n }\n\n pendingStoredThemeRef.current = { storageKey, theme: initialTheme };\n setThemeState((currentTheme) => (currentTheme === initialTheme ? currentTheme : initialTheme));\n }, [storageKey]);\n\n // Apply user changes before paint. Storage initialization and user updates now\n // have a single write direction, so a click cannot oscillate light <-> dark.\n useIsomorphicLayoutEffect(() => {\n const pendingStoredTheme = pendingStoredThemeRef.current;\n if (pendingStoredTheme?.storageKey === storageKey) {\n if (pendingStoredTheme.theme !== theme) return;\n pendingStoredThemeRef.current = null;\n }\n\n const currentResolved = computeResolved(theme);\n applyResolvedTheme(currentResolved);\n\n if (typeof window !== 'undefined' && typeof window.localStorage !== 'undefined') {\n try {\n window.localStorage.setItem(storageKey, theme);\r\n } catch {\r\n // Ignore storage access errors\n }\n }\n }, [theme, computeResolved, applyResolvedTheme, storageKey]);\n\r\n // System media query listener\r\n useEffect(() => {\r\n if (theme !== 'system' || !enableSystem || typeof window === 'undefined' || !window.matchMedia) return;\r\n\r\n const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');\n const handleChange = () => {\n const newResolved = mediaQuery.matches ? 'dark' : 'light';\n applyResolvedTheme(newResolved);\n };\n\r\n mediaQuery.addEventListener('change', handleChange);\n return () => mediaQuery.removeEventListener('change', handleChange);\n }, [theme, enableSystem, applyResolvedTheme]);\n\r\n const toggleTheme = () => {\r\n setThemeState((prev) => {\r\n if (prev === 'dark') return 'light';\r\n return 'dark';\r\n });\r\n };\r\n\r\n const value = useMemo(\r\n () => ({\r\n theme,\r\n resolvedTheme,\r\n setTheme: setThemeState,\r\n toggleTheme,\r\n }),\r\n [theme, resolvedTheme]\r\n );\r\n\r\n return <ThemeContext.Provider value={value}>{children}</ThemeContext.Provider>;\r\n}\r\n\r\nexport function useTheme(): ThemeContextType {\r\n const context = useContext(ThemeContext);\r\n if (!context) {\r\n throw new Error('useTheme must be used within a ThemeProvider');\r\n }\r\n return context;\r\n}\r\n","import { clsx, type ClassValue } from 'clsx';\r\nimport { twMerge } from 'tailwind-merge';\r\n\r\nexport function cn(...inputs: ClassValue[]) {\r\n return twMerge(clsx(inputs));\r\n}\r\n"]}
|
package/dist/core/index.js
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -26,6 +26,10 @@ __export(core_exports, {
|
|
|
26
26
|
useTheme: () => useTheme
|
|
27
27
|
});
|
|
28
28
|
var ThemeContext = React66.createContext(void 0);
|
|
29
|
+
var useIsomorphicLayoutEffect = typeof window === "undefined" ? React66.useEffect : React66.useLayoutEffect;
|
|
30
|
+
function isTheme(value) {
|
|
31
|
+
return value === "dark" || value === "light" || value === "system";
|
|
32
|
+
}
|
|
29
33
|
function ThemeProvider({
|
|
30
34
|
children,
|
|
31
35
|
defaultTheme = "light",
|
|
@@ -37,6 +41,7 @@ function ThemeProvider({
|
|
|
37
41
|
const [resolvedTheme, setResolvedTheme] = React66.useState(
|
|
38
42
|
defaultTheme === "dark" ? "dark" : "light"
|
|
39
43
|
);
|
|
44
|
+
const pendingStoredThemeRef = React66.useRef(null);
|
|
40
45
|
const computeResolved = React66.useCallback(
|
|
41
46
|
(t) => {
|
|
42
47
|
if (t === "system" && enableSystem && typeof window !== "undefined" && window.matchMedia) {
|
|
@@ -46,56 +51,53 @@ function ThemeProvider({
|
|
|
46
51
|
},
|
|
47
52
|
[enableSystem]
|
|
48
53
|
);
|
|
49
|
-
React66.
|
|
54
|
+
const applyResolvedTheme = React66.useCallback(
|
|
55
|
+
(nextResolvedTheme) => {
|
|
56
|
+
setResolvedTheme(nextResolvedTheme);
|
|
57
|
+
if (typeof document === "undefined") return;
|
|
58
|
+
const root = document.documentElement;
|
|
59
|
+
root.setAttribute(attribute, nextResolvedTheme);
|
|
60
|
+
root.classList.toggle("dark", nextResolvedTheme === "dark");
|
|
61
|
+
root.classList.toggle("light", nextResolvedTheme === "light");
|
|
62
|
+
},
|
|
63
|
+
[attribute]
|
|
64
|
+
);
|
|
65
|
+
useIsomorphicLayoutEffect(() => {
|
|
50
66
|
if (typeof window === "undefined") return;
|
|
67
|
+
let initialTheme = defaultTheme;
|
|
51
68
|
try {
|
|
52
69
|
const stored = window.localStorage?.getItem(storageKey);
|
|
53
|
-
if (stored
|
|
54
|
-
setThemeState(stored);
|
|
55
|
-
}
|
|
70
|
+
if (isTheme(stored)) initialTheme = stored;
|
|
56
71
|
} catch {
|
|
57
72
|
}
|
|
73
|
+
pendingStoredThemeRef.current = { storageKey, theme: initialTheme };
|
|
74
|
+
setThemeState((currentTheme) => currentTheme === initialTheme ? currentTheme : initialTheme);
|
|
58
75
|
}, [storageKey]);
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
root.setAttribute(attribute, currentResolved);
|
|
65
|
-
if (currentResolved === "dark") {
|
|
66
|
-
root.classList.add("dark");
|
|
67
|
-
root.classList.remove("light");
|
|
68
|
-
} else {
|
|
69
|
-
root.classList.add("light");
|
|
70
|
-
root.classList.remove("dark");
|
|
76
|
+
useIsomorphicLayoutEffect(() => {
|
|
77
|
+
const pendingStoredTheme = pendingStoredThemeRef.current;
|
|
78
|
+
if (pendingStoredTheme?.storageKey === storageKey) {
|
|
79
|
+
if (pendingStoredTheme.theme !== theme) return;
|
|
80
|
+
pendingStoredThemeRef.current = null;
|
|
71
81
|
}
|
|
82
|
+
const currentResolved = computeResolved(theme);
|
|
83
|
+
applyResolvedTheme(currentResolved);
|
|
72
84
|
if (typeof window !== "undefined" && typeof window.localStorage !== "undefined") {
|
|
73
85
|
try {
|
|
74
86
|
window.localStorage.setItem(storageKey, theme);
|
|
75
87
|
} catch {
|
|
76
88
|
}
|
|
77
89
|
}
|
|
78
|
-
}, [theme,
|
|
90
|
+
}, [theme, computeResolved, applyResolvedTheme, storageKey]);
|
|
79
91
|
React66.useEffect(() => {
|
|
80
92
|
if (theme !== "system" || !enableSystem || typeof window === "undefined" || !window.matchMedia) return;
|
|
81
93
|
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
82
94
|
const handleChange = () => {
|
|
83
95
|
const newResolved = mediaQuery.matches ? "dark" : "light";
|
|
84
|
-
|
|
85
|
-
if (typeof document !== "undefined") {
|
|
86
|
-
document.documentElement.setAttribute(attribute, newResolved);
|
|
87
|
-
if (newResolved === "dark") {
|
|
88
|
-
document.documentElement.classList.add("dark");
|
|
89
|
-
document.documentElement.classList.remove("light");
|
|
90
|
-
} else {
|
|
91
|
-
document.documentElement.classList.add("light");
|
|
92
|
-
document.documentElement.classList.remove("dark");
|
|
93
|
-
}
|
|
94
|
-
}
|
|
96
|
+
applyResolvedTheme(newResolved);
|
|
95
97
|
};
|
|
96
98
|
mediaQuery.addEventListener("change", handleChange);
|
|
97
99
|
return () => mediaQuery.removeEventListener("change", handleChange);
|
|
98
|
-
}, [theme, enableSystem,
|
|
100
|
+
}, [theme, enableSystem, applyResolvedTheme]);
|
|
99
101
|
const toggleTheme = () => {
|
|
100
102
|
setThemeState((prev) => {
|
|
101
103
|
if (prev === "dark") return "light";
|