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

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 (303) hide show
  1. package/README.md +314 -159
  2. package/dist/async-storage-B7_o6FKt.js +44 -0
  3. package/dist/async-storage-B7_o6FKt.js.map +1 -0
  4. package/dist/collector-C-keQH9m.js +243 -0
  5. package/dist/collector-C-keQH9m.js.map +1 -0
  6. package/dist/collector-osfWTeRd.d.ts +108 -0
  7. package/dist/config-BBiyxMCe.js +10559 -0
  8. package/dist/config-BBiyxMCe.js.map +1 -0
  9. package/dist/config-BoZDUHW5.d.ts +945 -0
  10. package/dist/context-CkSg-kDT.js +24 -0
  11. package/dist/context-CkSg-kDT.js.map +1 -0
  12. package/dist/core/index.d.ts +5 -33
  13. package/dist/core/index.js +6 -26
  14. package/dist/core-BO4319td.js +1598 -0
  15. package/dist/core-BO4319td.js.map +1 -0
  16. package/dist/css-writer-BWvwQzz0.js +351 -0
  17. package/dist/css-writer-BWvwQzz0.js.map +1 -0
  18. package/dist/format-global-rules-Dbc_1tc3.js +22 -0
  19. package/dist/format-global-rules-Dbc_1tc3.js.map +1 -0
  20. package/dist/format-rules-BSjeH4Z7.js +143 -0
  21. package/dist/format-rules-BSjeH4Z7.js.map +1 -0
  22. package/dist/hydrate-CcvrP4qJ.js +45 -0
  23. package/dist/hydrate-CcvrP4qJ.js.map +1 -0
  24. package/dist/index-B_k47mc_.d.ts +1655 -0
  25. package/dist/index-tcHuMPFt.d.ts +1286 -0
  26. package/dist/index.d.ts +5 -48
  27. package/dist/index.js +731 -32
  28. package/dist/index.js.map +1 -0
  29. package/dist/keyframes-BUQhdOSJ.js +588 -0
  30. package/dist/keyframes-BUQhdOSJ.js.map +1 -0
  31. package/dist/{utils/merge-styles.d.ts → merge-styles-BMWcH6MF.d.ts} +3 -3
  32. package/dist/{utils/merge-styles.js → merge-styles-Cd2vBl9b.js} +4 -6
  33. package/dist/merge-styles-Cd2vBl9b.js.map +1 -0
  34. package/dist/{utils/resolve-recipes.js → resolve-recipes-C1nrvnYh.js} +5 -8
  35. package/dist/resolve-recipes-C1nrvnYh.js.map +1 -0
  36. package/dist/ssr/astro-client.d.ts +1 -0
  37. package/dist/ssr/astro-client.js +19 -0
  38. package/dist/ssr/astro-client.js.map +1 -0
  39. package/dist/ssr/astro-middleware.d.ts +15 -0
  40. package/dist/ssr/astro-middleware.js +19 -0
  41. package/dist/ssr/astro-middleware.js.map +1 -0
  42. package/dist/ssr/astro.d.ts +97 -0
  43. package/dist/ssr/astro.js +149 -0
  44. package/dist/ssr/astro.js.map +1 -0
  45. package/dist/ssr/index.d.ts +44 -0
  46. package/dist/ssr/index.js +10 -0
  47. package/dist/ssr/index.js.map +1 -0
  48. package/dist/ssr/next.d.ts +46 -0
  49. package/dist/ssr/next.js +75 -0
  50. package/dist/ssr/next.js.map +1 -0
  51. package/dist/static/index.d.ts +91 -5
  52. package/dist/static/index.js +49 -4
  53. package/dist/static/index.js.map +1 -0
  54. package/dist/static/inject.d.ts +5 -0
  55. package/dist/static/inject.js +17 -0
  56. package/dist/static/inject.js.map +1 -0
  57. package/dist/zero/babel.d.ts +57 -84
  58. package/dist/zero/babel.js +232 -40
  59. package/dist/zero/babel.js.map +1 -1
  60. package/dist/zero/index.d.ts +67 -3
  61. package/dist/zero/index.js +2 -4
  62. package/dist/zero/next.d.ts +56 -30
  63. package/dist/zero/next.js +105 -40
  64. package/dist/zero/next.js.map +1 -1
  65. package/docs/README.md +31 -0
  66. package/docs/adoption.md +298 -0
  67. package/docs/comparison.md +419 -0
  68. package/docs/configuration.md +438 -0
  69. package/docs/debug.md +320 -0
  70. package/docs/design-system.md +436 -0
  71. package/docs/dsl.md +690 -0
  72. package/docs/getting-started.md +217 -0
  73. package/docs/injector.md +544 -0
  74. package/docs/methodology.md +642 -0
  75. package/docs/pipeline.md +699 -0
  76. package/docs/react-api.md +581 -0
  77. package/docs/ssr.md +444 -0
  78. package/docs/styles.md +598 -0
  79. package/docs/tasty-static.md +547 -0
  80. package/package.json +70 -37
  81. package/tasty.config.ts +1 -0
  82. package/dist/_virtual/_rolldown/runtime.js +0 -8
  83. package/dist/chunks/cacheKey.js +0 -70
  84. package/dist/chunks/cacheKey.js.map +0 -1
  85. package/dist/chunks/definitions.d.ts +0 -37
  86. package/dist/chunks/definitions.js +0 -260
  87. package/dist/chunks/definitions.js.map +0 -1
  88. package/dist/chunks/renderChunk.js +0 -61
  89. package/dist/chunks/renderChunk.js.map +0 -1
  90. package/dist/config.d.ts +0 -280
  91. package/dist/config.js +0 -403
  92. package/dist/config.js.map +0 -1
  93. package/dist/debug.d.ts +0 -204
  94. package/dist/debug.js +0 -733
  95. package/dist/debug.js.map +0 -1
  96. package/dist/hooks/useGlobalStyles.d.ts +0 -27
  97. package/dist/hooks/useGlobalStyles.js +0 -56
  98. package/dist/hooks/useGlobalStyles.js.map +0 -1
  99. package/dist/hooks/useKeyframes.d.ts +0 -56
  100. package/dist/hooks/useKeyframes.js +0 -54
  101. package/dist/hooks/useKeyframes.js.map +0 -1
  102. package/dist/hooks/useProperty.d.ts +0 -79
  103. package/dist/hooks/useProperty.js +0 -91
  104. package/dist/hooks/useProperty.js.map +0 -1
  105. package/dist/hooks/useRawCSS.d.ts +0 -53
  106. package/dist/hooks/useRawCSS.js +0 -28
  107. package/dist/hooks/useRawCSS.js.map +0 -1
  108. package/dist/hooks/useStyles.d.ts +0 -40
  109. package/dist/hooks/useStyles.js +0 -169
  110. package/dist/hooks/useStyles.js.map +0 -1
  111. package/dist/injector/index.d.ts +0 -157
  112. package/dist/injector/index.js +0 -154
  113. package/dist/injector/index.js.map +0 -1
  114. package/dist/injector/injector.d.ts +0 -139
  115. package/dist/injector/injector.js +0 -404
  116. package/dist/injector/injector.js.map +0 -1
  117. package/dist/injector/sheet-manager.d.ts +0 -127
  118. package/dist/injector/sheet-manager.js +0 -714
  119. package/dist/injector/sheet-manager.js.map +0 -1
  120. package/dist/injector/types.d.ts +0 -135
  121. package/dist/keyframes/index.js +0 -206
  122. package/dist/keyframes/index.js.map +0 -1
  123. package/dist/parser/classify.js +0 -319
  124. package/dist/parser/classify.js.map +0 -1
  125. package/dist/parser/const.js +0 -33
  126. package/dist/parser/const.js.map +0 -1
  127. package/dist/parser/lru.js +0 -109
  128. package/dist/parser/lru.js.map +0 -1
  129. package/dist/parser/parser.d.ts +0 -25
  130. package/dist/parser/parser.js +0 -116
  131. package/dist/parser/parser.js.map +0 -1
  132. package/dist/parser/tokenizer.js +0 -69
  133. package/dist/parser/tokenizer.js.map +0 -1
  134. package/dist/parser/types.d.ts +0 -51
  135. package/dist/parser/types.js +0 -46
  136. package/dist/parser/types.js.map +0 -1
  137. package/dist/pipeline/conditions.d.ts +0 -134
  138. package/dist/pipeline/conditions.js +0 -406
  139. package/dist/pipeline/conditions.js.map +0 -1
  140. package/dist/pipeline/exclusive.js +0 -231
  141. package/dist/pipeline/exclusive.js.map +0 -1
  142. package/dist/pipeline/index.d.ts +0 -53
  143. package/dist/pipeline/index.js +0 -660
  144. package/dist/pipeline/index.js.map +0 -1
  145. package/dist/pipeline/materialize.js +0 -844
  146. package/dist/pipeline/materialize.js.map +0 -1
  147. package/dist/pipeline/parseStateKey.d.ts +0 -15
  148. package/dist/pipeline/parseStateKey.js +0 -438
  149. package/dist/pipeline/parseStateKey.js.map +0 -1
  150. package/dist/pipeline/simplify.js +0 -516
  151. package/dist/pipeline/simplify.js.map +0 -1
  152. package/dist/pipeline/warnings.js +0 -18
  153. package/dist/pipeline/warnings.js.map +0 -1
  154. package/dist/plugins/okhsl-plugin.d.ts +0 -35
  155. package/dist/plugins/okhsl-plugin.js +0 -371
  156. package/dist/plugins/okhsl-plugin.js.map +0 -1
  157. package/dist/plugins/types.d.ts +0 -69
  158. package/dist/properties/index.js +0 -158
  159. package/dist/properties/index.js.map +0 -1
  160. package/dist/states/index.d.ts +0 -49
  161. package/dist/states/index.js +0 -416
  162. package/dist/states/index.js.map +0 -1
  163. package/dist/static/tastyStatic.d.ts +0 -46
  164. package/dist/static/tastyStatic.js +0 -31
  165. package/dist/static/tastyStatic.js.map +0 -1
  166. package/dist/static/types.d.ts +0 -49
  167. package/dist/static/types.js +0 -24
  168. package/dist/static/types.js.map +0 -1
  169. package/dist/styles/align.d.ts +0 -15
  170. package/dist/styles/align.js +0 -14
  171. package/dist/styles/align.js.map +0 -1
  172. package/dist/styles/border.d.ts +0 -25
  173. package/dist/styles/border.js +0 -114
  174. package/dist/styles/border.js.map +0 -1
  175. package/dist/styles/color.d.ts +0 -14
  176. package/dist/styles/color.js +0 -23
  177. package/dist/styles/color.js.map +0 -1
  178. package/dist/styles/createStyle.js +0 -77
  179. package/dist/styles/createStyle.js.map +0 -1
  180. package/dist/styles/dimension.js +0 -97
  181. package/dist/styles/dimension.js.map +0 -1
  182. package/dist/styles/display.d.ts +0 -37
  183. package/dist/styles/display.js +0 -67
  184. package/dist/styles/display.js.map +0 -1
  185. package/dist/styles/fade.d.ts +0 -15
  186. package/dist/styles/fade.js +0 -58
  187. package/dist/styles/fade.js.map +0 -1
  188. package/dist/styles/fill.d.ts +0 -42
  189. package/dist/styles/fill.js +0 -51
  190. package/dist/styles/fill.js.map +0 -1
  191. package/dist/styles/flow.d.ts +0 -16
  192. package/dist/styles/flow.js +0 -12
  193. package/dist/styles/flow.js.map +0 -1
  194. package/dist/styles/gap.d.ts +0 -31
  195. package/dist/styles/gap.js +0 -37
  196. package/dist/styles/gap.js.map +0 -1
  197. package/dist/styles/height.d.ts +0 -17
  198. package/dist/styles/height.js +0 -20
  199. package/dist/styles/height.js.map +0 -1
  200. package/dist/styles/index.d.ts +0 -2
  201. package/dist/styles/index.js +0 -9
  202. package/dist/styles/index.js.map +0 -1
  203. package/dist/styles/inset.d.ts +0 -52
  204. package/dist/styles/inset.js +0 -150
  205. package/dist/styles/inset.js.map +0 -1
  206. package/dist/styles/justify.d.ts +0 -15
  207. package/dist/styles/justify.js +0 -14
  208. package/dist/styles/justify.js.map +0 -1
  209. package/dist/styles/list.d.ts +0 -16
  210. package/dist/styles/list.js +0 -98
  211. package/dist/styles/list.js.map +0 -1
  212. package/dist/styles/margin.d.ts +0 -24
  213. package/dist/styles/margin.js +0 -104
  214. package/dist/styles/margin.js.map +0 -1
  215. package/dist/styles/outline.d.ts +0 -29
  216. package/dist/styles/outline.js +0 -65
  217. package/dist/styles/outline.js.map +0 -1
  218. package/dist/styles/padding.d.ts +0 -24
  219. package/dist/styles/padding.js +0 -104
  220. package/dist/styles/padding.js.map +0 -1
  221. package/dist/styles/predefined.d.ts +0 -73
  222. package/dist/styles/predefined.js +0 -241
  223. package/dist/styles/predefined.js.map +0 -1
  224. package/dist/styles/preset.d.ts +0 -47
  225. package/dist/styles/preset.js +0 -126
  226. package/dist/styles/preset.js.map +0 -1
  227. package/dist/styles/radius.d.ts +0 -14
  228. package/dist/styles/radius.js +0 -51
  229. package/dist/styles/radius.js.map +0 -1
  230. package/dist/styles/scrollbar.d.ts +0 -21
  231. package/dist/styles/scrollbar.js +0 -112
  232. package/dist/styles/scrollbar.js.map +0 -1
  233. package/dist/styles/shadow.d.ts +0 -14
  234. package/dist/styles/shadow.js +0 -24
  235. package/dist/styles/shadow.js.map +0 -1
  236. package/dist/styles/styledScrollbar.d.ts +0 -47
  237. package/dist/styles/styledScrollbar.js +0 -38
  238. package/dist/styles/styledScrollbar.js.map +0 -1
  239. package/dist/styles/transition.d.ts +0 -14
  240. package/dist/styles/transition.js +0 -158
  241. package/dist/styles/transition.js.map +0 -1
  242. package/dist/styles/types.d.ts +0 -498
  243. package/dist/styles/width.d.ts +0 -17
  244. package/dist/styles/width.js +0 -20
  245. package/dist/styles/width.js.map +0 -1
  246. package/dist/tasty.d.ts +0 -982
  247. package/dist/tasty.js +0 -206
  248. package/dist/tasty.js.map +0 -1
  249. package/dist/tokens/typography.d.ts +0 -19
  250. package/dist/tokens/typography.js +0 -237
  251. package/dist/tokens/typography.js.map +0 -1
  252. package/dist/types.d.ts +0 -184
  253. package/dist/utils/cache-wrapper.js +0 -26
  254. package/dist/utils/cache-wrapper.js.map +0 -1
  255. package/dist/utils/case-converter.js +0 -8
  256. package/dist/utils/case-converter.js.map +0 -1
  257. package/dist/utils/colors.d.ts +0 -5
  258. package/dist/utils/colors.js +0 -9
  259. package/dist/utils/colors.js.map +0 -1
  260. package/dist/utils/css-types.d.ts +0 -7
  261. package/dist/utils/dotize.d.ts +0 -26
  262. package/dist/utils/dotize.js +0 -122
  263. package/dist/utils/dotize.js.map +0 -1
  264. package/dist/utils/filter-base-props.d.ts +0 -15
  265. package/dist/utils/filter-base-props.js +0 -45
  266. package/dist/utils/filter-base-props.js.map +0 -1
  267. package/dist/utils/get-display-name.d.ts +0 -7
  268. package/dist/utils/get-display-name.js +0 -10
  269. package/dist/utils/get-display-name.js.map +0 -1
  270. package/dist/utils/hsl-to-rgb.js +0 -38
  271. package/dist/utils/hsl-to-rgb.js.map +0 -1
  272. package/dist/utils/is-dev-env.js +0 -19
  273. package/dist/utils/is-dev-env.js.map +0 -1
  274. package/dist/utils/is-valid-element-type.js +0 -15
  275. package/dist/utils/is-valid-element-type.js.map +0 -1
  276. package/dist/utils/merge-styles.js.map +0 -1
  277. package/dist/utils/mod-attrs.d.ts +0 -8
  278. package/dist/utils/mod-attrs.js +0 -21
  279. package/dist/utils/mod-attrs.js.map +0 -1
  280. package/dist/utils/okhsl-to-rgb.js +0 -296
  281. package/dist/utils/okhsl-to-rgb.js.map +0 -1
  282. package/dist/utils/process-tokens.d.ts +0 -31
  283. package/dist/utils/process-tokens.js +0 -171
  284. package/dist/utils/process-tokens.js.map +0 -1
  285. package/dist/utils/resolve-recipes.d.ts +0 -17
  286. package/dist/utils/resolve-recipes.js.map +0 -1
  287. package/dist/utils/string.js +0 -8
  288. package/dist/utils/string.js.map +0 -1
  289. package/dist/utils/styles.d.ts +0 -178
  290. package/dist/utils/styles.js +0 -590
  291. package/dist/utils/styles.js.map +0 -1
  292. package/dist/utils/typography.d.ts +0 -36
  293. package/dist/utils/typography.js +0 -53
  294. package/dist/utils/typography.js.map +0 -1
  295. package/dist/utils/warnings.d.ts +0 -16
  296. package/dist/utils/warnings.js +0 -16
  297. package/dist/utils/warnings.js.map +0 -1
  298. package/dist/zero/css-writer.d.ts +0 -45
  299. package/dist/zero/css-writer.js +0 -74
  300. package/dist/zero/css-writer.js.map +0 -1
  301. package/dist/zero/extractor.d.ts +0 -24
  302. package/dist/zero/extractor.js +0 -150
  303. package/dist/zero/extractor.js.map +0 -1
