@tenphi/tasty 0.0.0-snapshot.fafcbfe → 0.0.0-snapshot.ffe00e9

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 (306) hide show
  1. package/README.md +315 -159
  2. package/dist/_virtual/_rolldown/runtime.js +1 -2
  3. package/dist/chunks/cacheKey.d.ts +1 -0
  4. package/dist/chunks/cacheKey.js +17 -10
  5. package/dist/chunks/cacheKey.js.map +1 -1
  6. package/dist/chunks/definitions.d.ts +1 -1
  7. package/dist/chunks/definitions.js +3 -5
  8. package/dist/chunks/definitions.js.map +1 -1
  9. package/dist/chunks/index.d.ts +1 -0
  10. package/dist/chunks/renderChunk.d.ts +1 -0
  11. package/dist/chunks/renderChunk.js +32 -34
  12. package/dist/chunks/renderChunk.js.map +1 -1
  13. package/dist/compute-styles.d.ts +31 -0
  14. package/dist/compute-styles.js +357 -0
  15. package/dist/compute-styles.js.map +1 -0
  16. package/dist/config.d.ts +116 -30
  17. package/dist/config.js +144 -45
  18. package/dist/config.js.map +1 -1
  19. package/dist/core/index.d.ts +12 -11
  20. package/dist/core/index.js +11 -10
  21. package/dist/counter-style/index.js +51 -0
  22. package/dist/counter-style/index.js.map +1 -0
  23. package/dist/debug.d.ts +26 -141
  24. package/dist/debug.js +357 -637
  25. package/dist/debug.js.map +1 -1
  26. package/dist/font-face/index.js +63 -0
  27. package/dist/font-face/index.js.map +1 -0
  28. package/dist/hooks/index.d.ts +7 -0
  29. package/dist/hooks/useCounterStyle.d.ts +50 -0
  30. package/dist/hooks/useCounterStyle.js +45 -0
  31. package/dist/hooks/useCounterStyle.js.map +1 -0
  32. package/dist/hooks/useFontFace.d.ts +43 -0
  33. package/dist/hooks/useFontFace.js +69 -0
  34. package/dist/hooks/useFontFace.js.map +1 -0
  35. package/dist/hooks/useGlobalStyles.d.ts +3 -0
  36. package/dist/hooks/useGlobalStyles.js +23 -2
  37. package/dist/hooks/useGlobalStyles.js.map +1 -1
  38. package/dist/hooks/useKeyframes.js +13 -4
  39. package/dist/hooks/useKeyframes.js.map +1 -1
  40. package/dist/hooks/useProperty.js +31 -14
  41. package/dist/hooks/useProperty.js.map +1 -1
  42. package/dist/hooks/useRawCSS.js +8 -2
  43. package/dist/hooks/useRawCSS.js.map +1 -1
  44. package/dist/hooks/useStyles.d.ts +4 -4
  45. package/dist/hooks/useStyles.js +7 -147
  46. package/dist/hooks/useStyles.js.map +1 -1
  47. package/dist/index.d.ts +23 -12
  48. package/dist/index.js +14 -11
  49. package/dist/injector/index.d.ts +41 -11
  50. package/dist/injector/index.js +48 -11
  51. package/dist/injector/index.js.map +1 -1
  52. package/dist/injector/injector.d.ts +58 -3
  53. package/dist/injector/injector.js +204 -41
  54. package/dist/injector/injector.js.map +1 -1
  55. package/dist/injector/sheet-manager.d.ts +18 -17
  56. package/dist/injector/sheet-manager.js +45 -73
  57. package/dist/injector/sheet-manager.js.map +1 -1
  58. package/dist/injector/types.d.ts +119 -19
  59. package/dist/keyframes/index.js +1 -1
  60. package/dist/parser/classify.js +6 -6
  61. package/dist/parser/classify.js.map +1 -1
  62. package/dist/parser/const.js +30 -3
  63. package/dist/parser/const.js.map +1 -1
  64. package/dist/parser/lru.js +1 -1
  65. package/dist/parser/lru.js.map +1 -1
  66. package/dist/parser/parser.js +2 -3
  67. package/dist/parser/parser.js.map +1 -1
  68. package/dist/parser/tokenizer.js +1 -1
  69. package/dist/parser/tokenizer.js.map +1 -1
  70. package/dist/parser/types.js +1 -1
  71. package/dist/parser/types.js.map +1 -1
  72. package/dist/pipeline/conditions.js +15 -9
  73. package/dist/pipeline/conditions.js.map +1 -1
  74. package/dist/pipeline/exclusive.js +1 -2
  75. package/dist/pipeline/exclusive.js.map +1 -1
  76. package/dist/pipeline/index.d.ts +3 -1
  77. package/dist/pipeline/index.js +139 -72
  78. package/dist/pipeline/index.js.map +1 -1
  79. package/dist/pipeline/materialize.js +405 -210
  80. package/dist/pipeline/materialize.js.map +1 -1
  81. package/dist/pipeline/parseStateKey.d.ts +1 -1
  82. package/dist/pipeline/parseStateKey.js +38 -22
  83. package/dist/pipeline/parseStateKey.js.map +1 -1
  84. package/dist/pipeline/simplify.js +112 -154
  85. package/dist/pipeline/simplify.js.map +1 -1
  86. package/dist/pipeline/warnings.js +18 -0
  87. package/dist/pipeline/warnings.js.map +1 -0
  88. package/dist/plugins/index.d.ts +2 -0
  89. package/dist/plugins/okhsl-plugin.js +3 -277
  90. package/dist/plugins/okhsl-plugin.js.map +1 -1
  91. package/dist/plugins/types.d.ts +9 -2
  92. package/dist/properties/index.js +83 -19
  93. package/dist/properties/index.js.map +1 -1
  94. package/dist/properties/property-type-resolver.d.ts +24 -0
  95. package/dist/properties/property-type-resolver.js +90 -0
  96. package/dist/properties/property-type-resolver.js.map +1 -0
  97. package/dist/ssr/astro.d.ts +29 -0
  98. package/dist/ssr/astro.js +64 -0
  99. package/dist/ssr/astro.js.map +1 -0
  100. package/dist/ssr/async-storage.d.ts +17 -0
  101. package/dist/ssr/async-storage.js +34 -0
  102. package/dist/ssr/async-storage.js.map +1 -0
  103. package/dist/ssr/collect-auto-properties.js +39 -0
  104. package/dist/ssr/collect-auto-properties.js.map +1 -0
  105. package/dist/ssr/collector.d.ts +102 -0
  106. package/dist/ssr/collector.js +219 -0
  107. package/dist/ssr/collector.js.map +1 -0
  108. package/dist/ssr/format-global-rules.js +22 -0
  109. package/dist/ssr/format-global-rules.js.map +1 -0
  110. package/dist/ssr/format-keyframes.js +69 -0
  111. package/dist/ssr/format-keyframes.js.map +1 -0
  112. package/dist/ssr/format-property.js +49 -0
  113. package/dist/ssr/format-property.js.map +1 -0
  114. package/dist/ssr/format-rules.js +73 -0
  115. package/dist/ssr/format-rules.js.map +1 -0
  116. package/dist/ssr/hydrate.d.ts +22 -0
  117. package/dist/ssr/hydrate.js +49 -0
  118. package/dist/ssr/hydrate.js.map +1 -0
  119. package/dist/ssr/index.d.ts +4 -0
  120. package/dist/ssr/index.js +10 -0
  121. package/dist/ssr/index.js.map +1 -0
  122. package/dist/ssr/next.d.ts +43 -0
  123. package/dist/ssr/next.js +74 -0
  124. package/dist/ssr/next.js.map +1 -0
  125. package/dist/ssr/ssr-collector-ref.js +12 -0
  126. package/dist/ssr/ssr-collector-ref.js.map +1 -0
  127. package/dist/states/index.js +13 -259
  128. package/dist/states/index.js.map +1 -1
  129. package/dist/static/index.js +1 -2
  130. package/dist/static/inject.d.ts +5 -0
  131. package/dist/static/inject.js +17 -0
  132. package/dist/static/inject.js.map +1 -0
  133. package/dist/static/tastyStatic.js +1 -2
  134. package/dist/static/tastyStatic.js.map +1 -1
  135. package/dist/static/types.js +1 -1
  136. package/dist/styles/border.d.ts +2 -2
  137. package/dist/styles/border.js +30 -24
  138. package/dist/styles/border.js.map +1 -1
  139. package/dist/styles/color.d.ts +3 -3
  140. package/dist/styles/color.js +11 -8
  141. package/dist/styles/color.js.map +1 -1
  142. package/dist/styles/const.js +17 -0
  143. package/dist/styles/const.js.map +1 -0
  144. package/dist/styles/createStyle.js +29 -27
  145. package/dist/styles/createStyle.js.map +1 -1
  146. package/dist/styles/dimension.js +15 -3
  147. package/dist/styles/dimension.js.map +1 -1
  148. package/dist/styles/directional.js +133 -0
  149. package/dist/styles/directional.js.map +1 -0
  150. package/dist/styles/display.d.ts +3 -10
  151. package/dist/styles/display.js +45 -39
  152. package/dist/styles/display.js.map +1 -1
  153. package/dist/styles/fade.d.ts +2 -2
  154. package/dist/styles/fade.js +9 -5
  155. package/dist/styles/fade.js.map +1 -1
  156. package/dist/styles/fill.d.ts +15 -17
  157. package/dist/styles/fill.js +9 -9
  158. package/dist/styles/fill.js.map +1 -1
  159. package/dist/styles/flow.d.ts +3 -3
  160. package/dist/styles/flow.js +1 -1
  161. package/dist/styles/flow.js.map +1 -1
  162. package/dist/styles/gap.d.ts +1 -1
  163. package/dist/styles/gap.js +4 -3
  164. package/dist/styles/gap.js.map +1 -1
  165. package/dist/styles/height.d.ts +1 -1
  166. package/dist/styles/height.js +1 -2
  167. package/dist/styles/height.js.map +1 -1
  168. package/dist/styles/index.d.ts +0 -1
  169. package/dist/styles/index.js +3 -4
  170. package/dist/styles/index.js.map +1 -1
  171. package/dist/styles/inset.d.ts +1 -29
  172. package/dist/styles/inset.js +19 -135
  173. package/dist/styles/inset.js.map +1 -1
  174. package/dist/styles/list.d.ts +5 -5
  175. package/dist/styles/list.js +4 -2
  176. package/dist/styles/list.js.map +1 -1
  177. package/dist/styles/margin.d.ts +1 -1
  178. package/dist/styles/margin.js +17 -89
  179. package/dist/styles/margin.js.map +1 -1
  180. package/dist/styles/outline.d.ts +1 -1
  181. package/dist/styles/outline.js +6 -16
  182. package/dist/styles/outline.js.map +1 -1
  183. package/dist/styles/padding.d.ts +1 -1
  184. package/dist/styles/padding.js +17 -89
  185. package/dist/styles/padding.js.map +1 -1
  186. package/dist/styles/placement.d.ts +37 -0
  187. package/dist/styles/placement.js +74 -0
  188. package/dist/styles/placement.js.map +1 -0
  189. package/dist/styles/predefined.d.ts +4 -6
  190. package/dist/styles/predefined.js +9 -13
  191. package/dist/styles/predefined.js.map +1 -1
  192. package/dist/styles/preset.d.ts +6 -1
  193. package/dist/styles/preset.js +54 -53
  194. package/dist/styles/preset.js.map +1 -1
  195. package/dist/styles/radius.d.ts +2 -4
  196. package/dist/styles/radius.js +38 -6
  197. package/dist/styles/radius.js.map +1 -1
  198. package/dist/styles/scrollMargin.d.ts +24 -0
  199. package/dist/styles/scrollMargin.js +32 -0
  200. package/dist/styles/scrollMargin.js.map +1 -0
  201. package/dist/styles/scrollbar.d.ts +9 -5
  202. package/dist/styles/scrollbar.js +31 -85
  203. package/dist/styles/scrollbar.js.map +1 -1
  204. package/dist/styles/shadow.d.ts +3 -3
  205. package/dist/styles/shadow.js +4 -3
  206. package/dist/styles/shadow.js.map +1 -1
  207. package/dist/styles/shared.js +17 -0
  208. package/dist/styles/shared.js.map +1 -0
  209. package/dist/styles/transition.d.ts +2 -2
  210. package/dist/styles/transition.js +6 -5
  211. package/dist/styles/transition.js.map +1 -1
  212. package/dist/styles/types.d.ts +94 -28
  213. package/dist/styles/width.d.ts +1 -1
  214. package/dist/styles/width.js +1 -2
  215. package/dist/styles/width.js.map +1 -1
  216. package/dist/tasty.d.ts +46 -894
  217. package/dist/tasty.js +112 -60
  218. package/dist/tasty.js.map +1 -1
  219. package/dist/types.d.ts +3 -3
  220. package/dist/utils/cache-wrapper.js +4 -9
  221. package/dist/utils/cache-wrapper.js.map +1 -1
  222. package/dist/utils/case-converter.js +1 -1
  223. package/dist/utils/color-math.d.ts +46 -0
  224. package/dist/utils/color-math.js +749 -0
  225. package/dist/utils/color-math.js.map +1 -0
  226. package/dist/utils/color-space.d.ts +5 -0
  227. package/dist/utils/color-space.js +228 -0
  228. package/dist/utils/color-space.js.map +1 -0
  229. package/dist/utils/colors.d.ts +1 -1
  230. package/dist/utils/colors.js +3 -2
  231. package/dist/utils/colors.js.map +1 -1
  232. package/dist/utils/css-types.d.ts +1 -1
  233. package/dist/utils/dotize.d.ts +1 -1
  234. package/dist/utils/dotize.js +1 -1
  235. package/dist/utils/dotize.js.map +1 -1
  236. package/dist/utils/filter-base-props.js +1 -1
  237. package/dist/utils/get-display-name.js +1 -1
  238. package/dist/utils/has-keys.js +13 -0
  239. package/dist/utils/has-keys.js.map +1 -0
  240. package/dist/utils/is-dev-env.js +1 -1
  241. package/dist/utils/is-dev-env.js.map +1 -1
  242. package/dist/utils/is-valid-element-type.js +1 -1
  243. package/dist/utils/merge-styles.js +1 -2
  244. package/dist/utils/merge-styles.js.map +1 -1
  245. package/dist/utils/mod-attrs.d.ts +0 -2
  246. package/dist/utils/mod-attrs.js +3 -4
  247. package/dist/utils/mod-attrs.js.map +1 -1
  248. package/dist/utils/process-tokens.d.ts +3 -17
  249. package/dist/utils/process-tokens.js +19 -107
  250. package/dist/utils/process-tokens.js.map +1 -1
  251. package/dist/utils/resolve-recipes.js +1 -2
  252. package/dist/utils/resolve-recipes.js.map +1 -1
  253. package/dist/utils/selector-transform.js +32 -0
  254. package/dist/utils/selector-transform.js.map +1 -0
  255. package/dist/utils/string.js +1 -1
  256. package/dist/utils/string.js.map +1 -1
  257. package/dist/utils/styles.d.ts +5 -89
  258. package/dist/utils/styles.js +35 -400
  259. package/dist/utils/styles.js.map +1 -1
  260. package/dist/utils/typography.d.ts +24 -13
  261. package/dist/utils/typography.js +15 -17
  262. package/dist/utils/typography.js.map +1 -1
  263. package/dist/utils/warnings.js +1 -1
  264. package/dist/zero/babel.d.ts +84 -10
  265. package/dist/zero/babel.js +193 -37
  266. package/dist/zero/babel.js.map +1 -1
  267. package/dist/zero/css-writer.js +1 -2
  268. package/dist/zero/css-writer.js.map +1 -1
  269. package/dist/zero/extractor.js +119 -3
  270. package/dist/zero/extractor.js.map +1 -1
  271. package/dist/zero/index.js +1 -2
  272. package/dist/zero/next.d.ts +56 -30
  273. package/dist/zero/next.js +105 -40
  274. package/dist/zero/next.js.map +1 -1
  275. package/docs/PIPELINE.md +519 -0
  276. package/docs/README.md +31 -0
  277. package/docs/adoption.md +296 -0
  278. package/docs/comparison.md +420 -0
  279. package/docs/configuration.md +326 -0
  280. package/docs/debug.md +318 -0
  281. package/docs/design-system.md +424 -0
  282. package/docs/dsl.md +673 -0
  283. package/docs/getting-started.md +217 -0
  284. package/docs/injector.md +543 -0
  285. package/docs/methodology.md +616 -0
  286. package/docs/runtime.md +572 -0
  287. package/docs/ssr.md +354 -0
  288. package/docs/styles.md +596 -0
  289. package/docs/tasty-static.md +520 -0
  290. package/package.json +106 -31
  291. package/dist/styles/align.d.ts +0 -15
  292. package/dist/styles/align.js +0 -14
  293. package/dist/styles/align.js.map +0 -1
  294. package/dist/styles/justify.d.ts +0 -15
  295. package/dist/styles/justify.js +0 -14
  296. package/dist/styles/justify.js.map +0 -1
  297. package/dist/styles/styledScrollbar.d.ts +0 -47
  298. package/dist/styles/styledScrollbar.js +0 -38
  299. package/dist/styles/styledScrollbar.js.map +0 -1
  300. package/dist/tokens/typography.d.ts +0 -19
  301. package/dist/tokens/typography.js +0 -237
  302. package/dist/tokens/typography.js.map +0 -1
  303. package/dist/utils/hsl-to-rgb.js +0 -38
  304. package/dist/utils/hsl-to-rgb.js.map +0 -1
  305. package/dist/utils/okhsl-to-rgb.js +0 -296
  306. package/dist/utils/okhsl-to-rgb.js.map +0 -1
