css-in-js-engine-pb 0.2.3 → 0.2.5

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.
@@ -1,2 +1,2 @@
1
- export declare function insertRulesOnce(rules: string[]): void;
1
+ export declare function insertRulesOnce(rules: string[], bucket: number): void;
2
2
  //# sourceMappingURL=insertOnce.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"insertOnce.d.ts","sourceRoot":"","sources":["../src/insertOnce.ts"],"names":[],"mappings":"AAcA,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,QAkB9C"}
1
+ {"version":3,"file":"insertOnce.d.ts","sourceRoot":"","sources":["../src/insertOnce.ts"],"names":[],"mappings":"AA4CA,wBAAgB,eAAe,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,QAmB9D"}
@@ -1,20 +1,43 @@
1
1
  const insertedRules = new Set();
2
- let styleEl = null;
3
- let sheet = null;
4
- function ensureSheet() {
2
+ const styleElementsByBucket = new Map();
3
+ const sheetsByBucket = new Map();
4
+ const BUCKET_ORDER = [0, 1, 2, 3, 4, 5, 6, 7]; // Reset, Base, Appearance, Size, Shape, State, Focus, User
5
+ function ensureSheetForBucket(bucket) {
6
+ let sheet = sheetsByBucket.get(bucket);
5
7
  if (!sheet) {
6
- styleEl = document.createElement('style');
7
- document.head.appendChild(styleEl);
8
+ const styleEl = document.createElement('style');
9
+ styleEl.setAttribute('data-bucket', String(bucket));
10
+ // Find the correct insertion point in the DOM
11
+ // We need to insert this bucket's <style> tag AFTER any lower buckets
12
+ // but BEFORE any higher buckets
13
+ const existingBuckets = Array.from(styleElementsByBucket.entries())
14
+ .sort((a, b) => a[0] - b[0]);
15
+ let insertBeforeElement = null;
16
+ for (const [existingBucket, existingElement] of existingBuckets) {
17
+ if (existingBucket > bucket) {
18
+ insertBeforeElement = existingElement;
19
+ break;
20
+ }
21
+ }
22
+ if (insertBeforeElement) {
23
+ document.head.insertBefore(styleEl, insertBeforeElement);
24
+ }
25
+ else {
26
+ document.head.appendChild(styleEl);
27
+ }
8
28
  sheet = styleEl.sheet;
29
+ styleElementsByBucket.set(bucket, styleEl);
30
+ sheetsByBucket.set(bucket, sheet);
31
+ console.log(`[ensureSheetForBucket] Created new <style> for bucket ${bucket}`);
9
32
  }
10
33
  return sheet;
11
34
  }
12
- export function insertRulesOnce(rules) {
13
- console.log('[insertRulesOnce]', 'called with', rules.length, 'rules');
35
+ export function insertRulesOnce(rules, bucket) {
36
+ console.log('[insertRulesOnce]', `bucket ${bucket}:`, 'called with', rules.length, 'rules');
14
37
  console.log(rules);
15
38
  if (rules.length === 0)
16
39
  return;
17
- const sheet = ensureSheet();
40
+ const sheet = ensureSheetForBucket(bucket);
18
41
  for (const rule of rules) {
19
42
  if (insertedRules.has(rule))
20
43
  continue;
@@ -1 +1 @@
1
- {"version":3,"file":"insertWithBucket.d.ts","sourceRoot":"","sources":["../src/insertWithBucket.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAmC5C,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,WAAW,QAqDzE"}
1
+ {"version":3,"file":"insertWithBucket.d.ts","sourceRoot":"","sources":["../src/insertWithBucket.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAoC5C,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,WAAW,QAgDzE"}
@@ -14,6 +14,7 @@ const ORDER = [
14
14
  ];
15
15
  const OPTIONAL_BUCKETS = [
16
16
  StyleBucket.Base,
17
+ StyleBucket.Appearance,
17
18
  StyleBucket.Size,
18
19
  StyleBucket.Shape,
19
20
  StyleBucket.State,
@@ -47,8 +48,6 @@ export function insertRulesWithBucket(rules, bucket) {
47
48
  console.groupEnd();
48
49
  return;
49
50
  }
50
- // Don’t insert beyond unseen required buckets
51
- // (Reset + Appearance are required; Base/Size/Shape/State/Focus/User are optional via pre-seed)
52
51
  for (let i = 0; i <= targetIndex; i++) {
53
52
  if (!seenBuckets.has(ORDER[i])) {
54
53
  console.log(`⛔ blocked at ${bucketName(ORDER[i])} (not seen yet)`);
@@ -56,8 +55,6 @@ export function insertRulesWithBucket(rules, bucket) {
56
55
  return;
57
56
  }
58
57
  }
59
- // Insert buckets in order up to targetIndex.
60
- // IMPORTANT: we do NOT “flush forever”. We may insert more later.
61
58
  for (let i = 0; i <= targetIndex; i++) {
62
59
  const b = ORDER[i];
63
60
  const r = bucketRules.get(b);
@@ -66,7 +63,7 @@ export function insertRulesWithBucket(rules, bucket) {
66
63
  continue;
67
64
  }
68
65
  console.log(`➡ INSERTING ${r.length} queued rules from ${bucketName(b)}`);
69
- insertRulesOnce(r);
66
+ insertRulesOnce(r, b);
70
67
  bucketRules.set(b, []);
71
68
  highestInsertedBucketIndex = Math.max(highestInsertedBucketIndex, i);
72
69
  }
@@ -1 +1 @@
1
- {"version":3,"file":"resolveStyleRules.d.ts","sourceRoot":"","sources":["../src/resolveStyleRules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAa,aAAa,EAAE,MAAM,SAAS,CAAC;AAEvE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAyEhD,wBAAgB,yBAAyB,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,EACnE,aAAa,EAAE,aAAa,CAAC,KAAK,CAAC,EACnC,WAAW,CAAC,EAAE,eAAe,GAC9B,aAAa,CAAC,KAAK,CAAC,CAwBtB"}
1
+ {"version":3,"file":"resolveStyleRules.d.ts","sourceRoot":"","sources":["../src/resolveStyleRules.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAa,aAAa,EAAE,MAAM,SAAS,CAAC;AAEvE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAmDhD,wBAAgB,yBAAyB,CAAC,KAAK,SAAS,MAAM,GAAG,MAAM,EACnE,aAAa,EAAE,aAAa,CAAC,KAAK,CAAC,EACnC,WAAW,CAAC,EAAE,eAAe,GAC9B,aAAa,CAAC,KAAK,CAAC,CAetB"}
@@ -1,49 +1,32 @@
1
1
  import { camelToKebab, resolveValue } from './utils/css';
2
2
  import { AtomicRuleCache } from './atomicCache';
3
- function buildAtomicClasses(rule, cache, cssRulesOut, atRule, seenDecls = new Set()) {
3
+ function buildAtomicClasses(rule, cache, cssRulesOut, atRule) {
4
4
  const classNames = [];
5
5
  for (const key in rule) {
6
6
  const val = rule[key];
7
7
  if (val == null)
8
8
  continue;
9
- // --- at-rules (@media, @supports, etc.)
10
9
  if (key.startsWith('@') && typeof val === 'object') {
11
- const nested = buildAtomicClasses(val, cache, cssRulesOut, key, seenDecls);
10
+ const nested = buildAtomicClasses(val, cache, cssRulesOut, key);
12
11
  classNames.push(...nested);
13
12
  continue;
14
13
  }
15
- // --- pseudo selectors (:hover, :active, etc.)
16
14
  if (key.startsWith(':') && typeof val === 'object') {
17
- const selector = `&${key}`;
18
15
  for (const prop in val) {
19
16
  const v = val[prop];
20
17
  if (v == null || typeof v === 'object')
21
18
  continue;
22
- const cssProp = camelToKebab(prop);
23
- const declKey = `${atRule !== null && atRule !== void 0 ? atRule : ''}|${selector}|${cssProp}`;
24
- // 👇 LAST ONE WINS — drop earlier declarations
25
- if (seenDecls.has(declKey))
26
- continue;
27
- seenDecls.add(declKey);
28
- const decl = `${cssProp}:${resolveValue(v)}`;
29
- const res = cache.getOrCreate(selector, decl, atRule);
19
+ const decl = `${camelToKebab(prop)}:${resolveValue(v)}`;
20
+ const res = cache.getOrCreate(`&${key}`, decl, atRule);
30
21
  classNames.push(res.className);
31
22
  if (res.ruleText)
32
23
  cssRulesOut.push(res.ruleText);
33
24
  }
34
25
  continue;
35
26
  }
36
- // --- normal properties
37
27
  if (typeof val !== 'object') {
38
- const cssProp = camelToKebab(key);
39
- const selector = '&';
40
- const declKey = `${atRule !== null && atRule !== void 0 ? atRule : ''}|${selector}|${cssProp}`;
41
- // 👇 LAST ONE WINS — drop earlier declarations
42
- if (seenDecls.has(declKey))
43
- continue;
44
- seenDecls.add(declKey);
45
- const decl = `${cssProp}:${resolveValue(val)}`;
46
- const res = cache.getOrCreate(selector, decl, atRule);
28
+ const decl = `${camelToKebab(key)}:${resolveValue(val)}`;
29
+ const res = cache.getOrCreate('&', decl, atRule);
47
30
  classNames.push(res.className);
48
31
  if (res.ruleText)
49
32
  cssRulesOut.push(res.ruleText);
@@ -57,9 +40,7 @@ export function resolveStyleRulesForSlots(stylesBySlots, sharedCache) {
57
40
  const classNamesBySlot = {};
58
41
  for (const slot in stylesBySlots) {
59
42
  const rule = stylesBySlots[slot];
60
- // 👇 NEW: fresh seenDecls PER SLOT
61
- const seenDecls = new Set();
62
- const atomicClasses = buildAtomicClasses(rule !== null && rule !== void 0 ? rule : {}, cache, cssRules, undefined, seenDecls);
43
+ const atomicClasses = buildAtomicClasses(rule !== null && rule !== void 0 ? rule : {}, cache, cssRules);
63
44
  classNamesBySlot[slot] = atomicClasses.join(' ');
64
45
  }
65
46
  return { classNamesBySlot, cssRules };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "css-in-js-engine-pb",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "description": "A lightweight atomic CSS-in-JS engine built on the native CSS cascade",
5
5
  "license": "MIT",
6
6
  "type": "module",