stoop 0.3.0 → 0.4.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 +125 -0
- package/dist/api/core-api.d.ts +34 -0
- package/dist/api/{keyframes.js → core-api.js} +45 -5
- package/dist/api/global-css.js +11 -58
- package/dist/api/styled.d.ts +0 -1
- package/dist/api/styled.js +265 -16
- package/dist/api/theme-provider.d.ts +41 -0
- package/dist/api/theme-provider.js +223 -0
- package/dist/constants.d.ts +11 -1
- package/dist/constants.js +11 -1
- package/dist/core/compiler.d.ts +11 -0
- package/dist/core/compiler.js +15 -7
- package/dist/core/theme-manager.d.ts +34 -3
- package/dist/core/theme-manager.js +55 -45
- package/dist/core/variants.js +9 -3
- package/dist/create-stoop-internal.d.ts +30 -0
- package/dist/create-stoop-internal.js +123 -0
- package/dist/create-stoop-ssr.d.ts +10 -0
- package/dist/create-stoop-ssr.js +26 -0
- package/dist/create-stoop.d.ts +32 -1
- package/dist/create-stoop.js +78 -69
- package/dist/inject.d.ts +113 -0
- package/dist/inject.js +308 -0
- package/dist/types/index.d.ts +147 -12
- package/dist/types/react-polymorphic-types.d.ts +13 -2
- package/dist/utils/auto-preload.d.ts +45 -0
- package/dist/utils/auto-preload.js +167 -0
- package/dist/utils/helpers.d.ts +64 -0
- package/dist/utils/helpers.js +150 -0
- package/dist/utils/storage.d.ts +148 -0
- package/dist/utils/storage.js +396 -0
- package/dist/utils/{string.d.ts → theme-utils.d.ts} +20 -3
- package/dist/utils/{string.js → theme-utils.js} +109 -9
- package/dist/utils/theme.d.ts +14 -2
- package/dist/utils/theme.js +41 -16
- package/package.json +48 -23
- package/dist/api/create-theme.d.ts +0 -13
- package/dist/api/create-theme.js +0 -43
- package/dist/api/css.d.ts +0 -16
- package/dist/api/css.js +0 -20
- package/dist/api/keyframes.d.ts +0 -16
- package/dist/api/provider.d.ts +0 -19
- package/dist/api/provider.js +0 -109
- package/dist/api/use-theme.d.ts +0 -13
- package/dist/api/use-theme.js +0 -21
- package/dist/create-stoop-server.d.ts +0 -33
- package/dist/create-stoop-server.js +0 -130
- package/dist/index.d.ts +0 -6
- package/dist/index.js +0 -5
- package/dist/inject/browser.d.ts +0 -58
- package/dist/inject/browser.js +0 -149
- package/dist/inject/dedup.d.ts +0 -29
- package/dist/inject/dedup.js +0 -38
- package/dist/inject/index.d.ts +0 -40
- package/dist/inject/index.js +0 -75
- package/dist/inject/ssr.d.ts +0 -27
- package/dist/inject/ssr.js +0 -46
- package/dist/ssr.d.ts +0 -21
- package/dist/ssr.js +0 -19
- package/dist/utils/environment.d.ts +0 -6
- package/dist/utils/environment.js +0 -12
- package/dist/utils/theme-map.d.ts +0 -22
- package/dist/utils/theme-map.js +0 -97
- package/dist/utils/theme-validation.d.ts +0 -13
- package/dist/utils/theme-validation.js +0 -36
- package/dist/utils/type-guards.d.ts +0 -26
- package/dist/utils/type-guards.js +0 -38
- package/dist/utils/utilities.d.ts +0 -14
- package/dist/utils/utilities.js +0 -43
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Storage and theme detection utilities.
|
|
3
|
+
* Provides simplified localStorage and cookie management, plus automatic theme selection.
|
|
4
|
+
* Supports SSR compatibility and error handling.
|
|
5
|
+
*/
|
|
6
|
+
import { DEFAULT_COOKIE_MAX_AGE, DEFAULT_COOKIE_PATH } from "../constants";
|
|
7
|
+
import { isBrowser } from "./helpers";
|
|
8
|
+
// ============================================================================
|
|
9
|
+
// Storage Utilities
|
|
10
|
+
// ============================================================================
|
|
11
|
+
/**
|
|
12
|
+
* Parses a JSON value safely.
|
|
13
|
+
*
|
|
14
|
+
* @param value - String value to parse
|
|
15
|
+
* @param fallback - Fallback value if parsing fails
|
|
16
|
+
* @returns Parsed value or fallback
|
|
17
|
+
*/
|
|
18
|
+
function safeJsonParse(value, fallback) {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(value);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
return fallback;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Safely gets a value from localStorage.
|
|
28
|
+
*
|
|
29
|
+
* @param key - Storage key
|
|
30
|
+
* @returns Storage result
|
|
31
|
+
*/
|
|
32
|
+
export function getFromStorage(key) {
|
|
33
|
+
if (!isBrowser()) {
|
|
34
|
+
return { error: "Not in browser environment", success: false, value: null };
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
const value = localStorage.getItem(key);
|
|
38
|
+
return { source: "localStorage", success: true, value };
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
return {
|
|
42
|
+
error: error instanceof Error ? error.message : "localStorage access failed",
|
|
43
|
+
success: false,
|
|
44
|
+
value: null,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Safely sets a value in localStorage.
|
|
50
|
+
*
|
|
51
|
+
* @param key - Storage key
|
|
52
|
+
* @param value - Value to store
|
|
53
|
+
* @returns Storage result
|
|
54
|
+
*/
|
|
55
|
+
export function setInStorage(key, value) {
|
|
56
|
+
if (!isBrowser()) {
|
|
57
|
+
return { error: "Not in browser environment", success: false, value: undefined };
|
|
58
|
+
}
|
|
59
|
+
try {
|
|
60
|
+
localStorage.setItem(key, value);
|
|
61
|
+
return { success: true, value: undefined };
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
return {
|
|
65
|
+
error: error instanceof Error ? error.message : "localStorage write failed",
|
|
66
|
+
success: false,
|
|
67
|
+
value: undefined,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Safely removes a value from localStorage.
|
|
73
|
+
*
|
|
74
|
+
* @param key - Storage key
|
|
75
|
+
* @returns Storage result
|
|
76
|
+
*/
|
|
77
|
+
export function removeFromStorage(key) {
|
|
78
|
+
if (!isBrowser()) {
|
|
79
|
+
return { error: "Not in browser environment", success: false, value: undefined };
|
|
80
|
+
}
|
|
81
|
+
try {
|
|
82
|
+
localStorage.removeItem(key);
|
|
83
|
+
return { success: true, value: undefined };
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return {
|
|
87
|
+
error: error instanceof Error ? error.message : "localStorage remove failed",
|
|
88
|
+
success: false,
|
|
89
|
+
value: undefined,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Gets a cookie value.
|
|
95
|
+
*
|
|
96
|
+
* @param name - Cookie name
|
|
97
|
+
* @returns Cookie value or null if not found
|
|
98
|
+
*/
|
|
99
|
+
export function getCookie(name) {
|
|
100
|
+
if (!isBrowser())
|
|
101
|
+
return null;
|
|
102
|
+
const value = `; ${document.cookie}`;
|
|
103
|
+
const parts = value.split(`; ${name}=`);
|
|
104
|
+
if (parts.length === 2) {
|
|
105
|
+
return parts.pop()?.split(";").shift() || null;
|
|
106
|
+
}
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Sets a cookie value.
|
|
111
|
+
*
|
|
112
|
+
* @param name - Cookie name
|
|
113
|
+
* @param value - Cookie value
|
|
114
|
+
* @param options - Cookie options
|
|
115
|
+
* @returns Success status
|
|
116
|
+
*/
|
|
117
|
+
export function setCookie(name, value, options = {}) {
|
|
118
|
+
if (!isBrowser())
|
|
119
|
+
return false;
|
|
120
|
+
const { maxAge = DEFAULT_COOKIE_MAX_AGE, path = DEFAULT_COOKIE_PATH, secure = false } = options;
|
|
121
|
+
try {
|
|
122
|
+
document.cookie = `${name}=${value}; path=${path}; max-age=${maxAge}${secure ? "; secure" : ""}`;
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
catch {
|
|
126
|
+
return false;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Removes a cookie by setting it to expire.
|
|
131
|
+
*
|
|
132
|
+
* @param name - Cookie name
|
|
133
|
+
* @param path - Cookie path
|
|
134
|
+
* @returns Success status
|
|
135
|
+
*/
|
|
136
|
+
export function removeCookie(name, path = "/") {
|
|
137
|
+
if (!isBrowser())
|
|
138
|
+
return false;
|
|
139
|
+
try {
|
|
140
|
+
document.cookie = `${name}=; path=${path}; max-age=0`;
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
return false;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Unified storage API that works with both localStorage and cookies.
|
|
149
|
+
*
|
|
150
|
+
* @param key - Storage key
|
|
151
|
+
* @param options - Storage options
|
|
152
|
+
* @returns Storage result
|
|
153
|
+
*/
|
|
154
|
+
export function getStorage(key, options = {}) {
|
|
155
|
+
const { type = "localStorage" } = options;
|
|
156
|
+
if (type === "cookie") {
|
|
157
|
+
const value = getCookie(key);
|
|
158
|
+
return {
|
|
159
|
+
source: "cookie",
|
|
160
|
+
success: value !== null,
|
|
161
|
+
value,
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
return getFromStorage(key);
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Unified storage API that works with both localStorage and cookies.
|
|
168
|
+
*
|
|
169
|
+
* @param key - Storage key
|
|
170
|
+
* @param value - Value to store
|
|
171
|
+
* @param options - Storage options
|
|
172
|
+
* @returns Storage result
|
|
173
|
+
*/
|
|
174
|
+
export function setStorage(key, value, options = {}) {
|
|
175
|
+
const { type = "localStorage" } = options;
|
|
176
|
+
if (type === "cookie") {
|
|
177
|
+
const success = setCookie(key, value, options);
|
|
178
|
+
return {
|
|
179
|
+
error: success ? undefined : "Cookie write failed",
|
|
180
|
+
success,
|
|
181
|
+
value: undefined,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
return setInStorage(key, value);
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Unified storage API that works with both localStorage and cookies.
|
|
188
|
+
*
|
|
189
|
+
* @param key - Storage key
|
|
190
|
+
* @param options - Storage options
|
|
191
|
+
* @returns Storage result
|
|
192
|
+
*/
|
|
193
|
+
export function removeStorage(key, options = {}) {
|
|
194
|
+
const { type = "localStorage" } = options;
|
|
195
|
+
if (type === "cookie") {
|
|
196
|
+
const success = removeCookie(key, options.path);
|
|
197
|
+
return {
|
|
198
|
+
error: success ? undefined : "Cookie remove failed",
|
|
199
|
+
success,
|
|
200
|
+
value: undefined,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
return removeFromStorage(key);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Gets a JSON value from storage with automatic parsing.
|
|
207
|
+
*
|
|
208
|
+
* @param key - Storage key
|
|
209
|
+
* @param fallback - Fallback value if parsing fails or key not found
|
|
210
|
+
* @param options - Storage options
|
|
211
|
+
* @returns Parsed JSON value or fallback
|
|
212
|
+
*/
|
|
213
|
+
export function getJsonFromStorage(key, fallback, options = {}) {
|
|
214
|
+
const result = getStorage(key, options);
|
|
215
|
+
if (!result.success || result.value === null) {
|
|
216
|
+
return { ...result, value: fallback };
|
|
217
|
+
}
|
|
218
|
+
const parsed = safeJsonParse(result.value, fallback);
|
|
219
|
+
return { ...result, value: parsed };
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Sets a JSON value in storage with automatic serialization.
|
|
223
|
+
*
|
|
224
|
+
* @param key - Storage key
|
|
225
|
+
* @param value - Value to serialize and store
|
|
226
|
+
* @param options - Storage options
|
|
227
|
+
* @returns Storage result
|
|
228
|
+
*/
|
|
229
|
+
export function setJsonInStorage(key, value, options = {}) {
|
|
230
|
+
try {
|
|
231
|
+
const serialized = JSON.stringify(value);
|
|
232
|
+
return setStorage(key, serialized, options);
|
|
233
|
+
}
|
|
234
|
+
catch (error) {
|
|
235
|
+
return {
|
|
236
|
+
error: error instanceof Error ? error.message : "JSON serialization failed",
|
|
237
|
+
success: false,
|
|
238
|
+
value: undefined,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Creates a typed storage interface for a specific key.
|
|
244
|
+
*
|
|
245
|
+
* @param key - Storage key
|
|
246
|
+
* @param options - Storage options
|
|
247
|
+
* @returns Typed storage interface
|
|
248
|
+
*/
|
|
249
|
+
export function createStorage(key, options = {}) {
|
|
250
|
+
return {
|
|
251
|
+
get: () => getStorage(key, options),
|
|
252
|
+
getJson: (fallback) => getJsonFromStorage(key, fallback, options),
|
|
253
|
+
remove: () => removeStorage(key, options),
|
|
254
|
+
set: (value) => setStorage(key, value, options),
|
|
255
|
+
setJson: (value) => setJsonInStorage(key, value, options),
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
// ============================================================================
|
|
259
|
+
// Theme Detection Utilities
|
|
260
|
+
// ============================================================================
|
|
261
|
+
/**
|
|
262
|
+
* Gets localStorage value safely (internal helper for theme detection).
|
|
263
|
+
* Uses getFromStorage for consistency.
|
|
264
|
+
*
|
|
265
|
+
* @param key - Storage key
|
|
266
|
+
* @returns Stored value or null if not found or access failed
|
|
267
|
+
*/
|
|
268
|
+
function getLocalStorage(key) {
|
|
269
|
+
const result = getFromStorage(key);
|
|
270
|
+
return result.success ? result.value : null;
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* Detects system color scheme preference.
|
|
274
|
+
*
|
|
275
|
+
* @returns 'dark' or 'light' based on system preference
|
|
276
|
+
*/
|
|
277
|
+
function getSystemPreference() {
|
|
278
|
+
if (!isBrowser())
|
|
279
|
+
return null;
|
|
280
|
+
try {
|
|
281
|
+
return window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
|
|
282
|
+
}
|
|
283
|
+
catch {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Validates if a theme name exists in available themes.
|
|
289
|
+
*
|
|
290
|
+
* @param themeName - Theme name to validate
|
|
291
|
+
* @param themes - Available themes map
|
|
292
|
+
* @returns True if theme is valid
|
|
293
|
+
*/
|
|
294
|
+
function isValidTheme(themeName, themes) {
|
|
295
|
+
return !themes || !!themes[themeName];
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Detects the best theme to use based on multiple sources with priority.
|
|
299
|
+
*
|
|
300
|
+
* Priority order (highest to lowest):
|
|
301
|
+
* 1. Explicit localStorage preference
|
|
302
|
+
* 2. Cookie preference (for SSR compatibility)
|
|
303
|
+
* 3. System color scheme preference
|
|
304
|
+
* 4. Default theme
|
|
305
|
+
*
|
|
306
|
+
* @param options - Theme detection options
|
|
307
|
+
* @returns Theme detection result
|
|
308
|
+
*/
|
|
309
|
+
export function detectTheme(options = {}) {
|
|
310
|
+
const { cookie: cookieKey, default: defaultTheme = "light", localStorage: storageKey, systemPreference = true, themes, } = options;
|
|
311
|
+
// 1. Check localStorage (highest priority - explicit user choice)
|
|
312
|
+
if (storageKey) {
|
|
313
|
+
const stored = getLocalStorage(storageKey);
|
|
314
|
+
if (stored && isValidTheme(stored, themes)) {
|
|
315
|
+
return {
|
|
316
|
+
confidence: 0.9, // High confidence - explicit user choice
|
|
317
|
+
source: "localStorage",
|
|
318
|
+
theme: stored,
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
// 2. Check cookie (for SSR compatibility)
|
|
323
|
+
if (cookieKey) {
|
|
324
|
+
const cookieValue = getCookie(cookieKey);
|
|
325
|
+
if (cookieValue && isValidTheme(cookieValue, themes)) {
|
|
326
|
+
return {
|
|
327
|
+
confidence: 0.8, // High confidence - persisted preference
|
|
328
|
+
source: "cookie",
|
|
329
|
+
theme: cookieValue,
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
// 3. Check system preference
|
|
334
|
+
if (systemPreference) {
|
|
335
|
+
const system = getSystemPreference();
|
|
336
|
+
if (system && isValidTheme(system, themes)) {
|
|
337
|
+
return {
|
|
338
|
+
confidence: 0.6, // Medium confidence - system default
|
|
339
|
+
source: "system",
|
|
340
|
+
theme: system,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
// 4. Fall back to default
|
|
345
|
+
return {
|
|
346
|
+
confidence: 0.3, // Low confidence - fallback only
|
|
347
|
+
source: "default",
|
|
348
|
+
theme: defaultTheme,
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Creates a theme detector function with pre-configured options.
|
|
353
|
+
*
|
|
354
|
+
* @param options - Theme detection options
|
|
355
|
+
* @returns Theme detection function
|
|
356
|
+
*/
|
|
357
|
+
export function createThemeDetector(options) {
|
|
358
|
+
return () => detectTheme(options);
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Auto-detects theme for SSR contexts (server-side or during hydration).
|
|
362
|
+
* Uses only cookie and default sources since localStorage isn't available.
|
|
363
|
+
*
|
|
364
|
+
* @param options - Theme detection options
|
|
365
|
+
* @returns Theme name
|
|
366
|
+
*/
|
|
367
|
+
export function detectThemeForSSR(options = {}) {
|
|
368
|
+
const { cookie: cookieKey, default: defaultTheme = "light", themes } = options;
|
|
369
|
+
// Only check cookie in SSR context
|
|
370
|
+
if (cookieKey) {
|
|
371
|
+
const cookieValue = getCookie(cookieKey);
|
|
372
|
+
if (cookieValue && isValidTheme(cookieValue, themes)) {
|
|
373
|
+
return cookieValue;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return defaultTheme;
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Listens for system theme changes and calls callback when changed.
|
|
380
|
+
*
|
|
381
|
+
* @param callback - Function to call when system theme changes
|
|
382
|
+
* @returns Cleanup function
|
|
383
|
+
*/
|
|
384
|
+
export function onSystemThemeChange(callback) {
|
|
385
|
+
if (!isBrowser()) {
|
|
386
|
+
return () => { }; // No-op in SSR
|
|
387
|
+
}
|
|
388
|
+
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
|
389
|
+
const handleChange = (e) => {
|
|
390
|
+
callback(e.matches ? "dark" : "light");
|
|
391
|
+
};
|
|
392
|
+
mediaQuery.addEventListener("change", handleChange);
|
|
393
|
+
return () => {
|
|
394
|
+
mediaQuery.removeEventListener("change", handleChange);
|
|
395
|
+
};
|
|
396
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Provides hashing
|
|
4
|
-
* and CSS sanitization utilities for security.
|
|
2
|
+
* Theme-related string utilities and property mapping.
|
|
3
|
+
* Provides hashing, CSS sanitization, and theme scale mapping for property-aware token resolution.
|
|
5
4
|
*/
|
|
5
|
+
import type { ThemeScale } from "../types";
|
|
6
6
|
/**
|
|
7
7
|
* Generates a hash string from an input string using FNV-1a algorithm.
|
|
8
8
|
* Includes string length to reduce collision probability.
|
|
@@ -107,3 +107,20 @@ export declare function validateKeyframeKey(key: string): boolean;
|
|
|
107
107
|
* @returns RegExp for matching :root selector blocks
|
|
108
108
|
*/
|
|
109
109
|
export declare function getRootRegex(prefix?: string): RegExp;
|
|
110
|
+
/**
|
|
111
|
+
* Auto-detects theme scale from CSS property name using pattern matching.
|
|
112
|
+
* Used as fallback when property is not in DEFAULT_THEME_MAP.
|
|
113
|
+
*
|
|
114
|
+
* @param property - CSS property name
|
|
115
|
+
* @returns Theme scale name or undefined if no pattern matches
|
|
116
|
+
*/
|
|
117
|
+
export declare function autoDetectScale(property: string): ThemeScale | undefined;
|
|
118
|
+
/**
|
|
119
|
+
* Gets the theme scale for a CSS property.
|
|
120
|
+
* Checks user themeMap first, then default themeMap, then pattern matching.
|
|
121
|
+
*
|
|
122
|
+
* @param property - CSS property name
|
|
123
|
+
* @param userThemeMap - Optional user-provided themeMap override
|
|
124
|
+
* @returns Theme scale name or undefined if no mapping found
|
|
125
|
+
*/
|
|
126
|
+
export declare function getScaleForProperty(property: string, userThemeMap?: Record<string, ThemeScale>): ThemeScale | undefined;
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
* Provides hashing
|
|
4
|
-
* and CSS sanitization utilities for security.
|
|
2
|
+
* Theme-related string utilities and property mapping.
|
|
3
|
+
* Provides hashing, CSS sanitization, and theme scale mapping for property-aware token resolution.
|
|
5
4
|
*/
|
|
5
|
+
import { DEFAULT_THEME_MAP, SANITIZE_CACHE_SIZE_LIMIT } from "../constants";
|
|
6
|
+
// ============================================================================
|
|
7
|
+
// String Utilities
|
|
8
|
+
// ============================================================================
|
|
6
9
|
import { LRUCache } from "../core/cache";
|
|
7
|
-
const sanitizeCacheSizeLimit = 1000;
|
|
8
10
|
let cachedRootRegex = null;
|
|
9
|
-
const selectorCache = new LRUCache(
|
|
10
|
-
const propertyNameCache = new LRUCache(
|
|
11
|
-
const sanitizeClassNameCache = new LRUCache(
|
|
12
|
-
const variableNameCache = new LRUCache(
|
|
11
|
+
const selectorCache = new LRUCache(SANITIZE_CACHE_SIZE_LIMIT);
|
|
12
|
+
const propertyNameCache = new LRUCache(SANITIZE_CACHE_SIZE_LIMIT);
|
|
13
|
+
const sanitizeClassNameCache = new LRUCache(SANITIZE_CACHE_SIZE_LIMIT);
|
|
14
|
+
const variableNameCache = new LRUCache(SANITIZE_CACHE_SIZE_LIMIT);
|
|
13
15
|
/**
|
|
14
16
|
* Generates a hash string from an input string using FNV-1a algorithm.
|
|
15
17
|
* Includes string length to reduce collision probability.
|
|
@@ -247,7 +249,105 @@ export function getRootRegex(prefix = "") {
|
|
|
247
249
|
if (!cachedRootRegex) {
|
|
248
250
|
const rootSelector = ":root";
|
|
249
251
|
const escapedSelector = rootSelector.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
250
|
-
|
|
252
|
+
// Match :root block - use greedy match to handle nested braces (e.g., calc())
|
|
253
|
+
// Since theme vars should only have one :root block, greedy matching is safe
|
|
254
|
+
// Greedy matching ensures we capture the entire :root block, including nested functions
|
|
255
|
+
cachedRootRegex = new RegExp(`${escapedSelector}\\s*\\{[\\s\\S]*\\}`);
|
|
251
256
|
}
|
|
252
257
|
return cachedRootRegex;
|
|
253
258
|
}
|
|
259
|
+
// ============================================================================
|
|
260
|
+
// Theme Map Utilities
|
|
261
|
+
// ============================================================================
|
|
262
|
+
/**
|
|
263
|
+
* Auto-detects theme scale from CSS property name using pattern matching.
|
|
264
|
+
* Used as fallback when property is not in DEFAULT_THEME_MAP.
|
|
265
|
+
*
|
|
266
|
+
* @param property - CSS property name
|
|
267
|
+
* @returns Theme scale name or undefined if no pattern matches
|
|
268
|
+
*/
|
|
269
|
+
export function autoDetectScale(property) {
|
|
270
|
+
// Color properties
|
|
271
|
+
if (property.includes("Color") ||
|
|
272
|
+
property === "fill" ||
|
|
273
|
+
property === "stroke" ||
|
|
274
|
+
property === "accentColor" ||
|
|
275
|
+
property === "caretColor" ||
|
|
276
|
+
property === "border" ||
|
|
277
|
+
property === "outline" ||
|
|
278
|
+
(property.includes("background") && !property.includes("Size") && !property.includes("Image"))) {
|
|
279
|
+
return "colors";
|
|
280
|
+
}
|
|
281
|
+
// Spacing properties
|
|
282
|
+
if (/^(margin|padding|gap|inset|top|right|bottom|left|rowGap|columnGap|gridGap|gridRowGap|gridColumnGap)/.test(property) ||
|
|
283
|
+
property.includes("Block") ||
|
|
284
|
+
property.includes("Inline")) {
|
|
285
|
+
return "space";
|
|
286
|
+
}
|
|
287
|
+
// Size properties
|
|
288
|
+
if (/(width|height|size|basis)$/i.test(property) ||
|
|
289
|
+
property.includes("BlockSize") ||
|
|
290
|
+
property.includes("InlineSize")) {
|
|
291
|
+
return "sizes";
|
|
292
|
+
}
|
|
293
|
+
// Typography: Font Size
|
|
294
|
+
if (property === "fontSize" || (property === "font" && !property.includes("Family"))) {
|
|
295
|
+
return "fontSizes";
|
|
296
|
+
}
|
|
297
|
+
// Typography: Font Family
|
|
298
|
+
if (property === "fontFamily" || property.includes("FontFamily")) {
|
|
299
|
+
return "fonts";
|
|
300
|
+
}
|
|
301
|
+
// Typography: Font Weight
|
|
302
|
+
if (property === "fontWeight" || property.includes("FontWeight")) {
|
|
303
|
+
return "fontWeights";
|
|
304
|
+
}
|
|
305
|
+
// Typography: Letter Spacing
|
|
306
|
+
if (property === "letterSpacing" || property.includes("LetterSpacing")) {
|
|
307
|
+
return "letterSpacings";
|
|
308
|
+
}
|
|
309
|
+
// Border Radius
|
|
310
|
+
if (property.includes("Radius") || property.includes("radius")) {
|
|
311
|
+
return "radii";
|
|
312
|
+
}
|
|
313
|
+
// Shadows
|
|
314
|
+
if (property.includes("Shadow") ||
|
|
315
|
+
property.includes("shadow") ||
|
|
316
|
+
property === "filter" ||
|
|
317
|
+
property === "backdropFilter") {
|
|
318
|
+
return "shadows";
|
|
319
|
+
}
|
|
320
|
+
// Z-Index
|
|
321
|
+
if (property === "zIndex" || property.includes("ZIndex") || property.includes("z-index")) {
|
|
322
|
+
return "zIndices";
|
|
323
|
+
}
|
|
324
|
+
// Opacity
|
|
325
|
+
if (property === "opacity" || property.includes("Opacity")) {
|
|
326
|
+
return "opacities";
|
|
327
|
+
}
|
|
328
|
+
// Transitions and animations
|
|
329
|
+
if (property.startsWith("transition") ||
|
|
330
|
+
property.startsWith("animation") ||
|
|
331
|
+
property.includes("Transition") ||
|
|
332
|
+
property.includes("Animation")) {
|
|
333
|
+
return "transitions";
|
|
334
|
+
}
|
|
335
|
+
return undefined;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Gets the theme scale for a CSS property.
|
|
339
|
+
* Checks user themeMap first, then default themeMap, then pattern matching.
|
|
340
|
+
*
|
|
341
|
+
* @param property - CSS property name
|
|
342
|
+
* @param userThemeMap - Optional user-provided themeMap override
|
|
343
|
+
* @returns Theme scale name or undefined if no mapping found
|
|
344
|
+
*/
|
|
345
|
+
export function getScaleForProperty(property, userThemeMap) {
|
|
346
|
+
if (userThemeMap && property in userThemeMap) {
|
|
347
|
+
return userThemeMap[property];
|
|
348
|
+
}
|
|
349
|
+
if (property in DEFAULT_THEME_MAP) {
|
|
350
|
+
return DEFAULT_THEME_MAP[property];
|
|
351
|
+
}
|
|
352
|
+
return autoDetectScale(property);
|
|
353
|
+
}
|
package/dist/utils/theme.d.ts
CHANGED
|
@@ -28,9 +28,21 @@ export declare function tokenToCSSVar(token: string, theme?: Theme, property?: s
|
|
|
28
28
|
*
|
|
29
29
|
* @param theme - Theme object to convert to CSS variables
|
|
30
30
|
* @param prefix - Optional prefix for CSS variable names
|
|
31
|
-
* @
|
|
31
|
+
* @param attributeSelector - Optional attribute selector (e.g., '[data-theme="light"]'). Defaults to ':root'
|
|
32
|
+
* @returns CSS string with selector and CSS variables
|
|
32
33
|
*/
|
|
33
|
-
export declare function generateCSSVariables(theme: Theme, prefix?: string): string;
|
|
34
|
+
export declare function generateCSSVariables(theme: Theme, prefix?: string, attributeSelector?: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* Generates CSS custom properties for all themes using attribute selectors.
|
|
37
|
+
* This allows all themes to be available simultaneously, with theme switching
|
|
38
|
+
* handled by changing the data-theme attribute.
|
|
39
|
+
*
|
|
40
|
+
* @param themes - Map of theme names to theme objects
|
|
41
|
+
* @param prefix - Optional prefix for CSS variable names
|
|
42
|
+
* @param attribute - Attribute name for theme selection (defaults to 'data-theme')
|
|
43
|
+
* @returns CSS string with all theme CSS variables
|
|
44
|
+
*/
|
|
45
|
+
export declare function generateAllThemeVariables(themes: Record<string, Theme>, prefix?: string, attribute?: string): string;
|
|
34
46
|
/**
|
|
35
47
|
* Recursively replaces theme tokens with CSS variable references in a CSS object.
|
|
36
48
|
*
|