stoop 0.2.1 → 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.
Files changed (56) hide show
  1. package/README.md +48 -103
  2. package/dist/api/core-api.d.ts +34 -0
  3. package/dist/api/core-api.js +135 -0
  4. package/dist/api/global-css.d.ts +0 -11
  5. package/dist/api/global-css.js +42 -0
  6. package/dist/api/styled.d.ts +0 -1
  7. package/dist/api/styled.js +419 -0
  8. package/dist/api/theme-provider.d.ts +41 -0
  9. package/dist/api/theme-provider.js +223 -0
  10. package/dist/constants.d.ts +13 -4
  11. package/dist/constants.js +154 -0
  12. package/dist/core/cache.d.ts +5 -9
  13. package/dist/core/cache.js +68 -0
  14. package/dist/core/compiler.d.ts +11 -0
  15. package/dist/core/compiler.js +206 -0
  16. package/dist/core/theme-manager.d.ts +27 -4
  17. package/dist/core/theme-manager.js +107 -0
  18. package/dist/core/variants.js +38 -0
  19. package/dist/create-stoop-internal.d.ts +30 -0
  20. package/dist/create-stoop-internal.js +123 -0
  21. package/dist/create-stoop-ssr.d.ts +10 -0
  22. package/dist/create-stoop-ssr.js +26 -0
  23. package/dist/create-stoop.d.ts +32 -4
  24. package/dist/create-stoop.js +156 -0
  25. package/dist/inject.d.ts +113 -0
  26. package/dist/inject.js +308 -0
  27. package/dist/types/index.d.ts +157 -17
  28. package/dist/types/index.js +5 -0
  29. package/dist/types/react-polymorphic-types.d.ts +15 -8
  30. package/dist/utils/auto-preload.d.ts +45 -0
  31. package/dist/utils/auto-preload.js +167 -0
  32. package/dist/utils/helpers.d.ts +64 -0
  33. package/dist/utils/helpers.js +150 -0
  34. package/dist/utils/storage.d.ts +148 -0
  35. package/dist/utils/storage.js +396 -0
  36. package/dist/utils/{string.d.ts → theme-utils.d.ts} +36 -12
  37. package/dist/utils/theme-utils.js +353 -0
  38. package/dist/utils/theme.d.ts +17 -5
  39. package/dist/utils/theme.js +304 -0
  40. package/package.json +48 -24
  41. package/LICENSE.md +0 -21
  42. package/dist/api/create-theme.d.ts +0 -13
  43. package/dist/api/css.d.ts +0 -16
  44. package/dist/api/keyframes.d.ts +0 -16
  45. package/dist/api/provider.d.ts +0 -19
  46. package/dist/api/use-theme.d.ts +0 -13
  47. package/dist/index.d.ts +0 -6
  48. package/dist/index.js +0 -13
  49. package/dist/inject/browser.d.ts +0 -59
  50. package/dist/inject/dedup.d.ts +0 -29
  51. package/dist/inject/index.d.ts +0 -41
  52. package/dist/inject/ssr.d.ts +0 -28
  53. package/dist/utils/theme-map.d.ts +0 -25
  54. package/dist/utils/theme-validation.d.ts +0 -13
  55. package/dist/utils/type-guards.d.ts +0 -26
  56. package/dist/utils/utilities.d.ts +0 -14
@@ -1,20 +1,29 @@
1
1
  /**
2
2
  * Shared constants used throughout the library.
3
- * Includes cache size limits, nesting depth limits, and fallback context.
3
+ * Includes cache size limits and nesting depth limits.
4
4
  */
5
- import type { CSS, ThemeContextValue, ThemeScale } from "./types";
5
+ import type { CSS, ThemeScale } from "./types";
6
6
  export declare const EMPTY_CSS: CSS;
7
7
  export declare const MAX_CSS_CACHE_SIZE = 10000;
8
8
  export declare const MAX_CLASS_NAME_CACHE_SIZE = 5000;
9
9
  export declare const MAX_CSS_NESTING_DEPTH = 10;