@@ -1,714 +0,0 @@
1
- import { createStyle } from "../styles/createStyle.js";
2
- import { STYLE_HANDLER_MAP } from "../styles/index.js";
3
-
4
- //#region src/injector/sheet-manager.ts
5
- var SheetManager = class {
6
- rootRegistries = /* @__PURE__ */ new WeakMap();
7
- config;
8
- /** Dedicated style elements for raw CSS per root */
9
- rawStyleElements = /* @__PURE__ */ new WeakMap();
10
- /** Tracking for raw CSS blocks per root */
11
- rawCSSBlocks = /* @__PURE__ */ new WeakMap();
12
- /** Counter for generating unique raw CSS IDs */
13
- rawCSSCounter = 0;
14
- constructor(config) {
15
- this.config = config;
16
- }
17
- /**
18
- * Get or create registry for a root (Document or ShadowRoot)
19
- */
20
- getRegistry(root) {
21
- let registry = this.rootRegistries.get(root);
22
- if (!registry) {
23
- const metrics = this.config.devMode ? {
24
- hits: 0,
25
- misses: 0,
26
- bulkCleanups: 0,
27
- totalInsertions: 0,
28
- totalUnused: 0,
29
- stylesCleanedUp: 0,
30
- cleanupHistory: [],
31
- startTime: Date.now()
32
- } : void 0;
33
- registry = {
34
- sheets: [],
35
- refCounts: /* @__PURE__ */ new Map(),
36
- rules: /* @__PURE__ */ new Map(),
37
- cacheKeyToClassName: /* @__PURE__ */ new Map(),
38
- ruleTextSet: /* @__PURE__ */ new Set(),
39
- bulkCleanupTimeout: null,
40
- cleanupCheckTimeout: null,
41
- metrics,
42
- classCounter: 0,
43
- keyframesCache: /* @__PURE__ */ new Map(),
44
- keyframesNameToContent: /* @__PURE__ */ new Map(),
45
- keyframesCounter: 0,
46
- injectedProperties: /* @__PURE__ */ new Map(),
47
- globalRules: /* @__PURE__ */ new Map()
48
- };
49
- this.rootRegistries.set(root, registry);
50
- }
51
- return registry;
52
- }
53
- /**
54
- * Create a new stylesheet for the registry
55
- */
56
- createSheet(registry, root) {
57
- const sheetInfo = {
58
- sheet: this.createStyleElement(root),
59
- ruleCount: 0,
60
- holes: []
61
- };
62
- registry.sheets.push(sheetInfo);
63
- return sheetInfo;
64
- }
65
- /**
66
- * Create a style element and append to document
67
- */
68
- createStyleElement(root) {
69
- const style = root.createElement?.("style") || document.createElement("style");
70
- if (this.config.nonce) style.nonce = this.config.nonce;
71
- style.setAttribute("data-tasty", "");
72
- if ("head" in root && root.head) root.head.appendChild(style);
73
- else if ("appendChild" in root) root.appendChild(style);
74
- else document.head.appendChild(style);
75
- if (!style.isConnected && !this.config.forceTextInjection) console.error("SheetManager: Style element failed to connect to DOM!", {
76
- parentNode: style.parentNode?.nodeName,
77
- isConnected: style.isConnected
78
- });
79
- return style;
80
- }
81
- /**
82
- * Insert CSS rules as a single block
83
- */
84
- insertRule(registry, flattenedRules, className, root) {
85
- let targetSheet = this.findAvailableSheet(registry, root);
86
- if (!targetSheet) targetSheet = this.createSheet(registry, root);
87
- const sheetIndex = registry.sheets.indexOf(targetSheet);
88
- try {
89
- const groupedRules = [];
90
- const groupMap = /* @__PURE__ */ new Map();
91
- const atKey = (at) => at && at.length ? at.join("|") : "";
92
- flattenedRules.forEach((r) => {
93
- const key = `${atKey(r.atRules)}||${r.selector}`;
94
- const existing = groupMap.get(key);
95
- if (existing) existing.declarations = existing.declarations ? `${existing.declarations} ${r.declarations}` : r.declarations;
96
- else {
97
- groupMap.set(key, {
98
- idx: groupedRules.length,
99
- selector: r.selector,
100
- atRules: r.atRules,
101
- declarations: r.declarations
102
- });
103
- groupedRules.push({ ...r });
104
- }
105
- });
106
- groupMap.forEach((val) => {
107
- groupedRules[val.idx] = {
108
- selector: val.selector,
109
- atRules: val.atRules,
110
- declarations: val.declarations
111
- };
112
- });
113
- const insertedRuleTexts = [];
114
- const insertedIndices = [];
115
- let currentRuleIndex = this.findAvailableRuleIndex(targetSheet);
116
- let firstInsertedIndex = null;
117
- let lastInsertedIndex = null;
118
- for (const rule of groupedRules) {
119
- const declarations = rule.declarations;
120
- const baseRule = `${rule.selector} { ${declarations} }`;
121
- let fullRule = baseRule;
122
- if (rule.atRules && rule.atRules.length > 0) fullRule = rule.atRules.reduce((css, atRule) => `${atRule} { ${css} }`, baseRule);
123
- const styleElement = targetSheet.sheet;
124
- const styleSheet = styleElement.sheet;
125
- if (styleSheet && !this.config.forceTextInjection) {
126
- const maxIndex = styleSheet.cssRules.length;
127
- const atomicRuleIndex = this.findAvailableRuleIndex(targetSheet);
128
- const safeIndex = Math.min(Math.max(0, atomicRuleIndex), maxIndex);
129
- const splitSelectorsSafely = (selectorList) => {
130
- const parts = [];
131
- let buf = "";
132
- let depthSq = 0;
133
- let depthPar = 0;
134
- let inStr = "";
135
- for (let i = 0; i < selectorList.length; i++) {
136
- const ch = selectorList[i];
137
- if (inStr) {
138
- if (ch === inStr && selectorList[i - 1] !== "\\") inStr = "";
139
- buf += ch;
140
- continue;
141
- }
142
- if (ch === "\"" || ch === "'") {
143
- inStr = ch;
144
- buf += ch;
145
- continue;
146
- }
147
- if (ch === "[") depthSq++;
148
- else if (ch === "]") depthSq = Math.max(0, depthSq - 1);
149
- else if (ch === "(") depthPar++;
150
- else if (ch === ")") depthPar = Math.max(0, depthPar - 1);
151
- if (ch === "," && depthSq === 0 && depthPar === 0) {
152
- const part = buf.trim();
153
- if (part) parts.push(part);
154
- buf = "";
155
- } else buf += ch;
156
- }
157
- const tail = buf.trim();
158
- if (tail) parts.push(tail);
159
- return parts;
160
- };
161
- try {
162
- styleSheet.insertRule(fullRule, safeIndex);
163
- targetSheet.ruleCount++;
164
- insertedIndices.push(safeIndex);
165
- if (firstInsertedIndex == null) firstInsertedIndex = safeIndex;
166
- lastInsertedIndex = safeIndex;
167
- currentRuleIndex = safeIndex + 1;
168
- } catch (e) {
169
- const selectors = splitSelectorsSafely(rule.selector);
170
- if (selectors.length > 1) {
171
- let anyInserted = false;
172
- for (const sel of selectors) {
173
- const singleBase = `${sel} { ${declarations} }`;
174
- let singleRule = singleBase;
175
- if (rule.atRules && rule.atRules.length > 0) singleRule = rule.atRules.reduce((css, atRule) => `${atRule} { ${css} }`, singleBase);
176
- try {
177
- const maxIdx = styleSheet.cssRules.length;
178
- const atomicIdx = this.findAvailableRuleIndex(targetSheet);
179
- const idx = Math.min(Math.max(0, atomicIdx), maxIdx);
180
- styleSheet.insertRule(singleRule, idx);
181
- targetSheet.ruleCount++;
182
- insertedIndices.push(idx);
183
- if (firstInsertedIndex == null) firstInsertedIndex = idx;
184
- lastInsertedIndex = idx;
185
- currentRuleIndex = idx + 1;
186
- anyInserted = true;
187
- } catch (singleErr) {
188
- console.warn("[tasty] Browser rejected CSS rule:", singleRule, singleErr);
189
- }
190
- }
191
- if (!anyInserted) {}
192
- } else console.warn("[tasty] Browser rejected CSS rule:", fullRule, e);
193
- }
194
- } else {
195
- const atomicRuleIndex = this.findAvailableRuleIndex(targetSheet);
196
- styleElement.textContent = (styleElement.textContent || "") + "\n" + fullRule;
197
- targetSheet.ruleCount++;
198
- insertedIndices.push(atomicRuleIndex);
199
- if (firstInsertedIndex == null) firstInsertedIndex = atomicRuleIndex;
200
- lastInsertedIndex = atomicRuleIndex;
201
- currentRuleIndex = atomicRuleIndex + 1;
202
- }
203
- if (!styleElement.parentNode && !this.config.forceTextInjection) console.error("SheetManager: Style element is NOT in DOM! This is the problem!", {
204
- className,
205
- ruleIndex: currentRuleIndex
206
- });
207
- if (this.config.devMode) {
208
- insertedRuleTexts.push(fullRule);
209
- try {
210
- registry.ruleTextSet.add(fullRule);
211
- } catch {}
212
- }
213
- }
214
- if (insertedIndices.length === 0) return null;
215
- return {
216
- className,
217
- ruleIndex: firstInsertedIndex ?? 0,
218
- sheetIndex,
219
- cssText: this.config.devMode ? insertedRuleTexts : void 0,
220
- endRuleIndex: lastInsertedIndex ?? firstInsertedIndex ?? 0,
221
- indices: insertedIndices
222
- };
223
- } catch (error) {
224
- console.warn("Failed to insert CSS rules:", error, {
225
- flattenedRules,
226
- className
227
- });
228
- return null;
229
- }
230
- }
231
- /**
232
- * Insert global CSS rules
233
- */
234
- insertGlobalRule(registry, flattenedRules, globalKey, root) {
235
- const ruleInfo = this.insertRule(registry, flattenedRules, globalKey, root);
236
- if (ruleInfo) registry.globalRules.set(globalKey, ruleInfo);
237
- return ruleInfo;
238
- }
239
- /**
240
- * Delete a global CSS rule by key
241
- */
242
- deleteGlobalRule(registry, globalKey) {
243
- const ruleInfo = registry.globalRules.get(globalKey);
244
- if (!ruleInfo) return;
245
- this.deleteRule(registry, ruleInfo);
246
- registry.globalRules.delete(globalKey);
247
- }
248
- /**
249
- * Adjust rule indices after deletion to account for shifting
250
- */
251
- adjustIndicesAfterDeletion(registry, sheetIndex, startIdx, endIdx, deleteCount, deletedRuleInfo, deletedIndices) {
252
- try {
253
- const sortedDeleted = deletedIndices && deletedIndices.length > 0 ? [...deletedIndices].sort((a, b) => a - b) : null;
254
- const countDeletedBefore = (sorted, idx) => {
255
- let shift = 0;
256
- for (const delIdx of sorted) if (delIdx < idx) shift++;
257
- else break;
258
- return shift;
259
- };
260
- const adjustRuleInfo = (info) => {
261
- if (info === deletedRuleInfo) return;
262
- if (info.sheetIndex !== sheetIndex) return;
263
- if (!info.indices || info.indices.length === 0) return;
264
- if (sortedDeleted) info.indices = info.indices.map((idx) => {
265
- return idx - countDeletedBefore(sortedDeleted, idx);
266
- });
267
- else info.indices = info.indices.map((idx) => idx > endIdx ? Math.max(0, idx - deleteCount) : idx);
268
- if (info.indices.length > 0) {
269
- info.ruleIndex = Math.min(...info.indices);
270
- info.endRuleIndex = Math.max(...info.indices);
271
- }
272
- };
273
- for (const info of registry.rules.values()) adjustRuleInfo(info);
274
- for (const info of registry.globalRules.values()) adjustRuleInfo(info);
275
- for (const entry of registry.keyframesCache.values()) {
276
- const ki = entry.info;
277
- if (ki.sheetIndex !== sheetIndex) continue;
278
- if (sortedDeleted) {
279
- const shift = countDeletedBefore(sortedDeleted, ki.ruleIndex);
280
- if (shift > 0) ki.ruleIndex = Math.max(0, ki.ruleIndex - shift);
281
- } else if (ki.ruleIndex > endIdx) ki.ruleIndex = Math.max(0, ki.ruleIndex - deleteCount);
282
- }
283
- } catch {}
284
- }
285
- /**
286
- * Delete a CSS rule from the sheet
287
- */
288
- deleteRule(registry, ruleInfo) {
289
- const sheet = registry.sheets[ruleInfo.sheetIndex];
290
- if (!sheet) return;
291
- try {
292
- const texts = this.config.devMode && Array.isArray(ruleInfo.cssText) ? ruleInfo.cssText.slice() : [];
293
- const styleSheet = sheet.sheet.sheet;
294
- if (styleSheet) {
295
- const rules = styleSheet.cssRules;
296
- if (ruleInfo.indices && ruleInfo.indices.length > 0) {
297
- const sortedIndices = [...ruleInfo.indices].sort((a, b) => b - a);
298
- const deletedIndices = [];
299
- for (const idx of sortedIndices) if (idx >= 0 && idx < styleSheet.cssRules.length) try {
300
- styleSheet.deleteRule(idx);
301
- deletedIndices.push(idx);
302
- } catch (e) {
303
- console.warn(`Failed to delete rule at index ${idx}:`, e);
304
- }
305
- sheet.ruleCount = Math.max(0, sheet.ruleCount - deletedIndices.length);
306
- if (deletedIndices.length > 0) this.adjustIndicesAfterDeletion(registry, ruleInfo.sheetIndex, Math.min(...deletedIndices), Math.max(...deletedIndices), deletedIndices.length, ruleInfo, deletedIndices);
307
- } else {
308
- const startIdx = Math.max(0, ruleInfo.ruleIndex);
309
- const endIdx = Math.min(rules.length - 1, Number.isFinite(ruleInfo.endRuleIndex) ? ruleInfo.endRuleIndex : startIdx);
310
- if (Number.isFinite(startIdx) && endIdx >= startIdx) {
311
- const deleteCount = endIdx - startIdx + 1;
312
- for (let idx = endIdx; idx >= startIdx; idx--) {
313
- if (idx < 0 || idx >= styleSheet.cssRules.length) continue;
314
- styleSheet.deleteRule(idx);
315
- }
316
- sheet.ruleCount = Math.max(0, sheet.ruleCount - deleteCount);
317
- this.adjustIndicesAfterDeletion(registry, ruleInfo.sheetIndex, startIdx, endIdx, deleteCount, ruleInfo);
318
- }
319
- }
320
- }
321
- if (this.config.devMode && texts.length) try {
322
- for (const text of texts) registry.ruleTextSet.delete(text);
323
- } catch {}
324
- } catch (error) {
325
- console.warn("Failed to delete CSS rule:", error);
326
- }
327
- }
328
- /**
329
- * Find a sheet with available space or return null
330
- */
331
- findAvailableSheet(registry, _root) {
332
- const maxRules = this.config.maxRulesPerSheet;
333
- if (!maxRules) return registry.sheets[registry.sheets.length - 1] || null;
334
- for (const sheet of registry.sheets) if (sheet.ruleCount < maxRules) return sheet;
335
- return null;
336
- }
337
- /**
338
- * Find an available rule index in the sheet
339
- */
340
- findAvailableRuleIndex(sheet) {
341
- return sheet.ruleCount;
342
- }
343
- /**
344
- * Schedule bulk cleanup of all unused styles (non-stacking)
345
- */
346
- scheduleBulkCleanup(registry) {
347
- if (registry.bulkCleanupTimeout) {
348
- if (this.config.idleCleanup && typeof cancelIdleCallback !== "undefined") cancelIdleCallback(registry.bulkCleanupTimeout);
349
- else clearTimeout(registry.bulkCleanupTimeout);
350
- registry.bulkCleanupTimeout = null;
351
- }
352
- const performCleanup = () => {
353
- this.performBulkCleanup(registry);
354
- registry.bulkCleanupTimeout = null;
355
- };
356
- if (this.config.idleCleanup && typeof requestIdleCallback !== "undefined") registry.bulkCleanupTimeout = requestIdleCallback(performCleanup);
357
- else {
358
- const delay = this.config.bulkCleanupDelay || 5e3;
359
- registry.bulkCleanupTimeout = setTimeout(performCleanup, delay);
360
- }
361
- }
362
- /**
363
- * Force cleanup of unused styles
364
- */
365
- forceCleanup(registry) {
366
- this.performBulkCleanup(registry, true);
367
- }
368
- /**
369
- * Perform bulk cleanup of all unused styles
370
- */
371
- performBulkCleanup(registry, cleanupAll = false) {
372
- const cleanupStartTime = Date.now();
373
- const unusedClassNames = Array.from(registry.refCounts.entries()).filter(([, refCount]) => refCount === 0).map(([className]) => className);
374
- if (unusedClassNames.length === 0) return;
375
- const candidates = unusedClassNames.map((className) => {
376
- return {
377
- className,
378
- ruleInfo: registry.rules.get(className)
379
- };
380
- });
381
- if (candidates.length === 0) return;
382
- let selected = candidates;
383
- if (!cleanupAll) {
384
- const ratio = this.config.bulkCleanupBatchRatio ?? .5;
385
- const limit = Math.max(1, Math.floor(candidates.length * Math.min(1, Math.max(0, ratio))));
386
- selected = candidates.slice(0, limit);
387
- }
388
- let cleanedUpCount = 0;
389
- let totalCssSize = 0;
390
- let totalRulesDeleted = 0;
391
- const rulesBySheet = /* @__PURE__ */ new Map();
392
- for (const { className, ruleInfo } of selected) {
393
- const sheetIndex = ruleInfo.sheetIndex;
394
- if (this.config.devMode && Array.isArray(ruleInfo.cssText)) {
395
- const cssSize = ruleInfo.cssText.reduce((total, css) => total + css.length, 0);
396
- totalCssSize += cssSize;
397
- totalRulesDeleted += ruleInfo.cssText.length;
398
- }
399
- if (!rulesBySheet.has(sheetIndex)) rulesBySheet.set(sheetIndex, []);
400
- rulesBySheet.get(sheetIndex).push({
401
- className,
402
- ruleInfo
403
- });
404
- }
405
- for (const [_sheetIndex, rulesInSheet] of rulesBySheet) {
406
- rulesInSheet.sort((a, b) => b.ruleInfo.ruleIndex - a.ruleInfo.ruleIndex);
407
- for (const { className, ruleInfo } of rulesInSheet) {
408
- if ((registry.refCounts.get(className) || 0) > 0) continue;
409
- if (registry.rules.get(className) !== ruleInfo) continue;
410
- const sheetInfo = registry.sheets[ruleInfo.sheetIndex];
411
- if (!sheetInfo || !sheetInfo.sheet) continue;
412
- const styleSheet = sheetInfo.sheet.sheet;
413
- if (!styleSheet) continue;
414
- const maxRuleIndex = styleSheet.cssRules.length - 1;
415
- const startIdx = ruleInfo.ruleIndex;
416
- const endIdx = ruleInfo.endRuleIndex ?? ruleInfo.ruleIndex;
417
- if (startIdx < 0 || endIdx > maxRuleIndex || startIdx > endIdx) continue;
418
- this.deleteRule(registry, ruleInfo);
419
- registry.rules.delete(className);
420
- registry.refCounts.delete(className);
421
- const keysToDelete = [];
422
- for (const [key, mappedClassName] of registry.cacheKeyToClassName.entries()) if (mappedClassName === className) keysToDelete.push(key);
423
- for (const key of keysToDelete) registry.cacheKeyToClassName.delete(key);
424
- cleanedUpCount++;
425
- }
426
- }
427
- if (registry.metrics) {
428
- registry.metrics.bulkCleanups++;
429
- registry.metrics.stylesCleanedUp += cleanedUpCount;
430
- registry.metrics.cleanupHistory.push({
431
- timestamp: cleanupStartTime,
432
- classesDeleted: cleanedUpCount,
433
- cssSize: totalCssSize,
434
- rulesDeleted: totalRulesDeleted
435
- });
436
- }
437
- }
438
- /**
439
- * Get total number of rules across all sheets
440
- */
441
- getTotalRuleCount(registry) {
442
- return registry.sheets.reduce((total, sheet) => total + sheet.ruleCount - sheet.holes.length, 0);
443
- }
444
- /**
445
- * Get CSS text from all sheets (for SSR)
446
- */
447
- getCssText(registry) {
448
- const cssChunks = [];
449
- for (const sheet of registry.sheets) try {
450
- const styleElement = sheet.sheet;
451
- if (styleElement.textContent) cssChunks.push(styleElement.textContent);
452
- else if (styleElement.sheet) {
453
- const rules = Array.from(styleElement.sheet.cssRules);
454
- cssChunks.push(rules.map((rule) => rule.cssText).join("\n"));
455
- }
456
- } catch (error) {
457
- console.warn("Failed to read CSS from sheet:", error);
458
- }
459
- return cssChunks.join("\n");
460
- }
461
- /**
462
- * Get cache performance metrics
463
- */
464
- getMetrics(registry) {
465
- if (!registry.metrics) return null;
466
- const unusedRulesCount = Array.from(registry.refCounts.values()).filter((count) => count === 0).length;
467
- return {
468
- ...registry.metrics,
469
- unusedHits: unusedRulesCount
470
- };
471
- }
472
- /**
473
- * Reset cache performance metrics
474
- */
475
- resetMetrics(registry) {
476
- if (registry.metrics) registry.metrics = {
477
- hits: 0,
478
- misses: 0,
479
- bulkCleanups: 0,
480
- totalInsertions: 0,
481
- totalUnused: 0,
482
- stylesCleanedUp: 0,
483
- cleanupHistory: [],
484
- startTime: Date.now()
485
- };
486
- }
487
- /**
488
- * Convert keyframes steps to CSS string
489
- */
490
- stepsToCSS(steps) {
491
- const rules = [];
492
- for (const [key, value] of Object.entries(steps)) {
493
- if (typeof value === "string") {
494
- rules.push(`${key} { ${value.trim()} }`);
495
- continue;
496
- }
497
- const styleMap = value || {};
498
- const styleNames = Object.keys(styleMap).sort();
499
- const handlerQueue = [];
500
- const seenHandlers = /* @__PURE__ */ new Set();
501
- styleNames.forEach((styleName) => {
502
- let handlers = STYLE_HANDLER_MAP[styleName];
503
- if (!handlers) handlers = STYLE_HANDLER_MAP[styleName] = [createStyle(styleName)];
504
- handlers.forEach((handler) => {
505
- if (!seenHandlers.has(handler)) {
506
- seenHandlers.add(handler);
507
- handlerQueue.push(handler);
508
- }
509
- });
510
- });
511
- const declarationPairs = [];
512
- handlerQueue.forEach((handler) => {
513
- const result = handler(handler.__lookupStyles.reduce((acc, name) => {
514
- const v = styleMap[name];
515
- if (v !== void 0) acc[name] = v;
516
- return acc;
517
- }, {}));
518
- if (!result) return;
519
- (Array.isArray(result) ? result : [result]).forEach((cssMap) => {
520
- if (!cssMap || typeof cssMap !== "object") return;
521
- const { $: _$, ...props } = cssMap;
522
- Object.entries(props).forEach(([prop, val]) => {
523
- if (val == null || val === "") return;
524
- if (Array.isArray(val)) val.forEach((v) => {
525
- if (v != null && v !== "") declarationPairs.push({
526
- prop,
527
- value: String(v)
528
- });
529
- });
530
- else declarationPairs.push({
531
- prop,
532
- value: String(val)
533
- });
534
- });
535
- });
536
- });
537
- const declarations = declarationPairs.map((d) => `${d.prop}: ${d.value}`).join("; ");
538
- rules.push(`${key} { ${declarations.trim()} }`);
539
- }
540
- return rules.join(" ");
541
- }
542
- /**
543
- * Insert keyframes rule
544
- */
545
- insertKeyframes(registry, steps, name, root) {
546
- let targetSheet = this.findAvailableSheet(registry, root);
547
- if (!targetSheet) targetSheet = this.createSheet(registry, root);
548
- const ruleIndex = this.findAvailableRuleIndex(targetSheet);
549
- const sheetIndex = registry.sheets.indexOf(targetSheet);
550
- try {
551
- const fullRule = `@keyframes ${name} { ${this.stepsToCSS(steps)} }`;
552
- const styleElement = targetSheet.sheet;
553
- const styleSheet = styleElement.sheet;
554
- if (styleSheet && !this.config.forceTextInjection) {
555
- const safeIndex = Math.min(Math.max(0, ruleIndex), styleSheet.cssRules.length);
556
- styleSheet.insertRule(fullRule, safeIndex);
557
- } else styleElement.textContent = (styleElement.textContent || "") + "\n" + fullRule;
558
- targetSheet.ruleCount++;
559
- return {
560
- name,
561
- ruleIndex,
562
- sheetIndex,
563
- cssText: this.config.devMode ? fullRule : void 0
564
- };
565
- } catch (error) {
566
- console.warn("Failed to insert keyframes:", error);
567
- return null;
568
- }
569
- }
570
- /**
571
- * Delete keyframes rule
572
- */
573
- deleteKeyframes(registry, info) {
574
- const sheet = registry.sheets[info.sheetIndex];
575
- if (!sheet) return;
576
- try {
577
- const styleSheet = sheet.sheet.sheet;
578
- if (styleSheet) {
579
- if (info.ruleIndex >= 0 && info.ruleIndex < styleSheet.cssRules.length) {
580
- styleSheet.deleteRule(info.ruleIndex);
581
- sheet.ruleCount = Math.max(0, sheet.ruleCount - 1);
582
- this.adjustIndicesAfterDeletion(registry, info.sheetIndex, info.ruleIndex, info.ruleIndex, 1, {
583
- className: "",
584
- ruleIndex: info.ruleIndex,
585
- sheetIndex: info.sheetIndex
586
- }, [info.ruleIndex]);
587
- }
588
- }
589
- } catch (error) {
590
- console.warn("Failed to delete keyframes:", error);
591
- }
592
- }
593
- /**
594
- * Schedule async cleanup check (non-stacking)
595
- */
596
- checkCleanupNeeded(registry) {
597
- if (registry.cleanupCheckTimeout) {
598
- clearTimeout(registry.cleanupCheckTimeout);
599
- registry.cleanupCheckTimeout = null;
600
- }
601
- registry.cleanupCheckTimeout = setTimeout(() => {
602
- this.performCleanupCheck(registry);
603
- registry.cleanupCheckTimeout = null;
604
- }, 0);
605
- }
606
- /**
607
- * Perform the actual cleanup check (called asynchronously)
608
- */
609
- performCleanupCheck(registry) {
610
- if (Array.from(registry.refCounts.values()).filter((count) => count === 0).length >= (this.config.unusedStylesThreshold || 500)) this.scheduleBulkCleanup(registry);
611
- }
612
- /**
613
- * Clean up resources for a root
614
- */
615
- cleanup(root) {
616
- const registry = this.rootRegistries.get(root);
617
- if (!registry) return;
618
- if (registry.bulkCleanupTimeout) {
619
- if (this.config.idleCleanup && typeof cancelIdleCallback !== "undefined") cancelIdleCallback(registry.bulkCleanupTimeout);
620
- else clearTimeout(registry.bulkCleanupTimeout);
621
- registry.bulkCleanupTimeout = null;
622
- }
623
- if (registry.cleanupCheckTimeout) {
624
- clearTimeout(registry.cleanupCheckTimeout);
625
- registry.cleanupCheckTimeout = null;
626
- }
627
- for (const sheet of registry.sheets) try {
628
- const styleElement = sheet.sheet;
629
- if (styleElement.parentNode) styleElement.parentNode.removeChild(styleElement);
630
- } catch (error) {
631
- console.warn("Failed to cleanup sheet:", error);
632
- }
633
- this.rootRegistries.delete(root);
634
- const rawStyleElement = this.rawStyleElements.get(root);
635
- if (rawStyleElement?.parentNode) rawStyleElement.parentNode.removeChild(rawStyleElement);
636
- this.rawStyleElements.delete(root);
637
- this.rawCSSBlocks.delete(root);
638
- }
639
- /**
640
- * Get or create a dedicated style element for raw CSS
641
- * Raw CSS is kept separate from tasty-managed sheets to avoid index conflicts
642
- */
643
- getOrCreateRawStyleElement(root) {
644
- let styleElement = this.rawStyleElements.get(root);
645
- if (!styleElement) {
646
- styleElement = root.createElement?.("style") || document.createElement("style");
647
- if (this.config.nonce) styleElement.nonce = this.config.nonce;
648
- styleElement.setAttribute("data-tasty-raw", "");
649
- if ("head" in root && root.head) root.head.appendChild(styleElement);
650
- else if ("appendChild" in root) root.appendChild(styleElement);
651
- else document.head.appendChild(styleElement);
652
- this.rawStyleElements.set(root, styleElement);
653
- this.rawCSSBlocks.set(root, /* @__PURE__ */ new Map());
654
- }
655
- return styleElement;
656
- }
657
- /**
658
- * Inject raw CSS text directly without parsing
659
- * Returns a dispose function to remove the injected CSS
660
- */
661
- injectRawCSS(css, root) {
662
- if (!css.trim()) return { dispose: () => {} };
663
- const styleElement = this.getOrCreateRawStyleElement(root);
664
- const blocksMap = this.rawCSSBlocks.get(root);
665
- const id = `raw_${this.rawCSSCounter++}`;
666
- const currentContent = styleElement.textContent || "";
667
- const startOffset = currentContent.length;
668
- const cssWithNewline = (currentContent ? "\n" : "") + css;
669
- const endOffset = startOffset + cssWithNewline.length;
670
- styleElement.textContent = currentContent + cssWithNewline;
671
- const info = {
672
- id,
673
- css,
674
- startOffset,
675
- endOffset
676
- };
677
- blocksMap.set(id, info);
678
- return { dispose: () => {
679
- this.disposeRawCSS(id, root);
680
- } };
681
- }
682
- /**
683
- * Remove a raw CSS block by ID
684
- */
685
- disposeRawCSS(id, root) {
686
- const styleElement = this.rawStyleElements.get(root);
687
- const blocksMap = this.rawCSSBlocks.get(root);
688
- if (!styleElement || !blocksMap) return;
689
- if (!blocksMap.get(id)) return;
690
- blocksMap.delete(id);
691
- const remainingBlocks = Array.from(blocksMap.values());
692
- if (remainingBlocks.length === 0) styleElement.textContent = "";
693
- else {
694
- remainingBlocks.sort((a, b) => a.startOffset - b.startOffset);
695
- styleElement.textContent = remainingBlocks.map((block) => block.css).join("\n");
696
- let offset = 0;
697
- for (const block of remainingBlocks) {
698
- block.startOffset = offset;
699
- block.endOffset = offset + block.css.length;
700
- offset = block.endOffset + 1;
701
- }
702
- }
703
- }
704
- /**
705
- * Get the raw CSS content for SSR
706
- */
707
- getRawCSSText(root) {
708
- return this.rawStyleElements.get(root)?.textContent || "";
709
- }
710
- };
711
-
712
- //#endregion
713
- export { SheetManager };
714
- //# sourceMappingURL=sheet-manager.js.map