@koine/next 1.0.59 → 1.0.60
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 +3 -3
- package/useTheme.d.ts +3 -43
- package/useTheme.js +2 -253
package/useTheme.js
CHANGED
|
@@ -1,258 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
/**
|
|
4
|
-
* @file
|
|
5
|
-
*
|
|
6
|
-
* Adapted from [next-themes](https://github.com/pacocoursey/next-themes)
|
|
7
|
-
*
|
|
8
|
-
* Differences:
|
|
9
|
-
*
|
|
10
|
-
* - enableColorScheme: `false` by default (instead of `true`), this plays more
|
|
11
|
-
* nicely with tailwind `dark` class mode as dark theme is supposed to be only
|
|
12
|
-
* controlled by tailwind modifiers
|
|
13
|
-
*/
|
|
14
|
-
import { createContext, useCallback, useContext, useEffect, useState, memo, } from "react";
|
|
15
|
-
import NextScript from "next/script";
|
|
16
|
-
import { isServer } from "@koine/utils";
|
|
17
|
-
var THEME_STORAGE_KEY = "theme";
|
|
18
|
-
var colorSchemes = ["light", "dark"];
|
|
19
|
-
var MEDIA = "(prefers-color-scheme: dark)";
|
|
20
|
-
var ThemeContext = createContext({
|
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
22
|
-
setTheme: function (_) { },
|
|
23
|
-
themes: [],
|
|
24
|
-
});
|
|
1
|
+
import { useContext } from "react";
|
|
2
|
+
import { ThemeContext } from "./ThemeContext";
|
|
25
3
|
/**
|
|
26
4
|
* @borrows [next-themes](https://github.com/pacocoursey/next-themes)
|
|
27
5
|
*/
|
|
28
6
|
export var useTheme = function () { return useContext(ThemeContext); };
|
|
29
|
-
/**
|
|
30
|
-
* @borrows [next-themes](https://github.com/pacocoursey/next-themes)
|
|
31
|
-
*/
|
|
32
|
-
export var ThemeProvider = function (_a) {
|
|
33
|
-
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;
|
|
34
|
-
var _g = useState(function () {
|
|
35
|
-
return getTheme(THEME_STORAGE_KEY, defaultTheme);
|
|
36
|
-
}), theme = _g[0], setThemeState = _g[1];
|
|
37
|
-
var _h = useState(function () {
|
|
38
|
-
return getTheme(THEME_STORAGE_KEY);
|
|
39
|
-
}), resolvedTheme = _h[0], setResolvedTheme = _h[1];
|
|
40
|
-
var attrs = !value ? themes : Object.values(value);
|
|
41
|
-
var applyTheme = useCallback(function (theme) {
|
|
42
|
-
var _a;
|
|
43
|
-
var resolved = theme;
|
|
44
|
-
if (isServer || !resolved)
|
|
45
|
-
return;
|
|
46
|
-
// If theme is system, resolve it before setting theme
|
|
47
|
-
if (theme === "system" && enableSystem) {
|
|
48
|
-
resolved = getSystemTheme();
|
|
49
|
-
}
|
|
50
|
-
var name = value ? value[resolved] : resolved;
|
|
51
|
-
var enable = disableTransitionOnChange ? disableAnimation() : null;
|
|
52
|
-
var d = document.documentElement;
|
|
53
|
-
if (attribute === "class") {
|
|
54
|
-
(_a = d.classList).remove.apply(_a, attrs);
|
|
55
|
-
if (name)
|
|
56
|
-
d.classList.add(name);
|
|
57
|
-
}
|
|
58
|
-
else {
|
|
59
|
-
if (name) {
|
|
60
|
-
d.setAttribute(attribute, name);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
d.removeAttribute(attribute);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
if (enableColorScheme) {
|
|
67
|
-
var fallback = colorSchemes.includes(defaultTheme)
|
|
68
|
-
? defaultTheme
|
|
69
|
-
: "";
|
|
70
|
-
var colorScheme = colorSchemes.includes(resolved)
|
|
71
|
-
? resolved
|
|
72
|
-
: fallback;
|
|
73
|
-
d.style.colorScheme = colorScheme;
|
|
74
|
-
}
|
|
75
|
-
enable === null || enable === void 0 ? void 0 : enable();
|
|
76
|
-
}, [
|
|
77
|
-
attribute,
|
|
78
|
-
attrs,
|
|
79
|
-
defaultTheme,
|
|
80
|
-
disableTransitionOnChange,
|
|
81
|
-
enableColorScheme,
|
|
82
|
-
enableSystem,
|
|
83
|
-
value,
|
|
84
|
-
]);
|
|
85
|
-
var setTheme = useCallback(function (theme) {
|
|
86
|
-
setThemeState(theme);
|
|
87
|
-
// Save to storage
|
|
88
|
-
try {
|
|
89
|
-
localStorage.setItem(THEME_STORAGE_KEY, theme);
|
|
90
|
-
}
|
|
91
|
-
catch (e) {
|
|
92
|
-
// Unsupported
|
|
93
|
-
}
|
|
94
|
-
}, []);
|
|
95
|
-
var handleMediaQuery = useCallback(function (e) {
|
|
96
|
-
var resolved = getSystemTheme(e);
|
|
97
|
-
setResolvedTheme(resolved);
|
|
98
|
-
if (theme === "system" && enableSystem && !forcedTheme) {
|
|
99
|
-
applyTheme("system");
|
|
100
|
-
}
|
|
101
|
-
}, [theme, enableSystem, forcedTheme, applyTheme]);
|
|
102
|
-
// Always listen to System preference
|
|
103
|
-
useEffect(function () {
|
|
104
|
-
var media = window.matchMedia(MEDIA);
|
|
105
|
-
// Intentionally use deprecated listener methods to support iOS & old browsers
|
|
106
|
-
media.addListener(handleMediaQuery);
|
|
107
|
-
handleMediaQuery(media);
|
|
108
|
-
return function () { return media.removeListener(handleMediaQuery); };
|
|
109
|
-
}, [handleMediaQuery]);
|
|
110
|
-
// localStorage event handling
|
|
111
|
-
useEffect(function () {
|
|
112
|
-
var handleStorage = function (e) {
|
|
113
|
-
if (e.key !== THEME_STORAGE_KEY) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
// If default theme set, use it if localstorage === null (happens on local storage manual deletion)
|
|
117
|
-
var theme = e.newValue || defaultTheme;
|
|
118
|
-
setTheme(theme);
|
|
119
|
-
};
|
|
120
|
-
window.addEventListener("storage", handleStorage);
|
|
121
|
-
return function () { return window.removeEventListener("storage", handleStorage); };
|
|
122
|
-
}, [defaultTheme, setTheme]);
|
|
123
|
-
// Whenever theme or forcedTheme changes, apply it
|
|
124
|
-
useEffect(function () {
|
|
125
|
-
applyTheme(forcedTheme !== null && forcedTheme !== void 0 ? forcedTheme : theme);
|
|
126
|
-
}, [applyTheme, forcedTheme, theme]);
|
|
127
|
-
return (_jsxs(ThemeContext.Provider, __assign({ value: {
|
|
128
|
-
theme: theme,
|
|
129
|
-
setTheme: setTheme,
|
|
130
|
-
forcedTheme: forcedTheme,
|
|
131
|
-
resolvedTheme: theme === "system" ? resolvedTheme : theme,
|
|
132
|
-
themes: enableSystem ? __spreadArray(__spreadArray([], themes, true), ["system"], false) : themes,
|
|
133
|
-
systemTheme: (enableSystem ? resolvedTheme : undefined),
|
|
134
|
-
} }, { children: [_jsx(ThemeScript, __assign({}, {
|
|
135
|
-
forcedTheme: forcedTheme,
|
|
136
|
-
disableTransitionOnChange: disableTransitionOnChange,
|
|
137
|
-
enableSystem: enableSystem,
|
|
138
|
-
enableColorScheme: enableColorScheme,
|
|
139
|
-
themes: themes,
|
|
140
|
-
defaultTheme: defaultTheme,
|
|
141
|
-
attribute: attribute,
|
|
142
|
-
value: value,
|
|
143
|
-
children: children,
|
|
144
|
-
attrs: attrs,
|
|
145
|
-
nonce: nonce,
|
|
146
|
-
})), children] })));
|
|
147
|
-
};
|
|
148
|
-
var ThemeScript = memo(function (_a) {
|
|
149
|
-
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;
|
|
150
|
-
var defaultSystem = defaultTheme === "system";
|
|
151
|
-
// Code-golfing the amount of characters in the script
|
|
152
|
-
var optimization = (function () {
|
|
153
|
-
var removeClasses = "d.remove(".concat(attrs
|
|
154
|
-
.map(function (t) { return "'".concat(t, "'"); })
|
|
155
|
-
.join(","), ")");
|
|
156
|
-
return "var d=document.documentElement.classList;".concat(removeClasses, ";");
|
|
157
|
-
})();
|
|
158
|
-
var fallbackColorScheme = (function () {
|
|
159
|
-
if (!enableColorScheme) {
|
|
160
|
-
return "";
|
|
161
|
-
}
|
|
162
|
-
var fallback = colorSchemes.includes(defaultTheme)
|
|
163
|
-
? defaultTheme
|
|
164
|
-
: null;
|
|
165
|
-
if (fallback) {
|
|
166
|
-
return "if(e==='light'||e==='dark'||!e)d.style.colorScheme=e||'".concat(defaultTheme, "'");
|
|
167
|
-
}
|
|
168
|
-
else {
|
|
169
|
-
return "if(e==='light'||e==='dark')d.style.colorScheme=e";
|
|
170
|
-
}
|
|
171
|
-
})();
|
|
172
|
-
var updateDOM = function (name, literal, setColorScheme) {
|
|
173
|
-
if (literal === void 0) { literal = false; }
|
|
174
|
-
if (setColorScheme === void 0) { setColorScheme = true; }
|
|
175
|
-
var resolvedName = value ? value[name] : name;
|
|
176
|
-
var val = literal ? name + "|| ''" : "'".concat(resolvedName, "'");
|
|
177
|
-
var text = "";
|
|
178
|
-
// MUCH faster to set colorScheme alongside HTML attribute/class
|
|
179
|
-
// as it only incurs 1 style recalculation rather than 2
|
|
180
|
-
// This can save over 250ms of work for pages with big DOM
|
|
181
|
-
if (enableColorScheme &&
|
|
182
|
-
setColorScheme &&
|
|
183
|
-
!literal &&
|
|
184
|
-
colorSchemes.includes(name)) {
|
|
185
|
-
text += "d.style.colorScheme = '".concat(name, "';");
|
|
186
|
-
}
|
|
187
|
-
if (attribute === "class") {
|
|
188
|
-
if (literal || resolvedName) {
|
|
189
|
-
text += "d.add(".concat(val, ")");
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
text += "null";
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
if (resolvedName) {
|
|
197
|
-
text += "d[s](n, ".concat(val, ")");
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return text;
|
|
201
|
-
};
|
|
202
|
-
var scriptSrc = (function () {
|
|
203
|
-
if (forcedTheme) {
|
|
204
|
-
return "!function(){".concat(optimization).concat(updateDOM(forcedTheme), "}()");
|
|
205
|
-
}
|
|
206
|
-
if (enableSystem) {
|
|
207
|
-
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
|
|
208
|
-
? "else{" + updateDOM(defaultTheme, false, false) + "}"
|
|
209
|
-
: "").concat(fallbackColorScheme, "}catch(e){}}()");
|
|
210
|
-
}
|
|
211
|
-
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){}}();");
|
|
212
|
-
})();
|
|
213
|
-
// We MUST use next/script's `beforeInteractive` strategy to avoid flashing on load.
|
|
214
|
-
// However, it only accepts the `src` prop, not `dangerouslySetInnerHTML` or `children`
|
|
215
|
-
// But our script cannot be external because it changes at runtime based on React props
|
|
216
|
-
// so we trick next/script by passing `src` as a base64 JS script
|
|
217
|
-
var encodedScript = "data:text/javascript;base64,".concat(encodeBase64(scriptSrc));
|
|
218
|
-
return (_jsx(NextScript, { id: "next-theme-script", strategy: "beforeInteractive", src: encodedScript, nonce: nonce }));
|
|
219
|
-
},
|
|
220
|
-
// Never re-render this component
|
|
221
|
-
function () { return true; });
|
|
222
|
-
// Helpers
|
|
223
|
-
var getTheme = function (key, fallback) {
|
|
224
|
-
if (isServer)
|
|
225
|
-
return undefined;
|
|
226
|
-
var theme;
|
|
227
|
-
try {
|
|
228
|
-
theme = localStorage.getItem(key) || undefined;
|
|
229
|
-
}
|
|
230
|
-
catch (e) {
|
|
231
|
-
// Unsupported
|
|
232
|
-
}
|
|
233
|
-
return theme || fallback;
|
|
234
|
-
};
|
|
235
|
-
var disableAnimation = function () {
|
|
236
|
-
var css = document.createElement("style");
|
|
237
|
-
css.appendChild(document.createTextNode("*{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}"));
|
|
238
|
-
document.head.appendChild(css);
|
|
239
|
-
return function () {
|
|
240
|
-
// Force restyle
|
|
241
|
-
(function () { return window.getComputedStyle(document.body); })();
|
|
242
|
-
// Wait for next tick before removing
|
|
243
|
-
setTimeout(function () {
|
|
244
|
-
document.head.removeChild(css);
|
|
245
|
-
}, 1);
|
|
246
|
-
};
|
|
247
|
-
};
|
|
248
|
-
var getSystemTheme = function (e) {
|
|
249
|
-
if (!e)
|
|
250
|
-
e = window.matchMedia(MEDIA);
|
|
251
|
-
var isDark = e.matches;
|
|
252
|
-
var systemTheme = isDark ? "dark" : "light";
|
|
253
|
-
return systemTheme;
|
|
254
|
-
};
|
|
255
|
-
var encodeBase64 = function (str) {
|
|
256
|
-
return isServer ? Buffer.from(str).toString("base64") : btoa(str);
|
|
257
|
-
};
|
|
258
7
|
export default useTheme;
|