@tenphi/tasty 0.0.0-snapshot.c8bdaeb → 0.0.0-snapshot.c8c7b2a

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 (318) hide show
  1. package/README.md +309 -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 +322 -0
  15. package/dist/compute-styles.js.map +1 -0
  16. package/dist/config.d.ts +157 -30
  17. package/dist/config.js +233 -45
  18. package/dist/config.js.map +1 -1
  19. package/dist/core/index.d.ts +12 -11
  20. package/dist/core/index.js +12 -11
  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 +359 -639
  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 +36 -0
  30. package/dist/hooks/useCounterStyle.js +65 -0
  31. package/dist/hooks/useCounterStyle.js.map +1 -0
  32. package/dist/hooks/useFontFace.d.ts +45 -0
  33. package/dist/hooks/useFontFace.js +66 -0
  34. package/dist/hooks/useFontFace.js.map +1 -0
  35. package/dist/hooks/useGlobalStyles.d.ts +21 -2
  36. package/dist/hooks/useGlobalStyles.js +60 -28
  37. package/dist/hooks/useGlobalStyles.js.map +1 -1
  38. package/dist/hooks/useKeyframes.d.ts +4 -2
  39. package/dist/hooks/useKeyframes.js +44 -43
  40. package/dist/hooks/useKeyframes.js.map +1 -1
  41. package/dist/hooks/useProperty.d.ts +4 -2
  42. package/dist/hooks/useProperty.js +36 -31
  43. package/dist/hooks/useProperty.js.map +1 -1
  44. package/dist/hooks/useRawCSS.d.ts +13 -44
  45. package/dist/hooks/useRawCSS.js +91 -16
  46. package/dist/hooks/useRawCSS.js.map +1 -1
  47. package/dist/hooks/useStyles.d.ts +3 -3
  48. package/dist/hooks/useStyles.js +8 -146
  49. package/dist/hooks/useStyles.js.map +1 -1
  50. package/dist/index.d.ts +15 -12
  51. package/dist/index.js +15 -12
  52. package/dist/injector/index.d.ts +36 -11
  53. package/dist/injector/index.js +43 -12
  54. package/dist/injector/index.js.map +1 -1
  55. package/dist/injector/injector.d.ts +62 -3
  56. package/dist/injector/injector.js +293 -46
  57. package/dist/injector/injector.js.map +1 -1
  58. package/dist/injector/sheet-manager.d.ts +22 -17
  59. package/dist/injector/sheet-manager.js +64 -79
  60. package/dist/injector/sheet-manager.js.map +1 -1
  61. package/dist/injector/types.d.ts +121 -21
  62. package/dist/keyframes/index.js +1 -1
  63. package/dist/parser/classify.js +6 -6
  64. package/dist/parser/classify.js.map +1 -1
  65. package/dist/parser/const.js +30 -3
  66. package/dist/parser/const.js.map +1 -1
  67. package/dist/parser/lru.js +1 -1
  68. package/dist/parser/lru.js.map +1 -1
  69. package/dist/parser/parser.js +2 -3
  70. package/dist/parser/parser.js.map +1 -1
  71. package/dist/parser/tokenizer.js +1 -1
  72. package/dist/parser/tokenizer.js.map +1 -1
  73. package/dist/parser/types.js +1 -1
  74. package/dist/parser/types.js.map +1 -1
  75. package/dist/pipeline/conditions.js +1 -1
  76. package/dist/pipeline/conditions.js.map +1 -1
  77. package/dist/pipeline/exclusive.js +58 -4
  78. package/dist/pipeline/exclusive.js.map +1 -1
  79. package/dist/pipeline/index.d.ts +3 -1
  80. package/dist/pipeline/index.js +82 -34
  81. package/dist/pipeline/index.js.map +1 -1
  82. package/dist/pipeline/materialize.js +436 -123
  83. package/dist/pipeline/materialize.js.map +1 -1
  84. package/dist/pipeline/parseStateKey.d.ts +1 -1
  85. package/dist/pipeline/parseStateKey.js +21 -13
  86. package/dist/pipeline/parseStateKey.js.map +1 -1
  87. package/dist/pipeline/simplify.js +181 -7
  88. package/dist/pipeline/simplify.js.map +1 -1
  89. package/dist/pipeline/warnings.js +1 -1
  90. package/dist/plugins/index.d.ts +2 -0
  91. package/dist/plugins/okhsl-plugin.js +3 -277
  92. package/dist/plugins/okhsl-plugin.js.map +1 -1
  93. package/dist/plugins/types.d.ts +20 -2
  94. package/dist/properties/index.js +83 -19
  95. package/dist/properties/index.js.map +1 -1
  96. package/dist/properties/property-type-resolver.d.ts +24 -0
  97. package/dist/properties/property-type-resolver.js +90 -0
  98. package/dist/properties/property-type-resolver.js.map +1 -0
  99. package/dist/rsc-cache.js +79 -0
  100. package/dist/rsc-cache.js.map +1 -0
  101. package/dist/ssr/astro-client.d.ts +1 -0
  102. package/dist/ssr/astro-client.js +19 -0
  103. package/dist/ssr/astro-client.js.map +1 -0
  104. package/dist/ssr/astro-middleware.d.ts +15 -0
  105. package/dist/ssr/astro-middleware.js +19 -0
  106. package/dist/ssr/astro-middleware.js.map +1 -0
  107. package/dist/ssr/astro.d.ts +108 -0
  108. package/dist/ssr/astro.js +149 -0
  109. package/dist/ssr/astro.js.map +1 -0
  110. package/dist/ssr/async-storage.d.ts +17 -0
  111. package/dist/ssr/async-storage.js +44 -0
  112. package/dist/ssr/async-storage.js.map +1 -0
  113. package/dist/ssr/collect-auto-properties.js +58 -0
  114. package/dist/ssr/collect-auto-properties.js.map +1 -0
  115. package/dist/ssr/collector.d.ts +98 -0
  116. package/dist/ssr/collector.js +235 -0
  117. package/dist/ssr/collector.js.map +1 -0
  118. package/dist/ssr/context.js +16 -0
  119. package/dist/ssr/context.js.map +1 -0
  120. package/dist/ssr/format-global-rules.js +22 -0
  121. package/dist/ssr/format-global-rules.js.map +1 -0
  122. package/dist/ssr/format-keyframes.js +69 -0
  123. package/dist/ssr/format-keyframes.js.map +1 -0
  124. package/dist/ssr/format-property.js +49 -0
  125. package/dist/ssr/format-property.js.map +1 -0
  126. package/dist/ssr/format-rules.js +73 -0
  127. package/dist/ssr/format-rules.js.map +1 -0
  128. package/dist/ssr/hydrate.d.ts +29 -0
  129. package/dist/ssr/hydrate.js +45 -0
  130. package/dist/ssr/hydrate.js.map +1 -0
  131. package/dist/ssr/index.d.ts +4 -0
  132. package/dist/ssr/index.js +10 -0
  133. package/dist/ssr/index.js.map +1 -0
  134. package/dist/ssr/next.d.ts +46 -0
  135. package/dist/ssr/next.js +75 -0
  136. package/dist/ssr/next.js.map +1 -0
  137. package/dist/ssr/ssr-collector-ref.js +29 -0
  138. package/dist/ssr/ssr-collector-ref.js.map +1 -0
  139. package/dist/states/index.js +13 -259
  140. package/dist/states/index.js.map +1 -1
  141. package/dist/static/index.js +1 -2
  142. package/dist/static/inject.d.ts +5 -0
  143. package/dist/static/inject.js +17 -0
  144. package/dist/static/inject.js.map +1 -0
  145. package/dist/static/tastyStatic.js +1 -2
  146. package/dist/static/tastyStatic.js.map +1 -1
  147. package/dist/static/types.js +1 -1
  148. package/dist/styles/border.d.ts +1 -1
  149. package/dist/styles/border.js +30 -24
  150. package/dist/styles/border.js.map +1 -1
  151. package/dist/styles/color.d.ts +1 -1
  152. package/dist/styles/color.js +11 -8
  153. package/dist/styles/color.js.map +1 -1
  154. package/dist/styles/const.js +17 -0
  155. package/dist/styles/const.js.map +1 -0
  156. package/dist/styles/createStyle.js +28 -26
  157. package/dist/styles/createStyle.js.map +1 -1
  158. package/dist/styles/dimension.js +15 -3
  159. package/dist/styles/dimension.js.map +1 -1
  160. package/dist/styles/directional.js +133 -0
  161. package/dist/styles/directional.js.map +1 -0
  162. package/dist/styles/display.d.ts +3 -10
  163. package/dist/styles/display.js +45 -39
  164. package/dist/styles/display.js.map +1 -1
  165. package/dist/styles/fade.d.ts +1 -1
  166. package/dist/styles/fade.js +9 -5
  167. package/dist/styles/fade.js.map +1 -1
  168. package/dist/styles/fill.d.ts +2 -2
  169. package/dist/styles/fill.js +9 -9
  170. package/dist/styles/fill.js.map +1 -1
  171. package/dist/styles/flow.js +1 -1
  172. package/dist/styles/gap.d.ts +1 -1
  173. package/dist/styles/gap.js +4 -3
  174. package/dist/styles/gap.js.map +1 -1
  175. package/dist/styles/height.d.ts +1 -1
  176. package/dist/styles/height.js +1 -2
  177. package/dist/styles/height.js.map +1 -1
  178. package/dist/styles/index.d.ts +0 -1
  179. package/dist/styles/index.js +3 -4
  180. package/dist/styles/index.js.map +1 -1
  181. package/dist/styles/inset.d.ts +1 -29
  182. package/dist/styles/inset.js +19 -135
  183. package/dist/styles/inset.js.map +1 -1
  184. package/dist/styles/list.d.ts +5 -5
  185. package/dist/styles/list.js +4 -2
  186. package/dist/styles/list.js.map +1 -1
  187. package/dist/styles/margin.d.ts +1 -1
  188. package/dist/styles/margin.js +17 -89
  189. package/dist/styles/margin.js.map +1 -1
  190. package/dist/styles/outline.d.ts +1 -1
  191. package/dist/styles/outline.js +6 -16
  192. package/dist/styles/outline.js.map +1 -1
  193. package/dist/styles/padding.d.ts +1 -1
  194. package/dist/styles/padding.js +17 -89
  195. package/dist/styles/padding.js.map +1 -1
  196. package/dist/styles/placement.d.ts +37 -0
  197. package/dist/styles/placement.js +74 -0
  198. package/dist/styles/placement.js.map +1 -0
  199. package/dist/styles/predefined.d.ts +4 -6
  200. package/dist/styles/predefined.js +9 -13
  201. package/dist/styles/predefined.js.map +1 -1
  202. package/dist/styles/preset.d.ts +6 -1
  203. package/dist/styles/preset.js +54 -53
  204. package/dist/styles/preset.js.map +1 -1
  205. package/dist/styles/radius.d.ts +1 -3
  206. package/dist/styles/radius.js +38 -6
  207. package/dist/styles/radius.js.map +1 -1
  208. package/dist/styles/scrollMargin.d.ts +24 -0
  209. package/dist/styles/scrollMargin.js +32 -0
  210. package/dist/styles/scrollMargin.js.map +1 -0
  211. package/dist/styles/scrollbar.d.ts +9 -5
  212. package/dist/styles/scrollbar.js +31 -92
  213. package/dist/styles/scrollbar.js.map +1 -1
  214. package/dist/styles/shadow.d.ts +1 -1
  215. package/dist/styles/shadow.js +4 -3
  216. package/dist/styles/shadow.js.map +1 -1
  217. package/dist/styles/shared.js +17 -0
  218. package/dist/styles/shared.js.map +1 -0
  219. package/dist/styles/transition.d.ts +1 -1
  220. package/dist/styles/transition.js +6 -5
  221. package/dist/styles/transition.js.map +1 -1
  222. package/dist/styles/types.d.ts +94 -28
  223. package/dist/styles/width.d.ts +1 -1
  224. package/dist/styles/width.js +1 -2
  225. package/dist/styles/width.js.map +1 -1
  226. package/dist/tasty.d.ts +46 -894
  227. package/dist/tasty.js +101 -59
  228. package/dist/tasty.js.map +1 -1
  229. package/dist/types.d.ts +1 -1
  230. package/dist/utils/cache-wrapper.js +4 -9
  231. package/dist/utils/cache-wrapper.js.map +1 -1
  232. package/dist/utils/case-converter.js +1 -1
  233. package/dist/utils/color-math.d.ts +46 -0
  234. package/dist/utils/color-math.js +749 -0
  235. package/dist/utils/color-math.js.map +1 -0
  236. package/dist/utils/color-space.d.ts +5 -0
  237. package/dist/utils/color-space.js +228 -0
  238. package/dist/utils/color-space.js.map +1 -0
  239. package/dist/utils/colors.js +3 -2
  240. package/dist/utils/colors.js.map +1 -1
  241. package/dist/utils/deps-equal.js +15 -0
  242. package/dist/utils/deps-equal.js.map +1 -0
  243. package/dist/utils/dotize.js +1 -1
  244. package/dist/utils/dotize.js.map +1 -1
  245. package/dist/utils/filter-base-props.d.ts +1 -1
  246. package/dist/utils/filter-base-props.js +1 -1
  247. package/dist/utils/filter-base-props.js.map +1 -1
  248. package/dist/utils/get-display-name.js +1 -1
  249. package/dist/utils/has-keys.js +13 -0
  250. package/dist/utils/has-keys.js.map +1 -0
  251. package/dist/utils/hash.js +14 -0
  252. package/dist/utils/hash.js.map +1 -0
  253. package/dist/utils/is-dev-env.js +1 -1
  254. package/dist/utils/is-dev-env.js.map +1 -1
  255. package/dist/utils/is-valid-element-type.js +1 -1
  256. package/dist/utils/merge-styles.js +1 -2
  257. package/dist/utils/merge-styles.js.map +1 -1
  258. package/dist/utils/mod-attrs.d.ts +0 -2
  259. package/dist/utils/mod-attrs.js +3 -4
  260. package/dist/utils/mod-attrs.js.map +1 -1
  261. package/dist/utils/process-tokens.d.ts +3 -17
  262. package/dist/utils/process-tokens.js +19 -107
  263. package/dist/utils/process-tokens.js.map +1 -1
  264. package/dist/utils/resolve-recipes.js +1 -2
  265. package/dist/utils/resolve-recipes.js.map +1 -1
  266. package/dist/utils/selector-transform.js +32 -0
  267. package/dist/utils/selector-transform.js.map +1 -0
  268. package/dist/utils/string.js +1 -1
  269. package/dist/utils/styles.d.ts +3 -82
  270. package/dist/utils/styles.js +29 -399
  271. package/dist/utils/styles.js.map +1 -1
  272. package/dist/utils/typography.d.ts +35 -13
  273. package/dist/utils/typography.js +16 -18
  274. package/dist/utils/typography.js.map +1 -1
  275. package/dist/utils/warnings.js +1 -1
  276. package/dist/zero/babel.d.ts +57 -84
  277. package/dist/zero/babel.js +217 -37
  278. package/dist/zero/babel.js.map +1 -1
  279. package/dist/zero/css-writer.js +1 -2
  280. package/dist/zero/css-writer.js.map +1 -1
  281. package/dist/zero/extractor.js +119 -3
  282. package/dist/zero/extractor.js.map +1 -1
  283. package/dist/zero/index.js +1 -2
  284. package/dist/zero/next.d.ts +56 -30
  285. package/dist/zero/next.js +105 -40
  286. package/dist/zero/next.js.map +1 -1
  287. package/docs/README.md +31 -0
  288. package/docs/adoption.md +298 -0
  289. package/docs/comparison.md +419 -0
  290. package/docs/configuration.md +394 -0
  291. package/docs/debug.md +320 -0
  292. package/docs/design-system.md +436 -0
  293. package/docs/dsl.md +688 -0
  294. package/docs/getting-started.md +217 -0
  295. package/docs/injector.md +544 -0
  296. package/docs/methodology.md +616 -0
  297. package/docs/pipeline.md +519 -0
  298. package/docs/react-api.md +557 -0
  299. package/docs/ssr.md +442 -0
  300. package/docs/styles.md +596 -0
  301. package/docs/tasty-static.md +532 -0
  302. package/package.json +68 -36
  303. package/dist/styles/align.d.ts +0 -15
  304. package/dist/styles/align.js +0 -14
  305. package/dist/styles/align.js.map +0 -1
  306. package/dist/styles/justify.d.ts +0 -15
  307. package/dist/styles/justify.js +0 -14
  308. package/dist/styles/justify.js.map +0 -1
  309. package/dist/styles/styledScrollbar.d.ts +0 -47
  310. package/dist/styles/styledScrollbar.js +0 -38
  311. package/dist/styles/styledScrollbar.js.map +0 -1
  312. package/dist/tokens/typography.d.ts +0 -19
  313. package/dist/tokens/typography.js +0 -237
  314. package/dist/tokens/typography.js.map +0 -1
  315. package/dist/utils/hsl-to-rgb.js +0 -38
  316. package/dist/utils/hsl-to-rgb.js.map +0 -1
  317. package/dist/utils/okhsl-to-rgb.js +0 -296
  318. package/dist/utils/okhsl-to-rgb.js.map +0 -1
