@torpor/view 1.2.3 → 1.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.
package/dist/index.cjs CHANGED
@@ -455,6 +455,133 @@ function runMountCallback(fn) {
455
455
  //#endregion
456
456
  //#region src/render/animate.ts
457
457
  /**
458
+ * Parses a transform function argument as a number, returning null for
459
+ * anything we can't confidently evaluate (calcs, variables, etc.).
460
+ */
461
+ function parseNumber(value) {
462
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[a-z%]+)?$/i.test(value.trim())) return null;
463
+ return Number.parseFloat(value);
464
+ }
465
+ function isZero(value) {
466
+ return parseNumber(value) === 0;
467
+ }
468
+ function isOne(value) {
469
+ return parseNumber(value) === 1;
470
+ }
471
+ function all(args, expected, test) {
472
+ return args.length === expected && args.every(test);
473
+ }
474
+ function isIdentityFunction(name, args) {
475
+ switch (name) {
476
+ case "matrix": {
477
+ const numbers = args.map(parseNumber);
478
+ return numbers.length === 6 && numbers[0] === 1 && numbers[1] === 0 && numbers[2] === 0 && numbers[3] === 1 && numbers[4] === 0 && numbers[5] === 0;
479
+ }
480
+ case "matrix3d": {
481
+ const numbers = args.map(parseNumber);
482
+ const identity = [
483
+ 1,
484
+ 0,
485
+ 0,
486
+ 0,
487
+ 0,
488
+ 1,
489
+ 0,
490
+ 0,
491
+ 0,
492
+ 0,
493
+ 1,
494
+ 0,
495
+ 0,
496
+ 0,
497
+ 0,
498
+ 1
499
+ ];
500
+ return numbers.length === 16 && numbers.every((value, i) => value === identity[i]);
501
+ }
502
+ case "translate": return args.length >= 1 && args.length <= 2 && args.every(isZero);
503
+ case "translatex":
504
+ case "translatey":
505
+ case "translatez": return all(args, 1, isZero);
506
+ case "translate3d": return all(args, 3, isZero);
507
+ case "scale": return args.length >= 1 && args.length <= 2 && args.every(isOne);
508
+ case "scalex":
509
+ case "scaley":
510
+ case "scalez": return all(args, 1, isOne);
511
+ case "scale3d": return all(args, 3, isOne);
512
+ case "rotate":
513
+ case "rotatex":
514
+ case "rotatey":
515
+ case "rotatez":
516
+ case "skewx":
517
+ case "skewy": return all(args, 1, isZero);
518
+ case "rotate3d": return args.length === 4 && isZero(args[3]);
519
+ case "skew": return args.length >= 1 && args.length <= 2 && args.every(isZero);
520
+ default: return false;
521
+ }
522
+ }
523
+ /**
524
+ * Whether a transform value is the identity transform (and so creates a
525
+ * containing block without changing what is rendered).
526
+ */
527
+ function isIdentityTransform(value) {
528
+ const transform = value.trim().toLowerCase();
529
+ if (transform === "" || transform === "none") return true;
530
+ let rest = transform;
531
+ while (rest.length > 0) {
532
+ const match = /^([a-z0-9]+)\(([^()]*)\)/.exec(rest);
533
+ if (!match) return false;
534
+ const args = match[2].split(",").map((arg) => arg.trim());
535
+ if (!isIdentityFunction(match[1], args)) return false;
536
+ rest = rest.slice(match[0].length).trim();
537
+ }
538
+ return true;
539
+ }
540
+ /**
541
+ * Replaces a final identity transform in the keyframes with `none`.
542
+ *
543
+ * A forwards-filled animation keeps its final keyframe applied after it
544
+ * finishes. When that keyframe's transform is the identity, the only visible
545
+ * difference from not animating the transform at all is the side effect: any
546
+ * non-`none` transform makes the element the containing block for fixed
547
+ * descendants (so fixed-position popovers/tooltips inside it are positioned
548
+ * against it rather than the viewport). Ending on `none` instead keeps the
549
+ * animation's other persisted properties while letting the transform effect
550
+ * go away.
551
+ */
552
+ function withoutFinalIdentityTransform(keyframes) {
553
+ if (keyframes === null) return null;
554
+ if (Array.isArray(keyframes)) {
555
+ let index = keyframes.length - 1;
556
+ while (index >= 0 && keyframes[index] == null) index--;
557
+ if (index < 0) return keyframes;
558
+ const last = keyframes[index];
559
+ if (typeof last.transform !== "string" || !isIdentityTransform(last.transform)) return keyframes;
560
+ const next = keyframes.slice();
561
+ next[index] = {
562
+ ...last,
563
+ transform: "none"
564
+ };
565
+ return next;
566
+ }
567
+ const transform = keyframes.transform;
568
+ if (Array.isArray(transform)) {
569
+ const last = transform[transform.length - 1];
570
+ if (typeof last !== "string" || !isIdentityTransform(last)) return keyframes;
571
+ const values = transform.slice();
572
+ values[values.length - 1] = "none";
573
+ return {
574
+ ...keyframes,
575
+ transform: values
576
+ };
577
+ }
578
+ if (typeof transform === "string" && isIdentityTransform(transform)) return {
579
+ ...keyframes,
580
+ transform: "none"
581
+ };
582
+ return keyframes;
583
+ }
584
+ /**
458
585
  * Adds an animation for an element
459
586
  * @param el The element to animate
460
587
  * @param enter Whether the animation is being run on enter
@@ -468,7 +595,9 @@ async function animate(el, enter, keyframes, options) {
468
595
  easing: "ease-in-out",
469
596
  fill: "forwards"
470
597
  }, options);
471
- const animation = el.animate(keyframes, animationOptions);
598
+ const persisted = animationOptions.fill === "forwards" || animationOptions.fill === "both";
599
+ const effectiveKeyframes = enter === true && persisted ? withoutFinalIdentityTransform(keyframes) : keyframes;
600
+ const animation = el.animate(effectiveKeyframes, animationOptions);
472
601
  setTimeout(() => {
473
602
  animation.timeline = el.ownerDocument.timeline;
474
603
  }, 1);
@@ -2274,13 +2403,17 @@ const SHALLOW_WATCH_OPTIONS = { shallow: true };
2274
2403
  * caller can slot it into the live list. The sibling chain is left for the
2275
2404
  * end-of-pass relink.
2276
2405
  */
