@torpor/view 1.2.4 → 1.2.6

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.mjs CHANGED
@@ -46,6 +46,75 @@ function addEvent(el, type, listener) {
46
46
  });
47
47
  }
48
48
  //#endregion
49
+ //#region src/render/isTextNode.ts
50
+ function isTextNode(node) {
51
+ return node.nodeType === 3;
52
+ }
53
+ const HYDRATION_BREAK_REGEX = /^\^(?:\d+)?$/;
54
+ /**
55
+ * Whether a comment node's data is a branch-break marker (`^`, or `^<index>`
56
+ * when the server recorded the branch it rendered).
57
+ */
58
+ function isHydrationBreak(data) {
59
+ return HYDRATION_BREAK_REGEX.test(data);
60
+ }
61
+ /**
62
+ * The branch index encoded in a `^<index>` branch-break marker, or `undefined`
63
+ * for a plain `^` marker (e.g. from `@for`, which has no branch index).
64
+ */
65
+ function hydrationBreakIndex(data) {
66
+ if (!data.startsWith("^") || data.length === 1) return;
67
+ const index = Number(data.slice(1));
68
+ return Number.isNaN(index) ? void 0 : index;
69
+ }
70
+ //#endregion
71
+ //#region src/render/isCommentNode.ts
72
+ function isCommentNode(node) {
73
+ return node.nodeType === 8;
74
+ }
75
+ //#endregion
76
+ //#region src/render/nodeCheckHydrationBreak.ts
77
+ /**
78
+ * When hydrating, removes and skips hydration comments that are inserted at the
79
+ * start of branches and used to split up text nodes that would otherwise be
80
+ * joined in HTML.
81
+ *
82
+ * @param node The node to check and maybe skip.
83
+ * @returns The next node.
84
+ */
85
+ function nodeCheckHydrationBreak(node) {
86
+ if (node !== null && isCommentNode(node) && isHydrationBreak(node.data)) {
87
+ let comment = node;
88
+ node = node.nextSibling;
89
+ comment.remove();
90
+ }
91
+ context.hydrationNode = node;
92
+ return node;
93
+ }
94
+ //#endregion
95
+ //#region src/render/nodeNext.ts
96
+ /**
97
+ * Gets the next sibling of a node.
98
+ *
99
+ * When hydrating, sets the hydration node.
100
+ *
101
+ * @param node The node.
102
+ * @param text Whether we require a text node.
103
+ */
104
+ function nodeNext(node, text = false) {
105
+ if (context.hydrationNode !== null) {
106
+ if (text && isTextNode(node)) return node;
107
+ let next = nodeCheckHydrationBreak(node.nextSibling);
108
+ while (!text && next !== null && isTextNode(next) && next.textContent?.trim() === "") next = nodeCheckHydrationBreak(next.nextSibling);
109
+ if (!text && next !== null && next.nodeName === "TBODY" && next.firstChild && isTextNode(node)) {
110
+ next = next.firstChild;
111
+ context.hydrationNode = next;
112
+ }
113
+ return next;
114
+ }
115
+ return node.nextSibling;
116
+ }
117
+ //#endregion
49
118
  //#region src/watch/clearSources.ts
