@tamagui/next-theme 1.1.2 → 1.1.4

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.
Files changed (61) hide show
  1. package/dist/cjs/NextTheme.js +9 -273
  2. package/dist/cjs/NextTheme.js.map +3 -3
  3. package/dist/cjs/NextThemeProvider.js +245 -0
  4. package/dist/cjs/NextThemeProvider.js.map +7 -0
  5. package/dist/cjs/ThemeSettingContext.js +36 -0
  6. package/dist/cjs/ThemeSettingContext.js.map +7 -0
  7. package/dist/cjs/UseThemeProps.js +17 -0
  8. package/dist/cjs/UseThemeProps.js.map +7 -0
  9. package/dist/cjs/constants.js +35 -0
  10. package/dist/cjs/constants.js.map +7 -0
  11. package/dist/cjs/helpers.js +52 -0
  12. package/dist/cjs/helpers.js.map +7 -0
  13. package/dist/cjs/types.js +17 -0
  14. package/dist/cjs/types.js.map +7 -0
  15. package/dist/cjs/useIsomorphicLayoutEffect.js +30 -0
  16. package/dist/cjs/useIsomorphicLayoutEffect.js.map +7 -0
  17. package/dist/cjs/useRootTheme.js +49 -0
  18. package/dist/cjs/useRootTheme.js.map +7 -0
  19. package/dist/cjs/useTheme.js +34 -0
  20. package/dist/cjs/useTheme.js.map +7 -0
  21. package/dist/esm/NextTheme.js +8 -265
  22. package/dist/esm/NextTheme.js.map +3 -3
  23. package/dist/esm/NextThemeProvider.js +215 -0
  24. package/dist/esm/NextThemeProvider.js.map +7 -0
  25. package/dist/esm/ThemeSettingContext.js +12 -0
  26. package/dist/esm/ThemeSettingContext.js.map +7 -0
  27. package/dist/esm/UseThemeProps.js +1 -0
  28. package/dist/esm/UseThemeProps.js.map +7 -0
  29. package/dist/esm/constants.js +9 -0
  30. package/dist/esm/constants.js.map +7 -0
  31. package/dist/esm/helpers.js +26 -0
  32. package/dist/esm/helpers.js.map +7 -0
  33. package/dist/esm/types.js +1 -0
  34. package/dist/esm/types.js.map +7 -0
  35. package/dist/esm/useIsomorphicLayoutEffect.js +6 -0
  36. package/dist/esm/useIsomorphicLayoutEffect.js.map +7 -0
  37. package/dist/esm/useRootTheme.js +19 -0
  38. package/dist/esm/useRootTheme.js.map +7 -0
  39. package/dist/esm/useTheme.js +9 -0
  40. package/dist/esm/useTheme.js.map +7 -0
  41. package/package.json +3 -3
  42. package/src/NextTheme.tsx +10 -408
  43. package/src/NextThemeProvider.tsx +297 -0
  44. package/src/ThemeSettingContext.tsx +9 -0
  45. package/src/UseThemeProps.tsx +46 -0
  46. package/src/constants.tsx +3 -0
  47. package/src/helpers.tsx +25 -0
  48. package/src/types.tsx +5 -0
  49. package/src/useIsomorphicLayoutEffect.tsx +4 -0
  50. package/src/useRootTheme.tsx +21 -0
  51. package/src/useTheme.tsx +11 -0
  52. package/types/NextTheme.d.ts +8 -53
  53. package/types/NextThemeProvider.d.ts +4 -0
  54. package/types/ThemeSettingContext.d.ts +4 -0
  55. package/types/UseThemeProps.d.ts +43 -0
  56. package/types/constants.d.ts +4 -0
  57. package/types/helpers.d.ts +4 -0
  58. package/types/types.d.ts +5 -0
  59. package/types/useIsomorphicLayoutEffect.d.ts +3 -0
  60. package/types/useRootTheme.d.ts +3 -0
  61. package/types/useTheme.d.ts +6 -0
