@barocss/browser 0.0.3 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.es.js CHANGED
@@ -1,18 +1,64 @@
1
- import { parseResultCache as d, createContext as y, IncrementalParser as g, astCache as f } from "@barocss/kit";
2
- class p {
3
- constructor(t, e = 50, s = "barocss-style-partition-") {
4
- this.partitions = [], this.categoryPartitions = /* @__PURE__ */ new Map(), this.partitionCounter = 0, this.maxRulesPerPartition = 50, this.classToPartitionMap = /* @__PURE__ */ new Map(), this.classToCategoryPartitionMap = /* @__PURE__ */ new Map(), this.styleIdPrefix = "barocss-style-partition-", this.insertionPoint = t, this.maxRulesPerPartition = e, this.styleIdPrefix = s, this.initializeDefaultPartition();
1
+ import { parseClassName as y, createContext as g, IncrementalParser as p, clearAstCache as x } from "@barocss/kit";
2
+ const w = /^\s*@(media|container)\s+([^{]*)\{/, E = /prefers-color-scheme|\bprint\b|forced-colors|orientation/, S = /(?:min-width\s*:\s*|width\s*>=?\s*)([\d.]+)(px|rem|em)?/, A = /(?:max-width\s*:\s*|width\s*<=?\s*)([\d.]+)(px|rem|em)?/;
3
+ function m(n, t) {
4
+ const e = parseFloat(n);
5
+ return t === "rem" || t === "em" ? e * 16 : e;
6
+ }
7
+ function M(n, t) {
8
+ const e = n === "container", s = S.exec(t);
9
+ if (s) return [e ? 4 : 2, m(s[1], s[2])];
10
+ const i = A.exec(t);
11
+ return i ? [e ? 3 : 1, -m(i[1], i[2])] : !e && E.test(t) ? [5, 0] : [0, 0];
12
+ }
13
+ function C(n) {
14
+ const t = [];
15
+ let e = n, s;
16
+ for (; s = w.exec(e); ) {
17
+ const [i, r] = M(s[1], s[2]);
18
+ t.push(i, r), e = e.slice(s[0].length);
19
+ }
20
+ return t;
21
+ }
22
+ function P(n, t) {
23
+ const e = Math.min(n.length, t.length);
24
+ for (let s = 0; s < e; s++)
25
+ if (n[s] !== t[s]) return n[s] - t[s];
26
+ return n.length - t.length;
27
+ }
28
+ function v(n, t) {
29
+ let e = 0, s = n.length;
30
+ for (; e < s; ) {
31
+ const i = e + s >> 1;
32
+ P(n[i], t) <= 0 ? e = i + 1 : s = i;
33
+ }
34
+ return e;
35
+ }
36
+ class d {
37
+ constructor(t, e = 50, s = "barocss-style-partition-", i = (r) => y(r).utility?.category) {
38
+ this.partitions = [], this.categoryPartitions = /* @__PURE__ */ new Map(), this.partitionCounter = 0, this.maxRulesPerPartition = 50, this.classToPartitionMap = /* @__PURE__ */ new Map(), this.classToCategoryPartitionMap = /* @__PURE__ */ new Map(), this.styleIdPrefix = "barocss-style-partition-", this.insertionPoint = t, this.maxRulesPerPartition = e, this.styleIdPrefix = s, this.getCategory = i, this.initializeDefaultPartition();
39
+ }
40
+ /**
41
+ * Insert `rule` at its Tailwind variant position within `partition` (#254):
42
+ * one insertRule at a binary-searched index, no sheet rewrite.
43
+ */
44
+ insertSorted(t, e, s) {
45
+ const i = t.keys ??= [], r = v(i, s), o = t.styleElement.sheet;
46
+ o && o.cssRules.length === i.length ? (o.insertRule(this.escapeCssRule(e), r), t.styles.splice(r, 0, e)) : (t.styles.splice(r, 0, e), t.styleElement.textContent = t.styles.join(`
47
+ `) + `
48
+ `), i.splice(r, 0, s);
5
49
  }
6
50
  initializeDefaultPartition() {
7
51
  this.createNewPartition();
8
52
  }
9
- createNewCategoryPartition(t) {
10
- const e = {
53
+ createNewCategoryPartition(t, e = !1) {
54
+ const s = {
11
55
  id: this.styleIdPrefix + `-${t}`,
12
56
  styles: [],
13
57
  styleElement: document.createElement("style")
14
58
  };
15
- return e.styleElement.id = e.id, e.styleElement.setAttribute("data-barocss", "partition"), e.styleElement.setAttribute("data-category", t), this.insertionPoint.appendChild(e.styleElement), this.categoryPartitions.set(t, e), e;
59
+ s.styleElement.id = s.id, s.styleElement.setAttribute("data-barocss", "partition"), s.styleElement.setAttribute("data-category", t);
60
+ const i = this.insertionPoint.ownerDocument?.head;
61
+ return e && i ? i.insertBefore(s.styleElement, i.firstChild) : this.insertionPoint.appendChild(s.styleElement), this.categoryPartitions.set(t, s), s;
16
62
  }
17
63
  createNewPartition() {
18
64
  const t = {
@@ -43,6 +89,9 @@ class p {
43
89
  getCategoryPartition(t) {
44
90
  return this.categoryPartitions.get(t);
45
91
  }
92
+ hasDetachedPartitions() {
93
+ return [...this.partitions, ...this.categoryPartitions.values()].some((t) => !t.styleElement.isConnected);
94
+ }
46
95
  /**
47
96
  * Escape CSS rule text
48
97
  * - Properly escape special characters
@@ -54,16 +103,19 @@ class p {
54
103
  addRule(t) {
55
104
  if (this.hasRule(t))
56
105
  return !1;
57
- this.currentPartition.styles.length >= this.maxRulesPerPartition && this.createNewPartition();
58
- const e = this.currentPartition, s = this.partitions.length - 1;
106
+ const e = C(t);
107
+ let s = this.partitions.findIndex((r) => {
108
+ const o = r.keys;
109
+ return !!o && o.length > 0 && P(o[o.length - 1], e) > 0;
110
+ });
111
+ s === -1 && (this.currentPartition.styles.length >= this.maxRulesPerPartition && this.createNewPartition(), s = this.partitions.length - 1);
112
+ const i = this.partitions[s];
59
113
  try {
60
- const i = e.styleElement.sheet;
61
- return i ? i.insertRule(this.escapeCssRule(t), i.cssRules.length) : e.styleElement.textContent += t + `
62
- `, this.setRuleCache(t, s), e.styles.push(t), !0;
63
- } catch (i) {
114
+ return this.insertSorted(i, t, e), this.setRuleCache(t, s), !0;
115
+ } catch (r) {
64
116
  return console.warn(
65
117
  `[StylePartitionManager] Failed to insert rule: ${t}`,
66
- i
118
+ r
67
119
  ), !1;
68
120
  }
69
121
  }
@@ -73,16 +125,14 @@ class p {
73
125
  let s = this.getCategoryPartition(e);
74
126
  s || (s = this.createNewCategoryPartition(e));
75
127
  try {
76
- const i = s.styleElement.sheet;
77
- i ? i.insertRule(this.escapeCssRule(t), i.cssRules.length) : s.styleElement.textContent += t + `
78
- `;
128
+ this.insertSorted(s, t, C(t));
79
129
  } catch (i) {
80
130
  return console.warn(
81
131
  `[StylePartitionManager] Failed to insert rule in category: ${e} ${t}`,
82
132
  i
83
133
  ), !1;
84
134
  }
85
- return this.setCategoryRuleCache(t, e), s.styles.push(t), !0;
135
+ return this.setCategoryRuleCache(t, e), !0;
86
136
  }
87
137
  addRootRules(t) {
88
138
  let e = this.getCategoryPartition("root");
@@ -104,7 +154,7 @@ class p {
104
154
  addRules(t) {
105
155
  let e = 0, s = 0;
106
156
  for (const i of t) {
107
- const r = d.get(i.cls)?.utility?.category;
157
+ const r = this.getCategory(i.cls);
108
158
  if (r)
109
159
  for (const o of i.cssList)
110
160
  this.addCategoryRule(o, r);
@@ -124,13 +174,13 @@ class p {
124
174
  const s = this.classToCategoryPartitionMap.get(t);
125
175
  return s !== void 0 && this.categoryPartitions.get(s) || null;
126
176
  }
127
- updateRuleContent(t, e) {
128
- const s = this.getCategoryPartition(t);
129
- if (s)
130
- s.styleElement.textContent = e;
177
+ updateRuleContent(t, e, s = !1) {
178
+ const i = this.getCategoryPartition(t);
179
+ if (i)
180
+ i.styleElement.textContent = e;
131
181
  else {
132
- const i = this.createNewCategoryPartition(t);
133
- console.log(`[StylePartitionManager] Created new partition for category: ${t}`), i.styleElement.textContent = e;
182
+ const r = this.createNewCategoryPartition(t, s);
183
+ console.log(`[StylePartitionManager] Created new partition for category: ${t}`), r.styleElement.textContent = e;
134
184
  }
135
185
  }
136
186
  /**
@@ -141,24 +191,27 @@ class p {
141
191
  t.styleElement.parentNode && t.styleElement.parentNode.removeChild(t.styleElement);
142
192
  }), this.categoryPartitions.forEach((t) => {
143
193
  t.styleElement.parentNode && t.styleElement.parentNode.removeChild(t.styleElement);
144
- }), this.partitions = [], this.partitionCounter = 0, this.classToPartitionMap.clear(), this.classToCategoryPartitionMap.clear();
194
+ }), this.partitions = [], this.categoryPartitions.clear(), this.partitionCounter = 0, this.classToPartitionMap.clear(), this.classToCategoryPartitionMap.clear();
145
195
  }
146
196
  }
147
- function C(a) {
148
- return a ? a instanceof SVGAnimatedString ? a.baseVal.toString() : a.toString() : "";
197
+ function N(n) {
198
+ return n ? typeof n == "object" && typeof n.baseVal == "string" ? n.baseVal : n.toString() : "";
149
199
  }
150
- function c(a) {
151
- return a ? C(a).split(/\s+/).filter(Boolean) : [];
200
+ function h(n) {
201
+ return n ? N(n).split(/\s+/).filter(Boolean) : [];
152
202
  }
153
- class P {
203
+ class D {
154
204
  /**
155
205
  * Create a new ChangeDetector instance
156
206
  *
157
207
  * @param incrementalParser - IncrementalParser instance for class processing
158
208
  * @param BrowserRuntime - Optional BrowserRuntime instance for CSS injection
159
209
  */
160
- constructor(t, e) {
161
- this.observer = null, this.processedElements = /* @__PURE__ */ new WeakSet(), this.incrementalParser = t, this.BrowserRuntime = e;
210
+ constructor(t, e, s = (i) => y(i).utility?.category) {
211
+ this.observer = null, this.incrementalParser = t, this.BrowserRuntime = e, this.getCategory = s;
212
+ }
213
+ setParser(t) {
214
+ this.incrementalParser = t;
162
215
  }
163
216
  /**
164
217
  * Starts observing DOM changes for new CSS classes
@@ -179,22 +232,26 @@ class P {
179
232
  return typeof window > "u" ? new MutationObserver(() => {
180
233
  }) : (this.observer && this.observer.disconnect(), this.observer = new MutationObserver((s) => {
181
234
  const i = /* @__PURE__ */ new Set();
182
- if (s.forEach((n) => {
183
- if (n.type === "attributes" && n.attributeName === "class") {
184
- const r = n.target;
185
- r.className && c(r.className).forEach((l) => {
186
- this.incrementalParser.isProcessed(l) || i.add(l);
235
+ if (s.forEach((r) => {
236
+ if (r.type === "attributes" && r.attributeName === "class" && t.contains(r.target)) {
237
+ const o = r.target;
238
+ o.className && h(o.className).forEach((c) => {
239
+ this.incrementalParser.isProcessed(c) || i.add(c);
187
240
  });
188
241
  }
189
- n.type === "childList" && n.addedNodes.forEach((r) => {
190
- r instanceof HTMLElement && (this.processElement(r, i), r.querySelectorAll("[class]").forEach((o) => {
191
- this.processElement(o, i);
192
- }));
242
+ r.type === "childList" && r.addedNodes.forEach((o) => {
243
+ if (o.nodeType === Node.ELEMENT_NODE && t.contains(o)) {
244
+ const a = o;
245
+ this.processElement(a, i), a.querySelectorAll("[class]").forEach((c) => {
246
+ this.processElement(c, i);
247
+ });
248
+ }
193
249
  });
194
250
  }), i.size > 0) {
195
- const n = Array.from(i), r = this.incrementalParser.processClasses(n);
196
- this.BrowserRuntime?.applyParseResults(r);
197
- }
251
+ const r = Array.from(i), o = this.incrementalParser.processClasses(r);
252
+ this.BrowserRuntime?.applyParseResults(o);
253
+ } else
254
+ this.BrowserRuntime?.applyParseResults([]);
198
255
  }), this.observer.observe(t, {
199
256
  attributes: !0,
200
257
  subtree: !0,
@@ -207,39 +264,38 @@ class P {
207
264
  */
208
265
  scanExistingClasses(t, e) {
209
266
  const s = /* @__PURE__ */ new Set();
210
- t.className && c(t.className).forEach((r) => {
211
- this.incrementalParser.isProcessed(r) || s.add(r);
267
+ t.className && h(t.className).forEach((o) => {
268
+ this.incrementalParser.isProcessed(o) || s.add(o);
212
269
  });
213
270
  const i = t.querySelectorAll("[class]");
214
- for (const n of i)
215
- if (n.className) {
216
- const r = c(n.className);
217
- for (const o of r)
218
- this.incrementalParser.isProcessed(o) || s.add(o);
271
+ for (const r of i)
272
+ if (r.className) {
273
+ const o = h(r.className);
274
+ for (const a of o)
275
+ this.incrementalParser.isProcessed(a) || s.add(a);
219
276
  }
220
277
  if (s.size > 0) {
221
- const n = Array.from(s), r = this.incrementalParser.processClasses(n), o = r.filter((h) => d.get(h.cls)?.utility?.category === "layout"), l = r.filter((h) => d.get(h.cls)?.utility?.category !== "layout");
222
- this.BrowserRuntime?.applyParseResults(o), e?.onReady?.(), this.BrowserRuntime?.applyParseResults(l);
223
- }
278
+ const r = Array.from(s), o = this.incrementalParser.processClasses(r), a = o.filter((u) => this.getCategory(u.cls) === "layout"), c = o.filter((u) => this.getCategory(u.cls) !== "layout");
279
+ this.BrowserRuntime?.applyParseResults(a), e?.onReady?.(), this.BrowserRuntime?.applyParseResults(c);
280
+ } else
281
+ e?.onReady?.();
224
282
  }
225
283
  /**
226
284
  * Processes an individual element and extracts new classes
227
285
  *
228
286
  * This method is called for each element discovered during DOM mutations.
229
287
  * It:
230
- * - Checks if the element has already been processed
231
288
  * - Extracts all class names from the element's className
232
289
  * - Filters out already processed classes
233
290
  * - Adds new classes to the collection for batch processing
234
- * - Marks the element as processed to avoid duplicates
235
291
  *
236
292
  * @param element - The HTML element to process
237
293
  * @param newClasses - Set to collect newly discovered class names
238
294
  */
239
295
  processElement(t, e) {
240
- this.processedElements.has(t) || (t.className && c(t.className).forEach((i) => {
296
+ t.className && h(t.className).forEach((i) => {
241
297
  this.incrementalParser.isProcessed(i) || e.add(i);
242
- }), this.processedElements.add(t));
298
+ });
243
299
  }
244
300
  /**
245
301
  * Stops observing DOM changes and cleans up resources
@@ -252,16 +308,44 @@ class P {
252
308
  this.observer && (this.observer.disconnect(), this.observer = null);
253
309
  }
254
310
  }
255
- class m {
311
+ function I(n) {
312
+ return n.replace(/\\([0-9a-fA-F]{1,6})\s?|\\(.)/g, (t, e, s) => e ? String.fromCodePoint(parseInt(e, 16)) : s);
313
+ }
314
+ const L = /^\s*\.((?:\\[0-9a-fA-F]{1,6}\s?|\\.|[\w-]|[^\x00-\x7F])+)/;
315
+ function T(n) {
316
+ const t = [];
317
+ let e = 0, s = 0;
318
+ for (let i = 0; i < n.length; i++) {
319
+ const r = n[i];
320
+ r === "\\" ? i++ : r === "(" || r === "[" ? e++ : r === ")" || r === "]" ? e-- : r === "," && e === 0 && (t.push(n.slice(s, i)), s = i + 1);
321
+ }
322
+ return t.push(n.slice(s)), t;
323
+ }
324
+ function b(n, t = /* @__PURE__ */ new Set()) {
325
+ for (const e of Array.from(n)) {
326
+ const s = e.selectorText;
327
+ if (typeof s == "string")
328
+ for (const r of T(s)) {
329
+ const o = L.exec(r);
330
+ o && t.add(I(o[1]));
331
+ }
332
+ const i = e.cssRules;
333
+ i && i.length && b(i, t);
334
+ }
335
+ return t;
336
+ }
337
+ const $ = "@layer theme, base, components, utilities;";
338
+ class k {
256
339
  constructor(t = {}) {
257
- this.cache = /* @__PURE__ */ new Map(), this.rootCache = /* @__PURE__ */ new Set(), this.isDestroyed = !1;
340
+ this.cache = /* @__PURE__ */ new Map(), this.rootCache = /* @__PURE__ */ new Set(), this.isDestroyed = !1, this.existing = null, this.existingSheetCount = -1, this.getCategory = (s) => y(s, this.context).utility?.category;
258
341
  const e = {};
259
342
  this.options = {
260
343
  config: t.config || e,
261
344
  styleId: t.styleId || "barocss-runtime",
262
345
  insertionPoint: t.insertionPoint || "head",
263
- maxRulesPerPartition: t.maxRulesPerPartition || 50
264
- }, this.context = y(this.options.config), this.incrementalParser = new g(this.context), this.changeDetector = new P(this.incrementalParser, this), this.stylePartitionManager = new p(this.getInsertionPoint(), this.options.maxRulesPerPartition, `${this.options.styleId}-partition`), this.init();
346
+ maxRulesPerPartition: t.maxRulesPerPartition || 50,
347
+ skipExisting: t.skipExisting ?? !1
348
+ }, this.context = g(this.options.config), this.incrementalParser = new p(this.context), this.changeDetector = new D(this.incrementalParser, this, this.getCategory), this.stylePartitionManager = new d(this.getInsertionPoint(), this.options.maxRulesPerPartition, `${this.options.styleId}-partition`, this.getCategory), this.init();
265
349
  }
266
350
  // Debugging and logging helpers
267
351
  /**
@@ -274,9 +358,17 @@ class m {
274
358
  console.log("[BrowserRuntime] init"), this.injectPreflightCSS(), this.ensureCssVars();
275
359
  }
276
360
  injectPreflightCSS() {
277
- if (this.options.config.preflight) {
278
- const t = this.context.getPreflightCSS(this.options.config.preflight);
279
- this.stylePartitionManager.updateRuleContent("preflight", t);
361
+ const t = this.options.config.preflight ?? !0;
362
+ if (t) {
363
+ const e = this.context.getPreflightCSS(t);
364
+ this.stylePartitionManager.updateRuleContent(
365
+ "preflight",
366
+ `${$}
367
+ @layer base {
368
+ ${e}
369
+ }`,
370
+ !0
371
+ );
280
372
  }
281
373
  }
282
374
  ensureCssVars() {
@@ -285,7 +377,7 @@ class m {
285
377
  this.stylePartitionManager.updateRuleContent("css-vars", t);
286
378
  }
287
379
  getInsertionPoint() {
288
- if (this.options.insertionPoint instanceof HTMLElement)
380
+ if (typeof this.options.insertionPoint != "string")
289
381
  return this.options.insertionPoint;
290
382
  switch (this.options.insertionPoint) {
291
383
  case "body":
@@ -320,16 +412,45 @@ class m {
320
412
  * Public method to apply parser results, update internal caches, and inject CSS
321
413
  */
322
414
  applyParseResults(t, e) {
415
+ if (this.isDestroyed) return;
416
+ if (this.getInsertionPoint().isConnected && this.stylePartitionManager.hasDetachedPartitions()) {
417
+ const r = Array.from(this.cache.values());
418
+ this.reset(), t = [...r, ...t], t.forEach((o) => this.incrementalParser.markProcessed(o.cls));
419
+ }
420
+ if (this.options.skipExisting && t.length > 0 && typeof document < "u") {
421
+ const r = this.getExistingClasses();
422
+ t = t.filter((o) => !r.has(o.cls));
423
+ }
424
+ if (t.length === 0) return;
323
425
  const s = [], i = [];
324
- for (const n of t)
325
- if (n.css && Array.isArray(n.cssList) && s.push(n), n.rootCss && Array.isArray(n.rootCssList))
326
- for (const r of n.rootCssList)
327
- this.rootCache.has(r) || (this.rootCache.add(r), i.push(r));
426
+ for (const r of t)
427
+ if (r.css && Array.isArray(r.cssList) && (s.push(r), this.cache.set(r.cls, r)), r.rootCss && Array.isArray(r.rootCssList))
428
+ for (const o of r.rootCssList)
429
+ this.rootCache.has(o) || (this.rootCache.add(o), i.push(o));
328
430
  i.length > 0 && this.stylePartitionManager.addRootRules(i.filter(Boolean)), s.length > 0 && this.stylePartitionManager.addRules(s), this.debugLog("info", `Applied ${t.length} parser results`, {
329
431
  cssRuleCount: s.length,
330
432
  rootCssCount: i.length
331
433
  });
332
434
  }
435
+ /** Class names defined by the page's own stylesheets (BaroCSS's sheets and cross-origin sheets excluded). */
436
+ getExistingClasses() {
437
+ const t = Array.from(document.styleSheets).filter((s) => {
438
+ const i = s.ownerNode;
439
+ return !(i && typeof i.hasAttribute == "function" && (i.hasAttribute("data-barocss") || (i.id || "").startsWith(this.options.styleId)));
440
+ });
441
+ if (this.existing && t.length === this.existingSheetCount) return this.existing;
442
+ const e = /* @__PURE__ */ new Set();
443
+ for (const s of t) {
444
+ let i;
445
+ try {
446
+ i = s.cssRules;
447
+ } catch {
448
+ continue;
449
+ }
450
+ b(i, e);
451
+ }
452
+ return this.existing = e, this.existingSheetCount = t.length, e;
453
+ }
333
454
  /**
334
455
  * MutationObserver instance method to automatically call addClass when class attributes change in DOM
335
456
  */
@@ -347,7 +468,7 @@ class m {
347
468
  `);
348
469
  }
349
470
  getAllCss() {
350
- return Array.from(this.cache.values()).flatMap((e) => e.cssList).join(`
471
+ return [...this.rootCache, ...Array.from(this.cache.values()).flatMap((e) => e.cssList)].join(`
351
472
  `);
352
473
  }
353
474
  getClasses() {
@@ -357,36 +478,37 @@ class m {
357
478
  * Get comprehensive cache statistics
358
479
  */
359
480
  getCacheStats() {
481
+ const t = this.incrementalParser.getStats();
360
482
  return {
361
483
  runtime: {
362
484
  cachedClasses: this.cache.size,
363
485
  rootCacheSize: this.rootCache.size
364
486
  },
365
- ast: f.getStats(),
366
- incremental: this.incrementalParser.getStats()
487
+ ast: t.cacheStats.ast,
488
+ incremental: t
367
489
  };
368
490
  }
369
491
  /**
370
492
  * Clear all caches (useful for debugging or memory management)
371
493
  */
372
494
  clearCaches() {
373
- this.cache.clear(), this.rootCache.clear(), f.clear(), this.incrementalParser.clearProcessed(), this.stylePartitionManager.cleanup();
495
+ this.isDestroyed || (this.cache.clear(), this.rootCache.clear(), x(this.context), this.incrementalParser.clearProcessed(), this.stylePartitionManager.cleanup(), this.stylePartitionManager = new d(this.getInsertionPoint(), this.options.maxRulesPerPartition, `${this.options.styleId}-partition`, this.getCategory), this.injectPreflightCSS(), this.ensureCssVars());
374
496
  }
375
497
  reset() {
376
- this.cache.clear(), this.rootCache.clear(), this.stylePartitionManager.cleanup();
498
+ this.isDestroyed || (this.cache.clear(), this.rootCache.clear(), this.incrementalParser.clearProcessed(), this.stylePartitionManager.cleanup(), this.stylePartitionManager = new d(this.getInsertionPoint(), this.options.maxRulesPerPartition, `${this.options.styleId}-partition`, this.getCategory), this.injectPreflightCSS(), this.ensureCssVars());
377
499
  }
378
500
  updateConfig(t) {
379
- this.options.config = t, this.context = y(t);
501
+ if (this.isDestroyed) return;
380
502
  const e = Array.from(this.cache.keys());
381
- this.reset(), e.length > 0 && this.addClass(e);
503
+ this.options.config = t, this.context = g(t), this.incrementalParser = new p(this.context), this.changeDetector.setParser(this.incrementalParser), this.reset(), e.length > 0 && this.addClass(e);
382
504
  }
383
505
  removeClass(t) {
384
- const e = this.normalizeClasses(t);
385
- for (const s of e)
386
- this.cache.delete(s);
506
+ if (this.isDestroyed) return;
507
+ const e = new Set(this.normalizeClasses(t)), s = Array.from(this.cache.values()).filter((i) => !e.has(i.cls));
508
+ s.length !== this.cache.size && (this.reset(), s.forEach((i) => this.incrementalParser.markProcessed(i.cls)), this.applyParseResults(s));
387
509
  }
388
510
  destroy() {
389
- this.stylePartitionManager.cleanup(), this.cache.clear(), this.rootCache.clear(), this.isDestroyed = !0;
511
+ this.isDestroyed || (this.changeDetector.disconnect(), this.stylePartitionManager.cleanup(), this.cache.clear(), this.rootCache.clear(), this.isDestroyed = !0);
390
512
  }
391
513
  getStats() {
392
514
  return {
@@ -398,28 +520,98 @@ class m {
398
520
  };
399
521
  }
400
522
  }
401
- let u = null;
402
- function R(a) {
403
- return u || (u = new m(a)), u;
523
+ let l = null, f;
524
+ function B(n = {}) {
525
+ return !l || l.getStats().isDestroyed ? (l = new k(n), f = n.config) : n.config && n.config !== f && (l.updateConfig(n.config), f = n.config), l;
404
526
  }
405
- function b({ loadingClassName: a = "baro-boot", ...t } = {}) {
406
- const e = `${a}-doing`, s = `${a}-done`;
527
+ function R({ loadingClassName: n = "baro-boot", ...t } = {}) {
528
+ if (!document.body) {
529
+ document.addEventListener("DOMContentLoaded", () => R({ loadingClassName: n, ...t }), { once: !0 });
530
+ return;
531
+ }
532
+ const e = `${n}-doing`, s = `${n}-done`;
407
533
  try {
408
- document.body.classList.add(e), R(t).observe(document.body, { scan: !0, onReady: () => {
534
+ document.body.classList.add(e), B(t).observe(document.body, { scan: !0, onReady: () => {
409
535
  document.body.classList.remove(e), document.body.classList.add(s);
410
536
  } });
411
537
  } catch (i) {
412
- console.error("BaroCSS boot failed:", i);
538
+ document.body?.classList.remove(e), console.error("BaroCSS boot failed:", i);
413
539
  }
414
540
  }
415
- const w = b;
541
+ const V = R;
542
+ function j(n) {
543
+ if (!n || typeof n != "object" || Array.isArray(n)) return [];
544
+ const t = n.elements;
545
+ if (!t || typeof t != "object" || Array.isArray(t)) return [];
546
+ const e = /* @__PURE__ */ new Set();
547
+ for (const s of Object.keys(t)) {
548
+ const i = t[s];
549
+ if (!i || typeof i != "object" || Array.isArray(i)) continue;
550
+ const r = i.props;
551
+ if (!r || typeof r != "object" || Array.isArray(r)) continue;
552
+ const o = r.className;
553
+ if (typeof o == "string")
554
+ for (const a of o.split(/\s+/))
555
+ a && e.add(a);
556
+ }
557
+ return Array.from(e);
558
+ }
559
+ function O(n, t) {
560
+ const e = j(n);
561
+ e.length > 0 && t.addClass(e);
562
+ }
563
+ const z = [
564
+ "background",
565
+ "foreground",
566
+ "card",
567
+ "card-foreground",
568
+ "popover",
569
+ "popover-foreground",
570
+ "primary",
571
+ "primary-foreground",
572
+ "secondary",
573
+ "secondary-foreground",
574
+ "muted",
575
+ "muted-foreground",
576
+ "accent",
577
+ "accent-foreground",
578
+ "destructive",
579
+ "border",
580
+ "input",
581
+ "ring",
582
+ "chart-1",
583
+ "chart-2",
584
+ "chart-3",
585
+ "chart-4",
586
+ "chart-5",
587
+ "sidebar",
588
+ "sidebar-foreground",
589
+ "sidebar-primary",
590
+ "sidebar-primary-foreground",
591
+ "sidebar-accent",
592
+ "sidebar-accent-foreground",
593
+ "sidebar-border",
594
+ "sidebar-ring"
595
+ ], F = {
596
+ colors: Object.fromEntries(z.map((n) => [n, `var(--${n})`])),
597
+ borderRadius: {
598
+ sm: "calc(var(--radius) - 4px)",
599
+ md: "calc(var(--radius) - 2px)",
600
+ lg: "var(--radius)",
601
+ xl: "calc(var(--radius) + 4px)"
602
+ }
603
+ };
416
604
  export {
417
- m as BrowserRuntime,
418
- P as ChangeDetector,
419
- p as StylePartitionManager,
420
- b as baroBoot,
421
- w as baroStart,
422
- R as getRuntime,
423
- C as normalizeClassName,
424
- c as normalizeClassNameList
605
+ k as BrowserRuntime,
606
+ D as ChangeDetector,
607
+ $ as LAYER_ORDER,
608
+ d as StylePartitionManager,
609
+ R as baroBoot,
610
+ V as baroStart,
611
+ j as collectJsonRenderClassNames,
612
+ B as getRuntime,
613
+ N as normalizeClassName,
614
+ h as normalizeClassNameList,
615
+ O as preloadJsonRenderClasses,
616
+ F as shadcnTheme
425
617
  };