50
119
  /**
51
120
  * Clear unused source subscriptions after each Computed or Effect has been run.
@@ -454,6 +523,133 @@ function runMountCallback(fn) {
454
523
  //#endregion
455
524
  //#region src/render/animate.ts
456
525
  /**
526
+ * Parses a transform function argument as a number, returning null for
527
+ * anything we can't confidently evaluate (calcs, variables, etc.).
528
+ */
529
+ function parseNumber(value) {
530
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[a-z%]+)?$/i.test(value.trim())) return null;
531
+ return Number.parseFloat(value);
532
+ }
533
+ function isZero(value) {
534
+ return parseNumber(value) === 0;
535
+ }
536
+ function isOne(value) {
537
+ return parseNumber(value) === 1;
538
+ }
539
+ function all(args, expected, test) {
540
+ return args.length === expected && args.every(test);
541
+ }
542
+ function isIdentityFunction(name, args) {
543
+ switch (name) {
544
+ case "matrix": {
545
+ const numbers = args.map(parseNumber);
546
+ return numbers.length === 6 && numbers[0] === 1 && numbers[1] === 0 && numbers[2] === 0 && numbers[3] === 1 && numbers[4] === 0 && numbers[5] === 0;
547
+ }
548
+ case "matrix3d": {
549
+ const numbers = args.map(parseNumber);
550
+ const identity = [
551
+ 1,
552
+ 0,
553
+ 0,
554
+ 0,
555
+ 0,
556
+ 1,
557
+ 0,
558
+ 0,
559
+ 0,
560
+ 0,
561
+ 1,
562
+ 0,
563
+ 0,
564
+ 0,
565
+ 0,
566
+ 1
567
+ ];
568
+ return numbers.length === 16 && numbers.every((value, i) => value === identity[i]);
569
+ }
570
+ case "translate": return args.length >= 1 && args.length <= 2 && args.every(isZero);
571
+ case "translatex":
572
+ case "translatey":
573
+ case "translatez": return all(args, 1, isZero);
574
+ case "translate3d": return all(args, 3, isZero);
575
+ case "scale": return args.length >= 1 && args.length <= 2 && args.every(isOne);
576
+ case "scalex":
577
+ case "scaley":
578
+ case "scalez": return all(args, 1, isOne);
579
+ case "scale3d": return all(args, 3, isOne);
580
+ case "rotate":
581
+ case "rotatex":
582
+ case "rotatey":
583
+ case "rotatez":
584
+ case "skewx":
585
+ case "skewy": return all(args, 1, isZero);
586
+ case "rotate3d": return args.length === 4 && isZero(args[3]);
587
+ case "skew": return args.length >= 1 && args.length <= 2 && args.every(isZero);
588
+ default: return false;
589
+ }
590
+ }
591
+ /**
592
+ * Whether a transform value is the identity transform (and so creates a
593
+ * containing block without changing what is rendered).
594
+ */
595
+ function isIdentityTransform(value) {
596
+ const transform = value.trim().toLowerCase();
597
+ if (transform === "" || transform === "none") return true;
598
+ let rest = transform;
599
+ while (rest.length > 0) {
600
+ const match = /^([a-z0-9]+)\(([^()]*)\)/.exec(rest);
601
+ if (!match) return false;
602
+ const args = match[2].split(",").map((arg) => arg.trim());
603
+ if (!isIdentityFunction(match[1], args)) return false;
604
+ rest = rest.slice(match[0].length).trim();
605
+ }
606
+ return true;
607
+ }
608
+ /**
609
+ * Replaces a final identity transform in the keyframes with `none`.
610
+ *
611
+ * A forwards-filled animation keeps its final keyframe applied after it
612
+ * finishes. When that keyframe's transform is the identity, the only visible
613
+ * difference from not animating the transform at all is the side effect: any
614
+ * non-`none` transform makes the element the containing block for fixed
615
+ * descendants (so fixed-position popovers/tooltips inside it are positioned
616
+ * against it rather than the viewport). Ending on `none` instead keeps the
617
+ * animation's other persisted properties while letting the transform effect
618
+ * go away.
619
+ */
620
+ function withoutFinalIdentityTransform(keyframes) {
621
+ if (keyframes === null) return null;
622
+ if (Array.isArray(keyframes)) {
623
+ let index = keyframes.length - 1;
624
+ while (index >= 0 && keyframes[index] == null) index--;
625
+ if (index < 0) return keyframes;
626
+ const last = keyframes[index];
627
+ if (typeof last.transform !== "string" || !isIdentityTransform(last.transform)) return keyframes;
628
+ const next = keyframes.slice();
629
+ next[index] = {
630
+ ...last,
631
+ transform: "none"
632
+ };
633
+ return next;
634
+ }
635
+ const transform = keyframes.transform;
636
+ if (Array.isArray(transform)) {
637
+ const last = transform[transform.length - 1];
638
+ if (typeof last !== "string" || !isIdentityTransform(last)) return keyframes;
639
+ const values = transform.slice();
640
+ values[values.length - 1] = "none";
641
+ return {
642
+ ...keyframes,
643
+ transform: values
644
+ };
645
+ }
646
+ if (typeof transform === "string" && isIdentityTransform(transform)) return {
647
+ ...keyframes,
648
+ transform: "none"
649
+ };
650
+ return keyframes;
651
+ }
652
+ /**
457
653
  * Adds an animation for an element
458
654
  * @param el The element to animate
459
655
  * @param enter Whether the animation is being run on enter
@@ -467,7 +663,9 @@ async function animate(el, enter, keyframes, options) {
467
663
  easing: "ease-in-out",
468
664
  fill: "forwards"
469
665
  }, options);
470
- const animation = el.animate(keyframes, animationOptions);
666
+ const persisted = animationOptions.fill === "forwards" || animationOptions.fill === "both";
667
+ const effectiveKeyframes = enter === true && persisted ? withoutFinalIdentityTransform(keyframes) : keyframes;
668
+ const animation = el.animate(effectiveKeyframes, animationOptions);
471
669
  setTimeout(() => {
472
670
  animation.timeline = el.ownerDocument.timeline;
473
671
  }, 1);
@@ -710,6 +908,7 @@ function addElement(node, parent, before) {
710
908
  parent = before?.parentNode ?? parent;
711
909
  if (hydrationNode === null) parent.insertBefore(node, before);
712
910
  runMountSideEffects(parent, activeRegion, hydrationNode);
911
+ if (hydrationNode !== null) nodeNext(node);
713
912
  }
714
913
  //#endregion
715
914
  //#region src/render/addFragment.ts
@@ -726,6 +925,7 @@ function addFragment(fragment, parent, before, endNode, startNode) {
726
925
  parent = before?.parentNode ?? parent;
727
926
  if (hydrationNode === null) parent.insertBefore(fragment, before);
728
927
  runMountSideEffects(parent, activeRegion, hydrationNode);
928
+ if (hydrationNode !== null && endNode !== void 0) nodeNext(endNode);
729
929
  }
730
930
  //#endregion
731
931
  //#region src/render/applyProps.ts
@@ -836,19 +1036,6 @@ function releaseRegion(region) {
836
1036
  region.effects.length = 0;
837
1037
  }
838
1038
  //#endregion
839
- //#region src/render/clearLayoutSlot.ts
840
- /**
841
- * Clears a region that was created by `fillLayoutSlot`: tears down the
842
- * rendered content and its effects, leaving the surrounding layout intact
843
- * so the slot can be refilled.
844
- *
845
- * @param region The region to clear
846
- */
847
- function clearLayoutSlot(region) {
848
- context.previousRegion = region.previousRegion;
849
- clearRegion(region);
850
- }
851
- //#endregion
852
1039
  //#region src/render/newRegion.ts
