stoop 0.4.0 → 0.5.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 +9 -4
- package/dist/api/core-api.d.ts +1 -0
- package/dist/api/styled.d.ts +2 -8
- package/dist/core/cache.d.ts +0 -1
- package/dist/core/stringify.d.ts +19 -0
- package/dist/core/theme-manager.d.ts +3 -11
- package/dist/create-stoop-internal.d.ts +3 -5
- package/dist/create-stoop-ssr.d.ts +0 -2
- package/dist/create-stoop-ssr.js +16 -26
- package/dist/create-stoop.d.ts +3 -7
- package/dist/create-stoop.js +16 -156
- package/dist/inject.d.ts +2 -4
- package/dist/types/index.d.ts +20 -9
- package/dist/utils/helpers.d.ts +0 -3
- package/dist/utils/storage.d.ts +2 -95
- package/dist/utils/theme-utils.d.ts +0 -13
- package/dist/utils/theme.d.ts +1 -3
- package/package.json +11 -13
- package/dist/api/core-api.js +0 -135
- package/dist/api/global-css.d.ts +0 -7
- package/dist/api/global-css.js +0 -42
- package/dist/api/styled.js +0 -419
- package/dist/api/theme-provider.js +0 -223
- package/dist/constants.js +0 -154
- package/dist/core/cache.js +0 -68
- package/dist/core/compiler.js +0 -206
- package/dist/core/theme-manager.js +0 -107
- package/dist/core/variants.d.ts +0 -15
- package/dist/core/variants.js +0 -38
- package/dist/create-stoop-internal.js +0 -123
- package/dist/inject.js +0 -308
- package/dist/types/index.js +0 -5
- package/dist/utils/auto-preload.d.ts +0 -45
- package/dist/utils/auto-preload.js +0 -167
- package/dist/utils/helpers.js +0 -150
- package/dist/utils/storage.js +0 -396
- package/dist/utils/theme-utils.js +0 -353
- package/dist/utils/theme.js +0 -304
package/dist/types/index.d.ts
CHANGED
|
@@ -26,6 +26,17 @@ export type StylableElement = HTMLElements | ElementType;
|
|
|
26
26
|
export interface StyledBaseProps {
|
|
27
27
|
css?: CSS;
|
|
28
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* CSS object that includes variants configuration.
|
|
31
|
+
* Used for styled component definitions that combine base styles with variants.
|
|
32
|
+
* This type explicitly requires the `variants` property to be present and of type Variants.
|
|
33
|
+
* We exclude variants from the CSS index signature to make it distinct.
|
|
34
|
+
*/
|
|
35
|
+
export type CSSWithVariants = {
|
|
36
|
+
[K in keyof CSS as K extends "variants" ? never : K]: CSS[K];
|
|
37
|
+
} & {
|
|
38
|
+
variants: Variants;
|
|
39
|
+
};
|
|
29
40
|
export type UtilityFunction = (value: CSSPropertyValue | CSS | undefined) => CSS;
|
|
30
41
|
/**
|
|
31
42
|
* Theme scale type - represents valid keys from DefaultTheme.
|
|
@@ -95,10 +106,10 @@ export interface StoopServerInstance {
|
|
|
95
106
|
};
|
|
96
107
|
createTheme: (themeOverride: Partial<Theme>) => Theme;
|
|
97
108
|
css: (styles: CSS) => string;
|
|
98
|
-
getCssText: (
|
|
109
|
+
getCssText: () => string;
|
|
99
110
|
globalCss: (...args: CSS[]) => () => void;
|
|
100
111
|
keyframes: (definition: Record<string, CSS>) => string;
|
|
101
|
-
preloadTheme: (
|
|
112
|
+
preloadTheme: () => void;
|
|
102
113
|
theme: Theme;
|
|
103
114
|
warmCache: (styles: CSS[]) => void;
|
|
104
115
|
}
|
|
@@ -194,9 +205,11 @@ export type StyledComponent<DefaultElement extends ElementType, VariantsConfig e
|
|
|
194
205
|
};
|
|
195
206
|
/**
|
|
196
207
|
* Styled function type - the main styled() function signature
|
|
208
|
+
* Variants must be embedded in the baseStyles object, matching Stitches API.
|
|
197
209
|
*/
|
|
198
210
|
export interface StyledFunction {
|
|
199
|
-
<DefaultElement extends StylableElement,
|
|
211
|
+
<DefaultElement extends StylableElement, BaseStyles extends CSSWithVariants>(defaultElement: DefaultElement, baseStyles: BaseStyles): StyledComponent<DefaultElement, BaseStyles["variants"]>;
|
|
212
|
+
<DefaultElement extends StylableElement>(defaultElement: DefaultElement, baseStyles?: CSS): StyledComponent<DefaultElement, {}>;
|
|
200
213
|
}
|
|
201
214
|
export interface GetCssTextOptions {
|
|
202
215
|
theme?: Theme;
|
|
@@ -248,10 +261,9 @@ export interface StoopInstance {
|
|
|
248
261
|
* Gets all generated CSS text for server-side rendering.
|
|
249
262
|
* Always includes theme CSS variables.
|
|
250
263
|
*
|
|
251
|
-
* @param theme - Deprecated parameter, kept for backward compatibility but ignored
|
|
252
264
|
* @returns CSS text string with theme variables and component styles
|
|
253
265
|
*/
|
|
254
|
-
getCssText: (
|
|
266
|
+
getCssText: () => string;
|
|
255
267
|
config: StoopConfig;
|
|
256
268
|
/**
|
|
257
269
|
* Pre-compiles CSS objects to warm the cache.
|
|
@@ -261,12 +273,11 @@ export interface StoopInstance {
|
|
|
261
273
|
*/
|
|
262
274
|
warmCache: (styles: CSS[]) => void;
|
|
263
275
|
/**
|
|
264
|
-
* Preloads
|
|
276
|
+
* Preloads themes by injecting CSS variables before React renders.
|
|
265
277
|
* Useful for preventing FOUC when loading a non-default theme from localStorage.
|
|
266
|
-
*
|
|
267
|
-
* @param theme - Theme to preload (theme name string or Theme object)
|
|
278
|
+
* Always injects all configured themes.
|
|
268
279
|
*/
|
|
269
|
-
preloadTheme: (
|
|
280
|
+
preloadTheme: () => void;
|
|
270
281
|
/**
|
|
271
282
|
* Provider component for managing theme state and updates.
|
|
272
283
|
* Available when themes are provided in createStoop config.
|
package/dist/utils/helpers.d.ts
CHANGED
|
@@ -11,7 +11,6 @@ import type { CSS, DefaultTheme, StyledComponentRef, Theme, UtilityFunction } fr
|
|
|
11
11
|
export declare function isBrowser(): boolean;
|
|
12
12
|
/**
|
|
13
13
|
* Checks if running in production mode.
|
|
14
|
-
* Extracted to helper function for consistency.
|
|
15
14
|
*
|
|
16
15
|
* @returns True if running in production mode
|
|
17
16
|
*/
|
|
@@ -25,7 +24,6 @@ export declare function isProduction(): boolean;
|
|
|
25
24
|
export declare function isCSSObject(value: unknown): value is CSS;
|
|
26
25
|
/**
|
|
27
26
|
* Checks if a value is a styled component reference.
|
|
28
|
-
* Consolidated function used across the codebase.
|
|
29
27
|
*
|
|
30
28
|
* @param value - Value to check
|
|
31
29
|
* @returns True if value is a styled component reference
|
|
@@ -47,7 +45,6 @@ export declare function isValidCSSObject(value: unknown): value is CSS;
|
|
|
47
45
|
export declare function isThemeObject(value: unknown): value is Theme;
|
|
48
46
|
/**
|
|
49
47
|
* Validates that a theme object only contains approved scales.
|
|
50
|
-
* In production, skips all validation for performance.
|
|
51
48
|
*
|
|
52
49
|
* @param theme - Theme object to validate
|
|
53
50
|
* @returns Validated theme as DefaultTheme
|
package/dist/utils/storage.d.ts
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Storage and theme detection utilities.
|
|
3
|
-
* Provides simplified localStorage and cookie management
|
|
4
|
-
* Supports SSR compatibility and error handling.
|
|
3
|
+
* Provides simplified localStorage and cookie management with SSR compatibility and error handling.
|
|
5
4
|
*/
|
|
6
|
-
import type {
|
|
5
|
+
import type { StorageOptions, StorageResult } from "../types";
|
|
7
6
|
/**
|
|
8
7
|
* Safely gets a value from localStorage.
|
|
9
8
|
*
|
|
@@ -54,95 +53,3 @@ export declare function setCookie(name: string, value: string, options?: Omit<St
|
|
|
54
53
|
* @returns Success status
|
|
55
54
|
*/
|
|
56
55
|
export declare function removeCookie(name: string, path?: string): boolean;
|
|
57
|
-
/**
|
|
58
|
-
* Unified storage API that works with both localStorage and cookies.
|
|
59
|
-
*
|
|
60
|
-
* @param key - Storage key
|
|
61
|
-
* @param options - Storage options
|
|
62
|
-
* @returns Storage result
|
|
63
|
-
*/
|
|
64
|
-
export declare function getStorage(key: string, options?: StorageOptions): StorageResult<string | null>;
|
|
65
|
-
/**
|
|
66
|
-
* Unified storage API that works with both localStorage and cookies.
|
|
67
|
-
*
|
|
68
|
-
* @param key - Storage key
|
|
69
|
-
* @param value - Value to store
|
|
70
|
-
* @param options - Storage options
|
|
71
|
-
* @returns Storage result
|
|
72
|
-
*/
|
|
73
|
-
export declare function setStorage(key: string, value: string, options?: StorageOptions): StorageResult<void>;
|
|
74
|
-
/**
|
|
75
|
-
* Unified storage API that works with both localStorage and cookies.
|
|
76
|
-
*
|
|
77
|
-
* @param key - Storage key
|
|
78
|
-
* @param options - Storage options
|
|
79
|
-
* @returns Storage result
|
|
80
|
-
*/
|
|
81
|
-
export declare function removeStorage(key: string, options?: StorageOptions): StorageResult<void>;
|
|
82
|
-
/**
|
|
83
|
-
* Gets a JSON value from storage with automatic parsing.
|
|
84
|
-
*
|
|
85
|
-
* @param key - Storage key
|
|
86
|
-
* @param fallback - Fallback value if parsing fails or key not found
|
|
87
|
-
* @param options - Storage options
|
|
88
|
-
* @returns Parsed JSON value or fallback
|
|
89
|
-
*/
|
|
90
|
-
export declare function getJsonFromStorage<T>(key: string, fallback: T, options?: StorageOptions): StorageResult<T>;
|
|
91
|
-
/**
|
|
92
|
-
* Sets a JSON value in storage with automatic serialization.
|
|
93
|
-
*
|
|
94
|
-
* @param key - Storage key
|
|
95
|
-
* @param value - Value to serialize and store
|
|
96
|
-
* @param options - Storage options
|
|
97
|
-
* @returns Storage result
|
|
98
|
-
*/
|
|
99
|
-
export declare function setJsonInStorage<T>(key: string, value: T, options?: StorageOptions): StorageResult<void>;
|
|
100
|
-
/**
|
|
101
|
-
* Creates a typed storage interface for a specific key.
|
|
102
|
-
*
|
|
103
|
-
* @param key - Storage key
|
|
104
|
-
* @param options - Storage options
|
|
105
|
-
* @returns Typed storage interface
|
|
106
|
-
*/
|
|
107
|
-
export declare function createStorage<T = string>(key: string, options?: StorageOptions): {
|
|
108
|
-
get: () => StorageResult<string | null>;
|
|
109
|
-
getJson: (fallback: T) => StorageResult<T>;
|
|
110
|
-
remove: () => StorageResult<void>;
|
|
111
|
-
set: (value: string) => StorageResult<void>;
|
|
112
|
-
setJson: (value: T) => StorageResult<void>;
|
|
113
|
-
};
|
|
114
|
-
/**
|
|
115
|
-
* Detects the best theme to use based on multiple sources with priority.
|
|
116
|
-
*
|
|
117
|
-
* Priority order (highest to lowest):
|
|
118
|
-
* 1. Explicit localStorage preference
|
|
119
|
-
* 2. Cookie preference (for SSR compatibility)
|
|
120
|
-
* 3. System color scheme preference
|
|
121
|
-
* 4. Default theme
|
|
122
|
-
*
|
|
123
|
-
* @param options - Theme detection options
|
|
124
|
-
* @returns Theme detection result
|
|
125
|
-
*/
|
|
126
|
-
export declare function detectTheme(options?: ThemeDetectionOptions): ThemeDetectionResult;
|
|
127
|
-
/**
|
|
128
|
-
* Creates a theme detector function with pre-configured options.
|
|
129
|
-
*
|
|
130
|
-
* @param options - Theme detection options
|
|
131
|
-
* @returns Theme detection function
|
|
132
|
-
*/
|
|
133
|
-
export declare function createThemeDetector(options: ThemeDetectionOptions): () => ThemeDetectionResult;
|
|
134
|
-
/**
|
|
135
|
-
* Auto-detects theme for SSR contexts (server-side or during hydration).
|
|
136
|
-
* Uses only cookie and default sources since localStorage isn't available.
|
|
137
|
-
*
|
|
138
|
-
* @param options - Theme detection options
|
|
139
|
-
* @returns Theme name
|
|
140
|
-
*/
|
|
141
|
-
export declare function detectThemeForSSR(options?: ThemeDetectionOptions): string;
|
|
142
|
-
/**
|
|
143
|
-
* Listens for system theme changes and calls callback when changed.
|
|
144
|
-
*
|
|
145
|
-
* @param callback - Function to call when system theme changes
|
|
146
|
-
* @returns Cleanup function
|
|
147
|
-
*/
|
|
148
|
-
export declare function onSystemThemeChange(callback: (theme: "dark" | "light") => void): () => void;
|
|
@@ -36,7 +36,6 @@ export declare function escapeCSSValue(value: string | number): string;
|
|
|
36
36
|
/**
|
|
37
37
|
* Validates and sanitizes CSS selectors to prevent injection attacks.
|
|
38
38
|
* Only allows safe selector characters. Returns empty string for invalid selectors.
|
|
39
|
-
* Uses memoization for performance.
|
|
40
39
|
*
|
|
41
40
|
* @param selector - Selector to sanitize
|
|
42
41
|
* @returns Sanitized selector string or empty string if invalid
|
|
@@ -45,7 +44,6 @@ export declare function sanitizeCSSSelector(selector: string): string;
|
|
|
45
44
|
/**
|
|
46
45
|
* Validates and sanitizes CSS variable names to prevent injection attacks.
|
|
47
46
|
* CSS custom properties must start with -- and contain only valid characters.
|
|
48
|
-
* Uses memoization for performance.
|
|
49
47
|
*
|
|
50
48
|
* @param name - Variable name to sanitize
|
|
51
49
|
* @returns Sanitized variable name
|
|
@@ -78,20 +76,11 @@ export declare function sanitizeMediaQuery(mediaQuery: string): string;
|
|
|
78
76
|
/**
|
|
79
77
|
* Sanitizes CSS class names to prevent injection attacks.
|
|
80
78
|
* Only allows valid CSS class name characters.
|
|
81
|
-
* Uses memoization for performance.
|
|
82
79
|
*
|
|
83
80
|
* @param className - Class name(s) to sanitize
|
|
84
81
|
* @returns Sanitized class name string
|
|
85
82
|
*/
|
|
86
83
|
export declare function sanitizeClassName(className: string): string;
|
|
87
|
-
/**
|
|
88
|
-
* Sanitizes CSS property names to prevent injection attacks.
|
|
89
|
-
* Uses memoization for performance.
|
|
90
|
-
*
|
|
91
|
-
* @param propertyName - Property name to sanitize
|
|
92
|
-
* @returns Sanitized property name
|
|
93
|
-
*/
|
|
94
|
-
export declare function sanitizeCSSPropertyName(propertyName: string): string;
|
|
95
84
|
/**
|
|
96
85
|
* Validates keyframe percentage keys (e.g., "0%", "50%", "from", "to").
|
|
97
86
|
*
|
|
@@ -101,7 +90,6 @@ export declare function sanitizeCSSPropertyName(propertyName: string): string;
|
|
|
101
90
|
export declare function validateKeyframeKey(key: string): boolean;
|
|
102
91
|
/**
|
|
103
92
|
* Gets a pre-compiled regex for matching :root CSS selector blocks.
|
|
104
|
-
* Uses caching for performance.
|
|
105
93
|
*
|
|
106
94
|
* @param prefix - Optional prefix (unused, kept for API compatibility)
|
|
107
95
|
* @returns RegExp for matching :root selector blocks
|
|
@@ -109,7 +97,6 @@ export declare function validateKeyframeKey(key: string): boolean;
|
|
|
109
97
|
export declare function getRootRegex(prefix?: string): RegExp;
|
|
110
98
|
/**
|
|
111
99
|
* Auto-detects theme scale from CSS property name using pattern matching.
|
|
112
|
-
* Used as fallback when property is not in DEFAULT_THEME_MAP.
|
|
113
100
|
*
|
|
114
101
|
* @param property - CSS property name
|
|
115
102
|
* @returns Theme scale name or undefined if no pattern matches
|
package/dist/utils/theme.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Theme token resolution utilities.
|
|
3
3
|
* Converts theme tokens to CSS variables for runtime theme switching.
|
|
4
|
-
* Uses cached token index for efficient lookups and theme comparison.
|
|
5
4
|
*/
|
|
6
5
|
import type { CSS, Theme, ThemeScale } from "../types";
|
|
7
6
|
/**
|
|
@@ -34,8 +33,7 @@ export declare function tokenToCSSVar(token: string, theme?: Theme, property?: s
|
|
|
34
33
|
export declare function generateCSSVariables(theme: Theme, prefix?: string, attributeSelector?: string): string;
|
|
35
34
|
/**
|
|
36
35
|
* Generates CSS custom properties for all themes using attribute selectors.
|
|
37
|
-
*
|
|
38
|
-
* handled by changing the data-theme attribute.
|
|
36
|
+
* All themes are available simultaneously, with theme switching handled by changing the data-theme attribute.
|
|
39
37
|
*
|
|
40
38
|
* @param themes - Map of theme names to theme objects
|
|
41
39
|
* @param prefix - Optional prefix for CSS variable names
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "stoop",
|
|
3
3
|
"description": "CSS-in-JS library with type inference, theme creation, and variants support.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.5.0",
|
|
5
5
|
"author": "Jackson Dolman <mail@dolmios.com>",
|
|
6
6
|
"main": "./dist/create-stoop.js",
|
|
7
7
|
"types": "./dist/create-stoop.d.ts",
|
|
@@ -17,10 +17,13 @@
|
|
|
17
17
|
"react-polymorphic-types": "^2.0.0"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
+
"@stitches/stringify": "^1.2.8",
|
|
20
21
|
"@types/node": "^25.0.3",
|
|
21
22
|
"@types/react": "^19.2.7",
|
|
22
23
|
"@types/react-dom": "^19.2.3",
|
|
23
24
|
"bun-types": "^1.3.5",
|
|
25
|
+
"style-object-to-css-string": "^1.1.3",
|
|
26
|
+
"to-css": "^1.2.1",
|
|
24
27
|
"typescript": "^5.9.3"
|
|
25
28
|
},
|
|
26
29
|
"exports": {
|
|
@@ -36,12 +39,6 @@
|
|
|
36
39
|
"require": "./dist/utils/storage.js",
|
|
37
40
|
"default": "./dist/utils/storage.js"
|
|
38
41
|
},
|
|
39
|
-
"./utils/auto-preload": {
|
|
40
|
-
"types": "./dist/utils/auto-preload.d.ts",
|
|
41
|
-
"import": "./dist/utils/auto-preload.js",
|
|
42
|
-
"require": "./dist/utils/auto-preload.js",
|
|
43
|
-
"default": "./dist/utils/auto-preload.js"
|
|
44
|
-
},
|
|
45
42
|
"./types": {
|
|
46
43
|
"types": "./dist/types/index.d.ts",
|
|
47
44
|
"import": "./dist/types/index.js",
|
|
@@ -96,18 +93,19 @@
|
|
|
96
93
|
"react": ">=16.8.0",
|
|
97
94
|
"react-dom": ">=16.8.0"
|
|
98
95
|
},
|
|
99
|
-
"sideEffects": false,
|
|
100
|
-
"type": "module",
|
|
101
96
|
"scripts": {
|
|
102
97
|
"build": "bun run build:clean && bun run build:js && bun run build:js:ssr && bun run build:types",
|
|
103
98
|
"build:clean": "rm -rf dist",
|
|
104
99
|
"build:copy-declarations": "mkdir -p dist/types && cp src/types/react-polymorphic-types.d.ts dist/types/react-polymorphic-types.d.ts 2>/dev/null || :",
|
|
105
|
-
"build:js": "NODE_ENV=production bun build ./src/create-stoop.ts --
|
|
106
|
-
"build:js:ssr": "NODE_ENV=production bun build ./src/create-stoop-ssr.ts --
|
|
100
|
+
"build:js": "NODE_ENV=production bun build ./src/create-stoop.ts --outfile ./dist/create-stoop.js --target browser --format esm --minify --jsx=automatic --external react --external react-dom --external react/jsx-runtime --define process.env.NODE_ENV='\"production\"'",
|
|
101
|
+
"build:js:ssr": "NODE_ENV=production bun build ./src/create-stoop-ssr.ts --outfile ./dist/create-stoop-ssr.js --target browser --format esm --minify --jsx=automatic --external react --external react-dom --external react/jsx-runtime --define process.env.NODE_ENV='\"production\"'",
|
|
107
102
|
"build:types": "bunx tsc --project tsconfig.build.json && bun run build:copy-declarations",
|
|
108
103
|
"lint": "eslint src --fix",
|
|
104
|
+
"prepare": "bun run build",
|
|
109
105
|
"test": "bun test",
|
|
110
106
|
"test:coverage": "bun test --coverage",
|
|
111
107
|
"test:watch": "bun test --watch"
|
|
112
|
-
}
|
|
113
|
-
|
|
108
|
+
},
|
|
109
|
+
"sideEffects": false,
|
|
110
|
+
"type": "module"
|
|
111
|
+
}
|
package/dist/api/core-api.js
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Core API functions.
|
|
3
|
-
* Consolidates theme creation, CSS class generation, and keyframes animation APIs.
|
|
4
|
-
*/
|
|
5
|
-
import { LRUCache } from "../core/cache";
|
|
6
|
-
import { compileCSS } from "../core/compiler";
|
|
7
|
-
import { mergeThemes } from "../core/theme-manager";
|
|
8
|
-
import { injectCSS } from "../inject";
|
|
9
|
-
import { validateTheme } from "../utils/helpers";
|
|
10
|
-
import { replaceThemeTokensWithVars } from "../utils/theme";
|
|
11
|
-
import { hashObject, sanitizeCSSPropertyName, sanitizePrefix, validateKeyframeKey, } from "../utils/theme-utils";
|
|
12
|
-
// ============================================================================
|
|
13
|
-
// Theme Creation API
|
|
14
|
-
// ============================================================================
|
|
15
|
-
/**
|
|
16
|
-
* Creates a function that extends a base theme with overrides.
|
|
17
|
-
* The returned function deep merges theme overrides with the base theme.
|
|
18
|
-
*
|
|
19
|
-
* @param baseTheme - Base theme to extend
|
|
20
|
-
* @returns Function that accepts theme overrides and returns a merged theme
|
|
21
|
-
*/
|
|
22
|
-
export function createTheme(baseTheme) {
|
|
23
|
-
return function createTheme(themeOverrides = {}) {
|
|
24
|
-
const validatedOverrides = validateTheme(themeOverrides);
|
|
25
|
-
// Use shared mergeThemes function instead of duplicate deepMerge
|
|
26
|
-
return mergeThemes(baseTheme, validatedOverrides);
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
// ============================================================================
|
|
30
|
-
// CSS Class Generation API
|
|
31
|
-
// ============================================================================
|
|
32
|
-
/**
|
|
33
|
-
* Creates a CSS function that compiles CSS objects into class names.
|
|
34
|
-
*
|
|
35
|
-
* @param defaultTheme - Default theme for token resolution
|
|
36
|
-
* @param prefix - Optional prefix for generated class names
|
|
37
|
-
* @param media - Optional media query breakpoints
|
|
38
|
-
* @param utils - Optional utility functions
|
|
39
|
-
* @param themeMap - Optional theme scale mappings
|
|
40
|
-
* @returns Function that accepts CSS objects and returns class names
|
|
41
|
-
*/
|
|
42
|
-
export function createCSSFunction(defaultTheme, prefix = "stoop", media, utils, themeMap) {
|
|
43
|
-
return function css(styles) {
|
|
44
|
-
return compileCSS(styles, defaultTheme, prefix, media, utils, themeMap);
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
// ============================================================================
|
|
48
|
-
// Keyframes Animation API
|
|
49
|
-
// ============================================================================
|
|
50
|
-
/**
|
|
51
|
-
* Converts a keyframes object to a CSS @keyframes rule string.
|
|
52
|
-
*
|
|
53
|
-
* @param keyframesObj - Keyframes object with percentage/from/to keys
|
|
54
|
-
* @param animationName - Name for the animation
|
|
55
|
-
* @param theme - Optional theme for token resolution
|
|
56
|
-
* @param themeMap - Optional theme scale mappings
|
|
57
|
-
* @returns CSS @keyframes rule string
|
|
58
|
-
*/
|
|
59
|
-
function keyframesToCSS(keyframesObj, animationName, theme, themeMap) {
|
|
60
|
-
let css = `@keyframes ${animationName} {`;
|
|
61
|
-
const sortedKeys = Object.keys(keyframesObj).sort((a, b) => {
|
|
62
|
-
const aNum = parseFloat(a.replace("%", ""));
|
|
63
|
-
const bNum = parseFloat(b.replace("%", ""));
|
|
64
|
-
if (a === "from") {
|
|
65
|
-
return -1;
|
|
66
|
-
}
|
|
67
|
-
if (b === "from") {
|
|
68
|
-
return 1;
|
|
69
|
-
}
|
|
70
|
-
if (a === "to") {
|
|
71
|
-
return 1;
|
|
72
|
-
}
|
|
73
|
-
if (b === "to") {
|
|
74
|
-
return -1;
|
|
75
|
-
}
|
|
76
|
-
return aNum - bNum;
|
|
77
|
-
});
|
|
78
|
-
for (const key of sortedKeys) {
|
|
79
|
-
if (!validateKeyframeKey(key)) {
|
|
80
|
-
continue;
|
|
81
|
-
}
|
|
82
|
-
const styles = keyframesObj[key];
|
|
83
|
-
if (!styles || typeof styles !== "object") {
|
|
84
|
-
continue;
|
|
85
|
-
}
|
|
86
|
-
css += ` ${key} {`;
|
|
87
|
-
const themedStyles = replaceThemeTokensWithVars(styles, theme, themeMap);
|
|
88
|
-
// Sort properties for deterministic CSS generation
|
|
89
|
-
const sortedProps = Object.keys(themedStyles).sort();
|
|
90
|
-
for (const prop of sortedProps) {
|
|
91
|
-
const value = themedStyles[prop];
|
|
92
|
-
if (value !== undefined && (typeof value === "string" || typeof value === "number")) {
|
|
93
|
-
const sanitizedProp = sanitizeCSSPropertyName(prop);
|
|
94
|
-
if (sanitizedProp) {
|
|
95
|
-
// Don't escape keyframe values - escaping breaks complex CSS functions
|
|
96
|
-
const cssValue = String(value);
|
|
97
|
-
css += ` ${sanitizedProp}: ${cssValue};`;
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
css += " }";
|
|
102
|
-
}
|
|
103
|
-
css += " }";
|
|
104
|
-
return css;
|
|
105
|
-
}
|
|
106
|
-
import { KEYFRAME_CACHE_LIMIT } from "../constants";
|
|
107
|
-
/**
|
|
108
|
-
* Creates a keyframes animation function.
|
|
109
|
-
* Generates and injects @keyframes rules with caching to prevent duplicates.
|
|
110
|
-
*
|
|
111
|
-
* @param prefix - Optional prefix for animation names
|
|
112
|
-
* @param theme - Optional theme for token resolution
|
|
113
|
-
* @param themeMap - Optional theme scale mappings
|
|
114
|
-
* @returns Function that accepts keyframes objects and returns animation names
|
|
115
|
-
*/
|
|
116
|
-
export function createKeyframesFunction(prefix = "stoop", theme, themeMap) {
|
|
117
|
-
const sanitizedPrefix = sanitizePrefix(prefix);
|
|
118
|
-
const animationCache = new LRUCache(KEYFRAME_CACHE_LIMIT);
|
|
119
|
-
return function keyframes(keyframesObj) {
|
|
120
|
-
const keyframesKey = hashObject(keyframesObj);
|
|
121
|
-
const cachedName = animationCache.get(keyframesKey);
|
|
122
|
-
if (cachedName) {
|
|
123
|
-
return cachedName;
|
|
124
|
-
}
|
|
125
|
-
const hashValue = keyframesKey.slice(0, 8);
|
|
126
|
-
const animationName = sanitizedPrefix
|
|
127
|
-
? `${sanitizedPrefix}-${hashValue}`
|
|
128
|
-
: `stoop-${hashValue}`;
|
|
129
|
-
const css = keyframesToCSS(keyframesObj, animationName, theme, themeMap);
|
|
130
|
-
const ruleKey = `__keyframes_${animationName}`;
|
|
131
|
-
injectCSS(css, sanitizedPrefix, ruleKey);
|
|
132
|
-
animationCache.set(keyframesKey, animationName);
|
|
133
|
-
return animationName;
|
|
134
|
-
};
|
|
135
|
-
}
|
package/dist/api/global-css.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Global CSS injection API.
|
|
3
|
-
* Creates a function that injects global styles into the document.
|
|
4
|
-
* Supports media queries, nested selectors, and theme tokens.
|
|
5
|
-
*/
|
|
6
|
-
import type { CSS, Theme, ThemeScale, UtilityFunction } from "../types";
|
|
7
|
-
export declare function createGlobalCSSFunction(defaultTheme: Theme, prefix?: string, media?: Record<string, string>, utils?: Record<string, UtilityFunction>, themeMap?: Record<string, ThemeScale>): (styles: CSS) => () => void;
|
package/dist/api/global-css.js
DELETED
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Global CSS injection API.
|
|
3
|
-
* Creates a function that injects global styles into the document.
|
|
4
|
-
* Supports media queries, nested selectors, and theme tokens.
|
|
5
|
-
*/
|
|
6
|
-
import { cssObjectToString } from "../core/compiler";
|
|
7
|
-
import { injectCSS } from "../inject";
|
|
8
|
-
import { applyUtilities } from "../utils/helpers";
|
|
9
|
-
import { replaceThemeTokensWithVars } from "../utils/theme";
|
|
10
|
-
import { hashObject, sanitizePrefix } from "../utils/theme-utils";
|
|
11
|
-
/**
|
|
12
|
-
* Creates a global CSS injection function.
|
|
13
|
-
* Injects styles directly into the document with deduplication support.
|
|
14
|
-
*
|
|
15
|
-
* @param defaultTheme - Default theme for token resolution
|
|
16
|
-
* @param prefix - Optional prefix for CSS rules
|
|
17
|
-
* @param media - Optional media query breakpoints
|
|
18
|
-
* @param utils - Optional utility functions
|
|
19
|
-
* @param themeMap - Optional theme scale mappings
|
|
20
|
-
* @returns Function that accepts CSS objects and returns a cleanup function
|
|
21
|
-
*/
|
|
22
|
-
// Use CSS hash as key instead of theme object reference for better deduplication
|
|
23
|
-
const globalInjectedStyles = new Set();
|
|
24
|
-
export function createGlobalCSSFunction(defaultTheme, prefix = "stoop", media, utils, themeMap) {
|
|
25
|
-
return function globalCss(styles) {
|
|
26
|
-
const cssKey = hashObject(styles);
|
|
27
|
-
if (globalInjectedStyles.has(cssKey)) {
|
|
28
|
-
return () => { };
|
|
29
|
-
}
|
|
30
|
-
globalInjectedStyles.add(cssKey);
|
|
31
|
-
const sanitizedPrefix = sanitizePrefix(prefix);
|
|
32
|
-
const stylesWithUtils = applyUtilities(styles, utils);
|
|
33
|
-
const themedStyles = replaceThemeTokensWithVars(stylesWithUtils, defaultTheme, themeMap);
|
|
34
|
-
// Use cssObjectToString from compiler.ts instead of duplicate generateGlobalCSS
|
|
35
|
-
// Empty selector for global CSS (no base selector needed)
|
|
36
|
-
const cssText = cssObjectToString(themedStyles, "", 0, media);
|
|
37
|
-
injectCSS(cssText, sanitizedPrefix, `__global_${cssKey}`);
|
|
38
|
-
return () => {
|
|
39
|
-
globalInjectedStyles.delete(cssKey);
|
|
40
|
-
};
|
|
41
|
-
};
|
|
42
|
-
}
|