@mintjamsinc/ichigojs 0.1.79 → 0.1.81

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.
@@ -9218,8 +9218,9 @@ class VSlotOutletDirective {
9218
9218
  this.#scopeBindings.setLocal(this.#entry.scopeVar, this.#buildSlotProps());
9219
9219
  }
9220
9220
  // Clone the scoped template's content and create its VNode tree BEFORE
9221
- // inserting into the DOM, so components inside the slot content are
9222
- // compiled before their connectedCallback can expand them.
9221
+ // inserting into the DOM, so its directives run against a subtree
9222
+ // nothing else can observe yet. Components inside are expanded
9223
+ // afterwards, once the content is in the document.
9223
9224
  // (Created through the parent application to keep the VNode import
9224
9225
  // type-only; a direct `new VNode` here would close a module cycle
9225
9226
  // VNode -> VDirectiveManager -> VSlotOutletDirective -> VNode.)
@@ -9238,6 +9239,17 @@ class VSlotOutletDirective {
9238
9239
  slot.replaceWith(fragment);
9239
9240
  // Initial rendering
9240
9241
  this.#contentVNode.forceUpdate();
9242
+ // Components in the slot content expand once it is in the document:
9243
+ // now, if the outlet is connected, or — when it renders inside a
9244
+ // detached v-if / v-for clone — through expandComponents() below, when
9245
+ // the directive that inserts the clone walks it.
9246
+ this.#contentVNode.expandComponents();
9247
+ }
9248
+ /**
9249
+ * @inheritdoc
9250
+ */
9251
+ expandComponents() {
9252
+ this.#contentVNode?.expandComponents();
9241
9253
  }
9242
9254
  /**
9243
9255
  * Re-evaluates the slot props (component-side change) and pushes them into
@@ -9854,6 +9866,37 @@ class VNode {
9854
9866
  * remove the entire fragment atomically.
9855
9867
  */
9856
9868
  #fragmentRange;