2277
- function mountSpec(spec, region, before, create, noWatch) {
2406
+ function mountSpec(spec, region, before, anchor, create, noWatch) {
2278
2407
  const item = newListItem(spec.data, spec.key);
2279
2408
  const savedActiveRegion = context.activeRegion;
2280
2409
  item.depth = region.depth + 1;
2281
2410
  context.activeRegion = item;
2282
2411
  if (noWatch !== true) item.data = $watch(item.data, SHALLOW_WATCH_OPTIONS);
2412
+ const suspendHydration = context.hydrationNode !== null && context.hydrationNode === anchor;
2413
+ const savedHydrationNode = context.hydrationNode;
2414
+ if (suspendHydration) context.hydrationNode = null;
2283
2415
  create(item, before);
2416
+ if (suspendHydration) context.hydrationNode = savedHydrationNode;
2284
2417
  context.activeRegion = savedActiveRegion;
2285
2418
  return item;
2286
2419
  }
@@ -2349,7 +2482,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2349
2482
  } else break;
2350
2483
  if (oldStart > oldEnd && newStart <= newEnd) {
2351
2484
  const before = newEnd + 1 < newSpecs.length ? newItems[newEnd + 1].startNode : anchor;
2352
- for (let i = newStart; i <= newEnd; i++) newItems[i] = mountSpec(newSpecs[i], region, before, create, noWatch);
2485
+ for (let i = newStart; i <= newEnd; i++) newItems[i] = mountSpec(newSpecs[i], region, before, anchor, create, noWatch);
2353
2486
  chainNeedsRelink = true;
2354
2487
  } else if (newStart > newEnd) for (let i = oldStart; i <= oldEnd; i++) clearRegion(oldItems[i]);
