lego-dom 2.0.1 → 2.0.2

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 (3) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/main.js +14 -7
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  # Changelog
4
4
 
5
+ ## [2.0.2] - 2026-01-19
6
+
7
+ ### Features
8
+
9
+ - **Scoped Props (`b-logic`):** `b-logic` attributes now inherit the parent block's scope! You can pass state naturally to children without global variables.
10
+ ```html
11
+ <!-- 'user' is resolved from the parent block's state -->
12
+ <user-avatar b-logic="{ user: user }"></user-avatar>
13
+ ```
14
+
15
+ - **`$parent` Helper:** Added `this.$parent` (and template access) to easily find the nearest ancestor Block. It intelligently skips non-block elements (divs, spans) and handles Shadow DOM boundaries.
16
+ ```javascript
17
+ const parentBlock = this.$parent;
18
+ ```
19
+
5
20
  ## [2.0.1] - 2026-01-19
6
21
 
7
22
  ### The "Blocks" Update Refactor 🧱
package/main.js CHANGED
@@ -176,11 +176,10 @@ const Lego = (() => {
176
176
  return p;
177
177
  };
178
178
 
179
- const parseJSObject = (raw) => {
179
+ const parseJSObject = (raw, scope = {}) => {
180
180
  try {
181
- return (new Function(`return (${raw})`))();
181
+ return (new Function('scope', 'global', `with(global) { with(scope) { return (${raw}); } }`))(scope, Lego.globals);
182
182
  } catch (e) {
183
- console.error(`[Lego] Failed to parse b-data:`, raw, e);
184
183
  return {};
185
184
  }
186
185
  };
@@ -204,10 +203,13 @@ const Lego = (() => {
204
203
  };
205
204
 
206
205
  const findAncestor = (el, tagName) => {
207
- let parent = el.parentElement || el.getRootNode().host;
206
+ if (!el) return undefined;
207
+ let parent = el.parentElement || (el.getRootNode ? el.getRootNode().host : null);
208
208
  while (parent) {
209
- if (parent.tagName && parent.tagName.toLowerCase() === tagName.toLowerCase()) {
210
- return parent;
209
+ const pName = parent.tagName ? parent.tagName.toLowerCase() : '';
210
+ if (pName) {
211
+ if (tagName === '*' && registry[pName]) return parent;
212
+ if (pName === tagName.toLowerCase()) return parent;
211
213
  }
212
214
  parent = parent.parentElement || (parent.getRootNode && parent.getRootNode().host);
213
215
  }
@@ -561,9 +563,13 @@ const Lego = (() => {
561
563
  // TIER 1: Logic from Lego.block (Lego File)
562
564
  // TIER 2: Logic from the <template b-data="..."> attribute
563
565
  // TIER 3: Logic from the <my-comp b-data="..."> tag
566
+ // SCOPED PROPS: Use parent block state as scope for instance logic evaluation
567
+ const parentBlock = findAncestor(el, '*') || findAncestor(el.getRootNode().host, '*');
568
+ const parentScope = parentBlock && parentBlock.state ? parentBlock.state : {};
569
+
564
570
  const scriptLogic = legoFileLogic.get(name) || {};
565
571
  const templateLogic = parseJSObject(templateNode.getAttribute('b-logic') || templateNode.getAttribute('b-data') || '{}');
566
- const instanceLogic = parseJSObject(el.getAttribute('b-logic') || el.getAttribute('b-data') || '{}');
572
+ const instanceLogic = parseJSObject(el.getAttribute('b-logic') || el.getAttribute('b-data') || '{}', parentScope);
567
573
 
568
574
  el._studs = reactive({
569
575
  ...scriptLogic,
@@ -571,6 +577,7 @@ const Lego = (() => {
571
577
  ...instanceLogic,
572
578
  $vars: {},
573
579
  $element: el,
580
+ get $parent() { return findAncestor(el, '*') },
574
581
  $emit: (name, detail) => {
575
582
  el.dispatchEvent(new CustomEvent(name, { detail, bubbles: true, composed: true }));
576
583
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lego-dom",
3
- "version": "2.0.1",
3
+ "version": "2.0.2",
4
4
  "license": "MIT",
5
5
  "description": "A feature-rich web components + SFC frontend framework",
6
6
  "main": "main.js",