9869
+ /**
9870
+ * Set by {@link destroy}. A destroyed clone stays in its parent's child
9871
+ * list (v-if / v-for render new clones under the same parent), so walks
9872
+ * over the children must skip it.
9873
+ */
9874
+ #destroyed = false;
9875
+ /**
9876
+ * Custom element tags already reported by
9877
+ * {@link VNode.#warnIfUnknownCustomElement}.
9878
+ */
9879
+ static #warnedUnknownTags = new Set();
9880
+ /**
9881
+ * Warns about a hyphenated tag that no one has registered — a forgotten
9882
+ * import, a missing defineComponent() call, or a typo in the tag name.
9883
+ * Such an element is compiled as an ordinary element and renders its own
9884
+ * children, which looks enough like a working page to cost an afternoon.
9885
+ * Reported once per tag; third-party custom elements are recognised
9886
+ * through the registry the browser keeps.
9887
+ */
9888
+ static #warnIfUnknownCustomElement(nodeName) {
9889
+ const tag = nodeName.toLowerCase();
9890
+ if (!tag.includes('-') || VNode.#warnedUnknownTags.has(tag)) {
9891
+ return;
9892
+ }
9893
+ if (typeof customElements !== 'undefined' && customElements.get(tag)) {
9894
+ return;
9895
+ }
9896
+ VNode.#warnedUnknownTags.add(tag);
9897
+ console.warn(`[ichigo] <${tag}>: unknown custom element — nothing has registered this tag.`
9898
+ + ' Did you forget to import its module or call defineComponent(), or is the tag misspelled?');
9899
+ }
9857
9900
  /**
9858
9901
  * Creates an instance of the virtual node.
9859
9902
  * @param args The initialization arguments for the virtual node.
@@ -9885,6 +9928,9 @@ class VNode {
9885
9928
  if (isComponent) {
9886
9929
  element._ichigoHost = { vNode: this };
9887
9930
  }
9931
+ else {
9932
+ VNode.#warnIfUnknownCustomElement(this.#nodeName);
9933
+ }
9888
9934
  // Initialize child virtual nodes
9889
9935
  this.#childVNodes = [];
9890
9936
  // Initialize directive manager
@@ -9918,16 +9964,6 @@ class VNode {
9918
9964
  });
9919
9965
  }
9920
9966
  }
9921
- // A component element this application compiled but that has not
9922
- // expanded yet — it deferred to us because an ancestor component
9923
- // was attaching its template, or its <template> was not loaded when
9924
- // it connected — is expanded now: the host context is installed,
9925
- // the directives above delivered the prop values, and any authored
9926
- // slot content was compiled in this scope. A template source
9927
- // (v-if / v-for) is left alone; its clones expand on insertion.
9928
- if (isComponent && !this.#templatized && !isExpandedComponent) {
9929
- element._ichigoTryExpand?.();
9930
- }
9931
9967
  // After creating child nodes, call onMounted for directives that do not templatize
9932
9968
  if (!this.#templatized) {
9933
9969
  // animation frame to ensure DOM is updated
@@ -9953,6 +9989,58 @@ class VNode {
9953
9989
  // Register this node as a dependent of the parent node, if any
9954
9990
  this.#closers = this.#parentVNode?.addDependent(this);
9955
9991
  }
9992
+ /**
9993
+ * Expands the component elements this application compiled in this subtree
9994
+ * but has not expanded yet, parents before their children.
9995
+ *
9996
+ * This is the ONLY thing that expands a component. It is deliberately a
9997
+ * separate step from compilation rather than a call at the end of the
9998
+ * constructor: a v-if / v-for clone is compiled while it is still detached
9999
+ * (so that its directives run before anything can observe a half-built
10000
+ * subtree), and expanding a detached element would mount a component
10001
+ * application outside the document. The callers therefore compile, insert,
10002
+ * and then call this — see VApplication.mount, VConditionalDirective,
10003
+ * VForDirective and VSlotOutletDirective.
10004
+ *
10005
+ * Walking only {@link childVNodes} keeps the traversal inside this
10006
+ * application: the internals of an expanded component were never compiled
10007
+ * here, so they are not reachable and are left to their own application.
10008
+ * The one exception is content a directive rendered outside its own
10009
+ * children — scoped slot content — reached through
10010
+ * {@link VDirective.expandComponents}.
10011
+ *
10012
+ * Only connected elements are expanded. v-if / v-for call this right after
10013
+ * inserting a clone, but when that clone sits inside another clone that is
10014
+ * still detached (nested directives, or slot content rendered by an outlet
10015
+ * inside a component's v-if), the insertion is not into the document yet;
10016
+ * the outer inserter's walk reaches the same nodes once it is. A
10017
+ * templatized node (a v-if / v-for source) is not expanded itself, but its
10018
+ * rendered clones are its children and are walked.
10019
+ */
10020
+ expandComponents() {
10021
+ if (this.#destroyed) {
10022
+ return;
10023
+ }
10024
+ // Expand only a connected element. A subtree compiled and inserted
10025
+ // into a still-detached clone (a v-if / v-for nested in another
10026
+ // clone, or in scoped slot content rendered inside one) is reached
10027
+ // again by the walk of whoever inserts that clone into the document —
10028
+ // and by then its elements are upgraded and ready to expand.
10029
+ // A templatized node is a template source: never expanded itself,
10030
+ // but its rendered clones are its child VNodes and are walked below.
10031
+ if (!this.#templatized && this.#nodeType === Node.ELEMENT_NODE && IchigoElementRegistry.has(this.#nodeName)) {
10032
+ const element = this.#node;
10033
+ if (element._ichigoExpanded !== true && element.isConnected) {
10034
+ element._ichigoExpand?.();
10035
+ }
10036
+ }
10037
+ // Subtrees a directive rendered outside this VNode's children (the
10038
+ // scoped slot content of a <slot> outlet) are expanded here too: this
10039
+ // walk runs once the subtree is in the document, which the outlet
10040
+ // cannot know when it renders inside a detached v-if / v-for clone.
10041
+ this.#directiveManager?.directives?.forEach(d => d.expandComponents?.());
10042
+ this.#childVNodes?.forEach(child => child.expandComponents());
10043
+ }
9956
10044
  /**
9957
10045
  * The application instance associated with this virtual node.
9958
10046
  */
