@nayhoo/ui 0.9.4 → 0.9.5

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.
@@ -3,26 +3,27 @@ import * as react from 'react';
3
3
  import react__default from 'react';
4
4
  import * as ToastPrimitives from '@radix-ui/react-toast';
5
5
 
6
- type theme = "light" | "dark" | "system";
6
+ type Theme = "light" | "dark" | "system";
7
7
 
8
+ type ResolvedTheme = Exclude<Theme, "system">;
8
9
  type ThemeContextProps = {
9
- theme: theme;
10
- setTheme: react__default.Dispatch<react__default.SetStateAction<theme>>;
11
- themes: theme[];
10
+ theme: Theme;
11
+ setTheme: react__default.Dispatch<react__default.SetStateAction<Theme>>;
12
+ resolvedTheme: ResolvedTheme;
13
+ themes: Theme[];
12
14
  };
13
15
  declare const ThemeContext: react__default.Context<ThemeContextProps>;
14
16
  type ThemeProviderProps = {
15
17
  children: react__default.ReactNode;
16
- defaultTheme?: theme;
17
18
  };
18
19
  declare const ThemeProvider: ({ children }: ThemeProviderProps) => react_jsx_runtime.JSX.Element;
19
20
 
20
21
  type AddToastPayload = Omit<Toast, "status">;