853
1040
  function newRegion(name) {
854
1041
  return {
@@ -888,6 +1075,17 @@ function pushRegion(region, toParent = false) {
888
1075
  }
889
1076
  //#endregion
890
1077
  //#region src/render/fillLayoutSlot.ts
1078
+ const refillDepths = /* @__PURE__ */ new WeakMap();
1079
+ /**
1080
+ * Records the depth of a cleared layout-slot region for the fill that replaces
1081
+ * it (see `clearLayoutSlot`).
1082
+ *
1083
+ * @param parent The node the slot renders into
1084
+ * @param depth The cleared region's depth
1085
+ */
1086
+ function rememberSlotRefillDepth(parent, depth) {
1087
+ refillDepths.set(parent, depth);
1088
+ }
891
1089
  /**
892
1090
  * Renders a component into a persistent layout slot, and returns the region
893
1091
  * it was rendered into. Used by the layout engine during navigation: the
@@ -906,12 +1104,31 @@ function pushRegion(region, toParent = false) {
906
1104
  function fillLayoutSlot(component, slot, parent, anchor, $props, $context) {
907
1105
  const region = newRegion(devContext.enabled ? "Layout slot" : void 0);
908
1106
  const oldRegion = pushRegion(region, true);
909
- region.depth = region.previousRegion === null ? 0 : region.previousRegion.depth + 1;
1107
+ const refillDepth = refillDepths.get(parent);
1108
+ if (refillDepth !== void 0) {
1109
+ region.depth = refillDepth;
1110
+ refillDepths.delete(parent);
1111
+ }
910
1112
  component(parent, anchor, $props, $context, { _: slot });
911
1113
  popRegion(oldRegion);
912
1114
  return region;
913
1115
  }
914
1116
  //#endregion
1117
+ //#region src/render/clearLayoutSlot.ts
1118
+ /**
1119
+ * Clears a region that was created by `fillLayoutSlot`: tears down the
1120
+ * rendered content and its effects, leaving the surrounding layout intact
1121
+ * so the slot can be refilled.
1122
+ *
1123
+ * @param region The region to clear
1124
+ */
1125
+ function clearLayoutSlot(region) {
1126
+ context.previousRegion = region.previousRegion;
1127
+ const parent = region.startNode?.parentNode;
1128
+ if (parent) rememberSlotRefillDepth(parent, region.depth);
1129
+ clearRegion(region);
1130
+ }
1131
+ //#endregion
915
1132
  //#region src/render/firstInsideNode.ts
916
1133
  /**
917
1134
  * During hydration, `nodeAnchor` finds the first node inside a hydration
@@ -1118,75 +1335,6 @@ function takeBranchIndex(anchor) {
1118
1335
  if (index !== void 0) branchIndexes.delete(anchor);
1119
1336
  return index;
1120
1337
  }
1121
- const HYDRATION_BREAK_REGEX = /^\^(?:\d+)?$/;
1122
- /**
1123
- * Whether a comment node's data is a branch-break marker (`^`, or `^<index>`
1124
- * when the server recorded the branch it rendered).
1125
- */
1126
- function isHydrationBreak(data) {
1127
- return HYDRATION_BREAK_REGEX.test(data);
1128
- }
1129
- /**
1130
- * The branch index encoded in a `^<index>` branch-break marker, or `undefined`
1131
- * for a plain `^` marker (e.g. from `@for`, which has no branch index).
1132
- */
1133
- function hydrationBreakIndex(data) {
1134
- if (!data.startsWith("^") || data.length === 1) return;
1135
- const index = Number(data.slice(1));
1136
- return Number.isNaN(index) ? void 0 : index;
1137
- }
1138
- //#endregion
1139
- //#region src/render/isCommentNode.ts
1140
- function isCommentNode(node) {
1141
- return node.nodeType === 8;
1142
- }
1143
- //#endregion
1144
- //#region src/render/isTextNode.ts
1145
- function isTextNode(node) {
1146
- return node.nodeType === 3;
1147
- }
1148
- //#endregion
1149
- //#region src/render/nodeCheckHydrationBreak.ts
1150
- /**
1151
- * When hydrating, removes and skips hydration comments that are inserted at the
1152
- * start of branches and used to split up text nodes that would otherwise be
1153
- * joined in HTML.
1154
- *
1155
- * @param node The node to check and maybe skip.
1156
- * @returns The next node.
1157
- */
1158
- function nodeCheckHydrationBreak(node) {
1159
- if (node !== null && isCommentNode(node) && isHydrationBreak(node.data)) {
1160
- let comment = node;
1161
- node = node.nextSibling;
1162
- comment.remove();
1163
- }
1164
- context.hydrationNode = node;
1165
- return node;
1166
- }
1167
- //#endregion
1168
- //#region src/render/nodeNext.ts
1169
- /**
1170
- * Gets the next sibling of a node.
1171
- *
1172
- * When hydrating, sets the hydration node.
1173
- *
1174
- * @param node The node.
1175
- * @param text Whether we require a text node.
1176
- */
1177
- function nodeNext(node, text = false) {
1178
- if (context.hydrationNode !== null) {
1179
- if (text && isTextNode(node)) return node;
1180
- let next = nodeCheckHydrationBreak(node.nextSibling);
1181
- while (!text && next !== null && isTextNode(next) && next.textContent?.trim() === "") next = nodeCheckHydrationBreak(next.nextSibling);
1182
- if (!text && next !== null && next.nodeName === "TBODY" && next.firstChild && isTextNode(node)) {
1183
- next = next.firstChild;
1184
- context.hydrationNode = next;
1185
- }
1186
- return next;
1187
- }
1188
- return node.nextSibling;
1189
- }
1190
1338
  //#endregion
1191
1339
  //#region src/render/nodeAnchor.ts
1192
1340
  /**