10
- export declare const FALLBACK_CONTEXT: import("react").Context<ThemeContextValue>;
10
+ /**
11
+ * Cache size limits for various LRU caches.
12
+ */
13
+ export declare const KEYFRAME_CACHE_LIMIT = 500;
14
+ export declare const SANITIZE_CACHE_SIZE_LIMIT = 1000;
15
+ /**
16
+ * Cookie defaults.
17
+ */
18
+ export declare const DEFAULT_COOKIE_MAX_AGE = 31536000;
19
+ export declare const DEFAULT_COOKIE_PATH = "/";
11
20
  /**
12
21
  * Approved theme scales - only these scales are allowed in theme objects.
13
22
  */
14
23
  export declare const APPROVED_THEME_SCALES: ReadonlyArray<ThemeScale>;
15
24
  /**
16
25
  * Default themeMap mapping CSS properties to theme scales.
17
- * Covers 150+ common CSS properties for zero-config experience.
26
+ * Covers common CSS properties for zero-config experience.
18
27
  * Missing properties gracefully fallback to pattern-based auto-detection.
19
28
  */
20
29
  export declare const DEFAULT_THEME_MAP: Record<string, ThemeScale>;
@@ -0,0 +1,154 @@
1
+ /**
2
+ * Shared constants used throughout the library.
3
+ * Includes cache size limits and nesting depth limits.
4
+ */
5
+ export const EMPTY_CSS = Object.freeze({});
6
+ export const MAX_CSS_CACHE_SIZE = 10000;
7
+ export const MAX_CLASS_NAME_CACHE_SIZE = 5000;
8
+ export const MAX_CSS_NESTING_DEPTH = 10;
9
+ /**
10
+ * Cache size limits for various LRU caches.
11
+ */
12
+ export const KEYFRAME_CACHE_LIMIT = 500;
13
+ export const SANITIZE_CACHE_SIZE_LIMIT = 1000;
14
+ /**
15
+ * Cookie defaults.
16
+ */
17
+ export const DEFAULT_COOKIE_MAX_AGE = 31536000; // 1 year in seconds
18
+ export const DEFAULT_COOKIE_PATH = "/";
19
+ /**
20
+ * Approved theme scales - only these scales are allowed in theme objects.
21
+ */
22
+ export const APPROVED_THEME_SCALES = [
23
+ "colors",
24
+ "opacities",
25
+ "space",
26
+ "radii",
27
+ "sizes",
28
+ "fonts",
29
+ "fontWeights",
30
+ "fontSizes",
31
+ "lineHeights",
32
+ "letterSpacings",
33
+ "shadows",
34
+ "zIndices",
35
+ "transitions",
36
+ ];
37
+ /**
38
+ * Default themeMap mapping CSS properties to theme scales.
39
+ * Covers common CSS properties for zero-config experience.
40
+ * Missing properties gracefully fallback to pattern-based auto-detection.
41
+ */
42
+ export const DEFAULT_THEME_MAP = {
43
+ accentColor: "colors",
44
+ animation: "transitions",
45
+ animationDelay: "transitions",
46
+ animationDuration: "transitions",
47
+ animationTimingFunction: "transitions",
48
+ backdropFilter: "shadows",
49
+ background: "colors",
50
+ backgroundColor: "colors",
51
+ blockSize: "sizes",
52
+ border: "colors",
53
+ borderBlockColor: "colors",
54
+ borderBlockEndColor: "colors",
55
+ borderBlockStartColor: "colors",
56
+ borderBottomColor: "colors",
57
+ borderBottomLeftRadius: "radii",
58
+ borderBottomRightRadius: "radii",
59
+ borderColor: "colors",
60
+ borderEndEndRadius: "radii",
61
+ borderEndStartRadius: "radii",
62
+ borderInlineColor: "colors",
63
+ borderInlineEndColor: "colors",
64
+ borderInlineStartColor: "colors",
65
+ borderLeftColor: "colors",
66
+ borderRadius: "radii",
67
+ borderRightColor: "colors",
68
+ borderStartEndRadius: "radii",
69
+ borderStartStartRadius: "radii",
70
+ borderTopColor: "colors",
71
+ borderTopLeftRadius: "radii",
72
+ borderTopRightRadius: "radii",
73
+ bottom: "space",
74
+ boxShadow: "shadows",
75
+ caretColor: "colors",
76
+ color: "colors",
77
+ columnGap: "space",
78
+ columnRuleColor: "colors",
79
+ fill: "colors",
80
+ filter: "shadows",
81
+ flexBasis: "sizes",
82
+ floodColor: "colors",
83
+ font: "fontSizes",
84
+ fontFamily: "fonts",
85
+ fontSize: "fontSizes",
86
+ fontWeight: "fontWeights",
87
+ gap: "space",
88
+ gridColumnGap: "space",
89
+ gridGap: "space",
90
+ gridRowGap: "space",
91
+ height: "sizes",
92
+ inlineSize: "sizes",
93
+ inset: "space",
94
+ insetBlock: "space",
95
+ insetBlockEnd: "space",
96
+ insetBlockStart: "space",
97
+ insetInline: "space",
98
+ insetInlineEnd: "space",
99
+ insetInlineStart: "space",
100
+ left: "space",
101
+ letterSpacing: "letterSpacings",
102
+ lightingColor: "colors",
103
+ lineHeight: "lineHeights",
104
+ margin: "space",
105
+ marginBlock: "space",
106
+ marginBlockEnd: "space",
107
+ marginBlockStart: "space",
108
+ marginBottom: "space",
109
+ marginInline: "space",
110
+ marginInlineEnd: "space",
111
+ marginInlineStart: "space",
112
+ marginLeft: "space",
113
+ marginRight: "space",
114
+ marginTop: "space",
115
+ maxBlockSize: "sizes",
116
+ maxHeight: "sizes",
117
+ maxInlineSize: "sizes",
118
+ maxWidth: "sizes",
119
+ minBlockSize: "sizes",
120
+ minHeight: "sizes",
121
+ minInlineSize: "sizes",
122
+ minWidth: "sizes",
123
+ opacity: "opacities",
124
+ outline: "colors",
125
+ outlineColor: "colors",
126
+ padding: "space",
127
+ paddingBlock: "space",
128
+ paddingBlockEnd: "space",
129
+ paddingBlockStart: "space",
130
+ paddingBottom: "space",
131
+ paddingInline: "space",
132
+ paddingInlineEnd: "space",
133
+ paddingInlineStart: "space",
134
+ paddingLeft: "space",
135
+ paddingRight: "space",
136
+ paddingTop: "space",
137
+ right: "space",
138
+ rowGap: "space",
139
+ size: "sizes",
140
+ stopColor: "colors",
141
+ stroke: "colors",
142
+ textDecorationColor: "colors",
143
+ textEmphasisColor: "colors",
144
+ textShadow: "shadows",
145
+ top: "space",
146
+ transition: "transitions",
147
+ transitionDelay: "transitions",
148
+ transitionDuration: "transitions",
149
+ transitionProperty: "transitions",
150
+ transitionTimingFunction: "transitions",
151
+ width: "sizes",
152
+ zIndex: "zIndices",
153
+ };
154
+ export const STOOP_COMPONENT_SYMBOL = Symbol.for("stoop.component");
@@ -7,7 +7,7 @@
7
7
  * LRU Cache implementation for class names and CSS strings.