@@ -10360,6 +10448,7 @@ class VNode {
10360
10448
  * 6. Call onUnmounted lifecycle hooks
10361
10449
  */
10362
10450
  destroy() {
10451
+ this.#destroyed = true;
10363
10452
  // If no directive requires template preservation, call onUnmount for directives that do not templatize
10364
10453
  if (!this.#templatized) {
10365
10454
  this.#directiveManager?.directives?.forEach(d => {
@@ -10753,11 +10842,10 @@ class VConditionalDirective {
10753
10842
  }
10754
10843
  // Clone the original node and create a new VNode for it
10755
10844
  const clone = this.#cloneNode();
10756
- // Create a new VNode for the cloned element BEFORE inserting into DOM.
10757
- // This prevents custom elements (Web Components) from having their
10758
- // connectedCallback fire before VNode processes their children, which
10759
- // would cause the parent VApplication to incorrectly adopt the custom
10760
- // element's internal template content as its own VNode tree.
10845
+ // Compile the clone BEFORE inserting it, so its directives run against a
10846
+ // subtree nothing else can observe yet (v-if inside creates anchors and
10847
+ // detaches template sources). Components inside are expanded afterwards,
10848
+ // once the clone is in the document — see expandComponents() below.
10761
10849
  // Pass the current bindings to ensure loop variables from v-for are available
10762
10850
  const vNode = new VNode({
10763
10851
  node: clone,
@@ -10772,12 +10860,16 @@ class VConditionalDirective {
10772
10860
  vNode.fragmentRange = VFragmentRange.insert(anchorParent, nextSibling, 'vif-fragment', clone);
10773
10861
  this.#renderedVNode = vNode;
10774
10862
  this.#renderedVNode.forceUpdate();
10863
+ this.#renderedVNode.expandComponents();
10775
10864
  return;
10776
10865
  }
10777
10866
  const nodeToInsert = vNode.anchorNode || clone;
10778
10867
  anchorParent?.insertBefore(nodeToInsert, nextSibling);
10779
10868
  this.#renderedVNode = vNode;
10780
10869
  this.#renderedVNode.forceUpdate();
10870
+ // Components expand once the clone is in the document and its bindings
10871
+ // have been applied, so they mount connected and with their props set.
10872
+ this.#renderedVNode.expandComponents();
10781
10873
  }
10782
10874
  /**
10783
10875
  * Removes the node from the DOM.
@@ -11234,6 +11326,7 @@ class VForDirective {
11234
11326
  vNode.fragmentRange = range;
11235
11327
  newRenderedItems.push({ key, vNode });
11236
11328
  vNode.forceUpdate();
11329
+ vNode.expandComponents();
11237
11330
  prevNode = range.lastNode;
11238
11331
  continue;
11239
11332
  }
@@ -11248,6 +11341,10 @@ class VForDirective {
11248
11341
  }
11249
11342
  newRenderedItems.push({ key, vNode });
11250
11343
  vNode.forceUpdate();
11344
+ // Components expand once the clone is in the document and its
11345
+ // bindings have been applied, so they mount connected and with
11346
+ // their props set.
11347
+ vNode.expandComponents();
11251
11348
  }
11252
11349
  else {
11253
11350
  // Reuse existing item
@@ -14817,6 +14914,10 @@ class VApplication {
14817
14914
  });
14818
14915
  // Initial rendering
14819
14916
  this.#vNode.update();
14917
+ // Expand the components in the tree. Last, so every component element
14918
+ // has been compiled and has had its prop values delivered before its
14919
+ // template is cloned in. See VNode.expandComponents.
14920
+ this.#vNode.expandComponents();
14820
14921
  // Remove v-cloak attributes after mounting
14821
14922
  // v-cloak is used to hide un-compiled template until the app is ready
14822
14923
  // Usage: Add CSS rule "[v-cloak] { display: none; }" to hide elements with v-cloak
@@ -15381,6 +15482,19 @@ class VDOM {
15381
15482
  /**
15382
15483
  * Base class for ichigo.js-backed Web Components (Light DOM, no Shadow DOM).
15383
15484
  *
15485
+ * Expansion — cloning the template into this element — is driven by the
15486
+ * application that owns the element, not by connectedCallback: VNode compiles
15487
+ * the element first (installing the host context, delivering the prop values
15488
+ * and compiling the authored slot content in the owner's scope) and only then
15489
+ * calls {@link _ichigoExpand}. That leaves connectedCallback a single job,
15490
+ * re-attachment; see there.
15491
+ *
15492
+ * The component's <template> must be present in the document, and
15493
+ * defineComponent() must have run, before the owning application is mounted.
15494
+ * That is the application's responsibility. A missing template is reported as
15495
+ * a warning and the element is left empty — it is never silently retried,
15496
+ * because an app that renders anyway is an app whose bug cannot be found.
15497
+ *
15384
15498
  * Mount timing:
15385
15499
  * - If the component declares no props, the VApplication is mounted synchronously
15386
15500
  * at the end of connectedCallback (the template is known, no props to wait for).
@@ -15440,38 +15554,42 @@ class IchigoElement extends HTMLElement {
15440
15554
  get _ichigoScopedSlots() {
15441
15555
  return this.#slotCache?.scopedSlots;
15442
15556
  }
15557
+ /**
15558
+ * Handles RE-attachment, and nothing else.
15559
+ *
15560
+ * A first connection is not what expands this element: the owning
15561
+ * application does that from {@link _ichigoExpand} once it has compiled the
15562
+ * element. So a connection event on a not-yet-expanded element means the
15563
+ * owner has not reached it yet, and the right thing to do is nothing. That
15564
+ * covers every case the old guards enumerated one by one — a v-if / v-for
15565
+ * template source, an element inside an enclosing component's template
15566
+ * clone, a clone inserted before its directive expands it.
15567
+ *
15568
+ * An expanded element whose mount root has been torn down, on the other
15569
+ * hand, was disconnected and is now coming back: v-for reorders relocate
15570
+ * the host element, which disconnects and reconnects it. Its children are
15571
+ * the previous expansion, not authored slot content, so it re-expands from
15572
+ * the cached slot nodes. Testing the mount root rather than the expansion
15573
+ * flag alone keeps this from firing on an element that was expanded while
15574
+ * detached and is being inserted for the first time.
15575
+ */
15443
15576
  connectedCallback() {
15444
- // --- 0a. Guard: do not expand a template-source element ---
15445
- // An element still carrying a templatizing directive attribute (v-if
15446
- // family / v-for) is a template source: the owning application clones it
15447
- // and inserts the clones (whose directive attributes are stripped during
15448
- // clone compilation, so the clones expand normally). Expanding the source
15449
- // here would bake the expansion into every clone, and the clone's
15450
- // internals would then be compiled in the parent scope.
15451
- if (this.hasAttribute(StandardDirectiveName.V_IF) ||
15452
- this.hasAttribute(StandardDirectiveName.V_ELSE_IF) ||
15453
- this.hasAttribute(StandardDirectiveName.V_ELSE) ||
15454
- this.hasAttribute(StandardDirectiveName.V_FOR)) {
15455
- return;
15456
- }
15457
- // --- 0a2. Guard: wait to be compiled by the owning application ---
15458
- // This element came from an enclosing component's template clone and no
15459
- // application has compiled it yet: the enclosing component's appendChild
15460
- // connected it before that component's VApplication existed. Expanding
15461
- // now would capture the slot content and mount before the owning
15462
- // application installs the host context (_ichigoHost) and delivers the
15463
- // prop values — scoped slot outlets would find no parent scope and
15464
- // silently fall back to their default content. The owning application
15465
- // expands this element while it compiles it (VNode ->
15466
- // _ichigoTryExpand()), by which time both are in place.
15467
- //
15468
- // Only template-owned elements defer: authored slot content placed
15469
- // inside a component by an application is not part of the template, so
15470
- // it keeps expanding immediately (and is redistributed by the enclosing
15471
- // component on every re-expansion).
15472
- if (this._ichigoTemplateOwned === true && !this._ichigoHost) {
15473
- return;
15577
+ if (this._ichigoExpanded && !this.#mountRoot) {
15578
+ this._ichigoExpand();
15474
15579
  }
15580
+ }
15581
+ /**
15582
+ * Expands this element's template. Called by the application that owns the
15583
+ * element, right after it compiled it (VNode.expandComponents): the host
15584
+ * context is installed, the prop bindings have delivered their values and
15585
+ * the authored slot content has been compiled in the owner's scope, so the
15586
+ * capture below sees owner-compiled nodes and the component's own
15587
+ * application starts with its props already in hand.
15588
+ *
15589
+ * Also called by {@link connectedCallback} to rebuild the expansion after a
15590
+ * move.
15591
+ */
15592
+ _ichigoExpand() {
15475
15593
  // --- 0b. Reclaim prop values assigned before upgrade ---
15476
15594
  // A plain property set on the instance (e.g. by a binding delivered
15477
15595
  // while the element was detached or not yet upgraded) is an own data
@@ -15487,13 +15605,15 @@ class IchigoElement extends HTMLElement {
15487
15605
  this[prop] = value;
15488
15606
  }
15489
15607
  }
15490
- // --- 0c. Guard: skip if template not yet loaded ---
15491
- // customElements.define() may run before loadComponent() completes (e.g. when
15492
- // dynamic imports are inlined by the bundler). The static placeholder element
15493
- // in the HTML will be removed and replaced by v-for once the template is ready,
15494
- // so it is safe to do nothing here.
15608
+ // --- 0c. Resolve the template ---
15609
+ // Having the <template> in the document by now is the application's
15610
+ // contract (see the class doc). Report the breach and leave the element
15611
+ // empty: rendering something plausible here would hide the bug at the
15612
+ // one moment it is still cheap to find.
15495
15613
  const templateEl = document.querySelector(ctor._template);
15496
15614
  if (!templateEl || !(templateEl instanceof HTMLTemplateElement)) {
15615
+ IchigoElement.#warnOncePerTag(this.tagName, `template '${ctor._template}' was not found in the document. `
15616
+ + 'Inject the <template> and call defineComponent() before mounting the application.');
15497
15617
  return;
15498
15618
  }
15499
15619
  // --- 1. Capture slot content (first connection only) ---
@@ -15545,18 +15665,6 @@ class IchigoElement extends HTMLElement {
15545
15665
  // --- 2. Clone the component template ---
15546
15666
  const fragment = templateEl.content.cloneNode(true);
15547
15667
  const root = this.#findRootElement(fragment);
15548
- // Mark the component elements the template brought in, so they let this
15549
- // component's application compile them before they expand (guard 0a2).
15550
- // Marked before slot distribution, so authored slot content — which is
15551
- // not part of the template — keeps expanding under the application that
15552
- // authored it. Elements inside a nested <template> are not marked: they
15553
- // are inert until a directive clones them, and the clone is compiled
15554
- // before it is inserted.
15555
- for (const element of Array.from(fragment.querySelectorAll('*'))) {
15556
- if (IchigoElementRegistry.has(element.tagName)) {
15557
- element._ichigoTemplateOwned = true;
15558
- }
15559
- }
15560
15668
  // --- 3. Distribute named slot content ---
15561
15669
  for (const [name, nodes] of namedNodes) {
15562
15670
  const slot = root.querySelector(`slot[name="${name}"]`);
@@ -15594,21 +15702,6 @@ class IchigoElement extends HTMLElement {
15594
15702
  this.#app = undefined;
15595
15703
  }
15596
15704
  }
15597
- /**
15598
- * Expands this element's template on behalf of the application that owns
15599
- * it. Called by VNode right after it compiled this element — the host
15600
- * context is installed and the prop bindings have delivered their values,
15601
- * so a component that deferred its expansion (guard 0a2, or a template
15602
- * that was not loaded when it connected) can now expand in the right
15603
- * order. A no-op when the element is already expanded or not connected
15604
- * (a template-source clone expands when it is inserted).
15605
- */
15606
- _ichigoTryExpand() {
15607
- if (this._ichigoExpanded || !this.isConnected) {
15608
- return;
15609
- }
15610
- this.connectedCallback();
15611
- }
15612
15705
  /**
15613
15706
  * Called by the property setters generated by defineComponent().
15614
15707
  * Before mount: stores the value and schedules a mount microtask.
@@ -15835,6 +15928,23 @@ class IchigoElement extends HTMLElement {
15835
15928
  #warn(message) {
15836
15929
  console.warn(`[ichigo] <${this.tagName.toLowerCase()}>: ${message}`);
15837
15930
  }
15931
+ /**
15932
+ * Tags already reported by {@link #warnOncePerTag}.
15933
+ */
15934
+ static #warnedTags = new Set();
15935
+ /**
15936
+ * Reports a per-component-type problem once. A missing template breaks
15937
+ * every instance, and a v-for over a hundred rows would otherwise print a
15938
+ * hundred identical lines and bury the first one.
15939
+ */
15940
+ static #warnOncePerTag(tagName, message) {
15941
+ const tag = tagName.toLowerCase();
15942
+ if (IchigoElement.#warnedTags.has(tag)) {
15943
+ return;
15944
+ }
15945
+ IchigoElement.#warnedTags.add(tag);
15946
+ console.warn(`[ichigo] <${tag}>: ${message}`);
15947
+ }
15838
15948
  }
15839
15949
 
15840
15950
  // Copyright (c) 2025 MintJams Inc. Licensed under MIT License.