stoop 0.3.0 → 0.4.1

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 (65) hide show
  1. package/README.md +125 -0
  2. package/dist/api/core-api.d.ts +34 -0
  3. package/dist/api/styled.d.ts +0 -1
  4. package/dist/api/theme-provider.d.ts +41 -0
  5. package/dist/constants.d.ts +11 -1
  6. package/dist/core/compiler.d.ts +11 -0
  7. package/dist/core/theme-manager.d.ts +34 -3
  8. package/dist/create-stoop-internal.d.ts +30 -0
  9. package/dist/create-stoop-ssr.d.ts +10 -0
  10. package/dist/create-stoop-ssr.js +16 -0
  11. package/dist/create-stoop.d.ts +32 -1
  12. package/dist/create-stoop.js +16 -147
  13. package/dist/inject.d.ts +113 -0
  14. package/dist/types/index.d.ts +147 -12
  15. package/dist/types/react-polymorphic-types.d.ts +13 -2
  16. package/dist/utils/auto-preload.d.ts +45 -0
  17. package/dist/utils/helpers.d.ts +64 -0
  18. package/dist/utils/storage.d.ts +148 -0
  19. package/dist/utils/{string.d.ts → theme-utils.d.ts} +20 -3
  20. package/dist/utils/theme.d.ts +14 -2
  21. package/package.json +44 -17
  22. package/dist/api/create-theme.d.ts +0 -13
  23. package/dist/api/create-theme.js +0 -43
  24. package/dist/api/css.d.ts +0 -16
  25. package/dist/api/css.js +0 -20
  26. package/dist/api/global-css.js +0 -89
  27. package/dist/api/keyframes.d.ts +0 -16
  28. package/dist/api/keyframes.js +0 -95
  29. package/dist/api/provider.d.ts +0 -19
  30. package/dist/api/provider.js +0 -109
  31. package/dist/api/styled.js +0 -170
  32. package/dist/api/use-theme.d.ts +0 -13
  33. package/dist/api/use-theme.js +0 -21
  34. package/dist/constants.js +0 -144
  35. package/dist/core/cache.js +0 -68
  36. package/dist/core/compiler.js +0 -198
  37. package/dist/core/theme-manager.js +0 -97
  38. package/dist/core/variants.js +0 -32
  39. package/dist/create-stoop-server.d.ts +0 -33
  40. package/dist/create-stoop-server.js +0 -130
  41. package/dist/index.d.ts +0 -6
  42. package/dist/index.js +0 -5
  43. package/dist/inject/browser.d.ts +0 -58
  44. package/dist/inject/browser.js +0 -149
  45. package/dist/inject/dedup.d.ts +0 -29
  46. package/dist/inject/dedup.js +0 -38
  47. package/dist/inject/index.d.ts +0 -40
  48. package/dist/inject/index.js +0 -75
  49. package/dist/inject/ssr.d.ts +0 -27
  50. package/dist/inject/ssr.js +0 -46
  51. package/dist/ssr.d.ts +0 -21
  52. package/dist/ssr.js +0 -19
  53. package/dist/types/index.js +0 -5
  54. package/dist/utils/environment.d.ts +0 -6
  55. package/dist/utils/environment.js +0 -12
  56. package/dist/utils/string.js +0 -253
  57. package/dist/utils/theme-map.d.ts +0 -22
  58. package/dist/utils/theme-map.js +0 -97
  59. package/dist/utils/theme-validation.d.ts +0 -13
  60. package/dist/utils/theme-validation.js +0 -36
  61. package/dist/utils/theme.js +0 -279
  62. package/dist/utils/type-guards.d.ts +0 -26
  63. package/dist/utils/type-guards.js +0 -38
  64. package/dist/utils/utilities.d.ts +0 -14
  65. package/dist/utils/utilities.js +0 -43
