@tamagui/next-theme 1.27.1 → 1.27.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/next-theme",
3
- "version": "1.27.1",
3
+ "version": "1.27.3",
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.27.1"
19
+ "@tamagui/use-event": "1.27.3"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": "*"
23
23
  },
24
24
  "devDependencies": {
25
- "@tamagui/build": "1.27.1",
25
+ "@tamagui/build": "1.27.3",
26
26
  "react": "^18.2.0"
27
27
  },
28
28
  "publishConfig": {
@@ -1,9 +0,0 @@
1
- export * from "./NextThemeProvider";
2
- export * from "./ThemeSettingContext";
3
- export * from "./UseThemeProps";
4
- export * from "./helpers";
5
- export * from "./constants";
6
- export * from "./useTheme";
7
- export * from "./types";
8
- export * from "./useRootTheme";
9
- //# sourceMappingURL=NextTheme.mjs.map
@@ -1,233 +0,0 @@
1
- import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
- import { useEvent } from "@tamagui/use-event";
3
- import NextHead from "next/head";
4
- import * as React from "react";
5
- import { memo, useEffect, useMemo, useRef, useState } from "react";
6
- import { MEDIA, colorSchemes } from "./constants";
7
- import { getSystemTheme, getTheme } from "./helpers";
8
- import { ThemeSettingContext } from "./ThemeSettingContext";
9
- import { useIsomorphicLayoutEffect } from "./useIsomorphicLayoutEffect";
10
- const NextThemeProvider = ({
11
- forcedTheme,
12
- disableTransitionOnChange = true,
13
- enableSystem = true,
14
- enableColorScheme = true,
15
- storageKey = "theme",
16
- themes = colorSchemes,
17
- defaultTheme = enableSystem ? "system" : "light",
18
- attribute = "class",
19
- skipNextHead,
20
- onChangeTheme,
21
- value = {
22
- dark: "t_dark",
23
- light: "t_light"
24
- },
25
- children
26
- }) => {
27
- const [theme, setThemeState] = useState(() => getTheme(storageKey, defaultTheme));
28
- const [resolvedTheme, setResolvedTheme] = useState(() => getTheme(storageKey));
29
- const attrs = !value ? themes : Object.values(value);
30
- const handleMediaQuery = useEvent((e) => {
31
- const systemTheme2 = getSystemTheme(e);
32
- React.startTransition(() => {
33
- setResolvedTheme(systemTheme2);
34
- });
35
- if (theme === "system" && !forcedTheme) {
36
- handleChangeTheme(systemTheme2, false);
37
- }
38
- });
39
- const mediaListener = useRef(handleMediaQuery);
40
- mediaListener.current = handleMediaQuery;
41
- const handleChangeTheme = useEvent((theme2, updateStorage = true, updateDOM = true) => {
42
- let name = (value == null ? void 0 : value[theme2]) || theme2;
43
- if (updateStorage) {
44
- try {
45
- localStorage.setItem(storageKey, theme2);
46
- } catch (e) {
47
- }
48
- }
49
- if (theme2 === "system" && enableSystem) {
50
- const resolved = getSystemTheme();
51
- name = (value == null ? void 0 : value[resolved]) || resolved;
52
- }
53
- onChangeTheme == null ? void 0 : onChangeTheme(name.replace("t_", ""));
54
- if (updateDOM) {
55
- const d = document.documentElement;
56
- if (attribute === "class") {
57
- d.classList.remove(...attrs);
58
- d.classList.add(name);
59
- } else {
60
- d.setAttribute(attribute, name);
61
- }
62
- }
63
- });
64
- useIsomorphicLayoutEffect(() => {
65
- const handler = (...args) => mediaListener.current(...args);
66
- const media = window.matchMedia(MEDIA);
67
- media.addListener(handler);
68
- handler(media);
69
- return () => {
70
- media.removeListener(handler);
71
- };
72
- }, []);
73
- const set = useEvent((newTheme) => {
74
- if (forcedTheme) {
75
- handleChangeTheme(newTheme, true, false);
76
- } else {
77
- handleChangeTheme(newTheme);
78
- }
79
- setThemeState(newTheme);
80
- });
81
- useEffect(() => {
82
- const handleStorage = (e) => {
83
- if (e.key !== storageKey) {
84
- return;
85
- }
86
- const theme2 = e.newValue || defaultTheme;
87
- set(theme2);
88
- };
89
- window.addEventListener("storage", handleStorage);
90
- return () => {
91
- window.removeEventListener("storage", handleStorage);
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
104
- )
105
- )
106
- );
107
- const userPrefers = typeof window !== "undefined" && window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
108
- const wePrefer = colorScheme || "light";
109
- if (userPrefers !== wePrefer || wePrefer === "dark") {
110
- document.documentElement.style.setProperty("color-scheme", colorScheme);
111
- }
112
- }, [enableColorScheme, theme, resolvedTheme, forcedTheme]);
113
- const toggle = useEvent(() => {
114
- const order = resolvedTheme === "dark" ? ["system", "light", "dark"] : ["system", "dark", "light"];
115
- const next = order[(order.indexOf(theme) + 1) % order.length];
116
- set(next);
117
- });
118
- const contextResolvedTheme = theme === "system" ? resolvedTheme : theme;
119
- const systemTheme = enableSystem ? resolvedTheme : void 0;
120
- const contextValue = useMemo(() => {
121
- const value2 = {
122
- theme,
123
- current: theme,
124
- set,
125
- toggle,
126
- forcedTheme,
127
- resolvedTheme: contextResolvedTheme,
128
- themes: enableSystem ? [...themes, "system"] : themes,
129
- systemTheme
130
- };
131
- return value2;
132
- }, [
133
- theme,
134
- set,
135
- toggle,
136
- forcedTheme,
137
- contextResolvedTheme,
138
- enableSystem,
139
- themes,
140
- systemTheme
141
- ]);
142
- return /* @__PURE__ */ jsxs(ThemeSettingContext.Provider, { value: contextValue, children: [
143
- /* @__PURE__ */ jsx(
144
- ThemeScript,
145
- {
146
- ...{
147
- forcedTheme,
148
- storageKey,
149
- systemTheme: resolvedTheme,
150
- attribute,
151
- value,
152
- enableSystem,
153
- defaultTheme,
154
- attrs,
155
- skipNextHead
156
- }
157
- }
158
- ),
159
- useMemo(() => children, [children])
160
- ] });
161
- };
162
- const ThemeScript = memo(
163
- ({
164
- forcedTheme,
165
- storageKey,
166
- attribute,
167
- enableSystem,
168
- defaultTheme,
169
- value,
170
- attrs,
171
- skipNextHead
172
- }) => {
173
- const optimization = (() => {
174
- if (attribute === "class") {
175
- const removeClasses = attrs.map((t) => `d.remove('${t}')`).join(";");
176
- return `var d=document.documentElement.classList;${removeClasses};`;
177
- } else {
178
- return `var d=document.documentElement;`;
179
- }
180
- })();
181
- const updateDOM = (name, literal) => {
182
- name = (value == null ? void 0 : value[name]) || name;
183
- const val = literal ? name : `'${name}'`;
184
- if (attribute === "class") {
185
- return `d.add(${val})`;
186
- }
187
- return `d.setAttribute('${attribute}', ${val})`;
188
- };
189
- const defaultSystem = defaultTheme === "system";
190
- const contents = /* @__PURE__ */ jsx(Fragment, { children: forcedTheme ? /* @__PURE__ */ jsx(
191
- "script",
192
- {
193
- dangerouslySetInnerHTML: {
194
- // These are minified via Terser and then updated by hand, don't recommend
195
- __html: `!function(){${optimization}${updateDOM(forcedTheme)}}()`
196
- }
197
- },
198
- "next-themes-script"
199
- ) : enableSystem ? /* @__PURE__ */ jsx(
200
- "script",
201
- {
202
- dangerouslySetInnerHTML: {
203
- __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(
204
- "dark"
205
- )}:${updateDOM("light")}}else if(e) ${value ? `var x=${JSON.stringify(value)};` : ""}${updateDOM(value ? "x[e]" : "e", true)}}catch(e){}}()`
206
- }
207
- },
208
- "next-themes-script"
209
- ) : /* @__PURE__ */ jsx(
210
- "script",
211
- {
212
- dangerouslySetInnerHTML: {
213
- __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(
214
- defaultTheme
215
- )};}}catch(t){}}();`
216
- }
217
- },
218
- "next-themes-script"
219
- ) });
220
- if (skipNextHead)
221
- return contents;
222
- return /* @__PURE__ */ jsx(NextHead, { children: contents });
223
- },
224
- (prevProps, nextProps) => {
225
- if (prevProps.forcedTheme !== nextProps.forcedTheme)
226
- return false;
227
- return true;
228
- }
229
- );
230
- export {
231
- NextThemeProvider
232
- };
233
- //# sourceMappingURL=NextThemeProvider.mjs.map
@@ -1,12 +0,0 @@
1
- import { createContext } from "react";
2
- const ThemeSettingContext = createContext({
3
- toggle: () => {
4
- },
5
- set: (_) => {
6
- },
7
- themes: []
8
- });
9
- export {
10
- ThemeSettingContext
11
- };
12
- //# sourceMappingURL=ThemeSettingContext.mjs.map
@@ -1 +0,0 @@
1
- //# sourceMappingURL=UseThemeProps.mjs.map
@@ -1,9 +0,0 @@
1
- const constants = {};
2
- const colorSchemes = ["light", "dark"];
3
- const MEDIA = "(prefers-color-scheme: dark)";
4
- export {
5
- MEDIA,
6
- colorSchemes,
7
- constants
8
- };
9
- //# sourceMappingURL=constants.mjs.map
@@ -1,26 +0,0 @@
1
- import { MEDIA } from "./constants";
2
- const helpers = {};
3
- const getTheme = (key, fallback) => {
4
- if (typeof window === "undefined")
5
- return void 0;
6
- let theme;
7
- try {
8
- theme = localStorage.getItem(key) || void 0;
9
- } catch (e) {
10
- }
11
- return theme || fallback;
12
- };
13
- const getSystemTheme = (e) => {
14
- if (!e) {
15
- e = window.matchMedia(MEDIA);
16
- }
17
- const isDark = e.matches;
18
- const systemTheme = isDark ? "dark" : "light";
19
- return systemTheme;
20
- };
21
- export {
22
- getSystemTheme,
23
- getTheme,
24
- helpers
25
- };
26
- //# sourceMappingURL=helpers.mjs.map
@@ -1,2 +0,0 @@
1
- export * from "./NextTheme";
2
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- //# sourceMappingURL=types.mjs.map
@@ -1,6 +0,0 @@
1
- import { useEffect, useLayoutEffect } from "react";
2
- const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
3
- export {
4
- useIsomorphicLayoutEffect
5
- };
6
- //# sourceMappingURL=useIsomorphicLayoutEffect.mjs.map
@@ -1,18 +0,0 @@
1
- import { startTransition, useLayoutEffect, useState } from "react";
2
- const useRootTheme = ({ fallback = "light" } = {}) => {
3
- const [val, setVal] = useState(fallback);
4
- if (typeof document !== "undefined") {
5
- useLayoutEffect(() => {
6
- const classes = [...document.documentElement.classList];
7
- const val2 = classes.includes(`t_dark`) ? "dark" : classes.includes(`t_light`) ? "light" : fallback;
8
- startTransition(() => {
9
- setVal(val2);
10
- });
11
- }, []);
12
- }
13
- return [val, setVal];
14
- };
15
- export {
16
- useRootTheme
17
- };
18
- //# sourceMappingURL=useRootTheme.mjs.map
@@ -1,9 +0,0 @@
1
- import { useContext } from "react";
2
- import { ThemeSettingContext } from "./ThemeSettingContext";
3
- const useTheme = () => useContext(ThemeSettingContext);
4
- const useThemeSetting = () => useContext(ThemeSettingContext);
5
- export {
6
- useTheme,
7
- useThemeSetting
8
- };
9
- //# sourceMappingURL=useTheme.mjs.map