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,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal implementation for creating Stoop instances.
|
|
3
|
+
* This file is used by the SSR entry point and does NOT import React types at module level.
|
|
4
|
+
* React types are only imported conditionally when creating client instances.
|
|
5
|
+
*/
|
|
6
|
+
import { createTheme as createThemeFactory, createCSSFunction, createKeyframesFunction, } from "./api/core-api";
|
|
7
|
+
import { createGlobalCSSFunction } from "./api/global-css";
|
|
8
|
+
// Note: React APIs (styled, Provider) are NOT imported here for SSR safety
|
|
9
|
+
import { DEFAULT_THEME_MAP } from "./constants";
|
|
10
|
+
import { compileCSS } from "./core/compiler";
|
|
11
|
+
import { mergeWithDefaultTheme, registerDefaultTheme, injectAllThemes } from "./core/theme-manager";
|
|
12
|
+
import { getCssText as getCssTextBase, removeThemeVariableBlocks } from "./inject";
|
|
13
|
+
import { validateTheme } from "./utils/helpers";
|
|
14
|
+
import { generateAllThemeVariables, generateCSSVariables } from "./utils/theme";
|
|
15
|
+
import { sanitizePrefix } from "./utils/theme-utils";
|
|
16
|
+
/**
|
|
17
|
+
* Shared implementation for creating Stoop instances.
|
|
18
|
+
* Handles common setup logic for both client and server instances.
|
|
19
|
+
* Exported for use in SSR entry point.
|
|
20
|
+
*/
|
|
21
|
+
export function createStoopBase(config) {
|
|
22
|
+
const { globalCss: globalCssConfig, media: configMedia, prefix = "stoop", theme, themeMap: userThemeMap, utils, } = config;
|
|
23
|
+
const sanitizedPrefix = sanitizePrefix(prefix);
|
|
24
|
+
const validatedTheme = validateTheme(theme);
|
|
25
|
+
const media = validatedTheme.media || configMedia;
|
|
26
|
+
const mergedThemeMap = {
|
|
27
|
+
...DEFAULT_THEME_MAP,
|
|
28
|
+
...userThemeMap,
|
|
29
|
+
};
|
|
30
|
+
registerDefaultTheme(validatedTheme, sanitizedPrefix);
|
|
31
|
+
const css = createCSSFunction(validatedTheme, sanitizedPrefix, media, utils, mergedThemeMap);
|
|
32
|
+
const createTheme = createThemeFactory(validatedTheme);
|
|
33
|
+
const globalCss = createGlobalCSSFunction(validatedTheme, sanitizedPrefix, media, utils, mergedThemeMap);
|
|
34
|
+
const keyframes = createKeyframesFunction(sanitizedPrefix, validatedTheme, mergedThemeMap);
|
|
35
|
+
const themeObject = Object.freeze({ ...validatedTheme });
|
|
36
|
+
/**
|
|
37
|
+
* Pre-compiles CSS objects to warm the cache.
|
|
38
|
+
* Useful for eliminating FOUC by pre-compiling common styles.
|
|
39
|
+
*
|
|
40
|
+
* @param styles - Array of CSS objects to pre-compile
|
|
41
|
+
*/
|
|
42
|
+
function warmCache(styles) {
|
|
43
|
+
for (const style of styles) {
|
|
44
|
+
try {
|
|
45
|
+
compileCSS(style, validatedTheme, sanitizedPrefix, media, utils, mergedThemeMap);
|
|
46
|
+
}
|
|
47
|
+
catch {
|
|
48
|
+
// Skip invalid styles
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Preloads a theme by injecting its CSS variables before React renders.
|
|
54
|
+
* Useful for preventing FOUC when loading a non-default theme from localStorage.
|
|
55
|
+
* Uses injectAllThemes to ensure all themes are available.
|
|
56
|
+
*
|
|
57
|
+
* @param theme - Theme to preload (can be theme name string or Theme object)
|
|
58
|
+
*/
|
|
59
|
+
function preloadTheme(theme) {
|
|
60
|
+
if (!config.themes || Object.keys(config.themes).length === 0) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
// Always inject all themes for consistency and to prevent FOUC
|
|
64
|
+
injectAllThemes(config.themes, sanitizedPrefix);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Gets all injected CSS text for server-side rendering.
|
|
68
|
+
* Always includes all theme CSS variables using attribute selectors.
|
|
69
|
+
*
|
|
70
|
+
* @param theme - Deprecated parameter, kept for backward compatibility but ignored
|
|
71
|
+
* @returns CSS text string with theme variables and component styles
|
|
72
|
+
*/
|
|
73
|
+
function getCssText(theme) {
|
|
74
|
+
let result = "";
|
|
75
|
+
// Always include all themes using attribute selectors for consistency
|
|
76
|
+
if (config.themes && Object.keys(config.themes).length > 0) {
|
|
77
|
+
// Merge all themes with default theme for consistency with client-side behavior
|
|
78
|
+
const mergedThemes = {};
|
|
79
|
+
for (const [themeName, theme] of Object.entries(config.themes)) {
|
|
80
|
+
mergedThemes[themeName] = mergeWithDefaultTheme(theme, sanitizedPrefix);
|
|
81
|
+
}
|
|
82
|
+
const allThemeVars = generateAllThemeVariables(mergedThemes, sanitizedPrefix, "data-theme");
|
|
83
|
+
if (allThemeVars) {
|
|
84
|
+
result += allThemeVars + "\n";
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
// No themes configured, just use default theme
|
|
89
|
+
const themeVars = generateCSSVariables(validatedTheme, sanitizedPrefix);
|
|
90
|
+
if (themeVars) {
|
|
91
|
+
result += themeVars + "\n";
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const baseCss = getCssTextBase();
|
|
95
|
+
// Remove any existing theme variable blocks (they're already included above)
|
|
96
|
+
const cleanedCss = removeThemeVariableBlocks(baseCss).trim();
|
|
97
|
+
if (cleanedCss) {
|
|
98
|
+
result += (result ? "\n" : "") + cleanedCss;
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
config: { ...config, prefix: sanitizedPrefix },
|
|
104
|
+
createTheme,
|
|
105
|
+
css,
|
|
106
|
+
getCssText,
|
|
107
|
+
globalCss,
|
|
108
|
+
globalCssConfig,
|
|
109
|
+
keyframes,
|
|
110
|
+
media,
|
|
111
|
+
mergedThemeMap,
|
|
112
|
+
preloadTheme,
|
|
113
|
+
sanitizedPrefix,
|
|
114
|
+
theme: themeObject,
|
|
115
|
+
utils,
|
|
116
|
+
// Internal values for React API creation
|
|
117
|
+
validatedTheme,
|
|
118
|
+
warmCache,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
// Export createStoopBase for use in SSR entry point
|
|
122
|
+
// Note: This file does NOT export createStoop() - that's in create-stoop.ts
|
|
123
|
+
// This separation allows SSR entry point to avoid bundling React code
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSR-safe entry point for Stoop.
|
|
3
|
+
* Exports only server-safe APIs that don't require React.
|
|
4
|
+
* Use this import in Server Components: import { createStoop } from "stoop/ssr"
|
|
5
|
+
*
|
|
6
|
+
* This file does NOT import React types or create React components.
|
|
7
|
+
*/
|
|
8
|
+
import type { StoopConfig, StoopServerInstance } from "./types";
|
|
9
|
+
export type { CSS, Theme, StoopConfig, StoopServerInstance, UtilityFunction, ThemeScale, DefaultTheme, } from "./types";
|
|
10
|
+
export declare function createStoop(config: StoopConfig): StoopServerInstance;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SSR-safe entry point for Stoop.
|
|
3
|
+
* Exports only server-safe APIs that don't require React.
|
|
4
|
+
* Use this import in Server Components: import { createStoop } from "stoop/ssr"
|
|
5
|
+
*
|
|
6
|
+
* This file does NOT import React types or create React components.
|
|
7
|
+
*/
|
|
8
|
+
// Import createStoopBase from create-stoop-internal to avoid React type imports
|
|
9
|
+
// create-stoop-internal.ts is a copy of create-stoop.ts without React imports at module level
|
|
10
|
+
import { createStoopBase } from "./create-stoop-internal";
|
|
11
|
+
export function createStoop(config) {
|
|
12
|
+
const base = createStoopBase(config);
|
|
13
|
+
// Return only server-safe APIs (no React components)
|
|
14
|
+
// Ensure config.prefix is always a string (not undefined) for type compatibility
|
|
15
|
+
return {
|
|
16
|
+
config: { ...base.config, prefix: base.sanitizedPrefix },
|
|
17
|
+
createTheme: base.createTheme,
|
|
18
|
+
css: base.css,
|
|
19
|
+
getCssText: base.getCssText,
|
|
20
|
+
globalCss: base.globalCss,
|
|
21
|
+
keyframes: base.keyframes,
|
|
22
|
+
preloadTheme: base.preloadTheme,
|
|
23
|
+
theme: base.theme,
|
|
24
|
+
warmCache: base.warmCache,
|
|
25
|
+
};
|
|
26
|
+
}
|
package/dist/create-stoop.d.ts
CHANGED
|
@@ -1,9 +1,40 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Factory function that creates a Stoop instance.
|
|
3
|
+
* Supports both client-side (with React APIs) and server-side (without React) usage.
|
|
4
|
+
* Automatically detects environment and includes appropriate APIs.
|
|
5
|
+
*/
|
|
6
|
+
import type { CSS, StoopConfig, StoopInstance, Theme, ThemeScale } from "./types";
|
|
7
|
+
import { createCSSFunction, createKeyframesFunction } from "./api/core-api";
|
|
8
|
+
import { createGlobalCSSFunction } from "./api/global-css";
|
|
9
|
+
/**
|
|
10
|
+
* Shared implementation for creating Stoop instances.
|
|
11
|
+
* Handles common setup logic for both client and server instances.
|
|
12
|
+
* Exported for use in SSR entry point.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createStoopBase(config: StoopConfig): {
|
|
15
|
+
config: StoopConfig;
|
|
16
|
+
createTheme: (overrides?: Partial<Theme>) => Theme;
|
|
17
|
+
css: ReturnType<typeof createCSSFunction>;
|
|
18
|
+
getCssText: (theme?: string | Theme) => string;
|
|
19
|
+
globalCss: ReturnType<typeof createGlobalCSSFunction>;
|
|
20
|
+
globalCssConfig: StoopConfig["globalCss"];
|
|
21
|
+
keyframes: ReturnType<typeof createKeyframesFunction>;
|
|
22
|
+
media: StoopConfig["media"];
|
|
23
|
+
mergedThemeMap: Record<string, ThemeScale>;
|
|
24
|
+
preloadTheme: (theme: string | Theme) => void;
|
|
25
|
+
sanitizedPrefix: string;
|
|
26
|
+
theme: Theme;
|
|
27
|
+
utils: StoopConfig["utils"];
|
|
28
|
+
validatedTheme: Theme;
|
|
29
|
+
warmCache: (styles: CSS[]) => void;
|
|
30
|
+
};
|
|
2
31
|
/**
|
|
3
32
|
* Creates a Stoop instance with the provided configuration.
|
|
4
33
|
* Includes all APIs: styled, Provider, useTheme, etc.
|
|
34
|
+
* In server contexts without React, React APIs will be undefined.
|
|
5
35
|
*
|
|
6
36
|
* @param config - Configuration object containing theme, media queries, utilities, and optional prefix/themeMap
|
|
7
37
|
* @returns StoopInstance with all API functions
|
|
8
38
|
*/
|
|
39
|
+
export type { CSS, Theme, StoopConfig, StoopInstance, UtilityFunction, ThemeScale, DefaultTheme, } from "./types";
|
|
9
40
|
export declare function createStoop(config: StoopConfig): StoopInstance;
|
package/dist/create-stoop.js
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Factory function that creates a Stoop instance.
|
|
3
|
+
* Supports both client-side (with React APIs) and server-side (without React) usage.
|
|
4
|
+
* Automatically detects environment and includes appropriate APIs.
|
|
5
|
+
*/
|
|
6
|
+
import { createTheme as createThemeFactory, createCSSFunction, createKeyframesFunction, } from "./api/core-api";
|
|
4
7
|
import { createGlobalCSSFunction } from "./api/global-css";
|
|
5
|
-
import { createKeyframesFunction } from "./api/keyframes";
|
|
6
|
-
import { createProvider } from "./api/provider";
|
|
7
8
|
import { createStyledFunction } from "./api/styled";
|
|
8
|
-
import { createUseThemeHook } from "./api/
|
|
9
|
+
import { createProvider, createUseThemeHook } from "./api/theme-provider";
|
|
9
10
|
import { DEFAULT_THEME_MAP } from "./constants";
|
|
10
11
|
import { compileCSS } from "./core/compiler";
|
|
11
|
-
import { registerDefaultTheme,
|
|
12
|
-
import { getCssText as getCssTextBase,
|
|
13
|
-
import {
|
|
14
|
-
import { generateCSSVariables } from "./utils/theme";
|
|
15
|
-
import {
|
|
12
|
+
import { mergeWithDefaultTheme, registerDefaultTheme, injectAllThemes } from "./core/theme-manager";
|
|
13
|
+
import { getCssText as getCssTextBase, removeThemeVariableBlocks } from "./inject";
|
|
14
|
+
import { validateTheme } from "./utils/helpers";
|
|
15
|
+
import { generateAllThemeVariables, generateCSSVariables } from "./utils/theme";
|
|
16
|
+
import { sanitizePrefix } from "./utils/theme-utils";
|
|
16
17
|
/**
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
* @param config - Configuration object containing theme, media queries, utilities, and optional prefix/themeMap
|
|
21
|
-
* @returns StoopInstance with all API functions
|
|
18
|
+
* Shared implementation for creating Stoop instances.
|
|
19
|
+
* Handles common setup logic for both client and server instances.
|
|
20
|
+
* Exported for use in SSR entry point.
|
|
22
21
|
*/
|
|
23
|
-
export function
|
|
24
|
-
const { media: configMedia, prefix = "stoop", theme, themeMap: userThemeMap, utils } = config;
|
|
22
|
+
export function createStoopBase(config) {
|
|
23
|
+
const { globalCss: globalCssConfig, media: configMedia, prefix = "stoop", theme, themeMap: userThemeMap, utils, } = config;
|
|
25
24
|
const sanitizedPrefix = sanitizePrefix(prefix);
|
|
26
25
|
const validatedTheme = validateTheme(theme);
|
|
27
26
|
const media = validatedTheme.media || configMedia;
|
|
@@ -30,7 +29,6 @@ export function createStoop(config) {
|
|
|
30
29
|
...userThemeMap,
|
|
31
30
|
};
|
|
32
31
|
registerDefaultTheme(validatedTheme, sanitizedPrefix);
|
|
33
|
-
registerTheme(validatedTheme, sanitizedPrefix);
|
|
34
32
|
const css = createCSSFunction(validatedTheme, sanitizedPrefix, media, utils, mergedThemeMap);
|
|
35
33
|
const createTheme = createThemeFactory(validatedTheme);
|
|
36
34
|
const globalCss = createGlobalCSSFunction(validatedTheme, sanitizedPrefix, media, utils, mergedThemeMap);
|
|
@@ -55,64 +53,75 @@ export function createStoop(config) {
|
|
|
55
53
|
/**
|
|
56
54
|
* Preloads a theme by injecting its CSS variables before React renders.
|
|
57
55
|
* Useful for preventing FOUC when loading a non-default theme from localStorage.
|
|
56
|
+
* Uses injectAllThemes to ensure all themes are available.
|
|
58
57
|
*
|
|
59
58
|
* @param theme - Theme to preload (can be theme name string or Theme object)
|
|
60
59
|
*/
|
|
61
60
|
function preloadTheme(theme) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
if (config.themes && config.themes[theme]) {
|
|
65
|
-
themeToInject = config.themes[theme];
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
if (typeof process !== "undefined" && process.env?.NODE_ENV !== "production") {
|
|
69
|
-
// eslint-disable-next-line no-console
|
|
70
|
-
console.warn(`[Stoop] Theme "${theme}" not found. Available themes: ${config.themes ? Object.keys(config.themes).join(", ") : "none"}`);
|
|
71
|
-
}
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
61
|
+
if (!config.themes || Object.keys(config.themes).length === 0) {
|
|
62
|
+
return;
|
|
74
63
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
updateThemeVariables(themeToInject, sanitizedPrefix);
|
|
64
|
+
// Always inject all themes for consistency and to prevent FOUC
|
|
65
|
+
injectAllThemes(config.themes, sanitizedPrefix);
|
|
79
66
|
}
|
|
80
67
|
/**
|
|
81
68
|
* Gets all injected CSS text for server-side rendering.
|
|
82
|
-
* Always includes theme CSS variables.
|
|
69
|
+
* Always includes all theme CSS variables using attribute selectors.
|
|
83
70
|
*
|
|
84
|
-
* @param theme -
|
|
71
|
+
* @param theme - Deprecated parameter, kept for backward compatibility but ignored
|
|
85
72
|
* @returns CSS text string with theme variables and component styles
|
|
86
73
|
*/
|
|
87
74
|
function getCssText(theme) {
|
|
88
|
-
let
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
// eslint-disable-next-line no-console
|
|
96
|
-
console.warn(`[Stoop] Theme "${theme}" not found. Using default theme. Available: ${config.themes ? Object.keys(config.themes).join(", ") : "none"}`);
|
|
97
|
-
}
|
|
75
|
+
let result = "";
|
|
76
|
+
// Always include all themes using attribute selectors for consistency
|
|
77
|
+
if (config.themes && Object.keys(config.themes).length > 0) {
|
|
78
|
+
// Merge all themes with default theme for consistency with client-side behavior
|
|
79
|
+
const mergedThemes = {};
|
|
80
|
+
for (const [themeName, theme] of Object.entries(config.themes)) {
|
|
81
|
+
mergedThemes[themeName] = mergeWithDefaultTheme(theme, sanitizedPrefix);
|
|
98
82
|
}
|
|
99
|
-
|
|
100
|
-
|
|
83
|
+
const allThemeVars = generateAllThemeVariables(mergedThemes, sanitizedPrefix, "data-theme");
|
|
84
|
+
if (allThemeVars) {
|
|
85
|
+
result += allThemeVars + "\n";
|
|
101
86
|
}
|
|
102
87
|
}
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
88
|
+
else {
|
|
89
|
+
// No themes configured, just use default theme
|
|
90
|
+
const themeVars = generateCSSVariables(validatedTheme, sanitizedPrefix);
|
|
91
|
+
if (themeVars) {
|
|
92
|
+
result += themeVars + "\n";
|
|
93
|
+
}
|
|
107
94
|
}
|
|
108
95
|
const baseCss = getCssTextBase();
|
|
109
|
-
|
|
110
|
-
const
|
|
111
|
-
if (
|
|
112
|
-
result += (result ? "\n" : "") +
|
|
96
|
+
// Remove any existing theme variable blocks (they're already included above)
|
|
97
|
+
const cleanedCss = removeThemeVariableBlocks(baseCss).trim();
|
|
98
|
+
if (cleanedCss) {
|
|
99
|
+
result += (result ? "\n" : "") + cleanedCss;
|
|
113
100
|
}
|
|
114
101
|
return result;
|
|
115
102
|
}
|
|
103
|
+
return {
|
|
104
|
+
config: { ...config, prefix: sanitizedPrefix },
|
|
105
|
+
createTheme,
|
|
106
|
+
css,
|
|
107
|
+
getCssText,
|
|
108
|
+
globalCss,
|
|
109
|
+
globalCssConfig,
|
|
110
|
+
keyframes,
|
|
111
|
+
media,
|
|
112
|
+
mergedThemeMap,
|
|
113
|
+
preloadTheme,
|
|
114
|
+
sanitizedPrefix,
|
|
115
|
+
theme: themeObject,
|
|
116
|
+
utils,
|
|
117
|
+
// Internal values for React API creation
|
|
118
|
+
validatedTheme,
|
|
119
|
+
warmCache,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
export function createStoop(config) {
|
|
123
|
+
const base = createStoopBase(config);
|
|
124
|
+
// Create full client instance (React APIs may be undefined in SSR contexts)
|
|
116
125
|
// Create Provider and useTheme if themes are configured
|
|
117
126
|
let Provider;
|
|
118
127
|
let useTheme;
|
|
@@ -120,28 +129,28 @@ export function createStoop(config) {
|
|
|
120
129
|
if (config.themes) {
|
|
121
130
|
const mergedThemesForProvider = {};
|
|
122
131
|
for (const [themeName, themeOverride] of Object.entries(config.themes)) {
|
|
123
|
-
mergedThemesForProvider[themeName] = createTheme(themeOverride);
|
|
132
|
+
mergedThemesForProvider[themeName] = base.createTheme(themeOverride);
|
|
124
133
|
}
|
|
125
|
-
const { Provider: ProviderComponent, ThemeContext, ThemeManagementContext, } = createProvider(mergedThemesForProvider, validatedTheme, sanitizedPrefix);
|
|
134
|
+
const { Provider: ProviderComponent, ThemeContext, ThemeManagementContext, } = createProvider(mergedThemesForProvider, base.validatedTheme, base.sanitizedPrefix, base.globalCssConfig, base.globalCss);
|
|
126
135
|
themeContext = ThemeContext;
|
|
127
136
|
Provider = ProviderComponent;
|
|
128
137
|
useTheme = createUseThemeHook(ThemeManagementContext);
|
|
129
138
|
}
|
|
130
139
|
// Create styled function
|
|
131
|
-
const styled = createStyledFunction(validatedTheme, sanitizedPrefix, media, utils, mergedThemeMap, themeContext);
|
|
140
|
+
const styled = createStyledFunction(base.validatedTheme, base.sanitizedPrefix, base.media, base.utils, base.mergedThemeMap, themeContext);
|
|
132
141
|
// Return instance with all APIs
|
|
133
142
|
return {
|
|
134
|
-
config:
|
|
135
|
-
createTheme,
|
|
136
|
-
css,
|
|
137
|
-
getCssText,
|
|
138
|
-
globalCss,
|
|
139
|
-
keyframes,
|
|
140
|
-
preloadTheme,
|
|
143
|
+
config: base.config,
|
|
144
|
+
createTheme: base.createTheme,
|
|
145
|
+
css: base.css,
|
|
146
|
+
getCssText: base.getCssText,
|
|
147
|
+
globalCss: base.globalCss,
|
|
148
|
+
keyframes: base.keyframes,
|
|
149
|
+
preloadTheme: base.preloadTheme,
|
|
141
150
|
Provider,
|
|
142
151
|
styled,
|
|
143
|
-
theme:
|
|
152
|
+
theme: base.theme,
|
|
144
153
|
useTheme,
|
|
145
|
-
warmCache,
|
|
154
|
+
warmCache: base.warmCache,
|
|
146
155
|
};
|
|
147
156
|
}
|
package/dist/inject.d.ts
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CSS injection implementation.
|
|
3
|
+
* Consolidates browser-specific and SSR-specific CSS injection logic.
|
|
4
|
+
* Handles stylesheet management, theme variable injection, deduplication, and SSR caching.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Checks if a CSS rule has already been injected.
|
|
8
|
+
*
|
|
9
|
+
* @param key - Rule key to check
|
|
10
|
+
* @returns True if rule is already injected
|
|
11
|
+
*/
|
|
12
|
+
export declare function isInjectedRule(key: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Marks a CSS rule as injected.
|
|
15
|
+
*
|
|
16
|
+
* @param key - Rule key
|
|
17
|
+
* @param css - CSS string
|
|
18
|
+
*/
|
|
19
|
+
export declare function markRuleAsInjected(key: string, css: string): void;
|
|
20
|
+
/**
|
|
21
|
+
* Adds CSS to the SSR cache with LRU eviction.
|
|
22
|
+
*
|
|
23
|
+
* @param css - CSS string to cache
|
|
24
|
+
*/
|
|
25
|
+
export declare function addToSSRCache(css: string): void;
|
|
26
|
+
/**
|
|
27
|
+
* Gets all cached CSS text for SSR.
|
|
28
|
+
*
|
|
29
|
+
* @returns Joined CSS text string
|
|
30
|
+
*/
|
|
31
|
+
export declare function getSSRCacheText(): string;
|
|
32
|
+
/**
|
|
33
|
+
* Clears the SSR cache.
|
|
34
|
+
*/
|
|
35
|
+
export declare function clearSSRCache(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Checks if CSS is already in the SSR cache.
|
|
38
|
+
*
|
|
39
|
+
* @param css - CSS string to check
|
|
40
|
+
* @returns True if CSS is cached
|
|
41
|
+
*/
|
|
42
|
+
export declare function isInSSRCache(css: string): boolean;
|
|
43
|
+
/**
|
|
44
|
+
* Gets or creates the stylesheet element for CSS injection.
|
|
45
|
+
* Reuses the SSR stylesheet if it exists to prevent FOUC.
|
|
46
|
+
* Supports multiple Stoop instances with different prefixes.
|
|
47
|
+
*
|
|
48
|
+
* @param prefix - Optional prefix for stylesheet identification
|
|
49
|
+
* @returns HTMLStyleElement
|
|
50
|
+
* @throws Error if called in SSR context
|
|
51
|
+
*/
|
|
52
|
+
export declare function getStylesheet(prefix?: string): HTMLStyleElement;
|
|
53
|
+
/**
|
|
54
|
+
* Removes all theme variable blocks (both :root and attribute selectors) from CSS.
|
|
55
|
+
*
|
|
56
|
+
* @param css - CSS string to clean
|
|
57
|
+
* @returns CSS string without theme variable blocks
|
|
58
|
+
*/
|
|
59
|
+
export declare function removeThemeVariableBlocks(css: string): string;
|
|
60
|
+
/**
|
|
61
|
+
* Injects CSS variables for all themes using attribute selectors.
|
|
62
|
+
* This allows all themes to be available simultaneously, with theme switching
|
|
63
|
+
* handled by changing the data-theme attribute.
|
|
64
|
+
*
|
|
65
|
+
* @param allThemeVars - CSS string containing all theme CSS variables
|
|
66
|
+
* @param prefix - Optional prefix for CSS variables
|
|
67
|
+
*/
|
|
68
|
+
export declare function injectAllThemeVariables(allThemeVars: string, prefix?: string): void;
|
|
69
|
+
/**
|
|
70
|
+
* Updates the stylesheet with new CSS rules.
|
|
71
|
+
*
|
|
72
|
+
* @param css - CSS string to inject
|
|
73
|
+
* @param ruleKey - Unique key for deduplication
|
|
74
|
+
* @param prefix - Optional prefix for CSS rules
|
|
75
|
+
*/
|
|
76
|
+
export declare function updateStylesheet(css: string, ruleKey: string, prefix?: string): void;
|
|
77
|
+
/**
|
|
78
|
+
* Injects CSS into the browser stylesheet with deduplication.
|
|
79
|
+
*
|
|
80
|
+
* @param css - CSS string to inject
|
|
81
|
+
* @param ruleKey - Unique key for deduplication
|
|
82
|
+
* @param prefix - Optional prefix for CSS rules
|
|
83
|
+
*/
|
|
84
|
+
export declare function injectBrowserCSS(css: string, ruleKey: string, prefix?: string): void;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the current stylesheet element for a given prefix.
|
|
87
|
+
*
|
|
88
|
+
* @param prefix - Optional prefix for stylesheet identification
|
|
89
|
+
* @returns HTMLStyleElement or null if not created
|
|
90
|
+
*/
|
|
91
|
+
export declare function getStylesheetElement(prefix?: string): HTMLStyleElement | null;
|
|
92
|
+
/**
|
|
93
|
+
* Gets all injected rules (for internal use).
|
|
94
|
+
*/
|
|
95
|
+
export declare function getAllInjectedRules(): Map<string, string>;
|
|
96
|
+
/**
|
|
97
|
+
* Injects CSS into the document with automatic SSR support.
|
|
98
|
+
*
|
|
99
|
+
* @param css - CSS string to inject
|
|
100
|
+
* @param prefix - Optional prefix for CSS rules
|
|
101
|
+
* @param ruleKey - Optional unique key for deduplication
|
|
102
|
+
*/
|
|
103
|
+
export declare function injectCSS(css: string, prefix?: string, ruleKey?: string): void;
|
|
104
|
+
/**
|
|
105
|
+
* Gets all injected CSS text (browser or SSR).
|
|
106
|
+
*
|
|
107
|
+
* @returns CSS text string
|
|
108
|
+
*/
|
|
109
|
+
export declare function getCssText(prefix?: string): string;
|
|
110
|
+
/**
|
|
111
|
+
* Clears all injected CSS and caches.
|
|
112
|
+
*/
|
|
113
|
+
export declare function clearStylesheet(): void;
|