8
8
  * Automatically evicts least recently used entries when size limit is exceeded.
9
9
  */
10
- declare class LRUCache<K, V> extends Map<K, V> {
10
+ export declare class LRUCache<K, V> extends Map<K, V> {
11
11
  private readonly maxSize;
12
12
  constructor(maxSize: number);
13
13
  get(key: K): V | undefined;
@@ -16,24 +16,20 @@ declare class LRUCache<K, V> extends Map<K, V> {
16
16
  export declare const classNameCache: LRUCache<string, string>;
17
17
  export declare const cssStringCache: LRUCache<string, string>;
18
18
  /**
19
- * Checks if a CSS string is cached.
19
+ * Checks if a CSS string has been injected.
20
+ * Uses a Set for O(1) lookup.
20
21
  *
21
22
  * @param css - CSS string to check
22
- * @returns True if CSS is cached
23
+ * @returns True if CSS has been injected
23
24
  */
24
25
  export declare function isCachedStyle(css: string): boolean;
25
26
  /**
26
- * Marks a CSS string as cached.
27
+ * Marks a CSS string as injected.
27
28
  *
28
29
  * @param css - CSS string to cache
29
30
  */
30
31
  export declare function markStyleAsCached(css: string): void;
31
- /**
32
- * Limits cache size by evicting least recently used entries.
33
- */
34
- export declare function limitCacheSize(): void;
35
32
  /**
36
33
  * Clears all cached styles.
37
34
  */
38
35
  export declare function clearStyleCache(): void;
39
- export {};
@@ -0,0 +1,68 @@
1
+ /**
2
+ * CSS compilation caching system.
3
+ * Tracks compiled CSS strings and class names to prevent duplicate work.
4
+ * Implements LRU (Least Recently Used) eviction when cache size limits are exceeded.
5
+ */
6
+ import { MAX_CLASS_NAME_CACHE_SIZE, MAX_CSS_CACHE_SIZE } from "../constants";
7
+ /**
8
+ * LRU Cache implementation for class names and CSS strings.
9
+ * Automatically evicts least recently used entries when size limit is exceeded.
10
+ */
11
+ export class LRUCache extends Map {
12
+ maxSize;
13
+ constructor(maxSize) {
14
+ super();
15
+ this.maxSize = maxSize;
16
+ }
17
+ get(key) {
18
+ const value = super.get(key);
19
+ if (value !== undefined) {
20
+ super.delete(key);
21
+ super.set(key, value);
22
+ }
23
+ return value;
24
+ }
25
+ set(key, value) {
26
+ if (super.has(key)) {
27
+ super.delete(key);
28
+ }
29
+ else if (this.size >= this.maxSize) {
30
+ const firstKey = this.keys().next().value;
31
+ if (firstKey !== undefined) {
32
+ super.delete(firstKey);
33
+ }
34
+ }
35
+ super.set(key, value);
36
+ return this;
37
+ }
38
+ }
39
+ export const classNameCache = new LRUCache(MAX_CLASS_NAME_CACHE_SIZE);
40
+ export const cssStringCache = new LRUCache(MAX_CSS_CACHE_SIZE);
41
+ // Separate cache for tracking injected CSS strings (used by browser injection)
42
+ const injectedStylesCache = new Set();
43
+ /**
44
+ * Checks if a CSS string has been injected.
45
+ * Uses a Set for O(1) lookup.
46
+ *
47
+ * @param css - CSS string to check
48
+ * @returns True if CSS has been injected
49
+ */
50
+ export function isCachedStyle(css) {
51
+ return injectedStylesCache.has(css);
52
+ }
53
+ /**
54
+ * Marks a CSS string as injected.
55
+ *
56
+ * @param css - CSS string to cache
57
+ */
58
+ export function markStyleAsCached(css) {
59
+ injectedStylesCache.add(css);
60
+ }
61
+ /**
62
+ * Clears all cached styles.
63
+ */
64
+ export function clearStyleCache() {
65
+ classNameCache.clear();
66
+ cssStringCache.clear();
67
+ injectedStylesCache.clear();
68
+ }
@@ -4,6 +4,17 @@
4
4
  * Handles nested selectors, media queries, styled component targeting, and theme tokens.
5
5
  */
6
6
  import type { CSS, Theme, ThemeScale, UtilityFunction } from "../types";
7
+ /**
8
+ * Converts a CSS object to a CSS string with proper nesting and selectors.
9
+ * Handles pseudo-selectors, media queries, combinators, and styled component targeting.
10
+ *
11
+ * @param obj - CSS object to convert
12
+ * @param selector - Current selector context
13
+ * @param depth - Current nesting depth
14
+ * @param media - Media query breakpoints
15
+ * @returns CSS string
16
+ */
17
+ export declare function cssObjectToString(obj: CSS, selector?: string, depth?: number, media?: Record<string, string>): string;
7
18
  /**
8
19
  * Compiles CSS objects into CSS strings and generates unique class names.
9
20
  * Handles nested selectors, media queries, styled component targeting, and theme tokens.
@@ -0,0 +1,206 @@
1
+ /**
2
+ * CSS compilation engine.
3
+ * Converts CSS objects to CSS strings and generates unique class names.
4
+ * Handles nested selectors, media queries, styled component targeting, and theme tokens.
5
+ */
6
+ import { MAX_CSS_NESTING_DEPTH, STOOP_COMPONENT_SYMBOL } from "../constants";
7
+ import { injectCSS } from "../inject";
8
+ import { isValidCSSObject, applyUtilities, isStyledComponentRef } from "../utils/helpers";
9
+ import { replaceThemeTokensWithVars } from "../utils/theme";
10
+ import { escapeCSSValue, hash, sanitizeCSSPropertyName, sanitizeCSSSelector, sanitizeMediaQuery, sanitizePrefix, } from "../utils/theme-utils";
11
+ import { classNameCache, cssStringCache } from "./cache";
12
+ /**
13
+ * Checks if a key/value pair represents a styled component reference.
14
+ * Uses shared isStyledComponentRef helper for consistency.
15
+ *
16
+ * @param key - Property key to check
17
+ * @param value - Property value to check
18
+ * @returns True if key/value represents a styled component reference
19
+ */
20
+ function isStyledComponentKey(key, value) {
21
+ // Check symbol key
22
+ if (typeof key === "symbol" && key === STOOP_COMPONENT_SYMBOL) {
23
+ return true;
24
+ }
25
+ // Check if value is a styled component ref (consolidated check)
26
+ if (isStyledComponentRef(value)) {
27
+ return true;
28
+ }
29
+ // Check string key prefix
30
+ if (typeof key === "string" && key.startsWith("__STOOP_COMPONENT_")) {
31
+ return true;
32
+ }
33
+ return false;
34
+ }
35
+ /**
36
+ * Extracts the class name from a styled component reference.
37
+ *
38
+ * @param key - Property key
39
+ * @param value - Property value
40
+ * @returns Extracted class name or empty string
41
+ */
42
+ function getClassNameFromKeyOrValue(key, value) {
43
+ if (typeof value === "object" &&
44
+ value !== null &&
45
+ "__stoopClassName" in value &&
46
+ typeof value.__stoopClassName === "string") {
47
+ return value.__stoopClassName;
48
+ }
49
+ if (typeof key === "string" && key.startsWith("__STOOP_COMPONENT_")) {
50
+ return key.replace("__STOOP_COMPONENT_", "");
51
+ }
52
+ return "";
53
+ }
54
+ /**
55
+ * Converts a CSS object to a CSS string with proper nesting and selectors.
56
+ * Handles pseudo-selectors, media queries, combinators, and styled component targeting.
57
+ *
58
+ * @param obj - CSS object to convert
59
+ * @param selector - Current selector context
60
+ * @param depth - Current nesting depth
61
+ * @param media - Media query breakpoints
62
+ * @returns CSS string
63
+ */
64
+ export function cssObjectToString(obj, selector = "", depth = 0, media) {
65
+ if (!obj || typeof obj !== "object") {
66
+ return "";
67
+ }
68
+ if (depth > MAX_CSS_NESTING_DEPTH) {
69
+ return "";
70
+ }
71
+ const cssProperties = [];
72
+ const nestedRulesList = [];
73
+ const keys = Object.keys(obj).sort();
74
+ for (const key of keys) {
75
+ const value = obj[key];
76
+ if (isStyledComponentKey(key, value)) {
77
+ const componentClassName = getClassNameFromKeyOrValue(key, value);
78
+ if (!componentClassName) {
79
+ continue;
80
+ }
81
+ const sanitizedClassName = sanitizeCSSSelector(componentClassName);
82
+ if (!sanitizedClassName) {
83
+ continue;
84
+ }
85
+ const componentSelector = selector
86
+ ? `${selector} .${sanitizedClassName}`
87
+ : `.${sanitizedClassName}`;
88
+ const nestedCss = isValidCSSObject(value)
89
+ ? cssObjectToString(value, componentSelector, depth + 1, media)
90
+ : "";
91
+ if (nestedCss) {
92
+ nestedRulesList.push(nestedCss);
93
+ }
94
+ continue;
95
+ }
96
+ if (isValidCSSObject(value)) {
97
+ if (media && key in media) {
98
+ const mediaQuery = sanitizeMediaQuery(media[key]);
99
+ if (mediaQuery) {
100
+ const nestedCss = cssObjectToString(value, selector, depth + 1, media);
101
+ if (nestedCss) {
102
+ nestedRulesList.push(`${mediaQuery} { ${nestedCss} }`);
103
+ }
104
+ }
105
+ }
106
+ else if (key.startsWith("@")) {
107
+ const sanitizedKey = sanitizeCSSSelector(key);
108
+ if (sanitizedKey) {
109
+ const nestedCss = cssObjectToString(value, selector, depth + 1, media);
110
+ if (nestedCss) {
111
+ nestedRulesList.push(`${sanitizedKey} { ${nestedCss} }`);
112
+ }
113
+ }
114
+ }
115
+ else if (key.includes("&")) {
116
+ const sanitizedKey = sanitizeCSSSelector(key);
117
+ if (sanitizedKey) {
118
+ const parts = sanitizedKey.split("&");
119
+ const nestedSelector = parts.join(selector);
120
+ const nestedCss = cssObjectToString(value, nestedSelector, depth + 1, media);
121
+ if (nestedCss) {
122
+ nestedRulesList.push(nestedCss);
123
+ }
124
+ }
125
+ }
126
+ else if (key.startsWith(":")) {
127
+ const sanitizedKey = sanitizeCSSSelector(key);
128
+ if (sanitizedKey) {
129
+ const nestedSelector = `${selector}${sanitizedKey}`;
130
+ const nestedCss = cssObjectToString(value, nestedSelector, depth + 1, media);
131
+ if (nestedCss) {
132
+ nestedRulesList.push(nestedCss);
133
+ }
134
+ }
135
+ }
136
+ else if (key.includes(" ") || key.includes(">") || key.includes("+") || key.includes("~")) {
137
+ const sanitizedKey = sanitizeCSSSelector(key);
138
+ if (sanitizedKey) {
139
+ const startsWithCombinator = /^[\s>+~]/.test(sanitizedKey.trim());
140
+ const nestedSelector = startsWithCombinator
141
+ ? `${selector}${sanitizedKey}`
142
+ : `${selector} ${sanitizedKey}`;
143
+ const nestedCss = cssObjectToString(value, nestedSelector, depth + 1, media);
144
+ if (nestedCss) {
145
+ nestedRulesList.push(nestedCss);
146
+ }
147
+ }
148
+ }
149
+ else {
150
+ const sanitizedKey = sanitizeCSSSelector(key);
151
+ if (sanitizedKey) {
152
+ const nestedSelector = selector ? `${selector} ${sanitizedKey}` : sanitizedKey;
153
+ const nestedCss = cssObjectToString(value, nestedSelector, depth + 1, media);
154
+ if (nestedCss) {
155
+ nestedRulesList.push(nestedCss);
156
+ }
157
+ }
158
+ }
159
+ }
160
+ else if (value !== undefined) {
161
+ const property = sanitizeCSSPropertyName(key);
162
+ if (property && (typeof value === "string" || typeof value === "number")) {
163
+ const escapedValue = escapeCSSValue(value);
164
+ cssProperties.push(`${property}: ${escapedValue};`);
165
+ }
166
+ }
167
+ }
168
+ // Use array join pattern for better performance with large stylesheets
169
+ const parts = [];
170
+ if (cssProperties.length > 0) {
171
+ parts.push(`${selector} { ${cssProperties.join(" ")} }`);
172
+ }
173
+ parts.push(...nestedRulesList);
174
+ return parts.join("");
175
+ }
176
+ /**
177
+ * Compiles CSS objects into CSS strings and generates unique class names.
178
+ * Handles nested selectors, media queries, styled component targeting, and theme tokens.
179
+ *
180
+ * @param styles - CSS object to compile
181
+ * @param currentTheme - Theme for token resolution
182
+ * @param prefix - Optional prefix for generated class names
183
+ * @param media - Optional media query breakpoints
184
+ * @param utils - Optional utility functions
185
+ * @param themeMap - Optional theme scale mappings
186
+ * @returns Generated class name
187
+ */
188
+ export function compileCSS(styles, currentTheme, prefix = "stoop", media, utils, themeMap) {
189
+ const sanitizedPrefix = sanitizePrefix(prefix);
190
+ const stylesWithUtils = applyUtilities(styles, utils);
191
+ const themedStyles = replaceThemeTokensWithVars(stylesWithUtils, currentTheme, themeMap);
192
+ const cssString = cssObjectToString(themedStyles, "", 0, media);
193
+ const stylesHash = hash(cssString);
194
+ const className = sanitizedPrefix ? `${sanitizedPrefix}-${stylesHash}` : `css-${stylesHash}`;
195
+ const cacheKey = `${sanitizedPrefix}:${className}`;
196
+ const cachedCSS = cssStringCache.get(cacheKey);
197
+ if (cachedCSS) {
198
+ injectCSS(cachedCSS, sanitizedPrefix, cacheKey);
199
+ return className;
200
+ }
201
+ const fullCSS = cssObjectToString(themedStyles, `.${className}`, 0, media);
202
+ cssStringCache.set(cacheKey, fullCSS);
203
+ classNameCache.set(cacheKey, className);
204
+ injectCSS(fullCSS, sanitizedPrefix, cacheKey);
205
+ return className;
206
+ }
@@ -21,10 +21,33 @@ export declare function registerDefaultTheme(theme: Theme, prefix?: string): voi
21
21
  */
22
22
  export declare function getDefaultTheme(prefix?: string): Theme | null;
23
23
  /**
24
- * Updates CSS custom properties when theme changes.
25
- * Automatically merges the theme with the default theme to ensure all properties are present.
24
+ * Core theme merging logic.
25
+ * Merges source theme into target theme, handling nested objects.
26
+ * Shared implementation used by both mergeWithDefaultTheme and createTheme.
27
+ *
28
+ * @param target - Target theme to merge into
29
+ * @param source - Source theme to merge from
30
+ * @returns Merged theme
31
+ */
32
+ export declare function mergeThemes(target: Theme, source: Theme | Partial<Theme>): Theme;
33
+ /**
34
+ * Merges a theme with the default theme if it's not already the default theme.
35
+ * This ensures all themes extend the default theme, keeping all original properties.
36
+ * Uses caching to avoid repeated merging of the same theme objects.
37
+ *
38
+ * @param theme - Theme to merge
39
+ * @param prefix - Optional prefix for theme scoping
40
+ * @returns Merged theme (or original if it's the default theme)
41
+ */
42
+ export declare function mergeWithDefaultTheme(theme: Theme, prefix?: string): Theme;
43
+ /**
44
+ * Injects CSS variables for all themes using attribute selectors.
45
+ * This allows all themes to be available simultaneously, with theme switching
46
+ * handled by changing the data-theme attribute. This prevents layout shifts
47
+ * and eliminates the need to replace CSS variables on theme change.
26
48
  *
27
- * @param theme - Theme object to generate CSS variables from
49
+ * @param themes - Map of theme names to theme objects
28
50
  * @param prefix - Optional prefix for CSS variable names
51
+ * @param attribute - Attribute name for theme selection (defaults to 'data-theme')
29
52
  */
30
- export declare function updateThemeVariables(theme: Theme, prefix?: string): void;
53
+ export declare function injectAllThemes(themes: Record<string, Theme>, prefix?: string, attribute?: string): void;
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Theme variable management.
3
+ * Updates CSS custom properties when theme changes.
4
+ * Ensures CSS variables are injected and kept in sync with theme updates.
5
+ * Automatically merges themes with the default theme when applied.
6
+ */
7
+ import { injectAllThemeVariables } from "../inject";
8
+ import { isBrowser } from "../utils/helpers";
9
+ import { generateAllThemeVariables, themesAreEqual } from "../utils/theme";
10
+ const defaultThemes = new Map();
11
+ /**
12
+ * Registers the default theme for a given prefix.
13
+ * Called automatically by createStoop.
14
+ *
15
+ * @param theme - Default theme from createStoop
16
+ * @param prefix - Optional prefix for theme scoping
17
+ */
18
+ export function registerDefaultTheme(theme, prefix = "stoop") {
19
+ const sanitizedPrefix = prefix || "";
20
+ defaultThemes.set(sanitizedPrefix, theme);
21
+ }
22
+ /**
23
+ * Gets the default theme for a given prefix.
24
+ *
25
+ * @param prefix - Optional prefix for theme scoping
26
+ * @returns Default theme or null if not registered
27
+ */
28
+ export function getDefaultTheme(prefix = "stoop") {
29
+ const sanitizedPrefix = prefix || "";
30
+ return defaultThemes.get(sanitizedPrefix) || null;
31
+ }
32
+ /**
33
+ * Core theme merging logic.
34
+ * Merges source theme into target theme, handling nested objects.
35
+ * Shared implementation used by both mergeWithDefaultTheme and createTheme.
36
+ *
37
+ * @param target - Target theme to merge into
38
+ * @param source - Source theme to merge from
39
+ * @returns Merged theme
40
+ */
41
+ export function mergeThemes(target, source) {
42
+ const merged = { ...target };
43
+ const sourceKeys = Object.keys(source);
44
+ for (const key of sourceKeys) {
45
+ if (key === "media") {
46
+ continue;
47
+ }
48
+ const sourceValue = source[key];
49
+ const targetValue = target[key];
50
+ if (sourceValue &&
51
+ typeof sourceValue === "object" &&
52
+ !Array.isArray(sourceValue) &&
53
+ targetValue &&
54
+ typeof targetValue === "object" &&
55
+ !Array.isArray(targetValue)) {
56
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
57
+ merged[key] = { ...targetValue, ...sourceValue };
58
+ }
59
+ else if (sourceValue !== undefined) {
60
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
61
+ merged[key] = sourceValue;
62
+ }
63
+ }
64
+ return merged;
65
+ }
66
+ /**
67
+ * Merges a theme with the default theme if it's not already the default theme.
68
+ * This ensures all themes extend the default theme, keeping all original properties.
69
+ * Uses caching to avoid repeated merging of the same theme objects.
70
+ *
71
+ * @param theme - Theme to merge
72
+ * @param prefix - Optional prefix for theme scoping
73
+ * @returns Merged theme (or original if it's the default theme)
74
+ */
75
+ export function mergeWithDefaultTheme(theme, prefix = "stoop") {
76
+ const defaultTheme = getDefaultTheme(prefix);
77
+ if (!defaultTheme) {
78
+ return theme;
79
+ }
80
+ if (themesAreEqual(theme, defaultTheme)) {
81
+ return theme;
82
+ }
83
+ // Merge theme with default theme
84
+ return mergeThemes(defaultTheme, theme);
85
+ }
86
+ /**
87
+ * Injects CSS variables for all themes using attribute selectors.
88
+ * This allows all themes to be available simultaneously, with theme switching
89
+ * handled by changing the data-theme attribute. This prevents layout shifts
90
+ * and eliminates the need to replace CSS variables on theme change.
91
+ *
92
+ * @param themes - Map of theme names to theme objects
93
+ * @param prefix - Optional prefix for CSS variable names
94
+ * @param attribute - Attribute name for theme selection (defaults to 'data-theme')
95
+ */
96
+ export function injectAllThemes(themes, prefix = "stoop", attribute = "data-theme") {
97
+ if (!isBrowser()) {
98
+ return;
99
+ }
100
+ // Merge all themes with default theme
101
+ const mergedThemes = {};
102
+ for (const [themeName, theme] of Object.entries(themes)) {
103
+ mergedThemes[themeName] = mergeWithDefaultTheme(theme, prefix);
104
+ }
105
+ const allThemeVars = generateAllThemeVariables(mergedThemes, prefix, attribute);
106
+ injectAllThemeVariables(allThemeVars, prefix);
107
+ }