@torpor/view 1.2.4 → 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);
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);