@dheme/react 2.0.0
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/README.md +393 -0
- package/dist/index.d.mts +68 -0
- package/dist/index.d.ts +68 -0
- package/dist/index.js +476 -0
- package/dist/index.mjs +425 -0
- package/package.json +63 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,476 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
DhemeProvider: () => DhemeProvider,
|
|
34
|
+
DhemeScript: () => DhemeScript,
|
|
35
|
+
ThemeActionsContext: () => ThemeActionsContext,
|
|
36
|
+
ThemeDataContext: () => ThemeDataContext,
|
|
37
|
+
applyThemeCSSVariables: () => applyThemeCSSVariables,
|
|
38
|
+
buildCacheKey: () => buildCacheKey,
|
|
39
|
+
getBlockingScriptPayload: () => getBlockingScriptPayload,
|
|
40
|
+
getNextBlockingScriptPayload: () => getNextBlockingScriptPayload,
|
|
41
|
+
removeThemeCSSVariables: () => removeThemeCSSVariables,
|
|
42
|
+
themeToCSS: () => themeToCSS,
|
|
43
|
+
themeToCSSBothModes: () => themeToCSSBothModes,
|
|
44
|
+
useDhemeClient: () => useDhemeClient,
|
|
45
|
+
useGenerateTheme: () => useGenerateTheme,
|
|
46
|
+
useTheme: () => useTheme,
|
|
47
|
+
useThemeActions: () => useThemeActions
|
|
48
|
+
});
|
|
49
|
+
module.exports = __toCommonJS(index_exports);
|
|
50
|
+
|
|
51
|
+
// src/components/DhemeProvider.tsx
|
|
52
|
+
var import_react3 = __toESM(require("react"));
|
|
53
|
+
var import_sdk = require("@dheme/sdk");
|
|
54
|
+
|
|
55
|
+
// src/contexts/ThemeDataContext.ts
|
|
56
|
+
var import_react = require("react");
|
|
57
|
+
var ThemeDataContext = (0, import_react.createContext)(null);
|
|
58
|
+
|
|
59
|
+
// src/contexts/ThemeActionsContext.ts
|
|
60
|
+
var import_react2 = require("react");
|
|
61
|
+
var ThemeActionsContext = (0, import_react2.createContext)(null);
|
|
62
|
+
|
|
63
|
+
// src/constants.ts
|
|
64
|
+
var STORAGE_KEY_PREFIX = "dheme-cache";
|
|
65
|
+
var STORAGE_PARAMS_KEY = "dheme-params";
|
|
66
|
+
var STORAGE_MODE_KEY = "dheme-mode";
|
|
67
|
+
var CSS_TOKEN_KEYS = [
|
|
68
|
+
"background",
|
|
69
|
+
"foreground",
|
|
70
|
+
"card",
|
|
71
|
+
"cardForeground",
|
|
72
|
+
"popover",
|
|
73
|
+
"popoverForeground",
|
|
74
|
+
"primary",
|
|
75
|
+
"primaryForeground",
|
|
76
|
+
"secondary",
|
|
77
|
+
"secondaryForeground",
|
|
78
|
+
"muted",
|
|
79
|
+
"mutedForeground",
|
|
80
|
+
"accent",
|
|
81
|
+
"accentForeground",
|
|
82
|
+
"destructive",
|
|
83
|
+
"destructiveForeground",
|
|
84
|
+
"border",
|
|
85
|
+
"input",
|
|
86
|
+
"ring"
|
|
87
|
+
];
|
|
88
|
+
var TOKEN_TO_CSS_VAR = {
|
|
89
|
+
background: "--background",
|
|
90
|
+
foreground: "--foreground",
|
|
91
|
+
card: "--card",
|
|
92
|
+
cardForeground: "--card-foreground",
|
|
93
|
+
popover: "--popover",
|
|
94
|
+
popoverForeground: "--popover-foreground",
|
|
95
|
+
primary: "--primary",
|
|
96
|
+
primaryForeground: "--primary-foreground",
|
|
97
|
+
secondary: "--secondary",
|
|
98
|
+
secondaryForeground: "--secondary-foreground",
|
|
99
|
+
muted: "--muted",
|
|
100
|
+
mutedForeground: "--muted-foreground",
|
|
101
|
+
accent: "--accent",
|
|
102
|
+
accentForeground: "--accent-foreground",
|
|
103
|
+
destructive: "--destructive",
|
|
104
|
+
destructiveForeground: "--destructive-foreground",
|
|
105
|
+
border: "--border",
|
|
106
|
+
input: "--input",
|
|
107
|
+
ring: "--ring"
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
// src/utils/cssVariables.ts
|
|
111
|
+
function formatHSL(color) {
|
|
112
|
+
return `${color.h} ${color.s}% ${color.l}%`;
|
|
113
|
+
}
|
|
114
|
+
function themeToCSS(theme, mode) {
|
|
115
|
+
const colors = theme.colors[mode];
|
|
116
|
+
if (!colors) return "";
|
|
117
|
+
const parts = [];
|
|
118
|
+
for (const key of CSS_TOKEN_KEYS) {
|
|
119
|
+
const color = colors[key];
|
|
120
|
+
if (color) {
|
|
121
|
+
parts.push(`${TOKEN_TO_CSS_VAR[key]}:${formatHSL(color)}`);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
if (theme.radius != null) {
|
|
125
|
+
parts.push(`--radius:${theme.radius}rem`);
|
|
126
|
+
}
|
|
127
|
+
return parts.join(";");
|
|
128
|
+
}
|
|
129
|
+
function themeToCSSBothModes(theme) {
|
|
130
|
+
const lightCSS = themeToCSS(theme, "light");
|
|
131
|
+
const darkCSS = themeToCSS(theme, "dark");
|
|
132
|
+
return `:root{${lightCSS}}.dark{${darkCSS}}`;
|
|
133
|
+
}
|
|
134
|
+
function applyThemeCSSVariables(theme, mode) {
|
|
135
|
+
if (typeof document === "undefined") return;
|
|
136
|
+
const colors = theme.colors[mode];
|
|
137
|
+
if (!colors) return;
|
|
138
|
+
const style = document.documentElement.style;
|
|
139
|
+
for (const key of CSS_TOKEN_KEYS) {
|
|
140
|
+
const color = colors[key];
|
|
141
|
+
if (color) {
|
|
142
|
+
style.setProperty(TOKEN_TO_CSS_VAR[key], formatHSL(color));
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (theme.radius != null) {
|
|
146
|
+
style.setProperty("--radius", `${theme.radius}rem`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function removeThemeCSSVariables() {
|
|
150
|
+
if (typeof document === "undefined") return;
|
|
151
|
+
const style = document.documentElement.style;
|
|
152
|
+
for (const key of CSS_TOKEN_KEYS) {
|
|
153
|
+
style.removeProperty(TOKEN_TO_CSS_VAR[key]);
|
|
154
|
+
}
|
|
155
|
+
style.removeProperty("--radius");
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// src/utils/cacheKey.ts
|
|
159
|
+
function buildCacheKey(params) {
|
|
160
|
+
const normalized = {
|
|
161
|
+
t: params.theme.toLowerCase().replace("#", ""),
|
|
162
|
+
s: (params.secondaryColor || "").toLowerCase().replace("#", ""),
|
|
163
|
+
r: params.radius ?? 0.5,
|
|
164
|
+
sa: params.saturationAdjust ?? 0,
|
|
165
|
+
la: params.lightnessAdjust ?? 0,
|
|
166
|
+
ca: params.contrastAdjust ?? 0,
|
|
167
|
+
ci: params.cardIsColored ?? false,
|
|
168
|
+
bi: params.backgroundIsColored ?? true
|
|
169
|
+
};
|
|
170
|
+
return JSON.stringify(normalized);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
// src/utils/storage.ts
|
|
174
|
+
var memoryCache = /* @__PURE__ */ new Map();
|
|
175
|
+
function getItem(key) {
|
|
176
|
+
if (memoryCache.has(key)) return memoryCache.get(key);
|
|
177
|
+
try {
|
|
178
|
+
if (typeof window === "undefined") return null;
|
|
179
|
+
const value = localStorage.getItem(key);
|
|
180
|
+
if (value !== null) memoryCache.set(key, value);
|
|
181
|
+
return value;
|
|
182
|
+
} catch {
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
function setItem(key, value) {
|
|
187
|
+
memoryCache.set(key, value);
|
|
188
|
+
try {
|
|
189
|
+
if (typeof window !== "undefined") {
|
|
190
|
+
localStorage.setItem(key, value);
|
|
191
|
+
}
|
|
192
|
+
} catch {
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function removeItem(key) {
|
|
196
|
+
memoryCache.delete(key);
|
|
197
|
+
try {
|
|
198
|
+
if (typeof window !== "undefined") {
|
|
199
|
+
localStorage.removeItem(key);
|
|
200
|
+
}
|
|
201
|
+
} catch {
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function saveThemeToCache(cacheKey, theme) {
|
|
205
|
+
setItem(`${STORAGE_KEY_PREFIX}:${cacheKey}`, JSON.stringify(theme));
|
|
206
|
+
}
|
|
207
|
+
function loadThemeFromCache(cacheKey) {
|
|
208
|
+
const raw = getItem(`${STORAGE_KEY_PREFIX}:${cacheKey}`);
|
|
209
|
+
if (!raw) return null;
|
|
210
|
+
try {
|
|
211
|
+
return JSON.parse(raw);
|
|
212
|
+
} catch {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
function saveCurrentParams(params) {
|
|
217
|
+
setItem(STORAGE_PARAMS_KEY, JSON.stringify(params));
|
|
218
|
+
}
|
|
219
|
+
function saveMode(mode) {
|
|
220
|
+
setItem(STORAGE_MODE_KEY, mode);
|
|
221
|
+
}
|
|
222
|
+
function loadMode() {
|
|
223
|
+
const raw = getItem(STORAGE_MODE_KEY);
|
|
224
|
+
if (raw === "light" || raw === "dark") return raw;
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
function clearAll() {
|
|
228
|
+
removeItem(STORAGE_PARAMS_KEY);
|
|
229
|
+
removeItem(STORAGE_MODE_KEY);
|
|
230
|
+
const keysToRemove = [];
|
|
231
|
+
memoryCache.forEach((_, key) => {
|
|
232
|
+
if (key.startsWith(STORAGE_KEY_PREFIX + ":")) {
|
|
233
|
+
keysToRemove.push(key);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
keysToRemove.forEach(removeItem);
|
|
237
|
+
try {
|
|
238
|
+
if (typeof window !== "undefined") {
|
|
239
|
+
const len = localStorage.length;
|
|
240
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
241
|
+
const key = localStorage.key(i);
|
|
242
|
+
if (key && key.startsWith(STORAGE_KEY_PREFIX + ":")) {
|
|
243
|
+
localStorage.removeItem(key);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
} catch {
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// src/components/DhemeProvider.tsx
|
|
252
|
+
function DhemeProvider({
|
|
253
|
+
apiKey,
|
|
254
|
+
theme: primaryColor,
|
|
255
|
+
themeParams,
|
|
256
|
+
defaultMode = "light",
|
|
257
|
+
baseUrl,
|
|
258
|
+
persist = true,
|
|
259
|
+
autoApply = true,
|
|
260
|
+
onThemeChange,
|
|
261
|
+
onModeChange,
|
|
262
|
+
onError,
|
|
263
|
+
children
|
|
264
|
+
}) {
|
|
265
|
+
const client = (0, import_react3.useMemo)(() => new import_sdk.DhemeClient({ apiKey, baseUrl }), [apiKey, baseUrl]);
|
|
266
|
+
const [theme, setTheme] = (0, import_react3.useState)(null);
|
|
267
|
+
const [mode, setModeState] = (0, import_react3.useState)(() => {
|
|
268
|
+
if (typeof window === "undefined") return defaultMode;
|
|
269
|
+
return loadMode() || defaultMode;
|
|
270
|
+
});
|
|
271
|
+
const [isReady, setIsReady] = (0, import_react3.useState)(false);
|
|
272
|
+
const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
|
|
273
|
+
const [error, setError] = (0, import_react3.useState)(null);
|
|
274
|
+
const modeRef = (0, import_react3.useRef)(mode);
|
|
275
|
+
modeRef.current = mode;
|
|
276
|
+
const autoApplyRef = (0, import_react3.useRef)(autoApply);
|
|
277
|
+
autoApplyRef.current = autoApply;
|
|
278
|
+
const persistRef = (0, import_react3.useRef)(persist);
|
|
279
|
+
persistRef.current = persist;
|
|
280
|
+
const onThemeChangeRef = (0, import_react3.useRef)(onThemeChange);
|
|
281
|
+
onThemeChangeRef.current = onThemeChange;
|
|
282
|
+
const onModeChangeRef = (0, import_react3.useRef)(onModeChange);
|
|
283
|
+
onModeChangeRef.current = onModeChange;
|
|
284
|
+
const onErrorRef = (0, import_react3.useRef)(onError);
|
|
285
|
+
onErrorRef.current = onError;
|
|
286
|
+
const abortRef = (0, import_react3.useRef)(null);
|
|
287
|
+
const setMode = (0, import_react3.useCallback)((newMode) => {
|
|
288
|
+
setModeState(newMode);
|
|
289
|
+
if (persistRef.current) saveMode(newMode);
|
|
290
|
+
onModeChangeRef.current?.(newMode);
|
|
291
|
+
}, []);
|
|
292
|
+
(0, import_react3.useEffect)(() => {
|
|
293
|
+
if (theme && autoApply) {
|
|
294
|
+
applyThemeCSSVariables(theme, mode);
|
|
295
|
+
}
|
|
296
|
+
if (typeof document !== "undefined") {
|
|
297
|
+
if (mode === "dark") {
|
|
298
|
+
document.documentElement.classList.add("dark");
|
|
299
|
+
} else {
|
|
300
|
+
document.documentElement.classList.remove("dark");
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}, [theme, mode, autoApply]);
|
|
304
|
+
const generateTheme = (0, import_react3.useCallback)(
|
|
305
|
+
async (params) => {
|
|
306
|
+
abortRef.current?.abort();
|
|
307
|
+
setIsLoading(true);
|
|
308
|
+
setError(null);
|
|
309
|
+
try {
|
|
310
|
+
const response = await client.generateTheme(params);
|
|
311
|
+
const data = response.data;
|
|
312
|
+
setTheme(data);
|
|
313
|
+
setIsReady(true);
|
|
314
|
+
if (autoApplyRef.current) {
|
|
315
|
+
applyThemeCSSVariables(data, modeRef.current);
|
|
316
|
+
}
|
|
317
|
+
if (persistRef.current) {
|
|
318
|
+
const key = buildCacheKey(params);
|
|
319
|
+
saveThemeToCache(key, data);
|
|
320
|
+
saveCurrentParams(params);
|
|
321
|
+
}
|
|
322
|
+
onThemeChangeRef.current?.(data);
|
|
323
|
+
} catch (err) {
|
|
324
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
325
|
+
setError(error2);
|
|
326
|
+
onErrorRef.current?.(error2);
|
|
327
|
+
} finally {
|
|
328
|
+
setIsLoading(false);
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
[client]
|
|
332
|
+
);
|
|
333
|
+
const clearTheme = (0, import_react3.useCallback)(() => {
|
|
334
|
+
setTheme(null);
|
|
335
|
+
setIsReady(false);
|
|
336
|
+
setError(null);
|
|
337
|
+
if (autoApplyRef.current) removeThemeCSSVariables();
|
|
338
|
+
if (persistRef.current) clearAll();
|
|
339
|
+
}, []);
|
|
340
|
+
(0, import_react3.useEffect)(() => {
|
|
341
|
+
if (!primaryColor) return;
|
|
342
|
+
const params = {
|
|
343
|
+
theme: primaryColor,
|
|
344
|
+
...themeParams
|
|
345
|
+
};
|
|
346
|
+
const cacheKey = buildCacheKey(params);
|
|
347
|
+
const cached = persist ? loadThemeFromCache(cacheKey) : null;
|
|
348
|
+
if (cached) {
|
|
349
|
+
setTheme(cached);
|
|
350
|
+
setIsReady(true);
|
|
351
|
+
if (autoApply) applyThemeCSSVariables(cached, mode);
|
|
352
|
+
const controller = new AbortController();
|
|
353
|
+
abortRef.current = controller;
|
|
354
|
+
client.generateTheme(params).then((response) => {
|
|
355
|
+
if (controller.signal.aborted) return;
|
|
356
|
+
const data = response.data;
|
|
357
|
+
const cachedLight = JSON.stringify(cached.colors.light);
|
|
358
|
+
const freshLight = JSON.stringify(data.colors.light);
|
|
359
|
+
if (cachedLight !== freshLight) {
|
|
360
|
+
setTheme(data);
|
|
361
|
+
if (autoApply) applyThemeCSSVariables(data, modeRef.current);
|
|
362
|
+
saveThemeToCache(cacheKey, data);
|
|
363
|
+
onThemeChangeRef.current?.(data);
|
|
364
|
+
}
|
|
365
|
+
}).catch(() => {
|
|
366
|
+
});
|
|
367
|
+
} else {
|
|
368
|
+
generateTheme(params);
|
|
369
|
+
}
|
|
370
|
+
return () => {
|
|
371
|
+
abortRef.current?.abort();
|
|
372
|
+
};
|
|
373
|
+
}, []);
|
|
374
|
+
const themeDataValue = (0, import_react3.useMemo)(() => ({ theme, mode, isReady }), [theme, mode, isReady]);
|
|
375
|
+
const themeActionsValue = (0, import_react3.useMemo)(
|
|
376
|
+
() => ({ generateTheme, setMode, clearTheme, isLoading, error, client }),
|
|
377
|
+
[generateTheme, setMode, clearTheme, isLoading, error, client]
|
|
378
|
+
);
|
|
379
|
+
return import_react3.default.createElement(
|
|
380
|
+
ThemeDataContext.Provider,
|
|
381
|
+
{ value: themeDataValue },
|
|
382
|
+
import_react3.default.createElement(ThemeActionsContext.Provider, { value: themeActionsValue }, children)
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// src/components/DhemeScript.tsx
|
|
387
|
+
var import_react4 = __toESM(require("react"));
|
|
388
|
+
|
|
389
|
+
// src/utils/scriptPayload.ts
|
|
390
|
+
function getBlockingScriptPayload(defaultMode) {
|
|
391
|
+
return `(function(){try{var p=localStorage.getItem('dheme-params');if(!p)return;var params=JSON.parse(p);var n={t:(params.theme||'').toLowerCase().replace('#',''),s:(params.secondaryColor||'').toLowerCase().replace('#',''),r:params.radius!=null?params.radius:0.5,sa:params.saturationAdjust||0,la:params.lightnessAdjust||0,ca:params.contrastAdjust||0,ci:!!params.cardIsColored,bi:params.backgroundIsColored!=null?params.backgroundIsColored:true};var key='dheme-cache:'+JSON.stringify(n);var c=localStorage.getItem(key);if(!c)return;var theme=JSON.parse(c);var mode=localStorage.getItem('dheme-mode')||'${defaultMode}';var colors=theme.colors[mode];if(!colors)return;var d=document.documentElement.style;var m={background:'--background',foreground:'--foreground',card:'--card',cardForeground:'--card-foreground',popover:'--popover',popoverForeground:'--popover-foreground',primary:'--primary',primaryForeground:'--primary-foreground',secondary:'--secondary',secondaryForeground:'--secondary-foreground',muted:'--muted',mutedForeground:'--muted-foreground',accent:'--accent',accentForeground:'--accent-foreground',destructive:'--destructive',destructiveForeground:'--destructive-foreground',border:'--border',input:'--input',ring:'--ring'};for(var k in m){if(colors[k]){var v=colors[k];d.setProperty(m[k],v.h+' '+v.s+'% '+v.l+'%')}}if(theme.radius!=null)d.setProperty('--radius',theme.radius+'rem');if(mode==='dark')document.documentElement.classList.add('dark');else document.documentElement.classList.remove('dark')}catch(e){}})()`;
|
|
392
|
+
}
|
|
393
|
+
function getNextBlockingScriptPayload(defaultMode) {
|
|
394
|
+
return `(function(){try{var m=document.cookie.match(/dheme-mode=(\\w+)/);var mode=m?m[1]:null;if(!mode){mode=window.matchMedia('(prefers-color-scheme: dark)').matches?'dark':'${defaultMode}'}if(mode==='dark'){document.documentElement.classList.add('dark')}else{document.documentElement.classList.remove('dark')}}catch(e){}})()`;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// src/components/DhemeScript.tsx
|
|
398
|
+
function DhemeScript({
|
|
399
|
+
defaultMode = "light",
|
|
400
|
+
nonce
|
|
401
|
+
}) {
|
|
402
|
+
const scriptContent = getBlockingScriptPayload(defaultMode);
|
|
403
|
+
return import_react4.default.createElement("script", {
|
|
404
|
+
nonce,
|
|
405
|
+
dangerouslySetInnerHTML: { __html: scriptContent }
|
|
406
|
+
});
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
// src/hooks/useTheme.ts
|
|
410
|
+
var import_react5 = require("react");
|
|
411
|
+
function useTheme() {
|
|
412
|
+
const context = (0, import_react5.useContext)(ThemeDataContext);
|
|
413
|
+
if (!context) {
|
|
414
|
+
throw new Error("useTheme must be used within a <DhemeProvider>");
|
|
415
|
+
}
|
|
416
|
+
return context;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// src/hooks/useThemeActions.ts
|
|
420
|
+
var import_react6 = require("react");
|
|
421
|
+
function useThemeActions() {
|
|
422
|
+
const context = (0, import_react6.useContext)(ThemeActionsContext);
|
|
423
|
+
if (!context) {
|
|
424
|
+
throw new Error("useThemeActions must be used within a <DhemeProvider>");
|
|
425
|
+
}
|
|
426
|
+
return context;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// src/hooks/useGenerateTheme.ts
|
|
430
|
+
var import_react7 = require("react");
|
|
431
|
+
function useGenerateTheme() {
|
|
432
|
+
const { generateTheme: generate } = useThemeActions();
|
|
433
|
+
const [isGenerating, setIsGenerating] = (0, import_react7.useState)(false);
|
|
434
|
+
const [error, setError] = (0, import_react7.useState)(null);
|
|
435
|
+
const generateTheme = (0, import_react7.useCallback)(
|
|
436
|
+
async (params) => {
|
|
437
|
+
setIsGenerating(true);
|
|
438
|
+
setError(null);
|
|
439
|
+
try {
|
|
440
|
+
await generate(params);
|
|
441
|
+
} catch (err) {
|
|
442
|
+
const error2 = err instanceof Error ? err : new Error(String(err));
|
|
443
|
+
setError(error2);
|
|
444
|
+
throw error2;
|
|
445
|
+
} finally {
|
|
446
|
+
setIsGenerating(false);
|
|
447
|
+
}
|
|
448
|
+
},
|
|
449
|
+
[generate]
|
|
450
|
+
);
|
|
451
|
+
return { generateTheme, isGenerating, error };
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// src/hooks/useDhemeClient.ts
|
|
455
|
+
function useDhemeClient() {
|
|
456
|
+
const { client } = useThemeActions();
|
|
457
|
+
return client;
|
|
458
|
+
}
|
|
459
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
460
|
+
0 && (module.exports = {
|
|
461
|
+
DhemeProvider,
|
|
462
|
+
DhemeScript,
|
|
463
|
+
ThemeActionsContext,
|
|
464
|
+
ThemeDataContext,
|
|
465
|
+
applyThemeCSSVariables,
|
|
466
|
+
buildCacheKey,
|
|
467
|
+
getBlockingScriptPayload,
|
|
468
|
+
getNextBlockingScriptPayload,
|
|
469
|
+
removeThemeCSSVariables,
|
|
470
|
+
themeToCSS,
|
|
471
|
+
themeToCSSBothModes,
|
|
472
|
+
useDhemeClient,
|
|
473
|
+
useGenerateTheme,
|
|
474
|
+
useTheme,
|
|
475
|
+
useThemeActions
|
|
476
|
+
});
|