@@ -1,149 +0,0 @@
1
- /**
2
- * Browser-specific CSS injection.
3
- * Manages a single stylesheet element that gets updated with new CSS rules.
4
- * Handles theme variable injection, deduplication, and stylesheet lifecycle.
5
- */
6
- import { isBrowser } from "../utils/environment";
7
- import { getRootRegex, sanitizePrefix } from "../utils/string";
8
- import { generateCSSVariables } from "../utils/theme";
9
- import * as dedup from "./dedup";
10
- import * as ssr from "./ssr";
11
- let stylesheetElement = null;
12
- const lastInjectedThemes = new Map();
13
- const lastInjectedCSSVars = new Map();
14
- /**
15
- * Gets or creates the stylesheet element for CSS injection.
16
- * Reuses the SSR stylesheet if it exists to prevent FOUC.
17
- *
18
- * @param prefix - Optional prefix for stylesheet identification
19
- * @returns HTMLStyleElement
20
- * @throws Error if called in SSR context
21
- */
22
- export function getStylesheet(prefix = "stoop") {
23
- if (!isBrowser()) {
24
- throw new Error("Cannot access document in SSR context");
25
- }
26
- const sanitizedPrefix = sanitizePrefix(prefix);
27
- if (!stylesheetElement || !stylesheetElement.parentNode) {
28
- const ssrStylesheet = document.getElementById("stoop-ssr");
29
- if (ssrStylesheet) {
30
- stylesheetElement = ssrStylesheet;
31
- stylesheetElement.setAttribute("data-stoop", sanitizedPrefix || "stoop");
32
- }
33
- else {
34
- stylesheetElement = document.createElement("style");
35
- stylesheetElement.setAttribute("data-stoop", sanitizedPrefix || "stoop");
36
- document.head.appendChild(stylesheetElement);
37
- }
38
- }
39
- return stylesheetElement;
40
- }
41
- /**
42
- * Injects theme CSS variables into the stylesheet.
43
- * Automatically ensures stylesheet exists before injection.
44
- *
45
- * @param cssVars - CSS variables string
46
- * @param theme - Theme object
47
- * @param prefix - Optional prefix for CSS variables
48
- */
49
- export function injectThemeVariables(cssVars, theme, prefix = "stoop") {
50
- if (!cssVars) {
51
- return;
52
- }
53
- const sanitizedPrefix = sanitizePrefix(prefix);
54
- const key = `__theme_vars_${sanitizedPrefix}`;
55
- const lastCSSVars = lastInjectedCSSVars.get(key) ?? null;
56
- if (lastCSSVars === cssVars) {
57
- const lastTheme = lastInjectedThemes.get(key) ?? null;
58
- if (lastTheme !== theme) {
59
- lastInjectedThemes.set(key, theme);
60
- }
61
- return;
62
- }
63
- lastInjectedThemes.set(key, theme);
64
- lastInjectedCSSVars.set(key, cssVars);
65
- if (!isBrowser()) {
66
- ssr.addToSSRCache(cssVars);
67
- return;
68
- }
69
- const sheet = getStylesheet(sanitizedPrefix);
70
- const currentCSS = sheet.textContent || "";
71
- if (dedup.isInjectedRule(key)) {
72
- const rootRegex = getRootRegex(sanitizedPrefix);
73
- const withoutVars = currentCSS.replace(rootRegex, "").trim();
74
- sheet.textContent = cssVars + (withoutVars ? "\n" + withoutVars : "");
75
- dedup.markRuleAsInjected(key, cssVars);
76
- }
77
- else {
78
- sheet.textContent = cssVars + (currentCSS ? "\n" + currentCSS : "");
79
- dedup.markRuleAsInjected(key, cssVars);
80
- }
81
- }
82
- /**
83
- * Registers a theme for injection (browser-specific).
84
- * Automatically ensures stylesheet exists and injects theme variables.
85
- *
86
- * @param theme - Theme object to register
87
- * @param prefix - Optional prefix for CSS variables
88
- */
89
- export function registerTheme(theme, prefix = "stoop") {
90
- const sanitizedPrefix = sanitizePrefix(prefix);
91
- if (isBrowser()) {
92
- getStylesheet(sanitizedPrefix);
93
- const cssVars = generateCSSVariables(theme, sanitizedPrefix);
94
- injectThemeVariables(cssVars, theme, sanitizedPrefix);
95
- }
96
- }
97
- /**
98
- * Updates the stylesheet with new CSS rules.
99
- *
100
- * @param css - CSS string to inject
101
- * @param ruleKey - Unique key for deduplication
102
- * @param prefix - Optional prefix for CSS rules
103
- */
104
- export function updateStylesheet(css, ruleKey, prefix = "stoop") {
105
- if (!isBrowser()) {
106
- return;
107
- }
108
- const sanitizedPrefix = sanitizePrefix(prefix);
109
- if (dedup.isInjectedRule(ruleKey)) {
110
- return;
111
- }
112
- const sheet = getStylesheet(sanitizedPrefix);
113
- const currentCSS = sheet.textContent || "";
114
- sheet.textContent = currentCSS + (currentCSS ? "\n" : "") + css;
115
- dedup.markRuleAsInjected(ruleKey, css);
116
- }
117
- /**
118
- * Injects CSS into the browser stylesheet with deduplication.
119
- *
120
- * @param css - CSS string to inject
121
- * @param ruleKey - Unique key for deduplication
122
- * @param prefix - Optional prefix for CSS rules
123
- */
124
- export function injectBrowserCSS(css, ruleKey, prefix = "stoop") {
125
- if (dedup.isInjectedRule(ruleKey)) {
126
- return;
127
- }
128
- const sanitizedPrefix = sanitizePrefix(prefix);
129
- updateStylesheet(css, ruleKey, sanitizedPrefix);
130
- }
131
- /**
132
- * Gets the current stylesheet element.
133
- *
134
- * @returns HTMLStyleElement or null if not created
135
- */
136
- export function getStylesheetElement() {
137
- return stylesheetElement;
138
- }
139
- /**
140
- * Clears the stylesheet and all caches.
141
- */
142
- export function clearStylesheet() {
143
- if (stylesheetElement && stylesheetElement.parentNode) {
144
- stylesheetElement.parentNode.removeChild(stylesheetElement);
145
- }
146
- stylesheetElement = null;
147
- lastInjectedThemes.clear();
148
- lastInjectedCSSVars.clear();
149
- }
@@ -1,29 +0,0 @@
1
- /**
2
- * CSS injection deduplication.
3
- * Tracks which CSS rules have been injected to prevent duplicates.
4
- * Used by both browser and SSR injection systems.
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
- * Gets all injected rules as a new Map.
22
- *
23
- * @returns Map of all injected rules
24
- */
25
- export declare function getAllInjectedRules(): Map<string, string>;
26
- /**
27
- * Clears all injected rule tracking.
28
- */
29
- export declare function clearInjectedRules(): void;
@@ -1,38 +0,0 @@
1
- /**
2
- * CSS injection deduplication.
3
- * Tracks which CSS rules have been injected to prevent duplicates.
4
- * Used by both browser and SSR injection systems.
5
- */
6
- const injectedRules = new Map();
7
- /**
8
- * Checks if a CSS rule has already been injected.
9
- *
10
- * @param key - Rule key to check
11
- * @returns True if rule is already injected
12
- */
13
- export function isInjectedRule(key) {
14
- return injectedRules.has(key);
15
- }
16
- /**
17
- * Marks a CSS rule as injected.
18
- *
19
- * @param key - Rule key
20
- * @param css - CSS string
21
- */
22
- export function markRuleAsInjected(key, css) {
23
- injectedRules.set(key, css);
24
- }
25
- /**
26
- * Gets all injected rules as a new Map.
27
- *
28
- * @returns Map of all injected rules
29
- */
30
- export function getAllInjectedRules() {
31
- return new Map(injectedRules);
32
- }
33
- /**
34
- * Clears all injected rule tracking.
35
- */
36
- export function clearInjectedRules() {
37
- injectedRules.clear();
38
- }
@@ -1,40 +0,0 @@
1
- /**
2
- * CSS injection public API.
3
- * Composes browser, SSR, and deduplication modules into a unified interface.
4
- * Provides single stylesheet injection with automatic SSR support.
5
- */
6
- import type { Theme } from "../types";
7
- export { isInjectedRule } from "./dedup";
8
- /**
9
- * Injects CSS into the document with automatic SSR support.
10
- *
11
- * @param css - CSS string to inject
12
- * @param prefix - Optional prefix for CSS rules
13
- * @param ruleKey - Optional unique key for deduplication
14
- */
15
- export declare function injectCSS(css: string, prefix?: string, ruleKey?: string): void;
16
- /**
17
- * Injects theme CSS variables into the document.
18
- *
19
- * @param cssVars - CSS variables string
20
- * @param theme - Theme object
21
- * @param prefix - Optional prefix for CSS variables
22
- */
23
- export declare function injectThemeVariables(cssVars: string, theme: Theme, prefix?: string): void;
24
- /**
25
- * Registers a theme for injection.
26
- *
27
- * @param theme - Theme object to register
28
- * @param prefix - Optional prefix for CSS variables
29
- */
30
- export declare function registerTheme(theme: Theme, prefix?: string): void;
31
- /**
32
- * Gets all injected CSS text (browser or SSR).
33
- *
34
- * @returns CSS text string
35
- */
36
- export declare function getCssText(): string;
37
- /**
38
- * Clears all injected CSS and caches.
39
- */
40
- export declare function clearStylesheet(): void;
@@ -1,75 +0,0 @@
1
- /**
2
- * CSS injection public API.
3
- * Composes browser, SSR, and deduplication modules into a unified interface.
4
- * Provides single stylesheet injection with automatic SSR support.
5
- */
6
- import { clearStyleCache } from "../core/cache";
7
- import { isBrowser } from "../utils/environment";
8
- import * as browser from "./browser";
9
- import * as dedup from "./dedup";
10
- import * as ssr from "./ssr";
11
- export { isInjectedRule } from "./dedup";
12
- /**
13
- * Injects CSS into the document with automatic SSR support.
14
- *
15
- * @param css - CSS string to inject
16
- * @param prefix - Optional prefix for CSS rules
17
- * @param ruleKey - Optional unique key for deduplication
18
- */
19
- export function injectCSS(css, prefix = "stoop", ruleKey) {
20
- const key = ruleKey || css;
21
- if (!isBrowser()) {
22
- if (!dedup.isInjectedRule(key)) {
23
- dedup.markRuleAsInjected(key, css);
24
- }
25
- ssr.addToSSRCache(css);
26
- return;
27
- }
28
- browser.injectBrowserCSS(css, key, prefix);
29
- }
30
- /**
31
- * Injects theme CSS variables into the document.
32
- *
33
- * @param cssVars - CSS variables string
34
- * @param theme - Theme object
35
- * @param prefix - Optional prefix for CSS variables
36
- */
37
- export function injectThemeVariables(cssVars, theme, prefix = "stoop") {
38
- browser.injectThemeVariables(cssVars, theme, prefix);
39
- }
40
- /**
41
- * Registers a theme for injection.
42
- *
43
- * @param theme - Theme object to register
44
- * @param prefix - Optional prefix for CSS variables
45
- */
46
- export function registerTheme(theme, prefix = "stoop") {
47
- browser.registerTheme(theme, prefix);
48
- }
49
- /**
50
- * Gets all injected CSS text (browser or SSR).
51
- *
52
- * @returns CSS text string
53
- */
54
- export function getCssText() {
55
- if (isBrowser()) {
56
- const sheetElement = browser.getStylesheetElement();
57
- if (sheetElement && sheetElement.parentNode) {
58
- const sheetCSS = sheetElement.textContent || "";
59
- if (!sheetCSS && dedup.getAllInjectedRules().size > 0) {
60
- return ssr.getSSRCacheText();
61
- }
62
- return sheetCSS;
63
- }
64
- }
65
- return ssr.getSSRCacheText();
66
- }
67
- /**
68
- * Clears all injected CSS and caches.
69
- */
70
- export function clearStylesheet() {
71
- browser.clearStylesheet();
72
- dedup.clearInjectedRules();
73
- ssr.clearSSRCache();
74
- clearStyleCache();
75
- }
@@ -1,27 +0,0 @@
1
- /**
2
- * SSR cache management for CSS injection.
3
- * Maintains a cache of CSS text for server-side rendering.
4
- */
5
- /**
6
- * Adds CSS to the SSR cache with FIFO eviction.
7
- *
8
- * @param css - CSS string to cache
9
- */
10
- export declare function addToSSRCache(css: string): void;
11
- /**
12
- * Gets all cached CSS text for SSR.
13
- *
14
- * @returns Joined CSS text string
15
- */
16
- export declare function getSSRCacheText(): string;
17
- /**
18
- * Clears the SSR cache.
19
- */
20
- export declare function clearSSRCache(): void;
21
- /**
22
- * Checks if CSS is already in the SSR cache.
23
- *
24
- * @param css - CSS string to check
25
- * @returns True if CSS is cached
26
- */
27
- export declare function isInSSRCache(css: string): boolean;
@@ -1,46 +0,0 @@
1
- /**
2
- * SSR cache management for CSS injection.
3
- * Maintains a cache of CSS text for server-side rendering.
4
- */
5
- import { MAX_CSS_CACHE_SIZE } from "../constants";
6
- const cssTextCache = new Map();
7
- /**
8
- * Adds CSS to the SSR cache with FIFO eviction.
9
- *
10
- * @param css - CSS string to cache
11
- */
12
- export function addToSSRCache(css) {
13
- if (cssTextCache.has(css)) {
14
- return;
15
- }
16
- if (cssTextCache.size >= MAX_CSS_CACHE_SIZE) {
17
- const firstKey = cssTextCache.keys().next().value;
18
- if (firstKey !== undefined) {
19
- cssTextCache.delete(firstKey);
20
- }
21
- }
22
- cssTextCache.set(css, true);
23
- }
24
- /**
25
- * Gets all cached CSS text for SSR.
26
- *
27
- * @returns Joined CSS text string
28
- */
29
- export function getSSRCacheText() {
30
- return Array.from(cssTextCache.keys()).join("\n");
31
- }
32
- /**
33
- * Clears the SSR cache.
34
- */
35
- export function clearSSRCache() {
36
- cssTextCache.clear();
37
- }
38
- /**
39
- * Checks if CSS is already in the SSR cache.
40
- *
41
- * @param css - CSS string to check
42
- * @returns True if CSS is cached
43
- */
44
- export function isInSSRCache(css) {
45
- return cssTextCache.has(css);
46
- }
package/dist/ssr.d.ts DELETED
@@ -1,21 +0,0 @@
1
- /**
2
- * Server-safe entry point for Stoop.
3
- * Exports only functions that work in Server Components.
4
- * NO React dependencies, NO "use client" directive.
5
- *
6
- * Use this entry point when importing Stoop in Next.js Server Components.
7
- *
8
- * @example
9
- * ```typescript
10
- * // In a Server Component (layout.tsx)
11
- * import { createStoop } from 'stoop/ssr';
12
- *
13
- * const { getCssText } = createStoop(config);
14
- * const cssText = getCssText();
15
- * ```
16
- */
17
- export { createStoop } from "./create-stoop-server";
18
- export type { StoopServerInstance } from "./create-stoop-server";
19
- export { getCssText, clearStylesheet } from "./inject";
20
- export { generateCSSVariables } from "./utils/theme";
21
- export type { Theme, CSS, StoopConfig, UtilityFunction, Variants } from "./types";
package/dist/ssr.js DELETED
@@ -1,19 +0,0 @@
1
- /**
2
- * Server-safe entry point for Stoop.
3
- * Exports only functions that work in Server Components.
4
- * NO React dependencies, NO "use client" directive.
5
- *
6
- * Use this entry point when importing Stoop in Next.js Server Components.
7
- *
8
- * @example
9
- * ```typescript
10
- * // In a Server Component (layout.tsx)
11
- * import { createStoop } from 'stoop/ssr';
12
- *
13
- * const { getCssText } = createStoop(config);
14
- * const cssText = getCssText();
15
- * ```
16
- */
17
- export { createStoop } from "./create-stoop-server";
18
- export { getCssText, clearStylesheet } from "./inject";
19
- export { generateCSSVariables } from "./utils/theme";
@@ -1,5 +0,0 @@
1
- /**
2
- * Core TypeScript type definitions for Stoop.
3
- * Defines CSS objects, themes, variants, styled components, and instance types.
4
- */
5
- export {};
@@ -1,6 +0,0 @@
1
- /**
2
- * Checks if code is running in a browser environment.
3
- *
4
- * @returns True if running in browser, false if in SSR/Node environment
5
- */
6
- export declare function isBrowser(): boolean;
@@ -1,12 +0,0 @@
1
- /**
2
- * Checks if code is running in a browser environment.
3
- *
4
- * @returns True if running in browser, false if in SSR/Node environment
5
- */
6
- export function isBrowser() {
7
- return (typeof window !== "undefined" &&
8
- typeof document !== "undefined" &&
9
- typeof window.document === "object" &&
10
- // Ensure we're not in React Server Component or SSR context
11
- typeof window.requestAnimationFrame === "function");
12
- }