2355
2488
  else {
@@ -2364,7 +2497,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2364
2497
  let before = oldItems[oldEnd]?.endNode?.nextSibling ?? anchor;
2365
2498
  for (let i = oldEnd; i >= oldStart; i--) clearRegion(oldItems[i]);
2366
2499
  for (let i = newStart; i <= newEnd; i++) {
2367
- newItems[i] = mountSpec(newSpecs[i], region, before, create, noWatch);
2500
+ newItems[i] = mountSpec(newSpecs[i], region, before, anchor, create, noWatch);
2368
2501
  before = newItems[i].endNode.nextSibling;
2369
2502
  }
2370
2503
  chainNeedsRelink = true;
@@ -2386,7 +2519,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2386
2519
  const newIdx = newStart + i;
2387
2520
  const before = newIdx + 1 < newSpecs.length ? newItems[newIdx + 1].startNode : anchor;
2388
2521
  if (newIndexToOld[i] === 0) {
2389
- newItems[newIdx] = mountSpec(newSpecs[newIdx], region, before, create, noWatch);
2522
+ newItems[newIdx] = mountSpec(newSpecs[newIdx], region, before, anchor, create, noWatch);
2390
2523
  chainNeedsRelink = true;
2391
2524
  } else if (j < 0 || i !== seq[j]) {
2392
2525
  moveRegion(parent, newItems[newIdx], before);
package/dist/index.mjs CHANGED
@@ -454,6 +454,133 @@ function runMountCallback(fn) {
454
454
  //#endregion
455
455
  //#region src/render/animate.ts
456
456
  /**
457
+ * Parses a transform function argument as a number, returning null for
458
+ * anything we can't confidently evaluate (calcs, variables, etc.).
459
+ */
460
+ function parseNumber(value) {
461
+ if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[a-z%]+)?$/i.test(value.trim())) return null;
462
+ return Number.parseFloat(value);
463
+ }
464
+ function isZero(value) {
465
+ return parseNumber(value) === 0;
466
+ }
467
+ function isOne(value) {
468
+ return parseNumber(value) === 1;
469
+ }
470
+ function all(args, expected, test) {
471
+ return args.length === expected && args.every(test);
472
+ }
473
+ function isIdentityFunction(name, args) {
474
+ switch (name) {
475
+ case "matrix": {
476
+ const numbers = args.map(parseNumber);
477
+ return numbers.length === 6 && numbers[0] === 1 && numbers[1] === 0 && numbers[2] === 0 && numbers[3] === 1 && numbers[4] === 0 && numbers[5] === 0;
478
+ }
479
+ case "matrix3d": {
480
+ const numbers = args.map(parseNumber);
481
+ const identity = [
482
+ 1,
483
+ 0,
484
+ 0,
485
+ 0,
486
+ 0,
487
+ 1,
488
+ 0,
489
+ 0,
490
+ 0,
491
+ 0,
492
+ 1,
493
+ 0,
494
+ 0,
495
+ 0,
496
+ 0,
497
+ 1
498
+ ];
499
+ return numbers.length === 16 && numbers.every((value, i) => value === identity[i]);
500
+ }
501
+ case "translate": return args.length >= 1 && args.length <= 2 && args.every(isZero);
502
+ case "translatex":
503
+ case "translatey":
504
+ case "translatez": return all(args, 1, isZero);
505
+ case "translate3d": return all(args, 3, isZero);
506
+ case "scale": return args.length >= 1 && args.length <= 2 && args.every(isOne);
507
+ case "scalex":
508
+ case "scaley":
509
+ case "scalez": return all(args, 1, isOne);
510
+ case "scale3d": return all(args, 3, isOne);
511
+ case "rotate":
512
+ case "rotatex":
513
+ case "rotatey":
514
+ case "rotatez":
515
+ case "skewx":
516
+ case "skewy": return all(args, 1, isZero);
517
+ case "rotate3d": return args.length === 4 && isZero(args[3]);
518
+ case "skew": return args.length >= 1 && args.length <= 2 && args.every(isZero);
519
+ default: return false;
520
+ }
521
+ }
522
+ /**
523
+ * Whether a transform value is the identity transform (and so creates a
524
+ * containing block without changing what is rendered).
525
+ */
526
+ function isIdentityTransform(value) {
527
+ const transform = value.trim().toLowerCase();
528
+ if (transform === "" || transform === "none") return true;
529
+ let rest = transform;
530
+ while (rest.length > 0) {
531
+ const match = /^([a-z0-9]+)\(([^()]*)\)/.exec(rest);
532
+ if (!match) return false;
533
+ const args = match[2].split(",").map((arg) => arg.trim());
534
+ if (!isIdentityFunction(match[1], args)) return false;
535
+ rest = rest.slice(match[0].length).trim();
536
+ }
537
+ return true;
538
+ }
539
+ /**
540
+ * Replaces a final identity transform in the keyframes with `none`.
541
+ *
542
+ * A forwards-filled animation keeps its final keyframe applied after it
543
+ * finishes. When that keyframe's transform is the identity, the only visible
544
+ * difference from not animating the transform at all is the side effect: any
545
+ * non-`none` transform makes the element the containing block for fixed
546
+ * descendants (so fixed-position popovers/tooltips inside it are positioned
547
+ * against it rather than the viewport). Ending on `none` instead keeps the
548
+ * animation's other persisted properties while letting the transform effect
549
+ * go away.
550
+ */
551
+ function withoutFinalIdentityTransform(keyframes) {
552
+ if (keyframes === null) return null;
553
+ if (Array.isArray(keyframes)) {
554
+ let index = keyframes.length - 1;
555
+ while (index >= 0 && keyframes[index] == null) index--;
556
+ if (index < 0) return keyframes;
557
+ const last = keyframes[index];
558
+ if (typeof last.transform !== "string" || !isIdentityTransform(last.transform)) return keyframes;
559
+ const next = keyframes.slice();
560
+ next[index] = {
561
+ ...last,
562
+ transform: "none"
563
+ };
564
+ return next;
565
+ }
566
+ const transform = keyframes.transform;
567
+ if (Array.isArray(transform)) {
568
+ const last = transform[transform.length - 1];
569
+ if (typeof last !== "string" || !isIdentityTransform(last)) return keyframes;
570
+ const values = transform.slice();
571
+ values[values.length - 1] = "none";
572
+ return {
573
+ ...keyframes,
574
+ transform: values
575
+ };
576
+ }
577
+ if (typeof transform === "string" && isIdentityTransform(transform)) return {
578
+ ...keyframes,
579
+ transform: "none"
580
+ };
581
+ return keyframes;
582
+ }
583
+ /**
457
584
  * Adds an animation for an element
458
585
  * @param el The element to animate
459
586
  * @param enter Whether the animation is being run on enter
@@ -467,7 +594,9 @@ async function animate(el, enter, keyframes, options) {
467
594
  easing: "ease-in-out",
468
595
  fill: "forwards"
469
596
  }, options);
470
- const animation = el.animate(keyframes, animationOptions);
597
+ const persisted = animationOptions.fill === "forwards" || animationOptions.fill === "both";
598
+ const effectiveKeyframes = enter === true && persisted ? withoutFinalIdentityTransform(keyframes) : keyframes;
599
+ const animation = el.animate(effectiveKeyframes, animationOptions);
471
600
  setTimeout(() => {
472
601
  animation.timeline = el.ownerDocument.timeline;
473
602
  }, 1);
@@ -2273,13 +2402,17 @@ const SHALLOW_WATCH_OPTIONS = { shallow: true };
2273
2402
  * caller can slot it into the live list. The sibling chain is left for the
2274
2403
  * end-of-pass relink.
2275
2404
  */
2276
- function mountSpec(spec, region, before, create, noWatch) {
2405
+ function mountSpec(spec, region, before, anchor, create, noWatch) {
2277
2406
  const item = newListItem(spec.data, spec.key);
2278
2407
  const savedActiveRegion = context.activeRegion;
2279
2408
  item.depth = region.depth + 1;
2280
2409
  context.activeRegion = item;
2281
2410
  if (noWatch !== true) item.data = $watch(item.data, SHALLOW_WATCH_OPTIONS);
2411
+ const suspendHydration = context.hydrationNode !== null && context.hydrationNode === anchor;
2412
+ const savedHydrationNode = context.hydrationNode;
2413
+ if (suspendHydration) context.hydrationNode = null;
2282
2414
  create(item, before);
2415
+ if (suspendHydration) context.hydrationNode = savedHydrationNode;
2283
2416
  context.activeRegion = savedActiveRegion;
2284
2417
  return item;
2285
2418
  }
@@ -2348,7 +2481,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2348
2481
  } else break;
2349
2482
  if (oldStart > oldEnd && newStart <= newEnd) {
2350
2483
  const before = newEnd + 1 < newSpecs.length ? newItems[newEnd + 1].startNode : anchor;
2351
- for (let i = newStart; i <= newEnd; i++) newItems[i] = mountSpec(newSpecs[i], region, before, create, noWatch);
2484
+ for (let i = newStart; i <= newEnd; i++) newItems[i] = mountSpec(newSpecs[i], region, before, anchor, create, noWatch);
2352
2485
  chainNeedsRelink = true;
2353
2486
  } else if (newStart > newEnd) for (let i = oldStart; i <= oldEnd; i++) clearRegion(oldItems[i]);
2354
2487
  else {
@@ -2363,7 +2496,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2363
2496
  let before = oldItems[oldEnd]?.endNode?.nextSibling ?? anchor;
2364
2497
  for (let i = oldEnd; i >= oldStart; i--) clearRegion(oldItems[i]);
2365
2498
  for (let i = newStart; i <= newEnd; i++) {
2366
- newItems[i] = mountSpec(newSpecs[i], region, before, create, noWatch);
2499
+ newItems[i] = mountSpec(newSpecs[i], region, before, anchor, create, noWatch);
2367
2500
  before = newItems[i].endNode.nextSibling;
2368
2501
  }
2369
2502
  chainNeedsRelink = true;
@@ -2385,7 +2518,7 @@ function runListItems(region, parent, anchor, oldItems, newSpecs, create, update
2385
2518
  const newIdx = newStart + i;
2386
2519
  const before = newIdx + 1 < newSpecs.length ? newItems[newIdx + 1].startNode : anchor;
2387
2520
  if (newIndexToOld[i] === 0) {
2388
- newItems[newIdx] = mountSpec(newSpecs[newIdx], region, before, create, noWatch);
2521
+ newItems[newIdx] = mountSpec(newSpecs[newIdx], region, before, anchor, create, noWatch);
2389
2522
  chainNeedsRelink = true;
2390
2523
  } else if (j < 0 || i !== seq[j]) {
2391
2524
  moveRegion(parent, newItems[newIdx], before);