@tamagui/next-theme 1.0.1-beta.140 → 1.0.1-beta.141

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/next-theme",
3
- "version": "1.0.1-beta.140",
3
+ "version": "1.0.1-beta.141",
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/core": "^1.0.1-beta.140"
19
+ "@tamagui/core": "^1.0.1-beta.141"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": "*"
23
23
  },
24
24
  "devDependencies": {
25
- "@tamagui/build": "^1.0.1-beta.140",
25
+ "@tamagui/build": "^1.0.1-beta.141",
26
26
  "react": "*"
27
27
  },
28
28
  "publishConfig": {
@@ -1,219 +0,0 @@
1
- import NextHead from "next/head";
2
- import {
3
- createContext,
4
- memo,
5
- useCallback,
6
- useContext,
7
- useEffect,
8
- useLayoutEffect,
9
- useMemo,
10
- useRef,
11
- useState
12
- } from "react";
13
- const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
14
- const ThemeContext = createContext({
15
- toggleTheme: () => {
16
- },
17
- setTheme: (_) => {
18
- },
19
- themes: []
20
- });
21
- const useTheme = () => useContext(ThemeContext);
22
- const colorSchemes = ["light", "dark"];
23
- const MEDIA = "(prefers-color-scheme: dark)";
24
- const useRootTheme = () => {
25
- const isClient = typeof document !== "undefined";
26
- const classes = isClient ? [...document.documentElement.classList] : [];
27
- const isDark = classes.includes("t_dark");
28
- return useState(isDark ? "dark" : "light");
29
- };
30
- const NextThemeProvider = ({
31
- forcedTheme,
32
- disableTransitionOnChange = true,
33
- enableSystem = true,
34
- enableColorScheme = true,
35
- storageKey = "theme",
36
- themes = ["light", "dark"],
37
- defaultTheme = enableSystem ? "system" : "light",
38
- attribute = "class",
39
- onChangeTheme,
40
- value = {
41
- dark: "t_dark",
42
- light: "t_light"
43
- },
44
- children
45
- }) => {
46
- const [theme, setThemeState] = useState(() => getTheme(storageKey, defaultTheme));
47
- const [resolvedTheme, setResolvedTheme] = useState(() => getTheme(storageKey));
48
- const attrs = !value ? themes : Object.values(value);
49
- const handleMediaQuery = useCallback((e) => {
50
- const systemTheme = getSystemTheme(e);
51
- setResolvedTheme(systemTheme);
52
- if (theme === "system" && !forcedTheme)
53
- handleChangeTheme(systemTheme, false);
54
- }, [theme, forcedTheme]);
55
- const mediaListener = useRef(handleMediaQuery);
56
- mediaListener.current = handleMediaQuery;
57
- const handleChangeTheme = useCallback((theme2, updateStorage = true, updateDOM = true) => {
58
- let name = (value == null ? void 0 : value[theme2]) || theme2;
59
- const enable = disableTransitionOnChange && updateDOM ? disableAnimation() : null;
60
- if (updateStorage) {
61
- try {
62
- localStorage.setItem(storageKey, theme2);
63
- } catch (e) {
64
- }
65
- }
66
- if (theme2 === "system" && enableSystem) {
67
- const resolved = getSystemTheme();
68
- name = (value == null ? void 0 : value[resolved]) || resolved;
69
- }
70
- onChangeTheme == null ? void 0 : onChangeTheme(name.replace("t_", ""));
71
- if (updateDOM) {
72
- const d = document.documentElement;
73
- if (attribute === "class") {
74
- d.classList.remove(...attrs);
75
- d.classList.add(name);
76
- } else {
77
- d.setAttribute(attribute, name);
78
- }
79
- enable == null ? void 0 : enable();
80
- }
81
- }, []);
82
- useIsomorphicLayoutEffect(() => {
83
- const handler = (...args) => mediaListener.current(...args);
84
- const media = window.matchMedia(MEDIA);
85
- media.addListener(handler);
86
- handler(media);
87
- return () => {
88
- media.removeListener(handler);
89
- };
90
- }, []);
91
- const setTheme = useCallback((newTheme) => {
92
- if (forcedTheme) {
93
- handleChangeTheme(newTheme, true, false);
94
- } else {
95
- handleChangeTheme(newTheme);
96
- }
97
- setThemeState(newTheme);
98
- }, [forcedTheme]);
99
- useEffect(() => {
100
- const handleStorage = (e) => {
101
- if (e.key !== storageKey) {
102
- return;
103
- }
104
- const theme2 = e.newValue || defaultTheme;
105
- setTheme(theme2);
106
- };
107
- window.addEventListener("storage", handleStorage);
108
- return () => {
109
- window.removeEventListener("storage", handleStorage);
110
- };
111
- }, []);
112
- useIsomorphicLayoutEffect(() => {
113
- if (!enableColorScheme)
114
- return;
115
- let colorScheme = forcedTheme && colorSchemes.includes(forcedTheme) ? forcedTheme : theme && colorSchemes.includes(theme) ? theme : theme === "system" ? resolvedTheme || null : null;
116
- document.documentElement.style.setProperty("color-scheme", colorScheme);
117
- }, [enableColorScheme, theme, resolvedTheme, forcedTheme]);
118
- const contextValue = useMemo(() => {
119
- return {
120
- theme,
121
- setTheme,
122
- toggleTheme() {
123
- const order = resolvedTheme === "dark" ? ["system", "light", "dark"] : ["system", "dark", "light"];
124
- const next = order[(order.indexOf(theme) + 1) % order.length];
125
- setTheme(next);
126
- },
127
- forcedTheme,
128
- resolvedTheme: theme === "system" ? resolvedTheme : theme,
129
- themes: enableSystem ? [...themes, "system"] : themes,
130
- systemTheme: enableSystem ? resolvedTheme : void 0
131
- };
132
- }, [theme, forcedTheme, resolvedTheme, enableSystem]);
133
- return <ThemeContext.Provider value={contextValue}>
134
- <ThemeScript {...{
135
- forcedTheme,
136
- storageKey,
137
- systemTheme: resolvedTheme,
138
- attribute,
139
- value,
140
- enableSystem,
141
- defaultTheme,
142
- attrs
143
- }} />
144
- {children}
145
- </ThemeContext.Provider>;
146
- };
147
- const ThemeScript = memo(({
148
- forcedTheme,
149
- storageKey,
150
- attribute,
151
- enableSystem,
152
- defaultTheme,
153
- value,
154
- attrs
155
- }) => {
156
- const optimization = (() => {
157
- if (attribute === "class") {
158
- const removeClasses = attrs.map((t) => `d.remove('${t}')`).join(";");
159
- return `var d=document.documentElement.classList;${removeClasses};`;
160
- } else {
161
- return `var d=document.documentElement;`;
162
- }
163
- })();
164
- const updateDOM = (name, literal) => {
165
- name = (value == null ? void 0 : value[name]) || name;
166
- const val = literal ? name : `'${name}'`;
167
- if (attribute === "class") {
168
- return `d.add(${val})`;
169
- }
170
- return `d.setAttribute('${attribute}', ${val})`;
171
- };
172
- const defaultSystem = defaultTheme === "system";
173
- return <NextHead>{forcedTheme ? <script key="next-themes-script" dangerouslySetInnerHTML={{
174
- __html: `!function(){${optimization}${updateDOM(forcedTheme)}}()`
175
- }} /> : enableSystem ? <script key="next-themes-script" dangerouslySetInnerHTML={{
176
- __html: `!function(){try {${optimization}var e=localStorage.getItem('${storageKey}');${!defaultSystem ? updateDOM(defaultTheme) + ";" : ""}if("system"===e||(!e&&${defaultSystem})){var t="${MEDIA}",m=window.matchMedia(t);m.media!==t||m.matches?${updateDOM("dark")}:${updateDOM("light")}}else if(e) ${value ? `var x=${JSON.stringify(value)};` : ""}${updateDOM(value ? "x[e]" : "e", true)}}catch(e){}}()`
177
- }} /> : <script key="next-themes-script" dangerouslySetInnerHTML={{
178
- __html: `!function(){try{${optimization}var e=localStorage.getItem("${storageKey}");if(e){${value ? `var x=${JSON.stringify(value)};` : ""}${updateDOM(value ? "x[e]" : "e", true)}}else{${updateDOM(defaultTheme)};}}catch(t){}}();`
179
- }} />}</NextHead>;
180
- }, (prevProps, nextProps) => {
181
- if (prevProps.forcedTheme !== nextProps.forcedTheme)
182
- return false;
183
- return true;
184
- });
185
- const getTheme = (key, fallback) => {
186
- if (typeof window === "undefined")
187
- return void 0;
188
- let theme;
189
- try {
190
- theme = localStorage.getItem(key) || void 0;
191
- } catch (e) {
192
- }
193
- return theme || fallback;
194
- };
195
- const disableAnimation = () => {
196
- const css = document.createElement("style");
197
- css.appendChild(document.createTextNode(`*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}`));
198
- document.head.appendChild(css);
199
- return () => {
200
- ;
201
- (() => window.getComputedStyle(document.body))();
202
- setTimeout(() => {
203
- document.head.removeChild(css);
204
- }, 1);
205
- };
206
- };
207
- const getSystemTheme = (e) => {
208
- if (!e) {
209
- e = window.matchMedia(MEDIA);
210
- }
211
- const isDark = e.matches;
212
- const systemTheme = isDark ? "dark" : "light";
213
- return systemTheme;
214
- };
215
- export {
216
- NextThemeProvider,
217
- useRootTheme,
218
- useTheme
219
- };
package/dist/jsx/index.js DELETED
@@ -1 +0,0 @@
1
- export * from "./NextTheme";