@@ -0,0 +1,543 @@
1
+ # Tasty Style Injector
2
+
3
+ A high-performance CSS-in-JS solution that powers the Tasty design system with efficient style injection, automatic cleanup, and first-class SSR support.
4
+
5
+ ---
6
+
7
+ ## Overview
8
+
9
+ The Style Injector is the core engine behind Tasty's styling system, providing:
10
+
11
+ - **Hash-based deduplication** - Identical CSS gets the same className
12
+ - **Reference counting** - Automatic cleanup when components unmount (refCount = 0)
13
+ - **CSS nesting flattening** - Handles `&`, `.Class`, `SubElement` patterns
14
+ - **Keyframes injection** - First-class `@keyframes` support with immediate disposal
15
+ - **Smart cleanup** - CSS rules batched cleanup, keyframes disposed immediately
16
+ - **SSR support** - Deterministic class names and CSS extraction
17
+ - **Multiple roots** - Works with Document and ShadowRoot
18
+ - **Non-stacking cleanups** - Prevents timeout accumulation for better performance
19
+
20
+ > **Note:** This is internal infrastructure that powers Tasty components. Most developers will interact with the higher-level `tasty()` API instead.
21
+
22
+ ---
23
+
24
+ ## Architecture
25
+
26
+ ```
27
+ ┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
28
+ │ tasty() │────│ Style Injector │────│ Sheet Manager │
29
+ │ components │ │ │ │ │
30
+ └─────────────────┘ └──────────────────┘ └─────────────────┘
31
+ │ │ │
32
+ │ │ │
33
+ ▼ ▼ ▼
34
+ ┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
35
+ │ Style Results │ │ Keyframes Manager│ │ Root Registry │
36
+ │ (CSS rules) │ │ │ │ │
37
+ └─────────────────┘ └──────────────────┘ └─────────────────┘
38
+ │ │
39
+ │ │
40
+ ▼ ▼
41
+ ┌─────────────────┐ ┌─────────────────┐
42
+ │ Hash Cache │ │ <style> elements│
43
+ │ Deduplication │ │ CSSStyleSheet │
44
+ └─────────────────┘ └─────────────────┘
45
+ ```
46
+
47
+ ---
48
+
49
+ ## Core API
50
+
51
+ ### `inject(rules, options?): InjectResult`
52
+
53
+ Injects CSS rules and returns a className with dispose function.
54
+
55
+ ```typescript
56
+ import { inject } from '@tenphi/tasty';
57
+
58
+ // Component styling - generates tasty class names
59
+ const result = inject([{
60
+ selector: '.t-abc123',
61
+ declarations: 'color: red; padding: 10px;',
62
+ }]);
63
+
64
+ console.log(result.className); // 't-abc123'
65
+
66
+ // Cleanup when component unmounts (refCount decremented)
67
+ result.dispose();
68
+ ```
69
+
70
+ ### `injectGlobal(rules, options?): { dispose: () => void }`
71
+
72
+ Injects global styles that don't reserve tasty class names.
73
+
74
+ ```typescript
75
+ // Global styles - for body, resets, etc.
76
+ const globalResult = injectGlobal([
77
+ {
78
+ selector: 'body',
79
+ declarations: 'margin: 0; font-family: Arial;',
80
+ },
81
+ {
82
+ selector: '.header',
83
+ declarations: 'background: blue; color: white;',
84
+ atRules: ['@media (min-width: 768px)'],
85
+ }
86
+ ]);
87
+
88
+ // Only returns dispose function - no className needed for global styles
89
+ globalResult.dispose();
90
+ ```
91
+
92
+ ### `injectRawCSS(css, options?): { dispose: () => void }`
93
+
94
+ Injects raw CSS text directly without parsing. This is a low-overhead method for injecting CSS that doesn't need tasty processing.
95
+
96
+ ```typescript
97
+ import { injectRawCSS } from '@tenphi/tasty';
98
+
99
+ // Inject raw CSS
100
+ const { dispose } = injectRawCSS(`
101
+ body {
102
+ margin: 0;
103
+ padding: 0;
104
+ font-family: sans-serif;
105
+ }
106
+
107
+ .my-class {
108
+ color: red;
109
+ }
110
+ `);
111
+
112
+ // Later, remove the injected CSS
113
+ dispose();
114
+ ```
115
+
116
+ ### `useRawCSS(css, options?)` or `useRawCSS(factory, deps, options?)`
117
+
118
+ React hook for injecting raw CSS. Uses `useInsertionEffect` for proper timing and cleanup.
119
+
120
+ Supports two overloads:
121
+ - **Static CSS**: `useRawCSS(cssString, options?)`
122
+ - **Factory function**: `useRawCSS(() => cssString, deps, options?)` - re-evaluates when deps change (like `useMemo`)
123
+
124
+ ```tsx
125
+ import { useRawCSS } from '@tenphi/tasty';
126
+
127
+ // Static CSS
128
+ function GlobalReset() {
129
+ useRawCSS(`
130
+ body { margin: 0; padding: 0; }
131
+ `);
132
+ return null;
133
+ }
134
+
135
+ // Dynamic CSS with factory function (like useMemo)
136
+ function ThemeStyles({ theme }: { theme: 'dark' | 'light' }) {
137
+ useRawCSS(() => `
138
+ body {
139
+ margin: 0;
140
+ background: ${theme === 'dark' ? '#000' : '#fff'};
141
+ color: ${theme === 'dark' ? '#fff' : '#000'};
142
+ }
143
+ `, [theme]);
144
+
145
+ return null;
146
+ }
147
+ ```
148
+
149
+ ### `createInjector(config?): StyleInjector`
150
+
151
+ Creates an isolated injector instance with custom configuration.
152
+
153
+ ```typescript
154
+ import { createInjector } from '@tenphi/tasty';
155
+
156
+ // Create isolated instance for testing
157
+ const testInjector = createInjector({
158
+ devMode: true,
159
+ forceTextInjection: true,
160
+ });
161
+
162
+ const result = testInjector.inject(rules);
163
+ ```
164
+
165
+ ### `keyframes(steps, nameOrOptions?): KeyframesResult`
166
+
167
+ Injects CSS keyframes with automatic deduplication.
168
+
169
+ ```typescript
170
+ // Generated name (k0, k1, k2...)
171
+ const fadeIn = keyframes({
172
+ from: { opacity: 0 },
173
+ to: { opacity: 1 },
174
+ });
175
+
176
+ // Custom name
177
+ const slideIn = keyframes({
178
+ '0%': { transform: 'translateX(-100%)' },
179
+ '100%': { transform: 'translateX(0)' },
180
+ }, 'slideInAnimation');
181
+
182
+ // Use in tasty styles (recommended)
183
+ const AnimatedBox = tasty({
184
+ styles: {
185
+ animation: `${fadeIn} 300ms ease-in`,
186
+ },
187
+ });
188
+
189
+ // Or use with injectGlobal for fixed selectors
190
+ injectGlobal([{
191
+ selector: '.my-animated-class',
192
+ declarations: `animation: ${slideIn} 500ms ease-out;`
193
+ }]);
194
+
195
+ // Cleanup keyframes (if needed)
196
+ fadeIn.dispose(); // Immediate keyframes deletion from DOM
197
+ slideIn.dispose(); // Immediate keyframes deletion from DOM
198
+ ```
199
+
200
+ ### `configure(config): void`
201
+
202
+ Configures the Tasty style system. `configure()` is optional, but if you use it, it must be called **before** any styles are generated (before first render).
203
+
204
+ ```typescript
205
+ import { configure } from '@tenphi/tasty';
206
+
207
+ configure({
208
+ devMode: true, // Enable development features (auto-detected)
209
+ maxRulesPerSheet: 8192, // Cap rules per stylesheet (default: 8192)
210
+ forceTextInjection: false, // Force textContent insertion (auto-detected for tests)
211
+ nonce: 'csp-nonce', // CSP nonce for security
212
+ gc: { // Garbage collection for unused styles
213
+ auto: true, // Enable automatic background sweep
214
+ baseMaxAge: 60000, // Base TTL (ms) for single-use styles
215
+ cooldown: 30000, // Minimum time between GC runs
216
+ autoInterval: 300000, // Background sweep interval (ms)
217
+ cacheCapacity: 5000, // Hard cap on cached styles (optional)
218
+ },
219
+ states: { // Global predefined states for advanced state mapping
220
+ '@mobile': '@media(w < 768px)',
221
+ '@dark': '@root(schema=dark)',
222
+ },
223
+ });
224
+ ```
225
+
226
+ **Auto-Detection Features:**
227
+ - `devMode`: Automatically enabled in development environments (detected via `isDevEnv()`)
228
+ - `forceTextInjection`: Automatically enabled in test environments (Jest, Vitest, Mocha, jsdom)
229
+
230
+ **Configuration Notes:**
231
+ - Most options have sensible defaults and auto-detection
232
+ - `configure()` is optional - the injector works with defaults
233
+ - **Configuration is locked after styles are generated** - calling `configure()` after first render will emit a warning and be ignored
234
+ - `gc.baseMaxAge`: Base TTL for a style rendered only once. Popular styles get longer TTLs via logarithmic scaling (`baseMaxAge * log2(hitCount + 1)`).
235
+ - `gc.auto`: When true, runs a periodic background sweep at `gc.autoInterval` intervals using `requestIdleCallback`.
236
+ - `gc.cooldown`: Minimum time between GC runs to avoid thrashing.
237
+
238
+ ---
239
+
240
+ ## Advanced Features
241
+
242
+ ### Style Result Format
243
+
244
+ The injector works with `StyleResult` objects from the tasty parser:
245
+
246
+ ```typescript
247
+ interface StyleResult {
248
+ selector: string; // CSS selector
249
+ declarations: string; // CSS declarations
250
+ atRules?: string[]; // @media, @supports, etc.
251
+ nestingLevel?: number; // Nesting depth for specificity
252
+ }
253
+
254
+ // Example StyleResult
255
+ const styleRule: StyleResult = {
256
+ selector: '.t-button',
257
+ declarations: 'padding: 8px 16px; background: blue; color: white;',
258
+ atRules: ['@media (min-width: 768px)'],
259
+ nestingLevel: 0,
260
+ };
261
+ ```
262
+
263
+ ### Deduplication & Performance
264
+
265
+ ```typescript
266
+ // Identical CSS rules get the same className
267
+ const button1 = inject([{
268
+ selector: '.t-btn1',
269
+ declarations: 'padding: 8px; color: red;'
270
+ }]);
271
+
272
+ const button2 = inject([{
273
+ selector: '.t-btn2',
274
+ declarations: 'padding: 8px; color: red;' // Same declarations
275
+ }]);
276
+
277
+ // Both get the same className due to deduplication
278
+ console.log(button1.className === button2.className); // true
279
+ ```
280
+
281
+ ### Reference Counting
282
+
283
+ ```typescript
284
+ // Multiple components using the same styles
285
+ const comp1 = inject([commonStyle]);
286
+ const comp2 = inject([commonStyle]);
287
+ const comp3 = inject([commonStyle]);
288
+
289
+ // Style is kept alive while any component uses it
290
+ comp1.dispose(); // refCount: 3 → 2
291
+ comp2.dispose(); // refCount: 2 → 1
292
+ comp3.dispose(); // refCount: 1 → 0, eligible for bulk cleanup
293
+
294
+ // Rule exists but refCount = 0 means unused
295
+ // Next inject() with same styles will increment refCount and reuse immediately
296
+ ```
297
+
298
+ ### Garbage Collection
299
+
300
+ ```typescript
301
+ import { configure, gc, maybeGC } from '@tenphi/tasty';
302
+
303
+ // Keyframes: Disposed immediately when refCount = 0 (safer for global scope)
304
+ // CSS rules: Tracked by popularity and cleaned up via gc()
305
+
306
+ configure({
307
+ gc: {
308
+ auto: true, // Enable background sweep
309
+ baseMaxAge: 60000, // 1-minute base TTL
310
+ cooldown: 30000, // 30s between runs
311
+ },
312
+ });
313
+
314
+ // Manual GC (synchronous, returns number of swept styles):
315
+ gc();
316
+
317
+ // Event-driven GC with cooldown (e.g. on route change):
318
+ maybeGC();
319
+
320
+ // Benefits:
321
+ // - Popularity-aware: frequently used styles survive longer
322
+ // - DOM-safe: styles currently in the DOM are never evicted
323
+ // - Keyframes: Immediate cleanup prevents global namespace pollution
324
+ // - Unused styles can be instantly reactivated (just increment refCount)
325
+ ```
326
+
327
+ ### Shadow DOM Support
328
+
329
+ ```typescript
330
+ // Works with Shadow DOM
331
+ const shadowRoot = document.createElement('div').attachShadow({ mode: 'open' });
332
+
333
+ const shadowStyles = inject([{
334
+ selector: '.shadow-component',
335
+ declarations: 'color: purple;'
336
+ }], { root: shadowRoot });
337
+
338
+ // Keyframes in Shadow DOM
339
+ const shadowAnimation = keyframes({
340
+ from: { opacity: 0 },
341
+ to: { opacity: 1 }
342
+ }, { root: shadowRoot, name: 'shadowFade' });
343
+ ```
344
+
345
+ ---
346
+
347
+ ## SSR & Testing
348
+
349
+ ### Server-Side Rendering
350
+
351
+ ```typescript
352
+ import { getCssText, getCssTextForNode } from '@tenphi/tasty';
353
+
354
+ // Extract all CSS for SSR
355
+ const cssText = getCssText();
356
+
357
+ // Extract CSS for specific DOM subtree (like jest-styled-components)
358
+ const container = render(<MyComponent />);
359
+ const componentCSS = getCssTextForNode(container);
360
+ ```
361
+
362
+ ### Test Environment Detection
363
+
364
+ ```typescript
365
+ // Automatically detected test environments:
366
+ // - NODE_ENV === 'test'
367
+ // - Jest globals (jest, describe, it, expect)
368
+ // - jsdom user agent
369
+ // - Vitest globals (vitest)
370
+ // - Mocha globals (mocha)
371
+
372
+ import { configure, isTestEnvironment, resetConfig } from '@tenphi/tasty';
373
+
374
+ const isTest = isTestEnvironment();
375
+
376
+ // Reset config between tests to allow reconfiguration
377
+ beforeEach(() => {
378
+ resetConfig();
379
+ configure({
380
+ forceTextInjection: isTest, // More reliable in test environments
381
+ devMode: true, // Always enable dev features in tests
382
+ });
383
+ });
384
+ ```
385
+
386
+ ### Memory Management in Tests
387
+
388
+ ```typescript
389
+ // Clean up between tests
390
+ afterEach(() => {
391
+ cleanup(); // Force cleanup of unused styles
392
+ });
393
+
394
+ // Full cleanup after test suite
395
+ afterAll(() => {
396
+ destroy(); // Destroy all stylesheets and reset state
397
+ });
398
+ ```
399
+
400
+ ---
401
+
402
+ ## Development Features
403
+
404
+ ### Performance Metrics
405
+
406
+ When `devMode` is enabled, the injector tracks comprehensive metrics:
407
+
408
+ ```typescript
409
+ import { configure, injector } from '@tenphi/tasty';
410
+
411
+ configure({ devMode: true });
412
+
413
+ // Access metrics through the global injector
414
+ const metrics = injector.instance.getMetrics();
415
+
416
+ console.log({
417
+ cacheHits: metrics.hits, // Successful cache hits
418
+ cacheMisses: metrics.misses, // New styles injected
419
+ unusedHits: metrics.unusedHits, // Current unused styles (calculated on demand)
420
+ bulkCleanups: metrics.bulkCleanups, // Number of bulk cleanup operations
421
+ stylesCleanedUp: metrics.stylesCleanedUp, // Total styles removed in bulk cleanups
422
+ totalInsertions: metrics.totalInsertions, // Lifetime insertions
423
+ totalUnused: metrics.totalUnused, // Total styles marked as unused (refCount = 0)
424
+ startTime: metrics.startTime, // Metrics collection start timestamp
425
+ cleanupHistory: metrics.cleanupHistory, // Detailed cleanup operation history
426
+ });
427
+ ```
428
+
429
+ ### Debug Information
430
+
431
+ ```typescript
432
+ // Get detailed information about injected styles
433
+ const debugInfo = injector.instance.getDebugInfo();
434
+
435
+ console.log({
436
+ activeStyles: debugInfo.activeStyles, // Currently active styles
437
+ unusedStyles: debugInfo.unusedStyles, // Styles marked for cleanup
438
+ totalSheets: debugInfo.totalSheets, // Number of stylesheets
439
+ totalRules: debugInfo.totalRules, // Total CSS rules
440
+ });
441
+ ```
442
+
443
+ ### Cleanup History
444
+
445
+ ```typescript
446
+ // Track cleanup operations over time
447
+ const metrics = injector.instance.getMetrics();
448
+
449
+ metrics.cleanupHistory.forEach(cleanup => {
450
+ console.log({
451
+ timestamp: new Date(cleanup.timestamp),
452
+ classesDeleted: cleanup.classesDeleted,
453
+ rulesDeleted: cleanup.rulesDeleted,
454
+ cssSize: cleanup.cssSize, // Total CSS size removed (bytes)
455
+ });
456
+ });
457
+ ```
458
+
459
+ ---
460
+
461
+ ## Performance Optimizations
462
+
463
+ ### Best Practices
464
+
465
+ ```typescript
466
+ // ✅ Reuse styles - identical CSS gets deduplicated
467
+ const buttonBase = { padding: '8px 16px', borderRadius: '4px' };
468
+
469
+ // ✅ Avoid frequent disposal and re-injection
470
+ // Let the reference counting system handle cleanup
471
+
472
+ // ✅ Use bulk operations for global styles
473
+ injectGlobal([
474
+ { selector: 'body', declarations: 'margin: 0;' },
475
+ { selector: '*', declarations: 'box-sizing: border-box;' },
476
+ { selector: '.container', declarations: 'max-width: 1200px;' }
477
+ ]);
478
+
479
+ // ✅ Configure GC for your app (BEFORE first render!)
480
+ import { configure } from '@tenphi/tasty';
481
+
482
+ configure({
483
+ gc: {
484
+ auto: true, // Enable background sweep for long-lived pages
485
+ baseMaxAge: 60000, // Default base TTL (adjust based on app size)
486
+ },
487
+ });
488
+ ```
489
+
490
+ ### Memory Management
491
+
492
+ ```typescript
493
+ // The injector automatically manages memory through:
494
+
495
+ // 1. Hash-based deduplication - same CSS = same className
496
+ // 2. Reference counting - styles stay alive while in use (refCount > 0)
497
+ // 3. Immediate keyframes cleanup - disposed instantly when refCount = 0
498
+ // 4. Popularity-aware GC - unused CSS rules are scored by hitCount and age
499
+ // 5. DOM safety guard - styles visible in the DOM are never evicted
500
+
501
+ // Manual cleanup is rarely needed but available:
502
+ cleanup(); // Force immediate cleanup of all unused CSS rules (refCount = 0)
503
+ destroy(); // Nuclear option: remove all stylesheets and reset
504
+ ```
505
+
506
+ ---
507
+
508
+ ## Integration with Tasty
509
+
510
+ The Style Injector is seamlessly integrated with the higher-level Tasty API:
511
+
512
+ ```jsx
513
+ // High-level tasty() API
514
+ const StyledButton = tasty({
515
+ styles: {
516
+ padding: '2x 4x',
517
+ fill: '#purple',
518
+ color: '#white',
519
+ }
520
+ });
521
+
522
+ // Internally uses the injector:
523
+ // 1. Styles are parsed into StyleResult objects
524
+ // 2. inject() is called with the parsed results
525
+ // 3. Component gets the returned className
526
+ // 4. dispose() is called when component unmounts
527
+ ```
528
+
529
+ For most development, you'll use the [Runtime API](./runtime.md) rather than the injector directly. The injector provides the high-performance foundation that makes Tasty's declarative styling possible.
530
+
531
+ ---
532
+
533
+ ## When to Use Direct Injection
534
+
535
+ Direct injector usage is recommended for:
536
+
537
+ - **Custom CSS-in-JS libraries** built on top of Tasty
538
+ - **Global styles** that don't fit the component model
539
+ - **Third-party integration** where you need low-level CSS control
540
+ - **Performance-critical scenarios** where you need direct control
541
+ - **Testing utilities** that need to inject or extract CSS
542
+
543
+ For regular component styling, prefer the [`tasty()` API](./runtime.md) which provides a more developer-friendly interface.