@@ -0,0 +1,215 @@
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 = forcedTheme && colorSchemes.includes(forcedTheme) ? forcedTheme : theme && colorSchemes.includes(theme) ? theme : theme === "system" ? resolvedTheme || null : null;
98
+ const userPrefers = typeof window !== "undefined" && window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
99
+ const wePrefer = colorScheme || "light";
100
+ if (userPrefers !== wePrefer) {
101
+ document.documentElement.style.setProperty("color-scheme", colorScheme);
102
+ }
103
+ }, [enableColorScheme, theme, resolvedTheme, forcedTheme]);
104
+ const toggle = useEvent(() => {
105
+ const order = resolvedTheme === "dark" ? ["system", "light", "dark"] : ["system", "dark", "light"];
106
+ const next = order[(order.indexOf(theme) + 1) % order.length];
107
+ set(next);
108
+ });
109
+ const contextResolvedTheme = theme === "system" ? resolvedTheme : theme;
110
+ const systemTheme = enableSystem ? resolvedTheme : void 0;
111
+ const contextValue = useMemo(() => {
112
+ const value2 = {
113
+ theme,
114
+ current: theme,
115
+ set,
116
+ toggle,
117
+ forcedTheme,
118
+ resolvedTheme: contextResolvedTheme,
119
+ themes: enableSystem ? [...themes, "system"] : themes,
120
+ systemTheme
121
+ };
122
+ return value2;
123
+ }, [
124
+ theme,
125
+ set,
126
+ toggle,
127
+ forcedTheme,
128
+ contextResolvedTheme,
129
+ enableSystem,
130
+ themes,
131
+ systemTheme
132
+ ]);
133
+ return /* @__PURE__ */ jsxs(ThemeSettingContext.Provider, {
134
+ value: contextValue,
135
+ children: [
136
+ /* @__PURE__ */ jsx(ThemeScript, {
137
+ ...{
138
+ forcedTheme,
139
+ storageKey,
140
+ systemTheme: resolvedTheme,
141
+ attribute,
142
+ value,
143
+ enableSystem,
144
+ defaultTheme,
145
+ attrs,
146
+ skipNextHead
147
+ }
148
+ }),
149
+ useMemo(() => children, [children])
150
+ ]
151
+ });
152
+ };
153
+ const ThemeScript = memo(
154
+ ({
155
+ forcedTheme,
156
+ storageKey,
157
+ attribute,
158
+ enableSystem,
159
+ defaultTheme,
160
+ value,
161
+ attrs,
162
+ skipNextHead
163
+ }) => {
164
+ const optimization = (() => {
165
+ if (attribute === "class") {
166
+ const removeClasses = attrs.map((t) => `d.remove('${t}')`).join(";");
167
+ return `var d=document.documentElement.classList;${removeClasses};`;
168
+ } else {
169
+ return `var d=document.documentElement;`;
170
+ }
171
+ })();
172
+ const updateDOM = (name, literal) => {
173
+ name = (value == null ? void 0 : value[name]) || name;
174
+ const val = literal ? name : `'${name}'`;
175
+ if (attribute === "class") {
176
+ return `d.add(${val})`;
177
+ }
178
+ return `d.setAttribute('${attribute}', ${val})`;
179
+ };
180
+ const defaultSystem = defaultTheme === "system";
181
+ const contents = /* @__PURE__ */ jsx(Fragment, {
182
+ children: forcedTheme ? /* @__PURE__ */ jsx("script", {
183
+ dangerouslySetInnerHTML: {
184
+ __html: `!function(){${optimization}${updateDOM(forcedTheme)}}()`
185
+ }
186
+ }, "next-themes-script") : enableSystem ? /* @__PURE__ */ jsx("script", {
187
+ dangerouslySetInnerHTML: {
188
+ __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(
189
+ "dark"
190
+ )}:${updateDOM("light")}}else if(e) ${value ? `var x=${JSON.stringify(value)};` : ""}${updateDOM(value ? "x[e]" : "e", true)}}catch(e){}}()`
191
+ }
192
+ }, "next-themes-script") : /* @__PURE__ */ jsx("script", {
193
+ dangerouslySetInnerHTML: {
194
+ __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(
195
+ defaultTheme
196
+ )};}}catch(t){}}();`
197
+ }
198
+ }, "next-themes-script")
199
+ });
200
+ if (skipNextHead)
201
+ return contents;
202
+ return /* @__PURE__ */ jsx(NextHead, {
203
+ children: contents
204
+ });
205
+ },
206
+ (prevProps, nextProps) => {
207
+ if (prevProps.forcedTheme !== nextProps.forcedTheme)
208
+ return false;
209
+ return true;
210
+ }
211
+ );
212
+ export {
213
+ NextThemeProvider
214
+ };
215
+ //# sourceMappingURL=NextThemeProvider.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/NextThemeProvider.tsx"],
4
+ "sourcesContent": ["import { useEvent } from '@tamagui/use-event'\nimport NextHead from 'next/head'\nimport * as React from 'react'\nimport { memo, useEffect, useMemo, useRef, useState } from 'react'\n\nimport { MEDIA, colorSchemes } from './constants'\nimport { getSystemTheme, getTheme } from './helpers'\nimport { ThemeSettingContext } from './ThemeSettingContext'\nimport { ValueObject } from './types'\nimport { useIsomorphicLayoutEffect } from './useIsomorphicLayoutEffect'\nimport { ThemeProviderProps, UseThemeProps } from './UseThemeProps'\n\nexport const NextThemeProvider: React.FC<ThemeProviderProps> = ({\n forcedTheme,\n disableTransitionOnChange = true,\n enableSystem = true,\n enableColorScheme = true,\n storageKey = 'theme',\n themes = colorSchemes,\n defaultTheme = enableSystem ? 'system' : 'light',\n attribute = 'class',\n skipNextHead,\n onChangeTheme,\n value = {\n dark: 't_dark',\n light: 't_light',\n },\n children,\n}) => {\n const [theme, setThemeState] = useState(() => getTheme(storageKey, defaultTheme))\n const [resolvedTheme, setResolvedTheme] = useState(() => getTheme(storageKey))\n // const resolvedTheme = React.useDeferredValue(resolvedThemeFast)\n const attrs = !value ? themes : Object.values(value)\n\n const handleMediaQuery = useEvent((e?) => {\n const systemTheme = getSystemTheme(e)\n React.startTransition(() => {\n setResolvedTheme(systemTheme)\n })\n if (theme === 'system' && !forcedTheme) {\n handleChangeTheme(systemTheme, false)\n }\n })\n\n // Ref hack to avoid adding handleMediaQuery as a dep\n const mediaListener = useRef(handleMediaQuery)\n mediaListener.current = handleMediaQuery\n\n const handleChangeTheme = useEvent((theme, updateStorage = true, updateDOM = true) => {\n let name = value?.[theme] || theme\n\n if (updateStorage) {\n try {\n localStorage.setItem(storageKey, theme)\n } catch (e) {\n // Unsupported\n }\n }\n\n if (theme === 'system' && enableSystem) {\n const resolved = getSystemTheme()\n name = value?.[resolved] || resolved\n }\n\n onChangeTheme?.(name.replace('t_', ''))\n\n if (updateDOM) {\n const d = document.documentElement\n if (attribute === 'class') {\n d.classList.remove(...attrs)\n d.classList.add(name)\n } else {\n d.setAttribute(attribute, name)\n }\n }\n })\n\n useIsomorphicLayoutEffect(() => {\n const handler = (...args: any) => mediaListener.current(...args)\n // Always listen to System preference\n const media = window.matchMedia(MEDIA)\n // Intentionally use deprecated listener methods to support iOS & old browsers\n media.addListener(handler)\n handler(media)\n return () => {\n media.removeListener(handler)\n }\n }, [])\n\n const set = useEvent((newTheme) => {\n if (forcedTheme) {\n handleChangeTheme(newTheme, true, false)\n } else {\n handleChangeTheme(newTheme)\n }\n setThemeState(newTheme)\n })\n\n // localStorage event handling\n useEffect(() => {\n const handleStorage = (e: StorageEvent) => {\n if (e.key !== storageKey) {\n return\n }\n // If default theme set, use it if localstorage === null (happens on local storage manual deletion)\n const theme = e.newValue || defaultTheme\n set(theme)\n }\n window.addEventListener('storage', handleStorage)\n return () => {\n window.removeEventListener('storage', handleStorage)\n }\n }, [defaultTheme, set, storageKey])\n\n // color-scheme handling\n useIsomorphicLayoutEffect(() => {\n if (!enableColorScheme) return\n\n const colorScheme =\n // If theme is forced to light or dark, use that\n forcedTheme && colorSchemes.includes(forcedTheme)\n ? forcedTheme\n : // If regular theme is light or dark\n theme && colorSchemes.includes(theme)\n ? theme\n : // If theme is system, use the resolved version\n theme === 'system'\n ? resolvedTheme || null\n : null\n\n // color-scheme tells browser how to render built-in elements like forms, scrollbars, etc.\n // if color-scheme is null, this will remove the property\n const userPrefers =\n typeof window !== 'undefined' &&\n window.matchMedia &&\n window.matchMedia('(prefers-color-scheme: dark)').matches\n ? 'dark'\n : 'light'\n\n const wePrefer = colorScheme || 'light'\n\n // avoid running this because it causes full page reflow\n if (userPrefers !== wePrefer) {\n document.documentElement.style.setProperty('color-scheme', colorScheme)\n }\n }, [enableColorScheme, theme, resolvedTheme, forcedTheme])\n\n const toggle = useEvent(() => {\n const order =\n resolvedTheme === 'dark' ? ['system', 'light', 'dark'] : ['system', 'dark', 'light']\n const next = order[(order.indexOf(theme) + 1) % order.length]\n set(next)\n })\n\n const contextResolvedTheme = theme === 'system' ? resolvedTheme : theme\n const systemTheme = (enableSystem ? resolvedTheme : undefined) as\n | 'light'\n | 'dark'\n | undefined\n const contextValue = useMemo(() => {\n const value: UseThemeProps = {\n theme,\n current: theme,\n set,\n toggle,\n forcedTheme,\n resolvedTheme: contextResolvedTheme,\n themes: enableSystem ? [...themes, 'system'] : themes,\n systemTheme,\n } as const\n return value\n }, [\n theme,\n set,\n toggle,\n forcedTheme,\n contextResolvedTheme,\n enableSystem,\n themes,\n systemTheme,\n ])\n\n return (\n <ThemeSettingContext.Provider value={contextValue}>\n <ThemeScript\n {...{\n forcedTheme,\n storageKey,\n systemTheme: resolvedTheme,\n attribute,\n value,\n enableSystem,\n defaultTheme,\n attrs,\n skipNextHead,\n }}\n />\n {/* because on SSR we re-run and can avoid whole tree re-render */}\n {useMemo(() => children, [children])}\n </ThemeSettingContext.Provider>\n )\n}\nconst ThemeScript = memo(\n ({\n forcedTheme,\n storageKey,\n attribute,\n enableSystem,\n defaultTheme,\n value,\n attrs,\n skipNextHead,\n }: {\n forcedTheme?: string\n storageKey: string\n attribute?: string\n enableSystem?: boolean\n defaultTheme: string\n value?: ValueObject\n attrs: any\n skipNextHead?: boolean\n }) => {\n // Code-golfing the amount of characters in the script\n const optimization = (() => {\n if (attribute === 'class') {\n const removeClasses = attrs.map((t: string) => `d.remove('${t}')`).join(';')\n return `var d=document.documentElement.classList;${removeClasses};`\n } else {\n return `var d=document.documentElement;`\n }\n })()\n\n const updateDOM = (name: string, literal?: boolean) => {\n name = value?.[name] || name\n const val = literal ? name : `'${name}'`\n\n if (attribute === 'class') {\n return `d.add(${val})`\n }\n\n return `d.setAttribute('${attribute}', ${val})`\n }\n\n const defaultSystem = defaultTheme === 'system'\n\n const contents = (\n <>\n {forcedTheme ? (\n <script\n // nonce={nonce}\n key=\"next-themes-script\"\n dangerouslySetInnerHTML={{\n // These are minified via Terser and then updated by hand, don't recommend\n __html: `!function(){${optimization}${updateDOM(forcedTheme)}}()`,\n }}\n />\n ) : enableSystem ? (\n <script\n // nonce={nonce}\n key=\"next-themes-script\"\n dangerouslySetInnerHTML={{\n __html: `!function(){try {${optimization}var e=localStorage.getItem('${storageKey}');${\n !defaultSystem ? updateDOM(defaultTheme) + ';' : ''\n }if(\"system\"===e||(!e&&${defaultSystem})){var t=\"${MEDIA}\",m=window.matchMedia(t);m.media!==t||m.matches?${updateDOM(\n 'dark'\n )}:${updateDOM('light')}}else if(e) ${\n value ? `var x=${JSON.stringify(value)};` : ''\n }${updateDOM(value ? 'x[e]' : 'e', true)}}catch(e){}}()`,\n }}\n />\n ) : (\n <script\n // nonce={nonce}\n key=\"next-themes-script\"\n dangerouslySetInnerHTML={{\n __html: `!function(){try{${optimization}var e=localStorage.getItem(\"${storageKey}\");if(e){${\n value ? `var x=${JSON.stringify(value)};` : ''\n }${updateDOM(value ? 'x[e]' : 'e', true)}}else{${updateDOM(\n defaultTheme\n )};}}catch(t){}}();`,\n }}\n />\n )}\n </>\n )\n\n if (skipNextHead) return contents\n\n return <NextHead>{contents}</NextHead>\n },\n (prevProps, nextProps) => {\n // Only re-render when forcedTheme changes\n // the rest of the props should be completely stable\n if (prevProps.forcedTheme !== nextProps.forcedTheme) return false\n return true\n }\n)\n"],
5
+ "mappings": "AAu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A,EAC7B;AAAA,EACA,CAAC,WAAW,cAAc;AAGxB,QAAI,UAAU,gBAAgB,UAAU;AAAa,aAAO;AAC5D,WAAO;AAAA,EACT;AACF;",
6
+ "names": ["systemTheme", "theme", "value"]
7
+ }
@@ -0,0 +1,12 @@
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.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/ThemeSettingContext.tsx"],
4
+ "sourcesContent": ["import { createContext } from 'react'\n\nimport { UseThemeProps } from './UseThemeProps'\n\nexport const ThemeSettingContext = createContext<UseThemeProps>({\n toggle: () => {},\n set: (_) => {},\n themes: [],\n})\n"],
5
+ "mappings": "AAAA,SAAS,qBAAqB;AAIvB,MAAM,sBAAsB,cAA6B;AAAA,EAC9D,QAAQ,MAAM;AAAA,EAAC;AAAA,EACf,KAAK,CAAC,MAAM;AAAA,EAAC;AAAA,EACb,QAAQ,CAAC;AACX,CAAC;",
6
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=UseThemeProps.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,9 @@
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.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/constants.tsx"],
4
+ "sourcesContent": ["export const constants = {}\nexport const colorSchemes = ['light', 'dark']\nexport const MEDIA = '(prefers-color-scheme: dark)'\n"],
5
+ "mappings": "AAAO,MAAM,YAAY,CAAC;AACnB,MAAM,eAAe,CAAC,SAAS,MAAM;AACrC,MAAM,QAAQ;",
6
+ "names": []
7
+ }
@@ -0,0 +1,26 @@
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.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/helpers.tsx"],
4
+ "sourcesContent": ["import { MEDIA } from './constants'\n\nexport const helpers = {}\n// Helpers\n\nexport const getTheme = (key: string, fallback?: string) => {\n if (typeof window === 'undefined') return undefined\n let theme\n try {\n theme = localStorage.getItem(key) || undefined\n } catch (e) {\n // Unsupported\n }\n return theme || fallback\n}\n\nexport const getSystemTheme = (e?: MediaQueryList) => {\n if (!e) {\n e = window.matchMedia(MEDIA)\n }\n\n const isDark = e.matches\n const systemTheme = isDark ? 'dark' : 'light'\n return systemTheme\n}\n"],
5
+ "mappings": "AAAA,SAAS,aAAa;AAEf,MAAM,UAAU,CAAC;AAGjB,MAAM,WAAW,CAAC,KAAa,aAAsB;AAC1D,MAAI,OAAO,WAAW;AAAa,WAAO;AAC1C,MAAI;AACJ,MAAI;AACF,YAAQ,aAAa,QAAQ,GAAG,KAAK;AAAA,EACvC,SAAS,GAAP;AAAA,EAEF;AACA,SAAO,SAAS;AAClB;AAEO,MAAM,iBAAiB,CAAC,MAAuB;AACpD,MAAI,CAAC,GAAG;AACN,QAAI,OAAO,WAAW,KAAK;AAAA,EAC7B;AAEA,QAAM,SAAS,EAAE;AACjB,QAAM,cAAc,SAAS,SAAS;AACtC,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,6 @@
1
+ import { useEffect, useLayoutEffect } from "react";
2
+ const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
3
+ export {
4
+ useIsomorphicLayoutEffect
5
+ };
6
+ //# sourceMappingURL=useIsomorphicLayoutEffect.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/useIsomorphicLayoutEffect.tsx"],
4
+ "sourcesContent": ["import { useEffect, useLayoutEffect } from 'react'\n\nexport const useIsomorphicLayoutEffect =\n typeof window !== 'undefined' ? useLayoutEffect : useEffect\n"],
5
+ "mappings": "AAAA,SAAS,WAAW,uBAAuB;AAEpC,MAAM,4BACX,OAAO,WAAW,cAAc,kBAAkB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,19 @@
1
+ import * as React from "react";
2
+ import { useLayoutEffect, useState } from "react";
3
+ const useRootTheme = () => {
4
+ const [val, setVal] = useState("light");
5
+ if (typeof document !== "undefined") {
6
+ useLayoutEffect(() => {
7
+ const classes = [...document.documentElement.classList];
8
+ const isDark = classes.includes("t_dark");
9
+ React.startTransition(() => {
10
+ setVal(isDark ? "dark" : "light");
11
+ });
12
+ }, []);
13
+ }
14
+ return [val, setVal];
15
+ };
16
+ export {
17
+ useRootTheme
18
+ };
19
+ //# sourceMappingURL=useRootTheme.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/useRootTheme.tsx"],
4
+ "sourcesContent": ["import * as React from 'react'\nimport { useLayoutEffect, useState } from 'react'\n\n// note this only works for light being default for now...\n\nexport const useRootTheme = () => {\n const [val, setVal] = useState('light')\n\n if (typeof document !== 'undefined') {\n useLayoutEffect(() => {\n // @ts-ignore\n const classes = [...document.documentElement.classList]\n const isDark = classes.includes('t_dark')\n React.startTransition(() => {\n setVal(isDark ? 'dark' : 'light')\n })\n }, [])\n }\n\n return [val, setVal] as const\n}\n"],
5
+ "mappings": "AAAA,YAAY,WAAW;AACvB,SAAS,iBAAiB,gBAAgB;AAInC,MAAM,eAAe,MAAM;AAChC,QAAM,CAAC,KAAK,MAAM,IAAI,SAAS,OAAO;AAEtC,MAAI,OAAO,aAAa,aAAa;AACnC,oBAAgB,MAAM;AAEpB,YAAM,UAAU,CAAC,GAAG,SAAS,gBAAgB,SAAS;AACtD,YAAM,SAAS,QAAQ,SAAS,QAAQ;AACxC,YAAM,gBAAgB,MAAM;AAC1B,eAAO,SAAS,SAAS,OAAO;AAAA,MAClC,CAAC;AAAA,IACH,GAAG,CAAC,CAAC;AAAA,EACP;AAEA,SAAO,CAAC,KAAK,MAAM;AACrB;",
6
+ "names": []
7
+ }
@@ -0,0 +1,9 @@
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.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/useTheme.tsx"],
4
+ "sourcesContent": ["import { useContext } from 'react'\n\nimport { ThemeSettingContext } from './ThemeSettingContext'\n\n/**\n * @deprecated renamed to `useThemeSetting` to avoid confusion with core `useTheme` hook\n */\n\nexport const useTheme = () => useContext(ThemeSettingContext)\n\nexport const useThemeSetting = () => useContext(ThemeSettingContext)\n"],
5
+ "mappings": "AAAA,SAAS,kBAAkB;AAE3B,SAAS,2BAA2B;AAM7B,MAAM,WAAW,MAAM,WAAW,mBAAmB;AAErD,MAAM,kBAAkB,MAAM,WAAW,mBAAmB;",
6
+ "names": []
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamagui/next-theme",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
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.1.2"
19
+ "@tamagui/use-event": "^1.1.4"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "react": "*"
23
23
  },
24
24
  "devDependencies": {
25
- "@tamagui/build": "^1.1.2",
25
+ "@tamagui/build": "^1.1.4",
26
26
  "react": "^18.2.0"
27
27
  },
28
28
  "publishConfig": {