21
- type ToastProviderContextProps = ((payload: AddToastPayload) => void) & {
22
+ type ToastContextProps = ((payload: AddToastPayload) => void) & {
22
23
  error: (payload: AddToastPayload) => void;
23
24
  success: (payload: AddToastPayload) => void;
24
25
  };
25
- declare const ToastProviderContext: react.Context<ToastProviderContextProps>;
26
+ declare const ToastContext: react.Context<ToastContextProps>;
26
27
  declare const ToastProvider: ({ children, ...props }: ToastPrimitives.ToastProviderProps) => react_jsx_runtime.JSX.Element;
27
28
  type Toast = {
28
29
  title?: string;
@@ -35,4 +36,4 @@ declare const Toast: (props: ToastPrimitives.ToastProps & {
35
36
  }) => react_jsx_runtime.JSX.Element;
36
37
  declare const ToastAction: ({ ...props }: Omit<ToastPrimitives.ToastActionProps, "asChild">) => react_jsx_runtime.JSX.Element;
37
38
 
38
- export { ThemeContext, ThemeProvider, ToastAction, ToastProvider, ToastProviderContext };
39
+ export { ThemeContext, ThemeProvider, ToastAction, ToastContext, ToastProvider };
@@ -33,8 +33,8 @@ __export(providers_exports, {
33
33
  ThemeContext: () => ThemeContext,
34
34
  ThemeProvider: () => ThemeProvider,
35
35
  ToastAction: () => ToastAction,
36
- ToastProvider: () => ToastProvider,
37
- ToastProviderContext: () => ToastProviderContext
36
+ ToastContext: () => ToastContext,
37
+ ToastProvider: () => ToastProvider
38
38
  });
39
39
  module.exports = __toCommonJS(providers_exports);
40
40
 
@@ -46,68 +46,110 @@ var lightTheme = "light-theme__6in2mz0";
46
46
 
47
47
  // src/providers/theme-provider.tsx
48
48
  var import_react = require("react");
49
+
50
+ // src/providers/script.ts
51
+ var script = (storageKey2, defaultTheme2, classes2) => {
52
+ const el = document.documentElement;
53
+ function updateDOM(theme) {
54
+ el.classList.remove(...Object.values(classes2));
55
+ el.classList.add(classes2[theme]);
56
+ }
57
+ function getSystemTheme2() {
58
+ return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
59
+ }
60
+ try {
61
+ const themeName = localStorage.getItem(storageKey2) || defaultTheme2;
62
+ const isSystem = themeName === "system";
63
+ const theme = isSystem ? getSystemTheme2() : themeName;
64
+ updateDOM(theme);
65
+ } catch (e) {
66
+ }
67
+ };
68
+
69
+ // src/providers/theme-provider.tsx
49
70
  var import_jsx_runtime = require("react/jsx-runtime");
50
- var themes = ["light", "dark", "system"];
71
+ var MEDIA = "(prefers-color-scheme: dark)";
72
+ var isServer = typeof window === "undefined";
51
73
  var storageKey = "nayhoo-theme";
74
+ var themes = ["light", "dark", "system"];
75
+ var defaultTheme = "system";
76
+ var classes = {
77
+ light: lightTheme,
78
+ dark: darkTheme
79
+ };
52
80
  var ThemeContext = (0, import_react.createContext)({
53
- theme: "system",
81
+ theme: defaultTheme,
54
82
  setTheme: () => {
55
83
  },
84
+ resolvedTheme: "light",
56
85
  themes
57
86
  });
58
- var defaultTheme = "system";
59
87
  var ThemeProvider = ({ children }) => {
60
88
  const [theme, setTheme] = (0, import_react.useState)(() => getTheme());
61
- const applyTheme = (theme2) => {
62
- var _a, _b, _c, _d;
63
- const isDark = theme2 === "dark";
64
- const isSystem = theme2 === "system";
65
- if (isDark || (isSystem || !(storageKey in localStorage)) && window.matchMedia("(prefers-color-scheme: dark)").matches) {
66
- (_a = document.querySelector("html")) == null ? void 0 : _a.classList.add(darkTheme);
67
- (_b = document.querySelector("html")) == null ? void 0 : _b.classList.remove(lightTheme);
68
- } else {
69
- (_c = document.querySelector("html")) == null ? void 0 : _c.classList.add(lightTheme);
70
- (_d = document.querySelector("html")) == null ? void 0 : _d.classList.remove(darkTheme);
71
- }
72
- };
89
+ const [resolvedTheme, setResolvedTheme] = (0, import_react.useState)(
90
+ () => theme === "system" ? getSystemTheme() : theme
91
+ );
92
+ const applyTheme = (0, import_react.useCallback)((theme2) => {
93
+ const resolved = resolveTheme(theme2);
94
+ setResolvedTheme(resolved);
95
+ const d = document.documentElement;
96
+ d.classList.remove(...Object.values(classes));
97
+ d.classList.add(classes[resolved]);
98
+ }, []);
73
99
  (0, import_react.useEffect)(() => {
74
100
  applyTheme(theme);
75
101
  try {
76
102
  localStorage.setItem(storageKey, theme);
77
103
  } catch {
78
104
  }
79
- }, [theme]);
80
- return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ThemeContext, { value: { theme, setTheme, themes }, children: [
81
- /* @__PURE__ */ (0, import_jsx_runtime.jsx)("script", { children: `
82
- const isDark = localStorage.getItem("${storageKey}") === "dark";
83
- const isSystem = localStorage.getItem("${storageKey}") === "system";
84
-
85
- if (
86
- isDark ||
87
- ((isSystem || !("${storageKey}" in localStorage)) &&
88
- window.matchMedia("(prefers-color-scheme: dark)").matches)
89
- ) {
90
- document.querySelector("html")?.classList.add("${darkTheme}");
91
- document.querySelector("html")?.classList.remove("${lightTheme}");
92
- } else {
93
- document.querySelector("html")?.classList.add("${lightTheme}");
94
- document.querySelector("html")?.classList.remove("${darkTheme}");
95
- }
96
- ` }),
105
+ }, [applyTheme, theme]);
106
+ const value = (0, import_react.useMemo)(
107
+ () => ({
108
+ theme,
109
+ setTheme,
110
+ resolvedTheme: theme === "system" ? resolvedTheme : theme,
111
+ themes
112
+ }),
113
+ [theme, setTheme, resolvedTheme]
114
+ );
115
+ const scriptArgs = JSON.stringify([storageKey, defaultTheme, classes]).slice(
116
+ 1,
117
+ -1
118
+ );
119
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(ThemeContext, { value, children: [
120
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
121
+ "script",
122
+ {
123
+ suppressHydrationWarning: true,
124
+ dangerouslySetInnerHTML: {
125
+ __html: `(${script.toString()})(${scriptArgs})`
126
+ }
127
+ }
128
+ ),
97
129
  children
98
130
  ] });
99
131
  };
100
132
  var getTheme = () => {
101
- var _a;
102
- if (typeof window === "undefined") {
103
- return defaultTheme;
104
- }
105
- let theme;
133
+ if (isServer) return defaultTheme;
134
+ let themeName;
106
135
  try {
107
- theme = localStorage.getItem(storageKey) || void 0;
108
- } catch {
136
+ themeName = localStorage.getItem(storageKey) || void 0;
137
+ } catch (e) {
138
+ }
139
+ return themes.find((theme) => theme === themeName) || defaultTheme;
140
+ };
141
+ var getSystemTheme = (e) => {
142
+ if (!e) e = window.matchMedia(MEDIA);
143
+ const isDark = e.matches;
144
+ const systemTheme = isDark ? "dark" : "light";
145
+ return systemTheme;
146
+ };
147
+ var resolveTheme = (theme) => {
148
+ let resolved = theme;
149
+ if (theme === "system") {
150
+ resolved = getSystemTheme();
109
151
  }
110
- return (_a = themes.find((t) => t === theme)) != null ? _a : defaultTheme;
152
+ return resolved;
111
153
  };
112
154
 
113
155
  // src/theme/recipes/spinner.css.ts
@@ -115,8 +157,8 @@ var import_createRuntimeFn = require("@vanilla-extract/recipes/createRuntimeFn")
115
157
  var spinnerRecipe = (0, import_createRuntimeFn.createRuntimeFn)({ defaultClassName: "spinner_spinnerRecipe__15xzcy90", variantClassNames: { size: { "1": "spinner_spinnerRecipe_size_1__15xzcy91", "2": "spinner_spinnerRecipe_size_2__15xzcy92", "3": "spinner_spinnerRecipe_size_3__15xzcy93", "4": "spinner_spinnerRecipe_size_4__15xzcy94", "5": "spinner_spinnerRecipe_size_5__15xzcy95", "6": "spinner_spinnerRecipe_size_6__15xzcy96", "7": "spinner_spinnerRecipe_size_7__15xzcy97", "8": "spinner_spinnerRecipe_size_8__15xzcy98", "9": "spinner_spinnerRecipe_size_9__15xzcy99" } }, defaultVariants: { size: "3" }, compoundVariants: [] });
116
158
 
117
159
  // src/utils/merge-classes.ts
118
- function mergeClasses(...classes) {
119
- return classes.filter((cls) => Boolean(cls)).join(" ");
160
+ function mergeClasses(...classes2) {
161
+ return classes2.filter((cls) => Boolean(cls)).join(" ");
120
162
  }
121
163
 
122
164
  // src/utils/pick.ts
@@ -258,7 +300,7 @@ var defaultToast = Object.assign(() => {
258
300
  error: () => {
259
301
  }
260
302
  });
261
- var ToastProviderContext = React2.createContext(defaultToast);
303
+ var ToastContext = React2.createContext(defaultToast);
262
304
  var toastElementsMapRef = React2.createRef();
263
305
  var defaultToastImpl = {
264
306
  toastElementsMapRef,
@@ -356,7 +398,7 @@ var ToastProvider = ({
356
398
  }),
357
399
  [handleDispatchDefault, handleDispatchSuccess, handleDispatchError]
358
400
  );
359
- return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ToastProviderContext.Provider, { value: toastApi, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
401
+ return /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(ToastContext.Provider, { value: toastApi, children: /* @__PURE__ */ (0, import_jsx_runtime7.jsx)(
360
402
  ToastContextImpl.Provider,
361
403
  {
362
404
  value: React2.useMemo(
@@ -470,7 +512,7 @@ var ToastAction = ({
470
512
  ThemeContext,
471
513
  ThemeProvider,
472
514
  ToastAction,
473
- ToastProvider,
474
- ToastProviderContext
515
+ ToastContext,
516
+ ToastProvider
475
517
  });
476
518
  //# sourceMappingURL=index.js.map