@tamagui/next-theme 1.36.5 → 1.37.0
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/cjs/NextThemeProvider.js +144 -140
- package/dist/cjs/NextThemeProvider.js.map +1 -1
- package/dist/esm/NextThemeProvider.js +144 -140
- package/dist/esm/NextThemeProvider.js.map +1 -1
- package/package.json +3 -3
- package/src/NextThemeProvider.tsx +171 -164
- package/types/NextTheme.d.ts.map +1 -0
- package/types/NextThemeProvider.d.ts +1 -1
- package/types/NextThemeProvider.d.ts.map +1 -0
- package/types/ThemeSettingContext.d.ts.map +1 -0
- package/types/UseThemeProps.d.ts.map +1 -0
- package/types/constants.d.ts.map +1 -0
- package/types/helpers.d.ts.map +1 -0
- package/types/index.d.ts.map +1 -0
- package/types/types.d.ts.map +1 -0
- package/types/useIsomorphicLayoutEffect.d.ts.map +1 -0
- package/types/useRootTheme.d.ts.map +1 -0
- package/types/useTheme.d.ts.map +1 -0
|
@@ -40,158 +40,162 @@ var import_constants = require("./constants");
|
|
|
40
40
|
var import_helpers = require("./helpers");
|
|
41
41
|
var import_ThemeSettingContext = require("./ThemeSettingContext");
|
|
42
42
|
var import_useIsomorphicLayoutEffect = require("./useIsomorphicLayoutEffect");
|
|
43
|
-
const NextThemeProvider = (
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
43
|
+
const NextThemeProvider = (0, import_react.memo)(
|
|
44
|
+
({
|
|
45
|
+
forcedTheme,
|
|
46
|
+
disableTransitionOnChange = true,
|
|
47
|
+
enableSystem = true,
|
|
48
|
+
enableColorScheme = true,
|
|
49
|
+
storageKey = "theme",
|
|
50
|
+
themes = import_constants.colorSchemes,
|
|
51
|
+
defaultTheme = enableSystem ? "system" : "light",
|
|
52
|
+
attribute = "class",
|
|
53
|
+
skipNextHead,
|
|
54
|
+
onChangeTheme,
|
|
55
|
+
value = {
|
|
56
|
+
dark: "t_dark",
|
|
57
|
+
light: "t_light"
|
|
58
|
+
},
|
|
59
|
+
children
|
|
60
|
+
}) => {
|
|
61
|
+
const [theme, setThemeState] = (0, import_react.useState)(() => (0, import_helpers.getTheme)(storageKey, defaultTheme));
|
|
62
|
+
const [resolvedTheme, setResolvedTheme] = (0, import_react.useState)(() => (0, import_helpers.getTheme)(storageKey));
|
|
63
|
+
const attrs = !value ? themes : Object.values(value);
|
|
64
|
+
const handleMediaQuery = (0, import_use_event.useEvent)((e) => {
|
|
65
|
+
const systemTheme2 = (0, import_helpers.getSystemTheme)(e);
|
|
66
|
+
React.startTransition(() => {
|
|
67
|
+
setResolvedTheme(systemTheme2);
|
|
68
|
+
});
|
|
69
|
+
if (theme === "system" && !forcedTheme) {
|
|
70
|
+
handleChangeTheme(systemTheme2, false);
|
|
71
|
+
}
|
|
67
72
|
});
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
73
|
+
const mediaListener = (0, import_react.useRef)(handleMediaQuery);
|
|
74
|
+
mediaListener.current = handleMediaQuery;
|
|
75
|
+
const handleChangeTheme = (0, import_use_event.useEvent)(
|
|
76
|
+
(theme2, updateStorage = true, updateDOM = true) => {
|
|
77
|
+
let name = (value == null ? void 0 : value[theme2]) || theme2;
|
|
78
|
+
if (updateStorage) {
|
|
79
|
+
try {
|
|
80
|
+
localStorage.setItem(storageKey, theme2);
|
|
81
|
+
} catch (e) {
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (theme2 === "system" && enableSystem) {
|
|
85
|
+
const resolved = (0, import_helpers.getSystemTheme)();
|
|
86
|
+
name = (value == null ? void 0 : value[resolved]) || resolved;
|
|
87
|
+
}
|
|
88
|
+
onChangeTheme == null ? void 0 : onChangeTheme(name.replace("t_", ""));
|
|
89
|
+
if (updateDOM) {
|
|
90
|
+
const d = document.documentElement;
|
|
91
|
+
if (attribute === "class") {
|
|
92
|
+
d.classList.remove(...attrs);
|
|
93
|
+
d.classList.add(name);
|
|
94
|
+
} else {
|
|
95
|
+
d.setAttribute(attribute, name);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
80
98
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
99
|
+
);
|
|
100
|
+
(0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
|
101
|
+
const handler = (...args) => mediaListener.current(...args);
|
|
102
|
+
const media = window.matchMedia(import_constants.MEDIA);
|
|
103
|
+
media.addListener(handler);
|
|
104
|
+
handler(media);
|
|
105
|
+
return () => {
|
|
106
|
+
media.removeListener(handler);
|
|
107
|
+
};
|
|
108
|
+
}, []);
|
|
109
|
+
const set = (0, import_use_event.useEvent)((newTheme) => {
|
|
110
|
+
if (forcedTheme) {
|
|
111
|
+
handleChangeTheme(newTheme, true, false);
|
|
92
112
|
} else {
|
|
93
|
-
|
|
113
|
+
handleChangeTheme(newTheme);
|
|
94
114
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
setThemeState(newTheme);
|
|
113
|
-
});
|
|
114
|
-
(0, import_react.useEffect)(() => {
|
|
115
|
-
const handleStorage = (e) => {
|
|
116
|
-
if (e.key !== storageKey) {
|
|
115
|
+
setThemeState(newTheme);
|
|
116
|
+
});
|
|
117
|
+
(0, import_react.useEffect)(() => {
|
|
118
|
+
const handleStorage = (e) => {
|
|
119
|
+
if (e.key !== storageKey) {
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const theme2 = e.newValue || defaultTheme;
|
|
123
|
+
set(theme2);
|
|
124
|
+
};
|
|
125
|
+
window.addEventListener("storage", handleStorage);
|
|
126
|
+
return () => {
|
|
127
|
+
window.removeEventListener("storage", handleStorage);
|
|
128
|
+
};
|
|
129
|
+
}, [defaultTheme, set, storageKey]);
|
|
130
|
+
(0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
|
131
|
+
if (!enableColorScheme)
|
|
117
132
|
return;
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}, [defaultTheme, set, storageKey]);
|
|
127
|
-
(0, import_useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => {
|
|
128
|
-
if (!enableColorScheme)
|
|
129
|
-
return;
|
|
130
|
-
const colorScheme = (
|
|
131
|
-
// If theme is forced to light or dark, use that
|
|
132
|
-
forcedTheme && import_constants.colorSchemes.includes(forcedTheme) ? forcedTheme : (
|
|
133
|
-
// If regular theme is light or dark
|
|
134
|
-
theme && import_constants.colorSchemes.includes(theme) ? theme : (
|
|
135
|
-
// If theme is system, use the resolved version
|
|
136
|
-
theme === "system" ? resolvedTheme || null : null
|
|
133
|
+
const colorScheme = (
|
|
134
|
+
// If theme is forced to light or dark, use that
|
|
135
|
+
forcedTheme && import_constants.colorSchemes.includes(forcedTheme) ? forcedTheme : (
|
|
136
|
+
// If regular theme is light or dark
|
|
137
|
+
theme && import_constants.colorSchemes.includes(theme) ? theme : (
|
|
138
|
+
// If theme is system, use the resolved version
|
|
139
|
+
theme === "system" ? resolvedTheme || null : null
|
|
140
|
+
)
|
|
137
141
|
)
|
|
138
|
-
)
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
142
|
+
);
|
|
143
|
+
const userPrefers = typeof window !== "undefined" && window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
144
|
+
const wePrefer = colorScheme || "light";
|
|
145
|
+
if (userPrefers !== wePrefer || wePrefer === "dark") {
|
|
146
|
+
document.documentElement.style.setProperty("color-scheme", colorScheme);
|
|
147
|
+
}
|
|
148
|
+
}, [enableColorScheme, theme, resolvedTheme, forcedTheme]);
|
|
149
|
+
const toggle = (0, import_use_event.useEvent)(() => {
|
|
150
|
+
const order = resolvedTheme === "dark" ? ["system", "light", "dark"] : ["system", "dark", "light"];
|
|
151
|
+
const next = order[(order.indexOf(theme) + 1) % order.length];
|
|
152
|
+
set(next);
|
|
153
|
+
});
|
|
154
|
+
const contextResolvedTheme = theme === "system" ? resolvedTheme : theme;
|
|
155
|
+
const systemTheme = enableSystem ? resolvedTheme : void 0;
|
|
156
|
+
const contextValue = (0, import_react.useMemo)(() => {
|
|
157
|
+
const value2 = {
|
|
158
|
+
theme,
|
|
159
|
+
current: theme,
|
|
160
|
+
set,
|
|
161
|
+
toggle,
|
|
162
|
+
forcedTheme,
|
|
163
|
+
resolvedTheme: contextResolvedTheme,
|
|
164
|
+
themes: enableSystem ? [...themes, "system"] : themes,
|
|
165
|
+
systemTheme
|
|
166
|
+
};
|
|
167
|
+
return value2;
|
|
168
|
+
}, [
|
|
155
169
|
theme,
|
|
156
|
-
current: theme,
|
|
157
170
|
set,
|
|
158
171
|
toggle,
|
|
159
172
|
forcedTheme,
|
|
160
|
-
|
|
161
|
-
|
|
173
|
+
contextResolvedTheme,
|
|
174
|
+
enableSystem,
|
|
175
|
+
themes,
|
|
162
176
|
systemTheme
|
|
163
|
-
|
|
164
|
-
return
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
...{
|
|
180
|
-
forcedTheme,
|
|
181
|
-
storageKey,
|
|
182
|
-
systemTheme: resolvedTheme,
|
|
183
|
-
attribute,
|
|
184
|
-
value,
|
|
185
|
-
enableSystem,
|
|
186
|
-
defaultTheme,
|
|
187
|
-
attrs,
|
|
188
|
-
skipNextHead
|
|
177
|
+
]);
|
|
178
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_ThemeSettingContext.ThemeSettingContext.Provider, { value: contextValue, children: [
|
|
179
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
180
|
+
ThemeScript,
|
|
181
|
+
{
|
|
182
|
+
...{
|
|
183
|
+
forcedTheme,
|
|
184
|
+
storageKey,
|
|
185
|
+
systemTheme: resolvedTheme,
|
|
186
|
+
attribute,
|
|
187
|
+
value,
|
|
188
|
+
enableSystem,
|
|
189
|
+
defaultTheme,
|
|
190
|
+
attrs,
|
|
191
|
+
skipNextHead
|
|
192
|
+
}
|
|
189
193
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
194
|
+
),
|
|
195
|
+
(0, import_react.useMemo)(() => children, [children])
|
|
196
|
+
] });
|
|
197
|
+
}
|
|
198
|
+
);
|
|
195
199
|
const ThemeScript = (0, import_react.memo)(
|
|
196
200
|
({
|
|
197
201
|
forcedTheme,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
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AAAA,MAAC;AAAA;AAAA,QAGC,yBAAyB;AAAA,UACvB,QAAQ,mBAAmB,2CAA2C,sBACpE,QAAQ,SAAS,KAAK,UAAU,KAAK,OAAO,KAC3C,UAAU,QAAQ,SAAS,KAAK,IAAI,UAAU;AAAA,YAC/C;AAAA,UACF;AAAA,QACF;AAAA;AAAA,MAPI;AAAA,IAQN,GAEJ;AAGF,QAAI;AAAc,aAAO;AAEzB,WAAO,4CAAC,YAAAC,SAAA,EAAU,oBAAS;AAAA,EAC7B;AAAA,EACA,CAAC,WAAW,cAAc;AAGxB,QAAI,UAAU,gBAAgB,UAAU;AAAa,aAAO;AAC5D,WAAO;AAAA,EACT;AACF;",
|
|
5
5
|
"names": ["systemTheme", "theme", "value", "NextHead"]
|
|
6
6
|
}
|
|
@@ -7,158 +7,162 @@ import { MEDIA, colorSchemes } from "./constants";
|
|
|
7
7
|
import { getSystemTheme, getTheme } from "./helpers";
|
|
8
8
|
import { ThemeSettingContext } from "./ThemeSettingContext";
|
|
9
9
|
import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
|
|
10
|
-
const NextThemeProvider = (
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
10
|
+
const NextThemeProvider = memo(
|
|
11
|
+
({
|
|
12
|
+
forcedTheme,
|
|
13
|
+
disableTransitionOnChange = true,
|
|
14
|
+
enableSystem = true,
|
|
15
|
+
enableColorScheme = true,
|
|
16
|
+
storageKey = "theme",
|
|
17
|
+
themes = colorSchemes,
|
|
18
|
+
defaultTheme = enableSystem ? "system" : "light",
|
|
19
|
+
attribute = "class",
|
|
20
|
+
skipNextHead,
|
|
21
|
+
onChangeTheme,
|
|
22
|
+
value = {
|
|
23
|
+
dark: "t_dark",
|
|
24
|
+
light: "t_light"
|
|
25
|
+
},
|
|
26
|
+
children
|
|
27
|
+
}) => {
|
|
28
|
+
const [theme, setThemeState] = useState(() => getTheme(storageKey, defaultTheme));
|
|
29
|
+
const [resolvedTheme, setResolvedTheme] = useState(() => getTheme(storageKey));
|
|
30
|
+
const attrs = !value ? themes : Object.values(value);
|
|
31
|
+
const handleMediaQuery = useEvent((e) => {
|
|
32
|
+
const systemTheme2 = getSystemTheme(e);
|
|
33
|
+
React.startTransition(() => {
|
|
34
|
+
setResolvedTheme(systemTheme2);
|
|
35
|
+
});
|
|
36
|
+
if (theme === "system" && !forcedTheme) {
|
|
37
|
+
handleChangeTheme(systemTheme2, false);
|
|
38
|
+
}
|
|
34
39
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
const mediaListener = useRef(handleMediaQuery);
|
|
41
|
+
mediaListener.current = handleMediaQuery;
|
|
42
|
+
const handleChangeTheme = useEvent(
|
|
43
|
+
(theme2, updateStorage = true, updateDOM = true) => {
|
|
44
|
+
let name = (value == null ? void 0 : value[theme2]) || theme2;
|
|
45
|
+
if (updateStorage) {
|
|
46
|
+
try {
|
|
47
|
+
localStorage.setItem(storageKey, theme2);
|
|
48
|
+
} catch (e) {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (theme2 === "system" && enableSystem) {
|
|
52
|
+
const resolved = getSystemTheme();
|
|
53
|
+
name = (value == null ? void 0 : value[resolved]) || resolved;
|
|
54
|
+
}
|
|
55
|
+
onChangeTheme == null ? void 0 : onChangeTheme(name.replace("t_", ""));
|
|
56
|
+
if (updateDOM) {
|
|
57
|
+
const d = document.documentElement;
|
|
58
|
+
if (attribute === "class") {
|
|
59
|
+
d.classList.remove(...attrs);
|
|
60
|
+
d.classList.add(name);
|
|
61
|
+
} else {
|
|
62
|
+
d.setAttribute(attribute, name);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
47
65
|
}
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
66
|
+
);
|
|
67
|
+
useIsomorphicLayoutEffect(() => {
|
|
68
|
+
const handler = (...args) => mediaListener.current(...args);
|
|
69
|
+
const media = window.matchMedia(MEDIA);
|
|
70
|
+
media.addListener(handler);
|
|
71
|
+
handler(media);
|
|
72
|
+
return () => {
|
|
73
|
+
media.removeListener(handler);
|
|
74
|
+
};
|
|
75
|
+
}, []);
|
|
76
|
+
const set = useEvent((newTheme) => {
|
|
77
|
+
if (forcedTheme) {
|
|
78
|
+
handleChangeTheme(newTheme, true, false);
|
|
59
79
|
} else {
|
|
60
|
-
|
|
80
|
+
handleChangeTheme(newTheme);
|
|
61
81
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
setThemeState(newTheme);
|
|
80
|
-
});
|
|
81
|
-
useEffect(() => {
|
|
82
|
-
const handleStorage = (e) => {
|
|
83
|
-
if (e.key !== storageKey) {
|
|
82
|
+
setThemeState(newTheme);
|
|
83
|
+
});
|
|
84
|
+
useEffect(() => {
|
|
85
|
+
const handleStorage = (e) => {
|
|
86
|
+
if (e.key !== storageKey) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
const theme2 = e.newValue || defaultTheme;
|
|
90
|
+
set(theme2);
|
|
91
|
+
};
|
|
92
|
+
window.addEventListener("storage", handleStorage);
|
|
93
|
+
return () => {
|
|
94
|
+
window.removeEventListener("storage", handleStorage);
|
|
95
|
+
};
|
|
96
|
+
}, [defaultTheme, set, storageKey]);
|
|
97
|
+
useIsomorphicLayoutEffect(() => {
|
|
98
|
+
if (!enableColorScheme)
|
|
84
99
|
return;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}, [defaultTheme, set, storageKey]);
|
|
94
|
-
useIsomorphicLayoutEffect(() => {
|
|
95
|
-
if (!enableColorScheme)
|
|
96
|
-
return;
|
|
97
|
-
const colorScheme = (
|
|
98
|
-
// If theme is forced to light or dark, use that
|
|
99
|
-
forcedTheme && colorSchemes.includes(forcedTheme) ? forcedTheme : (
|
|
100
|
-
// If regular theme is light or dark
|
|
101
|
-
theme && colorSchemes.includes(theme) ? theme : (
|
|
102
|
-
// If theme is system, use the resolved version
|
|
103
|
-
theme === "system" ? resolvedTheme || null : null
|
|
100
|
+
const colorScheme = (
|
|
101
|
+
// If theme is forced to light or dark, use that
|
|
102
|
+
forcedTheme && colorSchemes.includes(forcedTheme) ? forcedTheme : (
|
|
103
|
+
// If regular theme is light or dark
|
|
104
|
+
theme && colorSchemes.includes(theme) ? theme : (
|
|
105
|
+
// If theme is system, use the resolved version
|
|
106
|
+
theme === "system" ? resolvedTheme || null : null
|
|
107
|
+
)
|
|
104
108
|
)
|
|
105
|
-
)
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
109
|
+
);
|
|
110
|
+
const userPrefers = typeof window !== "undefined" && window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
111
|
+
const wePrefer = colorScheme || "light";
|
|
112
|
+
if (userPrefers !== wePrefer || wePrefer === "dark") {
|
|
113
|
+
document.documentElement.style.setProperty("color-scheme", colorScheme);
|
|
114
|
+
}
|
|
115
|
+
}, [enableColorScheme, theme, resolvedTheme, forcedTheme]);
|
|
116
|
+
const toggle = useEvent(() => {
|
|
117
|
+
const order = resolvedTheme === "dark" ? ["system", "light", "dark"] : ["system", "dark", "light"];
|
|
118
|
+
const next = order[(order.indexOf(theme) + 1) % order.length];
|
|
119
|
+
set(next);
|
|
120
|
+
});
|
|
121
|
+
const contextResolvedTheme = theme === "system" ? resolvedTheme : theme;
|
|
122
|
+
const systemTheme = enableSystem ? resolvedTheme : void 0;
|
|
123
|
+
const contextValue = useMemo(() => {
|
|
124
|
+
const value2 = {
|
|
125
|
+
theme,
|
|
126
|
+
current: theme,
|
|
127
|
+
set,
|
|
128
|
+
toggle,
|
|
129
|
+
forcedTheme,
|
|
130
|
+
resolvedTheme: contextResolvedTheme,
|
|
131
|
+
themes: enableSystem ? [...themes, "system"] : themes,
|
|
132
|
+
systemTheme
|
|
133
|
+
};
|
|
134
|
+
return value2;
|
|
135
|
+
}, [
|
|
122
136
|
theme,
|
|
123
|
-
current: theme,
|
|
124
137
|
set,
|
|
125
138
|
toggle,
|
|
126
139
|
forcedTheme,
|
|
127
|
-
|
|
128
|
-
|
|
140
|
+
contextResolvedTheme,
|
|
141
|
+
enableSystem,
|
|
142
|
+
themes,
|
|
129
143
|
systemTheme
|
|
130
|
-
|
|
131
|
-
return
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
...{
|
|
147
|
-
forcedTheme,
|
|
148
|
-
storageKey,
|
|
149
|
-
systemTheme: resolvedTheme,
|
|
150
|
-
attribute,
|
|
151
|
-
value,
|
|
152
|
-
enableSystem,
|
|
153
|
-
defaultTheme,
|
|
154
|
-
attrs,
|
|
155
|
-
skipNextHead
|
|
144
|
+
]);
|
|
145
|
+
return /* @__PURE__ */ jsxs(ThemeSettingContext.Provider, { value: contextValue, children: [
|
|
146
|
+
/* @__PURE__ */ jsx(
|
|
147
|
+
ThemeScript,
|
|
148
|
+
{
|
|
149
|
+
...{
|
|
150
|
+
forcedTheme,
|
|
151
|
+
storageKey,
|
|
152
|
+
systemTheme: resolvedTheme,
|
|
153
|
+
attribute,
|
|
154
|
+
value,
|
|
155
|
+
enableSystem,
|
|
156
|
+
defaultTheme,
|
|
157
|
+
attrs,
|
|
158
|
+
skipNextHead
|
|
159
|
+
}
|
|
156
160
|
}
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
161
|
+
),
|
|
162
|
+
useMemo(() => children, [children])
|
|
163
|
+
] });
|
|
164
|
+
}
|
|
165
|
+
);
|
|
162
166
|
const ThemeScript = memo(
|
|
163
167
|
({
|
|
164
168
|
forcedTheme,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/NextThemeProvider.tsx"],
|
|
4
|
-
"mappings": "
|
|
4
|
+
"mappings": "AA6LM,SAiEA,UAhEE,KADF;AA7LN,SAAS,gBAAgB;AACzB,OAAO,cAAc;AACrB,YAAY,WAAW;AACvB,SAAS,MAAM,WAAW,SAAS,QAAQ,gBAAgB;AAE3D,SAAS,OAAO,oBAAoB;AACpC,SAAS,gBAAgB,gBAAgB;AACzC,SAAS,2BAA2B;AAEpC,SAAS,iCAAiC;AAGnC,MAAM,oBAAoB;AAAA,EAC/B,CAAC;AAAA,IACC;AAAA,IACA,4BAA4B;AAAA,IAC5B,eAAe;AAAA,IACf,oBAAoB;AAAA,IACpB,aAAa;AAAA,IACb,SAAS;AAAA,IAC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
1
1
|
{
|
|
2
2
|
"name": "@tamagui/next-theme",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.37.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"types": "./types/index.d.ts",
|
|
@@ -16,13 +16,13 @@
|
|
|
16
16
|
"watch": "tamagui-build --watch"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@tamagui/use-event": "1.
|
|
19
|
+
"@tamagui/use-event": "1.37.0"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
22
|
"react": "*"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
|
-
"@tamagui/build": "1.
|
|
25
|
+
"@tamagui/build": "1.37.0",
|
|
26
26
|
"react": "^18.2.0"
|
|
27
27
|
},
|
|
28
28
|
"publishConfig": {
|
|
@@ -10,197 +10,204 @@ import { ValueObject } from './types'
|
|
|
10
10
|
import { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'
|
|
11
11
|
import { ThemeProviderProps, UseThemeProps } from './UseThemeProps'
|
|
12
12
|
|
|
13
|
-
export const NextThemeProvider
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
13
|
+
export const NextThemeProvider = memo(
|
|
14
|
+
({
|
|
15
|
+
forcedTheme,
|
|
16
|
+
disableTransitionOnChange = true,
|
|
17
|
+
enableSystem = true,
|
|
18
|
+
enableColorScheme = true,
|
|
19
|
+
storageKey = 'theme',
|
|
20
|
+
themes = colorSchemes,
|
|
21
|
+
defaultTheme = enableSystem ? 'system' : 'light',
|
|
22
|
+
attribute = 'class',
|
|
23
|
+
skipNextHead,
|
|
24
|
+
onChangeTheme,
|
|
25
|
+
value = {
|
|
26
|
+
dark: 't_dark',
|
|
27
|
+
light: 't_light',
|
|
28
|
+
},
|
|
29
|
+
children,
|
|
30
|
+
}: ThemeProviderProps) => {
|
|
31
|
+
const [theme, setThemeState] = useState(() => getTheme(storageKey, defaultTheme))
|
|
32
|
+
const [resolvedTheme, setResolvedTheme] = useState(() => getTheme(storageKey))
|
|
33
|
+
// const resolvedTheme = React.useDeferredValue(resolvedThemeFast)
|
|
34
|
+
const attrs = !value ? themes : Object.values(value)
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
36
|
+
const handleMediaQuery = useEvent((e?) => {
|
|
37
|
+
const systemTheme = getSystemTheme(e)
|
|
38
|
+
React.startTransition(() => {
|
|
39
|
+
setResolvedTheme(systemTheme)
|
|
40
|
+
})
|
|
41
|
+
if (theme === 'system' && !forcedTheme) {
|
|
42
|
+
handleChangeTheme(systemTheme, false)
|
|
43
|
+
}
|
|
39
44
|
})
|
|
40
|
-
if (theme === 'system' && !forcedTheme) {
|
|
41
|
-
handleChangeTheme(systemTheme, false)
|
|
42
|
-
}
|
|
43
|
-
})
|
|
44
45
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
// Ref hack to avoid adding handleMediaQuery as a dep
|
|
47
|
+
const mediaListener = useRef(handleMediaQuery)
|
|
48
|
+
mediaListener.current = handleMediaQuery
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
|
|
50
|
+
const handleChangeTheme = useEvent(
|
|
51
|
+
(theme, updateStorage = true, updateDOM = true) => {
|
|
52
|
+
let name = value?.[theme] || theme
|
|
51
53
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
if (updateStorage) {
|
|
55
|
+
try {
|
|
56
|
+
localStorage.setItem(storageKey, theme)
|
|
57
|
+
} catch (e) {
|
|
58
|
+
// Unsupported
|
|
59
|
+
}
|
|
60
|
+
}
|
|
59
61
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
62
|
+
if (theme === 'system' && enableSystem) {
|
|
63
|
+
const resolved = getSystemTheme()
|
|
64
|
+
name = value?.[resolved] || resolved
|
|
65
|
+
}
|
|
64
66
|
|
|
65
|
-
|
|
67
|
+
onChangeTheme?.(name.replace('t_', ''))
|
|
66
68
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
69
|
+
if (updateDOM) {
|
|
70
|
+
const d = document.documentElement
|
|
71
|
+
if (attribute === 'class') {
|
|
72
|
+
d.classList.remove(...attrs)
|
|
73
|
+
d.classList.add(name)
|
|
74
|
+
} else {
|
|
75
|
+
d.setAttribute(attribute, name)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
74
78
|
}
|
|
75
|
-
|
|
76
|
-
})
|
|
79
|
+
)
|
|
77
80
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
81
|
+
useIsomorphicLayoutEffect(() => {
|
|
82
|
+
const handler = (...args: any) => mediaListener.current(...args)
|
|
83
|
+
// Always listen to System preference
|
|
84
|
+
const media = window.matchMedia(MEDIA)
|
|
85
|
+
// Intentionally use deprecated listener methods to support iOS & old browsers
|
|
86
|
+
media.addListener(handler)
|
|
87
|
+
handler(media)
|
|
88
|
+
return () => {
|
|
89
|
+
media.removeListener(handler)
|
|
90
|
+
}
|
|
91
|
+
}, [])
|
|
89
92
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
93
|
+
const set = useEvent((newTheme) => {
|
|
94
|
+
if (forcedTheme) {
|
|
95
|
+
handleChangeTheme(newTheme, true, false)
|
|
96
|
+
} else {
|
|
97
|
+
handleChangeTheme(newTheme)
|
|
98
|
+
}
|
|
99
|
+
setThemeState(newTheme)
|
|
100
|
+
})
|
|
98
101
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
// localStorage event handling
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
const handleStorage = (e: StorageEvent) => {
|
|
105
|
+
if (e.key !== storageKey) {
|
|
106
|
+
return
|
|
107
|
+
}
|
|
108
|
+
// If default theme set, use it if localstorage === null (happens on local storage manual deletion)
|
|
109
|
+
const theme = e.newValue || defaultTheme
|
|
110
|
+
set(theme)
|
|
104
111
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return () => {
|
|
111
|
-
window.removeEventListener('storage', handleStorage)
|
|
112
|
-
}
|
|
113
|
-
}, [defaultTheme, set, storageKey])
|
|
112
|
+
window.addEventListener('storage', handleStorage)
|
|
113
|
+
return () => {
|
|
114
|
+
window.removeEventListener('storage', handleStorage)
|
|
115
|
+
}
|
|
116
|
+
}, [defaultTheme, set, storageKey])
|
|
114
117
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
// color-scheme handling
|
|
119
|
+
useIsomorphicLayoutEffect(() => {
|
|
120
|
+
if (!enableColorScheme) return
|
|
118
121
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
122
|
+
const colorScheme =
|
|
123
|
+
// If theme is forced to light or dark, use that
|
|
124
|
+
forcedTheme && colorSchemes.includes(forcedTheme)
|
|
125
|
+
? forcedTheme
|
|
126
|
+
: // If regular theme is light or dark
|
|
127
|
+
theme && colorSchemes.includes(theme)
|
|
128
|
+
? theme
|
|
129
|
+
: // If theme is system, use the resolved version
|
|
130
|
+
theme === 'system'
|
|
131
|
+
? resolvedTheme || null
|
|
132
|
+
: null
|
|
130
133
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
134
|
+
// color-scheme tells browser how to render built-in elements like forms, scrollbars, etc.
|
|
135
|
+
// if color-scheme is null, this will remove the property
|
|
136
|
+
const userPrefers =
|
|
137
|
+
typeof window !== 'undefined' &&
|
|
138
|
+
window.matchMedia &&
|
|
139
|
+
window.matchMedia('(prefers-color-scheme: dark)').matches
|
|
140
|
+
? 'dark'
|
|
141
|
+
: 'light'
|
|
139
142
|
|
|
140
|
-
|
|
143
|
+
const wePrefer = colorScheme || 'light'
|
|
141
144
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
// avoid running this because it causes full page reflow
|
|
146
|
+
if (userPrefers !== wePrefer || wePrefer === 'dark') {
|
|
147
|
+
document.documentElement.style.setProperty('color-scheme', colorScheme)
|
|
148
|
+
}
|
|
149
|
+
}, [enableColorScheme, theme, resolvedTheme, forcedTheme])
|
|
147
150
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
151
|
+
const toggle = useEvent(() => {
|
|
152
|
+
const order =
|
|
153
|
+
resolvedTheme === 'dark'
|
|
154
|
+
? ['system', 'light', 'dark']
|
|
155
|
+
: ['system', 'dark', 'light']
|
|
156
|
+
const next = order[(order.indexOf(theme) + 1) % order.length]
|
|
157
|
+
set(next)
|
|
158
|
+
})
|
|
154
159
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
+
const contextResolvedTheme = theme === 'system' ? resolvedTheme : theme
|
|
161
|
+
const systemTheme = (enableSystem ? resolvedTheme : undefined) as
|
|
162
|
+
| 'light'
|
|
163
|
+
| 'dark'
|
|
164
|
+
| undefined
|
|
160
165
|
|
|
161
|
-
|
|
162
|
-
|
|
166
|
+
const contextValue = useMemo(() => {
|
|
167
|
+
const value: UseThemeProps = {
|
|
168
|
+
theme,
|
|
169
|
+
current: theme,
|
|
170
|
+
set,
|
|
171
|
+
toggle,
|
|
172
|
+
forcedTheme,
|
|
173
|
+
resolvedTheme: contextResolvedTheme,
|
|
174
|
+
themes: enableSystem ? [...themes, 'system'] : themes,
|
|
175
|
+
systemTheme,
|
|
176
|
+
} as const
|
|
177
|
+
return value
|
|
178
|
+
}, [
|
|
163
179
|
theme,
|
|
164
|
-
current: theme,
|
|
165
180
|
set,
|
|
166
181
|
toggle,
|
|
167
182
|
forcedTheme,
|
|
168
|
-
|
|
169
|
-
|
|
183
|
+
contextResolvedTheme,
|
|
184
|
+
enableSystem,
|
|
185
|
+
themes,
|
|
170
186
|
systemTheme,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
187
|
+
])
|
|
188
|
+
|
|
189
|
+
return (
|
|
190
|
+
<ThemeSettingContext.Provider value={contextValue}>
|
|
191
|
+
<ThemeScript
|
|
192
|
+
{...{
|
|
193
|
+
forcedTheme,
|
|
194
|
+
storageKey,
|
|
195
|
+
systemTheme: resolvedTheme,
|
|
196
|
+
attribute,
|
|
197
|
+
value,
|
|
198
|
+
enableSystem,
|
|
199
|
+
defaultTheme,
|
|
200
|
+
attrs,
|
|
201
|
+
skipNextHead,
|
|
202
|
+
}}
|
|
203
|
+
/>
|
|
204
|
+
{/* because on SSR we re-run and can avoid whole tree re-render */}
|
|
205
|
+
{useMemo(() => children, [children])}
|
|
206
|
+
</ThemeSettingContext.Provider>
|
|
207
|
+
)
|
|
208
|
+
}
|
|
209
|
+
)
|
|
183
210
|
|
|
184
|
-
return (
|
|
185
|
-
<ThemeSettingContext.Provider value={contextValue}>
|
|
186
|
-
<ThemeScript
|
|
187
|
-
{...{
|
|
188
|
-
forcedTheme,
|
|
189
|
-
storageKey,
|
|
190
|
-
systemTheme: resolvedTheme,
|
|
191
|
-
attribute,
|
|
192
|
-
value,
|
|
193
|
-
enableSystem,
|
|
194
|
-
defaultTheme,
|
|
195
|
-
attrs,
|
|
196
|
-
skipNextHead,
|
|
197
|
-
}}
|
|
198
|
-
/>
|
|
199
|
-
{/* because on SSR we re-run and can avoid whole tree re-render */}
|
|
200
|
-
{useMemo(() => children, [children])}
|
|
201
|
-
</ThemeSettingContext.Provider>
|
|
202
|
-
)
|
|
203
|
-
}
|
|
204
211
|
const ThemeScript = memo(
|
|
205
212
|
({
|
|
206
213
|
forcedTheme,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NextTheme.d.ts","sourceRoot":"","sources":["../src/NextTheme.tsx"],"names":[],"mappings":"AAGA,cAAc,qBAAqB,CAAA;AACnC,cAAc,uBAAuB,CAAA;AACrC,cAAc,iBAAiB,CAAA;AAC/B,cAAc,WAAW,CAAA;AACzB,cAAc,aAAa,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,SAAS,CAAA;AACvB,cAAc,gBAAgB,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
2
|
import { ThemeProviderProps } from './UseThemeProps';
|
|
3
|
-
export declare const NextThemeProvider: React.
|
|
3
|
+
export declare const NextThemeProvider: React.MemoExoticComponent<({ forcedTheme, disableTransitionOnChange, enableSystem, enableColorScheme, storageKey, themes, defaultTheme, attribute, skipNextHead, onChangeTheme, value, children, }: ThemeProviderProps) => JSX.Element>;
|
|
4
4
|
//# sourceMappingURL=NextThemeProvider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NextThemeProvider.d.ts","sourceRoot":"","sources":["../src/NextThemeProvider.tsx"],"names":[],"mappings":"AAEA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAA;AAQ9B,OAAO,EAAE,kBAAkB,EAAiB,MAAM,iBAAiB,CAAA;AAEnE,eAAO,MAAM,iBAAiB,sMAiBzB,kBAAkB,iBAmLtB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ThemeSettingContext.d.ts","sourceRoot":"","sources":["../src/ThemeSettingContext.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE/C,eAAO,MAAM,mBAAmB,wCAI9B,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UseThemeProps.d.ts","sourceRoot":"","sources":["../src/UseThemeProps.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,MAAM,WAAW,aAAa;IAC5B,wCAAwC;IACxC,MAAM,EAAE,MAAM,EAAE,CAAA;IAChB,6CAA6C;IAC7C,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,uBAAuB;IACvB,GAAG,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAA;IAC5B,MAAM,EAAE,MAAM,IAAI,CAAA;IAClB,uHAAuH;IACvH,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,oFAAoF;IACpF,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,4KAA4K;IAC5K,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,4HAA4H;IAC5H,WAAW,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;CAC/B;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,EAAE,GAAG,CAAA;IACd,wCAAwC;IACxC,MAAM,CAAC,EAAE,MAAM,EAAE,CAAA;IACjB,6CAA6C;IAC7C,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,oFAAoF;IACpF,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,wDAAwD;IACxD,yBAAyB,CAAC,EAAE,OAAO,CAAA;IACnC,yHAAyH;IACzH,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,sDAAsD;IACtD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,+HAA+H;IAC/H,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,oJAAoJ;IACpJ,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAA;IAC5B,yHAAyH;IACzH,KAAK,CAAC,EAAE,WAAW,CAAA;IACnB,aAAa,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAGtC,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.tsx"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,IAAK,CAAA;AAC3B,eAAO,MAAM,YAAY,UAAoB,CAAA;AAC7C,eAAO,MAAM,KAAK,iCAAiC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../src/helpers.tsx"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,IAAK,CAAA;AAGzB,eAAO,MAAM,QAAQ,QAAS,MAAM,aAAa,MAAM,QAStD,CAAA;AAED,eAAO,MAAM,cAAc,OAAQ,cAAc,qBAQhD,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.tsx"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG,MAAM,GAAG,OAAO,CAAA;AAE1C,MAAM,WAAW,WAAW;IAC1B,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CAAA;CAC5B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useIsomorphicLayoutEffect.d.ts","sourceRoot":"","sources":["../src/useIsomorphicLayoutEffect.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAa,eAAe,EAAE,MAAM,OAAO,CAAA;AAElD,eAAO,MAAM,yBAAyB,wBACuB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useRootTheme.d.ts","sourceRoot":"","sources":["../src/useRootTheme.tsx"],"names":[],"mappings":";AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAErC,eAAO,MAAM,YAAY;;mGAqBxB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTheme.d.ts","sourceRoot":"","sources":["../src/useTheme.tsx"],"names":[],"mappings":"AAIA;;GAEG;AAEH,eAAO,MAAM,QAAQ,+CAAwC,CAAA;AAE7D,eAAO,MAAM,eAAe,+CAAwC,CAAA"}
|