@@ -1,4 +1,4 @@
1
- import { CacheMetrics, GlobalInjectResult, InjectResult, KeyframesResult, KeyframesSteps, PropertyDefinition, RawCSSResult, StyleInjectorConfig } from "./types.js";
1
+ import { CacheMetrics, CounterStyleDescriptors, FontFaceDescriptors, GCOptions, GlobalInjectResult, InjectResult, KeyframesResult, KeyframesSteps, PropertyDefinition, RawCSSResult, StyleInjectorConfig } from "./types.js";
2
2
  import { StyleResult } from "../pipeline/index.js";
3
3
  import { SheetManager } from "./sheet-manager.js";
4
4
 
@@ -6,11 +6,16 @@ import { SheetManager } from "./sheet-manager.js";
6
6
  declare class StyleInjector {
7
7
  private sheetManager;
8
8
  private config;
9
- private cleanupScheduled;
10
9
  private globalRuleCounter;
10
+ private pendingGCHandle;
11
11
  /** @internal — exposed for debug utilities only */
12
12
  get _sheetManager(): SheetManager;
13
13
  constructor(config?: StyleInjectorConfig);
14
+ /**
15
+ * Check if `className` was hydrated from server-rendered styles and,
16
+ * if so, wire the cacheKey mapping. Returns true on hit.
17
+ */
18
+ private tryHydratedHit;
14
19
  /**
15
20
  * Allocate a className for a cacheKey without injecting styles yet.
16
21
  * This allows separating className allocation (render phase) from style injection (insertion phase).
@@ -51,7 +56,16 @@ declare class StyleInjector {
51
56
  root?: Document | ShadowRoot;
52
57
  }): string;
53
58
  /**
54
- * Dispose of a className
59
+ * Increment refCount for an already-injected cacheKey and return a dispose.
60
+ * Used by useStyles on cache hits (hydration or runtime reuse) where
61
+ * the pipeline was skipped but refCount tracking is still needed.
62
+ * Returns null if the cacheKey is not found.
63
+ */
64
+ trackRef(cacheKey: string, options?: {
65
+ root?: Document | ShadowRoot;
66
+ }): InjectResult | null;
67
+ /**
68
+ * Dispose of a className (decrements refCount only).
55
69
  */
56
70
  private dispose;
57
71
  /**
@@ -112,6 +126,24 @@ declare class StyleInjector {
112
126
  isPropertyDefined(name: string, options?: {
113
127
  root?: Document | ShadowRoot;
114
128
  }): boolean;
129
+ /**
130
+ * Inject a CSS @font-face rule.
131
+ *
132
+ * Permanent and global — no dispose or ref-counting.
133
+ * Deduplicates by content hash (family + descriptors).
134
+ */
135
+ fontFace(family: string, descriptors: FontFaceDescriptors, options?: {
136
+ root?: Document | ShadowRoot;
137
+ }): void;
138
+ /**
139
+ * Inject a CSS @counter-style rule.
140
+ *
141
+ * Permanent and global — no dispose or ref-counting.
142
+ * Deduplicates by name (first definition wins).
143
+ */
144
+ counterStyle(name: string, descriptors: CounterStyleDescriptors, options?: {
145
+ root?: Document | ShadowRoot;
146
+ }): void;
115
147
  /**
116
148
  * Inject keyframes and return object with toString() and dispose()
117
149
  *
@@ -129,6 +161,33 @@ declare class StyleInjector {
129
161
  * Dispose keyframes
130
162
  */
131
163
  private disposeKeyframes;
164
+ private static readonly TASTY_CLASS_RE;
165
+ /**
166
+ * Record a render-time usage hit for one or more classNames.
167
+ * Handles space-separated multi-chunk classNames.
168
+ * When the global touch counter reaches `touchInterval`, schedules a GC
169
+ * via `requestIdleCallback`.
170
+ * No-op on the server.
171
+ */
172
+ touch(className: string, options?: {
173
+ root?: Document | ShadowRoot;
174
+ }): void;
175
+ /**
176
+ * Schedule a GC via `requestIdleCallback` (or synchronously as fallback).
177
+ * Runs GC on all active roots. Avoids double-scheduling via `pendingGCHandle`.
178
+ */
179
+ private scheduleGC;
180
+ /**
181
+ * Synchronous garbage collection.
182
+ *
183
+ * 1. Quick upper-bound check: skip if unused count can't exceed capacity.
184
+ * 2. Scans the DOM for live tasty classNames (safety guard).
185
+ * 3. With `force: true`: deletes all unused entries inline.
186
+ * Without `force`: collects unused, sorts oldest-first, evicts over capacity.
187
+ *
188
+ * @returns Number of styles evicted.
189
+ */
190
+ gc(options?: GCOptions): number;
132
191
  /**
133
192
  * Destroy all resources for a root
134
193
  */
@@ -1,20 +1,73 @@
1
+ import { getEffectiveDefinition, normalizePropertyDefinition } from "../properties/index.js";
2
+ import { hashString } from "../utils/hash.js";
1
3
  import { isDevEnv } from "../utils/is-dev-env.js";
2
4
  import { parseStyle } from "../utils/styles.js";
3
- import { colorInitialValueToRgb, getEffectiveDefinition, normalizePropertyDefinition } from "../properties/index.js";
4
5
  import { SheetManager } from "./sheet-manager.js";
5
-
6
+ import { fontFaceContentHash, formatFontFaceDeclarations } from "../font-face/index.js";
7
+ import { formatCounterStyleDeclarations } from "../counter-style/index.js";
6
8
  //#region src/injector/injector.ts
7
9
  /**
8
- * Generate sequential class name with format t{number}
10
+ * Generate a deterministic class name from a cache key using content hash.
11
+ * The same cache key always produces the same class name across environments.
9
12
  */
10
- function generateClassName(counter) {
11
- return `t${counter}`;
13
+ function generateClassName(cacheKey) {
14
+ return `t${hashString(cacheKey)}`;
12
15
  }
13
- var StyleInjector = class {
16
+ const RSC_CLASS_RE = /\.(t[a-z0-9]+)\.\1/g;
17
+ /**
18
+ * Extract class names from `<style data-tasty-rsc>` tags.
19
+ * The doubled-specificity pattern `.tXXX.tXXX` makes extraction reliable.
20
+ */
21
+ function extractRSCClassNames() {
22
+ if (typeof document === "undefined") return [];
23
+ const styles = document.querySelectorAll("style[data-tasty-rsc]");
24
+ if (styles.length === 0) return [];
25
+ const classSet = /* @__PURE__ */ new Set();
26
+ for (const style of styles) {
27
+ const text = style.textContent;
28
+ if (!text) continue;
29
+ let match;
30
+ RSC_CLASS_RE.lastIndex = 0;
31
+ while ((match = RSC_CLASS_RE.exec(text)) !== null) classSet.add(match[1]);
32
+ }
33
+ return Array.from(classSet);
34
+ }
35
+ /**
36
+ * Lazily sync server-rendered class names into the client registry.
37
+ *
38
+ * Sources:
39
+ * 1. `window.__TASTY__` — pushed by SSR/RSC streaming scripts
40
+ * 2. `<style data-tasty-rsc>` tags — inline CSS emitted by RSC components
41
+ *
42
+ * Called inside `inject()` / `allocateClassName()` to pick up
43
+ * class names rendered on the server (including during SPA navigation).
44
+ */
45
+ function syncServerClasses(registry) {
46
+ if (typeof window === "undefined") return;
47
+ const classes = window.__TASTY__;
48
+ if (classes && classes.length > registry.serverClassSyncIndex) {
49
+ for (let i = registry.serverClassSyncIndex; i < classes.length; i++) registerHydratedClass(registry, classes[i]);
50
+ registry.serverClassSyncIndex = classes.length;
51
+ }
52
+ if (!registry.rscStylesScanned) {
53
+ registry.rscStylesScanned = true;
54
+ for (const cls of extractRSCClassNames()) registerHydratedClass(registry, cls);
55
+ }
56
+ }
57
+ function registerHydratedClass(registry, className) {
58
+ if (registry.rules.has(className)) return;
59
+ registry.rules.set(className, {
60
+ className,
61
+ ruleIndex: -2,
62
+ sheetIndex: -2
63
+ });
64
+ registry.refCounts.set(className, 0);
65
+ }
66
+ var StyleInjector = class StyleInjector {
14
67
  sheetManager;
15
68
  config;
16
- cleanupScheduled = false;
17
69
  globalRuleCounter = 0;
70
+ pendingGCHandle = null;
18
71
  /** @internal — exposed for debug utilities only */
19
72
  get _sheetManager() {
20
73
  return this.sheetManager;
@@ -24,6 +77,19 @@ var StyleInjector = class {
24
77
  this.sheetManager = new SheetManager(config);
25
78
  }
26
79
  /**
80
+ * Check if `className` was hydrated from server-rendered styles and,
81
+ * if so, wire the cacheKey mapping. Returns true on hit.
82
+ */
83
+ tryHydratedHit(registry, cacheKey, className) {
84
+ syncServerClasses(registry);
85
+ const rule = registry.rules.get(className);
86
+ if (rule && rule.ruleIndex === -2 && rule.sheetIndex === -2) {
87
+ registry.cacheKeyToClassName.set(cacheKey, className);
88
+ return true;
89
+ }
90
+ return false;
91
+ }
92
+ /**
27
93
  * Allocate a className for a cacheKey without injecting styles yet.
28
94
  * This allows separating className allocation (render phase) from style injection (insertion phase).
29
95
  */
@@ -34,7 +100,19 @@ var StyleInjector = class {
34
100
  className: registry.cacheKeyToClassName.get(cacheKey),
35
101
  isNewAllocation: false
36
102
  };
37
- const className = generateClassName(registry.classCounter++);
103
+ const className = generateClassName(cacheKey);
104
+ if (this.tryHydratedHit(registry, cacheKey, className)) return {
105
+ className,
106
+ isNewAllocation: false
107
+ };
108
+ if (registry.rules.get(className)) {
109
+ if (isDevEnv()) console.warn(`[tasty] Hash collision: cache keys produce the same class "${className}". Styles may be incorrect.`);
110
+ registry.cacheKeyToClassName.set(cacheKey, className);
111
+ return {
112
+ className,
113
+ isNewAllocation: false
114
+ };
115
+ }
38
116
  const placeholderRuleInfo = {
39
117
  className,
40
118
  ruleIndex: -1,
@@ -73,7 +151,17 @@ var StyleInjector = class {
73
151
  dispose: () => this.dispose(className, registry)
74
152
  };
75
153
  }
76
- } else className = generateClassName(registry.classCounter++);
154
+ } else if (cacheKey) {
155
+ className = generateClassName(cacheKey);
156
+ if (this.tryHydratedHit(registry, cacheKey, className)) {
157
+ registry.refCounts.set(className, (registry.refCounts.get(className) || 0) + 1);
158
+ if (registry.metrics) registry.metrics.hits++;
159
+ return {
160
+ className,
161
+ dispose: () => this.dispose(className, registry)
162
+ };
163
+ }
164
+ } else className = `t${hashString(rules.map((r) => `${r.selector}\0${r.declarations}`).join("\n"))}`;
77
165
  const rulesToInsert = rules.map((rule) => {
78
166
  let newSelector = rule.selector;
79
167
  if (rule.needsClassName) {
@@ -92,21 +180,19 @@ var StyleInjector = class {
92
180
  rootPrefix: void 0
93
181
  };
94
182
  });
95
- const colorPropRegex = /^\s*(--[a-z0-9-]+-color)\s*:/i;
96
- for (const rule of rulesToInsert) {
97
- if (!rule.declarations) continue;
98
- const parts = rule.declarations.split(/;+\s*/);
99
- for (const part of parts) {
100
- if (!part) continue;
101
- const match = colorPropRegex.exec(part);
102
- if (match) {
103
- const propName = match[1];
104
- if (!this.isPropertyDefined(propName, { root })) this.property(propName, {
105
- syntax: "<color>",
106
- initialValue: "transparent",
183
+ if (this.config.autoPropertyTypes !== false) {
184
+ const resolver = registry.propertyTypeResolver;
185
+ const defined = registry.injectedProperties;
186
+ for (const rule of rulesToInsert) {
187
+ if (!rule.declarations) continue;
188
+ resolver.scanDeclarations(rule.declarations, (name) => defined.has(name), (name, syntax, initialValue) => {
189
+ this.property(name, {
190
+ syntax,
191
+ inherits: true,
192
+ initialValue,
107
193
  root
108
194
  });
109
- }
195
+ });
110
196
  }
111
197
  }
112
198
  const ruleInfo = this.sheetManager.insertRule(registry, rulesToInsert, className, root);
@@ -141,6 +227,21 @@ var StyleInjector = class {
141
227
  const root = options?.root || document;
142
228
  const registry = this.sheetManager.getRegistry(root);
143
229
  if (!rules || rules.length === 0) return { dispose: () => {} };
230
+ if (this.config.autoPropertyTypes !== false) {
231
+ const resolver = registry.propertyTypeResolver;
232
+ const defined = registry.injectedProperties;
233
+ for (const rule of rules) {
234
+ if (!rule.declarations) continue;
235
+ resolver.scanDeclarations(rule.declarations, (name) => defined.has(name), (name, syntax, initialValue) => {
236
+ this.property(name, {
237
+ syntax,
238
+ inherits: true,
239
+ initialValue,
240
+ root
241
+ });
242
+ });
243
+ }
244
+ }
144
245
  const key = `global:${this.globalRuleCounter++}`;
145
246
  const info = this.sheetManager.insertGlobalRule(registry, rules, key, root);
146
247
  if (registry.metrics) registry.metrics.totalInsertions++;
@@ -165,17 +266,33 @@ var StyleInjector = class {
165
266
  return this.sheetManager.getRawCSSText(root);
166
267
  }
167
268
  /**
168
- * Dispose of a className
269
+ * Increment refCount for an already-injected cacheKey and return a dispose.
270
+ * Used by useStyles on cache hits (hydration or runtime reuse) where
271
+ * the pipeline was skipped but refCount tracking is still needed.
272
+ * Returns null if the cacheKey is not found.
273
+ */
274
+ trackRef(cacheKey, options) {
275
+ const root = options?.root || document;
276
+ const registry = this.sheetManager.getRegistry(root);
277
+ if (!registry.cacheKeyToClassName.has(cacheKey)) return null;
278
+ const className = registry.cacheKeyToClassName.get(cacheKey);
279
+ const currentRefCount = registry.refCounts.get(className) || 0;
280
+ registry.refCounts.set(className, currentRefCount + 1);
281
+ if (registry.metrics) registry.metrics.hits++;
282
+ return {
283
+ className,
284
+ dispose: () => this.dispose(className, registry)
285
+ };
286
+ }
287
+ /**
288
+ * Dispose of a className (decrements refCount only).
169
289
  */
170
290
  dispose(className, registry) {
171
291
  const currentRefCount = registry.refCounts.get(className);
172
292
  if (currentRefCount == null || currentRefCount <= 0) return;
173
293
  const newRefCount = currentRefCount - 1;
174
294
  registry.refCounts.set(className, newRefCount);
175
- if (newRefCount === 0) {
176
- if (registry.metrics) registry.metrics.totalUnused++;
177
- this.sheetManager.checkCleanupNeeded(registry);
178
- }
295
+ if (newRefCount === 0 && registry.metrics) registry.metrics.totalUnused++;
179
296
  }
180
297
  /**
181
298
  * Force bulk cleanup of unused styles
@@ -262,13 +379,7 @@ var StyleInjector = class {
262
379
  const cssName = effectiveResult.cssName;
263
380
  const definition = effectiveResult.definition;
264
381
  const normalizedDef = normalizePropertyDefinition(definition);
265
- const existingDef = registry.injectedProperties.get(cssName);
266
- if (existingDef !== void 0) {
267
- if (existingDef !== normalizedDef) {
268
- if (isDevEnv()) console.warn(`[Tasty] @property ${cssName} was already defined with a different declaration. The new declaration will be ignored. Original: ${existingDef}, New: ${normalizedDef}`);
269
- }
270
- return;
271
- }
382
+ if (registry.injectedProperties.get(cssName) !== void 0) return;
272
383
  const parts = [];
273
384
  if (definition.syntax != null) {
274
385
  let syntax = String(definition.syntax).trim();
@@ -290,15 +401,6 @@ var StyleInjector = class {
290
401
  };
291
402
  if (!this.sheetManager.insertGlobalRule(registry, [rule], `property:${name}`, root)) return;
292
403
  registry.injectedProperties.set(cssName, normalizedDef);
293
- if (effectiveResult.isColor) {
294
- const rgbCssName = `${cssName}-rgb`;
295
- if (!this.isPropertyDefined(rgbCssName, { root })) this.property(rgbCssName, {
296
- syntax: "<number>+",
297
- inherits: definition.inherits,
298
- initialValue: colorInitialValueToRgb(definition.initialValue),
299
- root
300
- });
301
- }
302
404
  }
303
405
  /**
304
406
  * Check whether a given @property name was already injected by this injector.
@@ -316,6 +418,39 @@ var StyleInjector = class {
316
418
  return registry.injectedProperties.has(effectiveResult.cssName);
317
419
  }
318
420
  /**
421
+ * Inject a CSS @font-face rule.
422
+ *
423
+ * Permanent and global — no dispose or ref-counting.
424
+ * Deduplicates by content hash (family + descriptors).
425
+ */
426
+ fontFace(family, descriptors, options) {
427
+ const root = options?.root || document;
428
+ const registry = this.sheetManager.getRegistry(root);
429
+ const hash = fontFaceContentHash(family, descriptors);
430
+ if (registry.injectedFontFaces.has(hash)) return;
431
+ const rule = {
432
+ selector: "@font-face",
433
+ declarations: formatFontFaceDeclarations(family, descriptors)
434
+ };
435
+ if (this.sheetManager.insertGlobalRule(registry, [rule], `fontface:${hash}`, root)) registry.injectedFontFaces.add(hash);
436
+ }
437
+ /**
438
+ * Inject a CSS @counter-style rule.
439
+ *
440
+ * Permanent and global — no dispose or ref-counting.
441
+ * Deduplicates by name (first definition wins).
442
+ */
443
+ counterStyle(name, descriptors, options) {
444
+ const root = options?.root || document;
445
+ const registry = this.sheetManager.getRegistry(root);
446
+ if (registry.injectedCounterStyles.has(name)) return;
447
+ const rule = {
448
+ selector: `@counter-style ${name}`,
449
+ declarations: formatCounterStyleDeclarations(descriptors)
450
+ };
451
+ if (this.sheetManager.insertGlobalRule(registry, [rule], `counterstyle:${name}`, root)) registry.injectedCounterStyles.add(name);
452
+ }
453
+ /**
319
454
  * Inject keyframes and return object with toString() and dispose()
320
455
  *
321
456
  * Keyframes are cached by content (steps). If the same content is injected
@@ -351,11 +486,20 @@ var StyleInjector = class {
351
486
  registry.keyframesNameToContent.set(providedName, contentHash);
352
487
  }
353
488
  } else actualName = `k${registry.keyframesCounter++}`;
354
- const info = this.sheetManager.insertKeyframes(registry, steps, actualName, root);
355
- if (!info) return {
489
+ const result = this.sheetManager.insertKeyframes(registry, steps, actualName, root);
490
+ if (!result) return {
356
491
  toString: () => "",
357
492
  dispose: () => {}
358
493
  };
494
+ const { info, declarations } = result;
495
+ if (this.config.autoPropertyTypes !== false && declarations) registry.propertyTypeResolver.scanDeclarations(declarations, (name) => registry.injectedProperties.has(name), (name, syntax, initialValue) => {
496
+ this.property(name, {
497
+ syntax,
498
+ inherits: true,
499
+ initialValue,
500
+ root
501
+ });
502
+ });
359
503
  registry.keyframesCache.set(contentHash, {
360
504
  name: actualName,
361
505
  refCount: 1,
@@ -390,15 +534,118 @@ var StyleInjector = class {
390
534
  }
391
535
  }
392
536
  }
537
+ static TASTY_CLASS_RE = /^t[a-z0-9]+$/;
538
+ /**
539
+ * Record a render-time usage hit for one or more classNames.
540
+ * Handles space-separated multi-chunk classNames.
541
+ * When the global touch counter reaches `touchInterval`, schedules a GC
542
+ * via `requestIdleCallback`.
543
+ * No-op on the server.
544
+ */
545
+ touch(className, options) {
546
+ if (typeof document === "undefined") return;
547
+ if (!this.config.gc) return;
548
+ const root = options?.root || document;
549
+ const registry = this.sheetManager.getRegistry(root);
550
+ const now = Date.now();
551
+ const parts = className.indexOf(" ") === -1 ? [className] : className.split(" ");
552
+ for (const cls of parts) {
553
+ if (!StyleInjector.TASTY_CLASS_RE.test(cls)) continue;
554
+ if (!registry.rules.has(cls)) continue;
555
+ const entry = registry.usageMap.get(cls);
556
+ if (entry) entry.lastTouchedAt = now;
557
+ else registry.usageMap.set(cls, { lastTouchedAt: now });
558
+ registry.touchCount++;
559
+ }
560
+ const touchInterval = this.config.gc.touchInterval ?? 1e3;
561
+ if (registry.touchCount >= touchInterval) {
562
+ registry.touchCount = 0;
563
+ this.scheduleGC();
564
+ }
565
+ }
566
+ /**
567
+ * Schedule a GC via `requestIdleCallback` (or synchronously as fallback).
568
+ * Runs GC on all active roots. Avoids double-scheduling via `pendingGCHandle`.
569
+ */
570
+ scheduleGC() {
571
+ if (this.pendingGCHandle != null) return;
572
+ const runGC = () => {
573
+ this.pendingGCHandle = null;
574
+ this.sheetManager.pruneDisconnectedRoots();
575
+ for (const root of this.sheetManager.getActiveRoots()) this.gc({ root });
576
+ };
577
+ if (typeof requestIdleCallback !== "undefined") this.pendingGCHandle = requestIdleCallback(() => runGC());
578
+ else runGC();
579
+ }
580
+ /**
581
+ * Synchronous garbage collection.
582
+ *
583
+ * 1. Quick upper-bound check: skip if unused count can't exceed capacity.
584
+ * 2. Scans the DOM for live tasty classNames (safety guard).
585
+ * 3. With `force: true`: deletes all unused entries inline.
586
+ * Without `force`: collects unused, sorts oldest-first, evicts over capacity.
587
+ *
588
+ * @returns Number of styles evicted.
589
+ */
590
+ gc(options) {
591
+ if (typeof document === "undefined") return 0;
592
+ if (this.pendingGCHandle != null) {
593
+ if (typeof cancelIdleCallback !== "undefined") cancelIdleCallback(this.pendingGCHandle);
594
+ this.pendingGCHandle = null;
595
+ }
596
+ const root = options?.root || document;
597
+ const force = options?.force ?? false;
598
+ const registry = this.sheetManager.getRegistry(root);
599
+ const capacity = this.config.gc?.capacity ?? 1e3;
600
+ if (!force) {
601
+ let activeCount = 0;
602
+ for (const refCount of registry.refCounts.values()) if (refCount > 0) activeCount++;
603
+ if (registry.usageMap.size - activeCount <= capacity) return 0;
604
+ }
605
+ const liveClasses = /* @__PURE__ */ new Set();
606
+ for (const el of root.querySelectorAll("[class]")) for (const token of el.classList) if (StyleInjector.TASTY_CLASS_RE.test(token)) liveClasses.add(token);
607
+ let swept = 0;
608
+ if (force) for (const [className] of registry.usageMap) {
609
+ if (liveClasses.has(className)) continue;
610
+ if ((registry.refCounts.get(className) ?? 0) > 0) continue;
611
+ registry.usageMap.delete(className);
612
+ swept++;
613
+ }
614
+ else {
615
+ const unused = [];
616
+ for (const [className, usage] of registry.usageMap) {
617
+ if (liveClasses.has(className)) continue;
618
+ if ((registry.refCounts.get(className) ?? 0) > 0) continue;
619
+ unused.push({
620
+ className,
621
+ lastTouchedAt: usage.lastTouchedAt
622
+ });
623
+ }
624
+ if (unused.length > capacity) {
625
+ unused.sort((a, b) => a.lastTouchedAt - b.lastTouchedAt);
626
+ const toEvict = unused.length - capacity;
627
+ for (let i = 0; i < toEvict; i++) {
628
+ registry.usageMap.delete(unused[i].className);
629
+ swept++;
630
+ }
631
+ }
632
+ }
633
+ if (swept > 0) this.sheetManager.forceCleanup(registry);
634
+ return swept;
635
+ }
393
636
  /**
394
637
  * Destroy all resources for a root
395
638
  */
396
639
  destroy(root) {
397
640
  const targetRoot = root || document;
398
641
  this.sheetManager.cleanup(targetRoot);
642
+ if (this.pendingGCHandle != null && !this.sheetManager.hasActiveRoots()) {
643
+ if (typeof cancelIdleCallback !== "undefined") cancelIdleCallback(this.pendingGCHandle);
644
+ this.pendingGCHandle = null;
645
+ }
399
646
  }
400
647
  };
401
-
402
648
  //#endregion
403
649
  export { StyleInjector };
650
+
404
651
  //# sourceMappingURL=injector.js.map