@koine/next 1.0.59 → 1.0.62
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/DisableErrorOverlay.d.ts +7 -0
- package/DisableErrorOverlay.js +13 -0
- package/Link.d.ts +1 -1
- package/ThemeContext.d.ts +17 -0
- package/ThemeContext.js +7 -0
- package/ThemeProvider.d.ts +35 -0
- package/ThemeProvider.js +246 -0
- package/app/css/AppTheme.d.ts +1 -1
- package/app/css/AppTheme.js +1 -1
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/node/DisableErrorOverlay.js +18 -0
- package/node/ThemeContext.js +10 -0
- package/node/ThemeProvider.js +250 -0
- package/node/app/css/AppTheme.js +2 -2
- package/node/index.js +3 -0
- package/node/useTheme.js +3 -255
- package/package.json +15 -15
- package/useTheme.d.ts +3 -43
- package/useTheme.js +2 -253
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ThemeProvider = void 0;
|
|
4
|
+
var tslib_1 = require("tslib");
|
|
5
|
+
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
+
var react_1 = require("react");
|
|
7
|
+
var script_1 = tslib_1.__importDefault(require("next/script"));
|
|
8
|
+
var utils_1 = require("@koine/utils");
|
|
9
|
+
var ThemeContext_1 = require("./ThemeContext");
|
|
10
|
+
var colorSchemes = ["light", "dark"];
|
|
11
|
+
var MEDIA = "(prefers-color-scheme: dark)";
|
|
12
|
+
var THEME_STORAGE_KEY = "theme";
|
|
13
|
+
/**
|
|
14
|
+
* @borrows [next-themes](https://github.com/pacocoursey/next-themes)
|
|
15
|
+
*
|
|
16
|
+
* Differences:
|
|
17
|
+
*
|
|
18
|
+
* - enableColorScheme: `false` by default (instead of `true`), this plays more
|
|
19
|
+
* nicely with tailwind `dark` class mode as dark theme is supposed to be only
|
|
20
|
+
* controlled by tailwind modifiers
|
|
21
|
+
*/
|
|
22
|
+
var ThemeProvider = function (_a) {
|
|
23
|
+
var forcedTheme = _a.forcedTheme, _b = _a.disableTransitionOnChange, disableTransitionOnChange = _b === void 0 ? false : _b, _c = _a.enableSystem, enableSystem = _c === void 0 ? true : _c, enableColorScheme = _a.enableColorScheme, _d = _a.themes, themes = _d === void 0 ? ["light", "dark"] : _d, _e = _a.defaultTheme, defaultTheme = _e === void 0 ? enableSystem ? "system" : "light" : _e, _f = _a.attribute, attribute = _f === void 0 ? "data-theme" : _f, value = _a.value, children = _a.children, nonce = _a.nonce;
|
|
24
|
+
var _g = (0, react_1.useState)(function () {
|
|
25
|
+
return getTheme(THEME_STORAGE_KEY, defaultTheme);
|
|
26
|
+
}), theme = _g[0], setThemeState = _g[1];
|
|
27
|
+
var _h = (0, react_1.useState)(function () {
|
|
28
|
+
return getTheme(THEME_STORAGE_KEY);
|
|
29
|
+
}), resolvedTheme = _h[0], setResolvedTheme = _h[1];
|
|
30
|
+
var attrs = !value ? themes : Object.values(value);
|
|
31
|
+
var applyTheme = (0, react_1.useCallback)(function (theme) {
|
|
32
|
+
var _a;
|
|
33
|
+
var resolved = theme;
|
|
34
|
+
if (utils_1.isServer || !resolved)
|
|
35
|
+
return;
|
|
36
|
+
// If theme is system, resolve it before setting theme
|
|
37
|
+
if (theme === "system" && enableSystem) {
|
|
38
|
+
resolved = getSystemTheme();
|
|
39
|
+
}
|
|
40
|
+
var name = value ? value[resolved] : resolved;
|
|
41
|
+
var enable = disableTransitionOnChange ? disableAnimation() : null;
|
|
42
|
+
var d = document.documentElement;
|
|
43
|
+
if (attribute === "class") {
|
|
44
|
+
(_a = d.classList).remove.apply(_a, attrs);
|
|
45
|
+
if (name)
|
|
46
|
+
d.classList.add(name);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
if (name) {
|
|
50
|
+
d.setAttribute(attribute, name);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
d.removeAttribute(attribute);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (enableColorScheme) {
|
|
57
|
+
var fallback = colorSchemes.includes(defaultTheme)
|
|
58
|
+
? defaultTheme
|
|
59
|
+
: "";
|
|
60
|
+
var colorScheme = colorSchemes.includes(resolved)
|
|
61
|
+
? resolved
|
|
62
|
+
: fallback;
|
|
63
|
+
d.style.colorScheme = colorScheme;
|
|
64
|
+
}
|
|
65
|
+
enable === null || enable === void 0 ? void 0 : enable();
|
|
66
|
+
}, [
|
|
67
|
+
attribute,
|
|
68
|
+
attrs,
|
|
69
|
+
defaultTheme,
|
|
70
|
+
disableTransitionOnChange,
|
|
71
|
+
enableColorScheme,
|
|
72
|
+
enableSystem,
|
|
73
|
+
value,
|
|
74
|
+
]);
|
|
75
|
+
var setTheme = (0, react_1.useCallback)(function (theme) {
|
|
76
|
+
setThemeState(theme);
|
|
77
|
+
// Save to storage
|
|
78
|
+
try {
|
|
79
|
+
localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
// Unsupported
|
|
83
|
+
}
|
|
84
|
+
}, []);
|
|
85
|
+
var handleMediaQuery = (0, react_1.useCallback)(function (e) {
|
|
86
|
+
var resolved = getSystemTheme(e);
|
|
87
|
+
setResolvedTheme(resolved);
|
|
88
|
+
if (theme === "system" && enableSystem && !forcedTheme) {
|
|
89
|
+
applyTheme("system");
|
|
90
|
+
}
|
|
91
|
+
}, [theme, enableSystem, forcedTheme, applyTheme]);
|
|
92
|
+
// Always listen to System preference
|
|
93
|
+
(0, react_1.useEffect)(function () {
|
|
94
|
+
var media = window.matchMedia(MEDIA);
|
|
95
|
+
// Intentionally use deprecated listener methods to support iOS & old browsers
|
|
96
|
+
media.addListener(handleMediaQuery);
|
|
97
|
+
handleMediaQuery(media);
|
|
98
|
+
return function () { return media.removeListener(handleMediaQuery); };
|
|
99
|
+
}, [handleMediaQuery]);
|
|
100
|
+
// localStorage event handling
|
|
101
|
+
(0, react_1.useEffect)(function () {
|
|
102
|
+
var handleStorage = function (e) {
|
|
103
|
+
if (e.key !== THEME_STORAGE_KEY) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
// If default theme set, use it if localstorage === null (happens on local storage manual deletion)
|
|
107
|
+
var theme = e.newValue || defaultTheme;
|
|
108
|
+
setTheme(theme);
|
|
109
|
+
};
|
|
110
|
+
window.addEventListener("storage", handleStorage);
|
|
111
|
+
return function () { return window.removeEventListener("storage", handleStorage); };
|
|
112
|
+
}, [defaultTheme, setTheme]);
|
|
113
|
+
// Whenever theme or forcedTheme changes, apply it
|
|
114
|
+
(0, react_1.useEffect)(function () {
|
|
115
|
+
applyTheme(forcedTheme !== null && forcedTheme !== void 0 ? forcedTheme : theme);
|
|
116
|
+
}, [applyTheme, forcedTheme, theme]);
|
|
117
|
+
return ((0, jsx_runtime_1.jsxs)(ThemeContext_1.ThemeContext.Provider, tslib_1.__assign({ value: {
|
|
118
|
+
theme: theme,
|
|
119
|
+
setTheme: setTheme,
|
|
120
|
+
forcedTheme: forcedTheme,
|
|
121
|
+
resolvedTheme: theme === "system" ? resolvedTheme : theme,
|
|
122
|
+
themes: enableSystem ? tslib_1.__spreadArray(tslib_1.__spreadArray([], themes, true), ["system"], false) : themes,
|
|
123
|
+
systemTheme: (enableSystem ? resolvedTheme : undefined),
|
|
124
|
+
} }, { children: [(0, jsx_runtime_1.jsx)(ThemeScript, tslib_1.__assign({}, {
|
|
125
|
+
forcedTheme: forcedTheme,
|
|
126
|
+
disableTransitionOnChange: disableTransitionOnChange,
|
|
127
|
+
enableSystem: enableSystem,
|
|
128
|
+
enableColorScheme: enableColorScheme,
|
|
129
|
+
themes: themes,
|
|
130
|
+
defaultTheme: defaultTheme,
|
|
131
|
+
attribute: attribute,
|
|
132
|
+
value: value,
|
|
133
|
+
children: children,
|
|
134
|
+
attrs: attrs,
|
|
135
|
+
nonce: nonce,
|
|
136
|
+
})), children] })));
|
|
137
|
+
};
|
|
138
|
+
exports.ThemeProvider = ThemeProvider;
|
|
139
|
+
var ThemeScript = (0, react_1.memo)(function (_a) {
|
|
140
|
+
var forcedTheme = _a.forcedTheme, attribute = _a.attribute, enableSystem = _a.enableSystem, enableColorScheme = _a.enableColorScheme, defaultTheme = _a.defaultTheme, value = _a.value, attrs = _a.attrs, nonce = _a.nonce;
|
|
141
|
+
var defaultSystem = defaultTheme === "system";
|
|
142
|
+
// Code-golfing the amount of characters in the script
|
|
143
|
+
var optimization = (function () {
|
|
144
|
+
var removeClasses = "d.remove(".concat(attrs
|
|
145
|
+
.map(function (t) { return "'".concat(t, "'"); })
|
|
146
|
+
.join(","), ")");
|
|
147
|
+
return "var d=document.documentElement.classList;".concat(removeClasses, ";");
|
|
148
|
+
})();
|
|
149
|
+
var fallbackColorScheme = (function () {
|
|
150
|
+
if (!enableColorScheme) {
|
|
151
|
+
return "";
|
|
152
|
+
}
|
|
153
|
+
var fallback = colorSchemes.includes(defaultTheme)
|
|
154
|
+
? defaultTheme
|
|
155
|
+
: null;
|
|
156
|
+
if (fallback) {
|
|
157
|
+
return "if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'".concat(defaultTheme, "'");
|
|
158
|
+
}
|
|
159
|
+
else {
|
|
160
|
+
return "if(e==='light'||e==='dark')d.style.colorScheme=e";
|
|
161
|
+
}
|
|
162
|
+
})();
|
|
163
|
+
var updateDOM = function (name, literal, setColorScheme) {
|
|
164
|
+
if (literal === void 0) { literal = false; }
|
|
165
|
+
if (setColorScheme === void 0) { setColorScheme = true; }
|
|
166
|
+
var resolvedName = value ? value[name] : name;
|
|
167
|
+
var val = literal ? name + "|| ''" : "'".concat(resolvedName, "'");
|
|
168
|
+
var text = "";
|
|
169
|
+
// MUCH faster to set colorScheme alongside HTML attribute/class
|
|
170
|
+
// as it only incurs 1 style recalculation rather than 2
|
|
171
|
+
// This can save over 250ms of work for pages with big DOM
|
|
172
|
+
if (enableColorScheme &&
|
|
173
|
+
setColorScheme &&
|
|
174
|
+
!literal &&
|
|
175
|
+
colorSchemes.includes(name)) {
|
|
176
|
+
text += "d.style.colorScheme = '".concat(name, "';");
|
|
177
|
+
}
|
|
178
|
+
if (attribute === "class") {
|
|
179
|
+
if (literal || resolvedName) {
|
|
180
|
+
text += "d.add(".concat(val, ")");
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
text += "null";
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
if (resolvedName) {
|
|
188
|
+
text += "d[s](n, ".concat(val, ")");
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
return text;
|
|
192
|
+
};
|
|
193
|
+
var scriptSrc = (function () {
|
|
194
|
+
if (forcedTheme) {
|
|
195
|
+
return "!function(){".concat(optimization).concat(updateDOM(forcedTheme), "}()");
|
|
196
|
+
}
|
|
197
|
+
if (enableSystem) {
|
|
198
|
+
return "!function(){try {".concat(optimization, "var e=localStorage.getItem('").concat(THEME_STORAGE_KEY, "');if(\"system\"===e||(!e&&").concat(defaultSystem, ")){var t=\"").concat(MEDIA, "\",m=window.matchMedia(t);if(m.media!==t||m.matches){").concat(updateDOM("dark"), "}else{").concat(updateDOM("light"), "}}else if(e){").concat(value ? "var x=".concat(JSON.stringify(value), ";") : "").concat(updateDOM(value ? "x[e]" : "e", true), "}").concat(!defaultSystem
|
|
199
|
+
? "else{" + updateDOM(defaultTheme, false, false) + "}"
|
|
200
|
+
: "").concat(fallbackColorScheme, "}catch(e){}}()");
|
|
201
|
+
}
|
|
202
|
+
return "!function(){try{".concat(optimization, "var e=localStorage.getItem(\"").concat(THEME_STORAGE_KEY, "\");if(e){").concat(value ? "var x=".concat(JSON.stringify(value), ";") : "").concat(updateDOM(value ? "x[e]" : "e", true), "}else{").concat(updateDOM(defaultTheme, false, false), ";}").concat(fallbackColorScheme, "}catch(t){}}();");
|
|
203
|
+
})();
|
|
204
|
+
// We MUST use next/script's `beforeInteractive` strategy to avoid flashing on load.
|
|
205
|
+
// However, it only accepts the `src` prop, not `dangerouslySetInnerHTML` or `children`
|
|
206
|
+
// But our script cannot be external because it changes at runtime based on React props
|
|
207
|
+
// so we trick next/script by passing `src` as a base64 JS script
|
|
208
|
+
var encodedScript = "data:text/javascript;base64,".concat(encodeBase64(scriptSrc));
|
|
209
|
+
return ((0, jsx_runtime_1.jsx)(script_1.default, { id: "next-theme-script", strategy: "beforeInteractive", src: encodedScript, nonce: nonce }));
|
|
210
|
+
},
|
|
211
|
+
// Never re-render this component
|
|
212
|
+
function () { return true; });
|
|
213
|
+
// Helpers
|
|
214
|
+
var getTheme = function (key, fallback) {
|
|
215
|
+
if (utils_1.isServer)
|
|
216
|
+
return undefined;
|
|
217
|
+
var theme;
|
|
218
|
+
try {
|
|
219
|
+
theme = localStorage.getItem(key) || undefined;
|
|
220
|
+
}
|
|
221
|
+
catch (e) {
|
|
222
|
+
// Unsupported
|
|
223
|
+
}
|
|
224
|
+
return theme || fallback;
|
|
225
|
+
};
|
|
226
|
+
var disableAnimation = function () {
|
|
227
|
+
var d = document;
|
|
228
|
+
var css = d.createElement("style");
|
|
229
|
+
css.appendChild(d.createTextNode("*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}"));
|
|
230
|
+
d.head.appendChild(css);
|
|
231
|
+
return function () {
|
|
232
|
+
// Force restyle
|
|
233
|
+
(function () { return window.getComputedStyle(d.body); })();
|
|
234
|
+
// Wait for next tick before removing
|
|
235
|
+
setTimeout(function () {
|
|
236
|
+
d.head.removeChild(css);
|
|
237
|
+
}, 1);
|
|
238
|
+
};
|
|
239
|
+
};
|
|
240
|
+
var getSystemTheme = function (e) {
|
|
241
|
+
if (!e)
|
|
242
|
+
e = window.matchMedia(MEDIA);
|
|
243
|
+
var isDark = e.matches;
|
|
244
|
+
var systemTheme = isDark ? "dark" : "light";
|
|
245
|
+
return systemTheme;
|
|
246
|
+
};
|
|
247
|
+
var encodeBase64 = function (str) {
|
|
248
|
+
return utils_1.isServer ? Buffer.from(str).toString("base64") : btoa(str);
|
|
249
|
+
};
|
|
250
|
+
exports.default = exports.ThemeProvider;
|
package/node/app/css/AppTheme.js
CHANGED
|
@@ -4,7 +4,7 @@ exports.AppTheme = void 0;
|
|
|
4
4
|
var tslib_1 = require("tslib");
|
|
5
5
|
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
6
|
// import { ThemeVanillaProvider, ThemeVanillaValue } from "@koine/react";
|
|
7
|
-
var
|
|
7
|
+
var ThemeProvider_1 = require("../../ThemeProvider");
|
|
8
8
|
/**
|
|
9
9
|
* App theme with vanilla class based theme (good for `tailwindcss`)
|
|
10
10
|
*/
|
|
@@ -13,6 +13,6 @@ var AppTheme = function (_a) {
|
|
|
13
13
|
// return (
|
|
14
14
|
// <ThemeVanillaProvider initialTheme={theme}>{children}</ThemeVanillaProvider>
|
|
15
15
|
// );
|
|
16
|
-
return ((0, jsx_runtime_1.jsx)(
|
|
16
|
+
return ((0, jsx_runtime_1.jsx)(ThemeProvider_1.ThemeProvider, tslib_1.__assign({ defaultTheme: theme, attribute: "class" }, { children: children })));
|
|
17
17
|
};
|
|
18
18
|
exports.AppTheme = AppTheme;
|
package/node/index.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
var tslib_1 = require("tslib");
|
|
4
4
|
tslib_1.__exportStar(require("./AnalyticsGoogle"), exports);
|
|
5
5
|
// export * from "./createEmotionCache";
|
|
6
|
+
tslib_1.__exportStar(require("./DisableErrorOverlay"), exports);
|
|
6
7
|
tslib_1.__exportStar(require("./DynamicNamespaces"), exports);
|
|
7
8
|
tslib_1.__exportStar(require("./Favicon"), exports);
|
|
8
9
|
tslib_1.__exportStar(require("./getSiteUrl"), exports);
|
|
@@ -18,6 +19,8 @@ tslib_1.__exportStar(require("./Seo"), exports);
|
|
|
18
19
|
// export * from "./seoBuildTags";
|
|
19
20
|
tslib_1.__exportStar(require("./SeoDefaults"), exports);
|
|
20
21
|
tslib_1.__exportStar(require("./T"), exports);
|
|
22
|
+
tslib_1.__exportStar(require("./ThemeContext"), exports);
|
|
23
|
+
tslib_1.__exportStar(require("./ThemeProvider"), exports);
|
|
21
24
|
tslib_1.__exportStar(require("./to"), exports);
|
|
22
25
|
tslib_1.__exportStar(require("./translationAsOptions"), exports);
|
|
23
26
|
tslib_1.__exportStar(require("./types-i18n"), exports);
|
package/node/useTheme.js
CHANGED
|
@@ -1,263 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
var tslib_1 = require("tslib");
|
|
5
|
-
var jsx_runtime_1 = require("react/jsx-runtime");
|
|
6
|
-
/**
|
|
7
|
-
* @file
|
|
8
|
-
*
|
|
9
|
-
* Adapted from [next-themes](https://github.com/pacocoursey/next-themes)
|
|
10
|
-
*
|
|
11
|
-
* Differences:
|
|
12
|
-
*
|
|
13
|
-
* - enableColorScheme: `false` by default (instead of `true`), this plays more
|
|
14
|
-
* nicely with tailwind `dark` class mode as dark theme is supposed to be only
|
|
15
|
-
* controlled by tailwind modifiers
|
|
16
|
-
*/
|
|
3
|
+
exports.useTheme = void 0;
|
|
17
4
|
var react_1 = require("react");
|
|
18
|
-
var
|
|
19
|
-
var utils_1 = require("@koine/utils");
|
|
20
|
-
var THEME_STORAGE_KEY = "theme";
|
|
21
|
-
var colorSchemes = ["light", "dark"];
|
|
22
|
-
var MEDIA = "(prefers-color-scheme: dark)";
|
|
23
|
-
var ThemeContext = (0, react_1.createContext)({
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
25
|
-
setTheme: function (_) { },
|
|
26
|
-
themes: [],
|
|
27
|
-
});
|
|
5
|
+
var ThemeContext_1 = require("./ThemeContext");
|
|
28
6
|
/**
|
|
29
7
|
* @borrows [next-themes](https://github.com/pacocoursey/next-themes)
|
|
30
8
|
*/
|
|
31
|
-
var useTheme = function () { return (0, react_1.useContext)(ThemeContext); };
|
|
9
|
+
var useTheme = function () { return (0, react_1.useContext)(ThemeContext_1.ThemeContext); };
|
|
32
10
|
exports.useTheme = useTheme;
|
|
33
|
-
/**
|
|
34
|
-
* @borrows [next-themes](https://github.com/pacocoursey/next-themes)
|
|
35
|
-
*/
|
|
36
|
-
var ThemeProvider = function (_a) {
|
|
37
|
-
var forcedTheme = _a.forcedTheme, _b = _a.disableTransitionOnChange, disableTransitionOnChange = _b === void 0 ? false : _b, _c = _a.enableSystem, enableSystem = _c === void 0 ? true : _c, enableColorScheme = _a.enableColorScheme, _d = _a.themes, themes = _d === void 0 ? ["light", "dark"] : _d, _e = _a.defaultTheme, defaultTheme = _e === void 0 ? enableSystem ? "system" : "light" : _e, _f = _a.attribute, attribute = _f === void 0 ? "data-theme" : _f, value = _a.value, children = _a.children, nonce = _a.nonce;
|
|
38
|
-
var _g = (0, react_1.useState)(function () {
|
|
39
|
-
return getTheme(THEME_STORAGE_KEY, defaultTheme);
|
|
40
|
-
}), theme = _g[0], setThemeState = _g[1];
|
|
41
|
-
var _h = (0, react_1.useState)(function () {
|
|
42
|
-
return getTheme(THEME_STORAGE_KEY);
|
|
43
|
-
}), resolvedTheme = _h[0], setResolvedTheme = _h[1];
|
|
44
|
-
var attrs = !value ? themes : Object.values(value);
|
|
45
|
-
var applyTheme = (0, react_1.useCallback)(function (theme) {
|
|
46
|
-
var _a;
|
|
47
|
-
var resolved = theme;
|
|
48
|
-
if (utils_1.isServer || !resolved)
|
|
49
|
-
return;
|
|
50
|
-
// If theme is system, resolve it before setting theme
|
|
51
|
-
if (theme === "system" && enableSystem) {
|
|
52
|
-
resolved = getSystemTheme();
|
|
53
|
-
}
|
|
54
|
-
var name = value ? value[resolved] : resolved;
|
|
55
|
-
var enable = disableTransitionOnChange ? disableAnimation() : null;
|
|
56
|
-
var d = document.documentElement;
|
|
57
|
-
if (attribute === "class") {
|
|
58
|
-
(_a = d.classList).remove.apply(_a, attrs);
|
|
59
|
-
if (name)
|
|
60
|
-
d.classList.add(name);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
if (name) {
|
|
64
|
-
d.setAttribute(attribute, name);
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
d.removeAttribute(attribute);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
if (enableColorScheme) {
|
|
71
|
-
var fallback = colorSchemes.includes(defaultTheme)
|
|
72
|
-
? defaultTheme
|
|
73
|
-
: "";
|
|
74
|
-
var colorScheme = colorSchemes.includes(resolved)
|
|
75
|
-
? resolved
|
|
76
|
-
: fallback;
|
|
77
|
-
d.style.colorScheme = colorScheme;
|
|
78
|
-
}
|
|
79
|
-
enable === null || enable === void 0 ? void 0 : enable();
|
|
80
|
-
}, [
|
|
81
|
-
attribute,
|
|
82
|
-
attrs,
|
|
83
|
-
defaultTheme,
|
|
84
|
-
disableTransitionOnChange,
|
|
85
|
-
enableColorScheme,
|
|
86
|
-
enableSystem,
|
|
87
|
-
value,
|
|
88
|
-
]);
|
|
89
|
-
var setTheme = (0, react_1.useCallback)(function (theme) {
|
|
90
|
-
setThemeState(theme);
|
|
91
|
-
// Save to storage
|
|
92
|
-
try {
|
|
93
|
-
localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
94
|
-
}
|
|
95
|
-
catch (e) {
|
|
96
|
-
// Unsupported
|
|
97
|
-
}
|
|
98
|
-
}, []);
|
|
99
|
-
var handleMediaQuery = (0, react_1.useCallback)(function (e) {
|
|
100
|
-
var resolved = getSystemTheme(e);
|
|
101
|
-
setResolvedTheme(resolved);
|
|
102
|
-
if (theme === "system" && enableSystem && !forcedTheme) {
|
|
103
|
-
applyTheme("system");
|
|
104
|
-
}
|
|
105
|
-
}, [theme, enableSystem, forcedTheme, applyTheme]);
|
|
106
|
-
// Always listen to System preference
|
|
107
|
-
(0, react_1.useEffect)(function () {
|
|
108
|
-
var media = window.matchMedia(MEDIA);
|
|
109
|
-
// Intentionally use deprecated listener methods to support iOS & old browsers
|
|
110
|
-
media.addListener(handleMediaQuery);
|
|
111
|
-
handleMediaQuery(media);
|
|
112
|
-
return function () { return media.removeListener(handleMediaQuery); };
|
|
113
|
-
}, [handleMediaQuery]);
|
|
114
|
-
// localStorage event handling
|
|
115
|
-
(0, react_1.useEffect)(function () {
|
|
116
|
-
var handleStorage = function (e) {
|
|
117
|
-
if (e.key !== THEME_STORAGE_KEY) {
|
|
118
|
-
return;
|
|
119
|
-
}
|
|
120
|
-
// If default theme set, use it if localstorage === null (happens on local storage manual deletion)
|
|
121
|
-
var theme = e.newValue || defaultTheme;
|
|
122
|
-
setTheme(theme);
|
|
123
|
-
};
|
|
124
|
-
window.addEventListener("storage", handleStorage);
|
|
125
|
-
return function () { return window.removeEventListener("storage", handleStorage); };
|
|
126
|
-
}, [defaultTheme, setTheme]);
|
|
127
|
-
// Whenever theme or forcedTheme changes, apply it
|
|
128
|
-
(0, react_1.useEffect)(function () {
|
|
129
|
-
applyTheme(forcedTheme !== null && forcedTheme !== void 0 ? forcedTheme : theme);
|
|
130
|
-
}, [applyTheme, forcedTheme, theme]);
|
|
131
|
-
return ((0, jsx_runtime_1.jsxs)(ThemeContext.Provider, tslib_1.__assign({ value: {
|
|
132
|
-
theme: theme,
|
|
133
|
-
setTheme: setTheme,
|
|
134
|
-
forcedTheme: forcedTheme,
|
|
135
|
-
resolvedTheme: theme === "system" ? resolvedTheme : theme,
|
|
136
|
-
themes: enableSystem ? tslib_1.__spreadArray(tslib_1.__spreadArray([], themes, true), ["system"], false) : themes,
|
|
137
|
-
systemTheme: (enableSystem ? resolvedTheme : undefined),
|
|
138
|
-
} }, { children: [(0, jsx_runtime_1.jsx)(ThemeScript, tslib_1.__assign({}, {
|
|
139
|
-
forcedTheme: forcedTheme,
|
|
140
|
-
disableTransitionOnChange: disableTransitionOnChange,
|
|
141
|
-
enableSystem: enableSystem,
|
|
142
|
-
enableColorScheme: enableColorScheme,
|
|
143
|
-
themes: themes,
|
|
144
|
-
defaultTheme: defaultTheme,
|
|
145
|
-
attribute: attribute,
|
|
146
|
-
value: value,
|
|
147
|
-
children: children,
|
|
148
|
-
attrs: attrs,
|
|
149
|
-
nonce: nonce,
|
|
150
|
-
})), children] })));
|
|
151
|
-
};
|
|
152
|
-
exports.ThemeProvider = ThemeProvider;
|
|
153
|
-
var ThemeScript = (0, react_1.memo)(function (_a) {
|
|
154
|
-
var forcedTheme = _a.forcedTheme, attribute = _a.attribute, enableSystem = _a.enableSystem, enableColorScheme = _a.enableColorScheme, defaultTheme = _a.defaultTheme, value = _a.value, attrs = _a.attrs, nonce = _a.nonce;
|
|
155
|
-
var defaultSystem = defaultTheme === "system";
|
|
156
|
-
// Code-golfing the amount of characters in the script
|
|
157
|
-
var optimization = (function () {
|
|
158
|
-
var removeClasses = "d.remove(".concat(attrs
|
|
159
|
-
.map(function (t) { return "'".concat(t, "'"); })
|
|
160
|
-
.join(","), ")");
|
|
161
|
-
return "var d=document.documentElement.classList;".concat(removeClasses, ";");
|
|
162
|
-
})();
|
|
163
|
-
var fallbackColorScheme = (function () {
|
|
164
|
-
if (!enableColorScheme) {
|
|
165
|
-
return "";
|
|
166
|
-
}
|
|
167
|
-
var fallback = colorSchemes.includes(defaultTheme)
|
|
168
|
-
? defaultTheme
|
|
169
|
-
: null;
|
|
170
|
-
if (fallback) {
|
|
171
|
-
return "if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'".concat(defaultTheme, "'");
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
return "if(e==='light'||e==='dark')d.style.colorScheme=e";
|
|
175
|
-
}
|
|
176
|
-
})();
|
|
177
|
-
var updateDOM = function (name, literal, setColorScheme) {
|
|
178
|
-
if (literal === void 0) { literal = false; }
|
|
179
|
-
if (setColorScheme === void 0) { setColorScheme = true; }
|
|
180
|
-
var resolvedName = value ? value[name] : name;
|
|
181
|
-
var val = literal ? name + "|| ''" : "'".concat(resolvedName, "'");
|
|
182
|
-
var text = "";
|
|
183
|
-
// MUCH faster to set colorScheme alongside HTML attribute/class
|
|
184
|
-
// as it only incurs 1 style recalculation rather than 2
|
|
185
|
-
// This can save over 250ms of work for pages with big DOM
|
|
186
|
-
if (enableColorScheme &&
|
|
187
|
-
setColorScheme &&
|
|
188
|
-
!literal &&
|
|
189
|
-
colorSchemes.includes(name)) {
|
|
190
|
-
text += "d.style.colorScheme = '".concat(name, "';");
|
|
191
|
-
}
|
|
192
|
-
if (attribute === "class") {
|
|
193
|
-
if (literal || resolvedName) {
|
|
194
|
-
text += "d.add(".concat(val, ")");
|
|
195
|
-
}
|
|
196
|
-
else {
|
|
197
|
-
text += "null";
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
else {
|
|
201
|
-
if (resolvedName) {
|
|
202
|
-
text += "d[s](n, ".concat(val, ")");
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return text;
|
|
206
|
-
};
|
|
207
|
-
var scriptSrc = (function () {
|
|
208
|
-
if (forcedTheme) {
|
|
209
|
-
return "!function(){".concat(optimization).concat(updateDOM(forcedTheme), "}()");
|
|
210
|
-
}
|
|
211
|
-
if (enableSystem) {
|
|
212
|
-
return "!function(){try {".concat(optimization, "var e=localStorage.getItem('").concat(THEME_STORAGE_KEY, "');if(\"system\"===e||(!e&&").concat(defaultSystem, ")){var t=\"").concat(MEDIA, "\",m=window.matchMedia(t);if(m.media!==t||m.matches){").concat(updateDOM("dark"), "}else{").concat(updateDOM("light"), "}}else if(e){").concat(value ? "var x=".concat(JSON.stringify(value), ";") : "").concat(updateDOM(value ? "x[e]" : "e", true), "}").concat(!defaultSystem
|
|
213
|
-
? "else{" + updateDOM(defaultTheme, false, false) + "}"
|
|
214
|
-
: "").concat(fallbackColorScheme, "}catch(e){}}()");
|
|
215
|
-
}
|
|
216
|
-
return "!function(){try{".concat(optimization, "var e=localStorage.getItem(\"").concat(THEME_STORAGE_KEY, "\");if(e){").concat(value ? "var x=".concat(JSON.stringify(value), ";") : "").concat(updateDOM(value ? "x[e]" : "e", true), "}else{").concat(updateDOM(defaultTheme, false, false), ";}").concat(fallbackColorScheme, "}catch(t){}}();");
|
|
217
|
-
})();
|
|
218
|
-
// We MUST use next/script's `beforeInteractive` strategy to avoid flashing on load.
|
|
219
|
-
// However, it only accepts the `src` prop, not `dangerouslySetInnerHTML` or `children`
|
|
220
|
-
// But our script cannot be external because it changes at runtime based on React props
|
|
221
|
-
// so we trick next/script by passing `src` as a base64 JS script
|
|
222
|
-
var encodedScript = "data:text/javascript;base64,".concat(encodeBase64(scriptSrc));
|
|
223
|
-
return ((0, jsx_runtime_1.jsx)(script_1.default, { id: "next-theme-script", strategy: "beforeInteractive", src: encodedScript, nonce: nonce }));
|
|
224
|
-
},
|
|
225
|
-
// Never re-render this component
|
|
226
|
-
function () { return true; });
|
|
227
|
-
// Helpers
|
|
228
|
-
var getTheme = function (key, fallback) {
|
|
229
|
-
if (utils_1.isServer)
|
|
230
|
-
return undefined;
|
|
231
|
-
var theme;
|
|
232
|
-
try {
|
|
233
|
-
theme = localStorage.getItem(key) || undefined;
|
|
234
|
-
}
|
|
235
|
-
catch (e) {
|
|
236
|
-
// Unsupported
|
|
237
|
-
}
|
|
238
|
-
return theme || fallback;
|
|
239
|
-
};
|
|
240
|
-
var disableAnimation = function () {
|
|
241
|
-
var css = document.createElement("style");
|
|
242
|
-
css.appendChild(document.createTextNode("*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}"));
|
|
243
|
-
document.head.appendChild(css);
|
|
244
|
-
return function () {
|
|
245
|
-
// Force restyle
|
|
246
|
-
(function () { return window.getComputedStyle(document.body); })();
|
|
247
|
-
// Wait for next tick before removing
|
|
248
|
-
setTimeout(function () {
|
|
249
|
-
document.head.removeChild(css);
|
|
250
|
-
}, 1);
|
|
251
|
-
};
|
|
252
|
-
};
|
|
253
|
-
var getSystemTheme = function (e) {
|
|
254
|
-
if (!e)
|
|
255
|
-
e = window.matchMedia(MEDIA);
|
|
256
|
-
var isDark = e.matches;
|
|
257
|
-
var systemTheme = isDark ? "dark" : "light";
|
|
258
|
-
return systemTheme;
|
|
259
|
-
};
|
|
260
|
-
var encodeBase64 = function (str) {
|
|
261
|
-
return utils_1.isServer ? Buffer.from(str).toString("base64") : btoa(str);
|
|
262
|
-
};
|
|
263
11
|
exports.default = exports.useTheme;
|
package/package.json
CHANGED
|
@@ -17,31 +17,31 @@
|
|
|
17
17
|
"dependencies": {},
|
|
18
18
|
"peerDependencies": {
|
|
19
19
|
"react": "^16.8 || ^17 || ^18",
|
|
20
|
-
"next": "^12.2.
|
|
21
|
-
"@koine/utils": "1.0.
|
|
22
|
-
"framer-motion": "^6.
|
|
23
|
-
"
|
|
20
|
+
"next": "^12.2.3",
|
|
21
|
+
"@koine/utils": "1.0.62",
|
|
22
|
+
"framer-motion": "^6.5.1",
|
|
23
|
+
"next-auth": "^4.10.2",
|
|
24
|
+
"@mui/material": "^5.9.1",
|
|
25
|
+
"@emotion/react": "^11.9.3",
|
|
24
26
|
"styled-components": "^5.3.5",
|
|
25
|
-
"
|
|
27
|
+
"react-hook-form": "^7.33.1",
|
|
28
|
+
"@koine/react": "1.0.62",
|
|
29
|
+
"@mui/base": "^5.0.0-alpha.90",
|
|
26
30
|
"react-icons": "^4.4.0",
|
|
27
|
-
"date-fns": "^2.
|
|
31
|
+
"date-fns": "^2.29.1",
|
|
28
32
|
"react-swipeable": "^7.0.0",
|
|
29
33
|
"@tiptap/react": "^2.0.0-beta.114",
|
|
30
|
-
"@tiptap/starter-kit": "^2.0.0-beta.
|
|
34
|
+
"@tiptap/starter-kit": "^2.0.0-beta.191",
|
|
31
35
|
"@kuus/yup": "^1.0.0-beta.4",
|
|
32
|
-
"
|
|
33
|
-
"type-fest": "^2.16.0",
|
|
36
|
+
"type-fest": "^2.17.0",
|
|
34
37
|
"react-popper": "^2.3.0",
|
|
35
38
|
"tslib": "^2.4.0",
|
|
36
|
-
"next-auth": "^4.9.0",
|
|
37
|
-
"@mui/material": "^5.8.7",
|
|
38
|
-
"@emotion/react": "^11.9.3",
|
|
39
39
|
"@emotion/server": "^11.4.0",
|
|
40
40
|
"next-translate": "^1.5.0",
|
|
41
|
-
"next-seo": "^5.
|
|
42
|
-
"@hookform/resolvers": "^2.9.
|
|
41
|
+
"next-seo": "^5.5.0",
|
|
42
|
+
"@hookform/resolvers": "^2.9.6"
|
|
43
43
|
},
|
|
44
|
-
"version": "1.0.
|
|
44
|
+
"version": "1.0.62",
|
|
45
45
|
"module": "./index.js",
|
|
46
46
|
"types": "./index.d.ts"
|
|
47
47
|
}
|
package/useTheme.d.ts
CHANGED
|
@@ -1,47 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
declare type
|
|
3
|
-
[themeName: string]: string;
|
|
4
|
-
};
|
|
5
|
-
export declare type UseThemeProps = {
|
|
6
|
-
/** List of all available theme names */
|
|
7
|
-
themes: string[];
|
|
8
|
-
/** Forced theme name for the current page */
|
|
9
|
-
forcedTheme?: string;
|
|
10
|
-
/** Update the theme */
|
|
11
|
-
setTheme: (theme: string) => void;
|
|
12
|
-
/** Active theme name */
|
|
13
|
-
theme?: string;
|
|
14
|
-
/** If `enableSystem` is true and the active theme is "system", this returns whether the system preference resolved to "dark" or "light". Otherwise, identical to `theme` */
|
|
15
|
-
resolvedTheme?: string;
|
|
16
|
-
/** If enableSystem is true, returns the System theme preference ("dark" or "light"), regardless what the active theme is */
|
|
17
|
-
systemTheme?: "dark" | "light";
|
|
18
|
-
};
|
|
19
|
-
export declare type ThemeProviderProps = React.PropsWithChildren<{
|
|
20
|
-
/** List of all available theme names */
|
|
21
|
-
themes?: string[];
|
|
22
|
-
/** Forced theme name for the current page */
|
|
23
|
-
forcedTheme?: string;
|
|
24
|
-
/** Whether to switch between dark and light themes based on prefers-color-scheme */
|
|
25
|
-
enableSystem?: boolean;
|
|
26
|
-
/** Disable all CSS transitions when switching themes */
|
|
27
|
-
disableTransitionOnChange?: boolean;
|
|
28
|
-
/** Whether to indicate to browsers which color scheme is used (dark or light) for built-in UI like inputs and buttons */
|
|
29
|
-
enableColorScheme?: boolean;
|
|
30
|
-
/** Default theme name (for v0.0.12 and lower the default was light). If `enableSystem` is false, the default theme is light */
|
|
31
|
-
defaultTheme?: string;
|
|
32
|
-
/** HTML attribute modified based on the active theme. Accepts `class` and `data-*` (meaning any data attribute, `data-mode`, `data-color`, etc.) */
|
|
33
|
-
attribute?: string | "class";
|
|
34
|
-
/** Mapping of theme name to HTML attribute value. Object where key is the theme name and value is the attribute value */
|
|
35
|
-
value?: ValueObject;
|
|
36
|
-
/** Nonce string to pass to the inline script for CSP headers */
|
|
37
|
-
nonce?: string;
|
|
38
|
-
}>;
|
|
1
|
+
import { ThemeContextProps } from "./ThemeContext";
|
|
2
|
+
export declare type UseThemeProps = ThemeContextProps;
|
|
39
3
|
/**
|
|
40
4
|
* @borrows [next-themes](https://github.com/pacocoursey/next-themes)
|
|
41
5
|
*/
|
|
42
|
-
export declare const useTheme: () =>
|
|
43
|
-
/**
|
|
44
|
-
* @borrows [next-themes](https://github.com/pacocoursey/next-themes)
|
|
45
|
-
*/
|
|
46
|
-
export declare const ThemeProvider: ({ forcedTheme, disableTransitionOnChange, enableSystem, enableColorScheme, themes, defaultTheme, attribute, value, children, nonce, }: ThemeProviderProps) => JSX.Element;
|
|
6
|
+
export declare const useTheme: () => ThemeContextProps;
|
|
47
7
|
export default useTheme;
|