@hilux/ngx-motion 2.6.0 → 2.7.0
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/README.md +23 -0
- package/fesm2022/hilux-ngx-motion.mjs +122 -14
- package/fesm2022/hilux-ngx-motion.mjs.map +1 -1
- package/package.json +1 -1
- package/types/hilux-ngx-motion.d.ts +36 -3
package/README.md
CHANGED
|
@@ -156,6 +156,23 @@ export class VariantsExample {
|
|
|
156
156
|
}
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
+
### Gesture Variants
|
|
160
|
+
Give `whileHover`, `whileTap` or `whileFocus` a variant name instead of styles, and it passes down: while the parent is hovered, every motion child with a variant of that name animates to it (as in Framer Motion). One hover, many parts:
|
|
161
|
+
|
|
162
|
+
```html
|
|
163
|
+
<a motionone whileHover="hover" whileTap="press"
|
|
164
|
+
[variants]="{ hover: { y: -4, transition: { staggerChildren: 0.04 } }, press: { scale: 0.97 } }">
|
|
165
|
+
<div motionone initial="rest" [variants]="{ rest: { rotate: 0 }, hover: { rotate: -15, scale: 1.1 } }">★</div>
|
|
166
|
+
<span motionone initial="rest" [variants]="{ rest: { scaleX: 0 }, hover: { scaleX: 1 } }"></span>
|
|
167
|
+
<span motionone initial="rest" [variants]="{ rest: { x: 0 }, hover: { x: 6 } }">→</span>
|
|
168
|
+
</a>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
- Children without that variant are left alone; grandchildren get it through their own parent.
|
|
172
|
+
- The parent's variant transition can stagger them: `staggerChildren`, `delayChildren`, `staggerDirection`.
|
|
173
|
+
- When the gesture ends, everything returns to its `animate` state, else its `initial` one. Values only a gesture sets return to neutral (`x: 0`, `scale: 1`…) or to what they were before.
|
|
174
|
+
- Gestures layer: tap over focus over hover. Release a press while still hovering and it goes back to the hover state.
|
|
175
|
+
|
|
159
176
|
### Conditional Animation with motionIf
|
|
160
177
|
```html
|
|
161
178
|
<button (click)="isVisible = !isVisible">Toggle</button>
|
|
@@ -581,6 +598,7 @@ The directives work seamlessly with Angular's new control flow syntax:
|
|
|
581
598
|
| `<motion.div>` | `<div motionone>` |
|
|
582
599
|
| `initial` / `animate` / `exit` props | `[initial]` / `[animate]` / `[exit]` inputs |
|
|
583
600
|
| `whileHover` / `whileTap` / `whileFocus` | `[whileHover]` / `[whileTap]` / `[whileFocus]` |
|
|
601
|
+
| `whileHover="hover"` passed down to children's `variants` | `whileHover="hover"` with `[variants]` on the children |
|
|
584
602
|
| `whileInView`, `viewport={{ once: true }}` | `[whileInView]`, `runInView="once"` |
|
|
585
603
|
| `variants` + `staggerChildren` | `[variants]` + `[transition]="{ staggerChildren }"` |
|
|
586
604
|
| `transition={{ type: 'spring' }}` | `[transition]="{ type: 'spring' }"` |
|
|
@@ -627,6 +645,11 @@ A lot of animation is plain CSS now, and where CSS is enough it is the better ch
|
|
|
627
645
|
|
|
628
646
|
## Changelog
|
|
629
647
|
|
|
648
|
+
**2.7.0**
|
|
649
|
+
- Gesture variants: `whileHover`, `whileTap` and `whileFocus` take a variant name that passes down to children with that variant, with `staggerChildren` / `delayChildren` from the parent's variant. See [Gesture Variants](#gesture-variants).
|
|
650
|
+
- Gestures layer like Framer Motion's: tap over focus over hover. Releasing a press while hovering goes back to the hover state, not to rest.
|
|
651
|
+
- Fixed: an element with only `whileHover` (or tap or focus) and no `initial` or `animate` stayed in its gesture state after the gesture ended. Its values now return to neutral (`x: 0`, `scale: 1`…) or to what they were before.
|
|
652
|
+
|
|
630
653
|
**2.6.0**
|
|
631
654
|
- `injectAnimate()`: animate elements, number signals and sequences from TypeScript, with Motion's `stagger()`. Scoped to the component and stopped when it's destroyed. See [Animate from Code](#animate-from-code).
|
|
632
655
|
- Reduced motion now applies to `motionone` too (before, only layout, pointer, scroll and spring signals respected it): movement jumps to its end and fades still play. New `provideMotionConfig({ reducedMotion: 'user' | 'always' | 'never' })` to change it. See [Reduced Motion](#reduced-motion).
|
|
@@ -1078,6 +1078,21 @@ function nearestMotionAncestor(el, stop) {
|
|
|
1078
1078
|
if (found) return found;
|
|
1079
1079
|
}
|
|
1080
1080
|
}
|
|
1081
|
+
const NEUTRAL_TRANSFORMS = {
|
|
1082
|
+
x: 0,
|
|
1083
|
+
y: 0,
|
|
1084
|
+
z: 0,
|
|
1085
|
+
scale: 1,
|
|
1086
|
+
scaleX: 1,
|
|
1087
|
+
scaleY: 1,
|
|
1088
|
+
rotate: 0,
|
|
1089
|
+
rotateX: 0,
|
|
1090
|
+
rotateY: 0,
|
|
1091
|
+
rotateZ: 0,
|
|
1092
|
+
skew: 0,
|
|
1093
|
+
skewX: 0,
|
|
1094
|
+
skewY: 0
|
|
1095
|
+
};
|
|
1081
1096
|
var MotionOneDirective = class MotionOneDirective {
|
|
1082
1097
|
el;
|
|
1083
1098
|
motionService;
|
|
@@ -1462,8 +1477,104 @@ var MotionOneDirective = class MotionOneDirective {
|
|
|
1462
1477
|
}
|
|
1463
1478
|
}
|
|
1464
1479
|
hasKeys(value) {
|
|
1480
|
+
if (typeof value === "string") return value.length > 0;
|
|
1465
1481
|
return !!value && typeof value === "object" && Object.keys(value).length > 0;
|
|
1466
1482
|
}
|
|
1483
|
+
restingState() {
|
|
1484
|
+
return this.hasKeys(this.animate) ? this.animate : this.initial;
|
|
1485
|
+
}
|
|
1486
|
+
gestureBase = {};
|
|
1487
|
+
captureGestureBase(gesture) {
|
|
1488
|
+
if (!isPlatformBrowser(this.platformId)) return;
|
|
1489
|
+
const rest = resolveVariant(this.restingState(), this.variants);
|
|
1490
|
+
const style = getComputedStyle(this.el.nativeElement);
|
|
1491
|
+
for (const key of Object.keys(extractStyleProperties(resolveVariant(gesture, this.variants)))) {
|
|
1492
|
+
if (key in rest || key in this.gestureBase || key in NEUTRAL_TRANSFORMS) continue;
|
|
1493
|
+
this.gestureBase[key] = style.getPropertyValue(key.replace(/[A-Z]/g, (c) => "-" + c.toLowerCase())) || style[key];
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
restTarget(gesture) {
|
|
1497
|
+
const rest = this.restingState();
|
|
1498
|
+
const resolved = resolveVariant(rest, this.variants);
|
|
1499
|
+
const missing = Object.keys(extractStyleProperties(resolveVariant(gesture, this.variants))).filter((k) => !(k in resolved));
|
|
1500
|
+
if (!missing.length) return rest;
|
|
1501
|
+
const out = { ...resolved };
|
|
1502
|
+
for (const key of missing) {
|
|
1503
|
+
const value = key in NEUTRAL_TRANSFORMS ? NEUTRAL_TRANSFORMS[key] : this.gestureBase[key];
|
|
1504
|
+
if (value !== void 0 && value !== "") out[key] = value;
|
|
1505
|
+
}
|
|
1506
|
+
return out;
|
|
1507
|
+
}
|
|
1508
|
+
activeGestures = /* @__PURE__ */ new Set();
|
|
1509
|
+
topGesture() {
|
|
1510
|
+
const top = [
|
|
1511
|
+
["tap", this.whileTap],
|
|
1512
|
+
["focus", this.whileFocus],
|
|
1513
|
+
["hover", this.whileHover]
|
|
1514
|
+
].find(([type, value]) => this.activeGestures.has(type) && this.hasKeys(value));
|
|
1515
|
+
return top && {
|
|
1516
|
+
type: top[0],
|
|
1517
|
+
value: top[1]
|
|
1518
|
+
};
|
|
1519
|
+
}
|
|
1520
|
+
asVariant(v) {
|
|
1521
|
+
return resolveVariant(v, this.variants);
|
|
1522
|
+
}
|
|
1523
|
+
beginGesture(type, value) {
|
|
1524
|
+
this.activeGestures.add(type);
|
|
1525
|
+
if (this.topGesture()?.type !== type) return;
|
|
1526
|
+
this.captureGestureBase(value);
|
|
1527
|
+
this.playAnimationToTarget(value, type);
|
|
1528
|
+
this.propagateGesture(value, true);
|
|
1529
|
+
}
|
|
1530
|
+
endGesture(type, value) {
|
|
1531
|
+
const wasTop = this.topGesture()?.type === type;
|
|
1532
|
+
this.activeGestures.delete(type);
|
|
1533
|
+
if (!wasTop) return;
|
|
1534
|
+
const next = this.topGesture();
|
|
1535
|
+
if (!next) {
|
|
1536
|
+
this.playAnimationToTarget(this.restTarget(value));
|
|
1537
|
+
this.propagateGesture(value, false);
|
|
1538
|
+
return;
|
|
1539
|
+
}
|
|
1540
|
+
this.playAnimationToTarget({
|
|
1541
|
+
...this.asVariant(this.restTarget(value)),
|
|
1542
|
+
...this.asVariant(next.value)
|
|
1543
|
+
}, next.type);
|
|
1544
|
+
if (typeof next.value === "string") this.propagateGesture(next.value, true, value);
|
|
1545
|
+
else this.propagateGesture(value, false);
|
|
1546
|
+
}
|
|
1547
|
+
gestureTimer;
|
|
1548
|
+
propagateGesture(label, active, ended) {
|
|
1549
|
+
if (typeof label !== "string" || !label) return;
|
|
1550
|
+
const transition = resolveVariant(label, this.variants)?.transition ?? {};
|
|
1551
|
+
const each = active && !ended ? transition["staggerChildren"] ?? 0 : 0;
|
|
1552
|
+
const first = active && !ended ? transition["delayChildren"] ?? 0 : 0;
|
|
1553
|
+
const children = this.motionChildren;
|
|
1554
|
+
const reversed = (transition["staggerDirection"] ?? 1) < 0;
|
|
1555
|
+
children.forEach((child, i) => {
|
|
1556
|
+
const slot = reversed ? children.length - 1 - i : i;
|
|
1557
|
+
const run = () => {
|
|
1558
|
+
child.gestureTimer = void 0;
|
|
1559
|
+
const endedHere = typeof ended === "string" && !!child.variants?.[ended];
|
|
1560
|
+
if (child.variants?.[label]) {
|
|
1561
|
+
if (active) child.captureGestureBase(label);
|
|
1562
|
+
if (!active) child.playAnimationToTarget(child.restTarget(label));
|
|
1563
|
+
else if (endedHere) child.playAnimationToTarget({
|
|
1564
|
+
...child.asVariant(child.restTarget(ended)),
|
|
1565
|
+
...child.getVariant(label)
|
|
1566
|
+
});
|
|
1567
|
+
else child.playAnimationToTarget(label);
|
|
1568
|
+
} else if (active && endedHere) child.playAnimationToTarget(child.restTarget(ended));
|
|
1569
|
+
child.propagateGesture(label, active, ended);
|
|
1570
|
+
};
|
|
1571
|
+
child.cancelScheduled(child.gestureTimer);
|
|
1572
|
+
child.gestureTimer = void 0;
|
|
1573
|
+
const delay = first + slot * each;
|
|
1574
|
+
if (delay > 0) child.gestureTimer = child.schedule(run, delay * 1e3);
|
|
1575
|
+
else run();
|
|
1576
|
+
});
|
|
1577
|
+
}
|
|
1467
1578
|
cancelForReset() {
|
|
1468
1579
|
const cancel = (c) => {
|
|
1469
1580
|
if (!c) return;
|
|
@@ -1498,36 +1609,34 @@ var MotionOneDirective = class MotionOneDirective {
|
|
|
1498
1609
|
elements.forEach((el) => el.style.transition = "");
|
|
1499
1610
|
}
|
|
1500
1611
|
onMouseEnter() {
|
|
1501
|
-
if (
|
|
1612
|
+
if (this.hasKeys(this.whileHover)) {
|
|
1502
1613
|
clearTimeout(this.hoverTimeout);
|
|
1503
1614
|
if (!this.gestureService.shouldAllowGesture(this.elementId, "hover")) return;
|
|
1504
1615
|
this.gestureService.cancelGesture(this.elementId);
|
|
1505
|
-
this.
|
|
1616
|
+
this.beginGesture("hover", this.whileHover);
|
|
1506
1617
|
}
|
|
1507
1618
|
}
|
|
1508
1619
|
onMouseLeave() {
|
|
1509
|
-
if (
|
|
1620
|
+
if (this.hasKeys(this.whileHover)) {
|
|
1510
1621
|
clearTimeout(this.hoverTimeout);
|
|
1511
1622
|
this.gestureService.cancelGesture(this.elementId);
|
|
1512
1623
|
this.hoverTimeout = this.ngZone.runOutsideAngular(() => setTimeout(() => {
|
|
1513
|
-
|
|
1514
|
-
else this.playAnimationToTarget(this.initial);
|
|
1624
|
+
this.endGesture("hover", this.whileHover);
|
|
1515
1625
|
}, 100));
|
|
1516
1626
|
}
|
|
1517
1627
|
}
|
|
1518
1628
|
onPress(event) {
|
|
1519
|
-
if (
|
|
1629
|
+
if (this.hasKeys(this.whileTap)) {
|
|
1520
1630
|
if (!this.gestureService.shouldAllowGesture(this.elementId, "tap")) return;
|
|
1521
1631
|
this.gestureService.cancelGesture(this.elementId);
|
|
1522
|
-
this.
|
|
1632
|
+
this.beginGesture("tap", this.whileTap);
|
|
1523
1633
|
if (isPlatformBrowser(this.platformId)) {
|
|
1524
1634
|
const endEvent = event.type === "mousedown" ? "mouseup" : "touchend";
|
|
1525
1635
|
this.pressEndCleanup?.();
|
|
1526
1636
|
const endListener = () => {
|
|
1527
1637
|
this.pressEndCleanup = null;
|
|
1528
1638
|
this.gestureService.cancelGesture(this.elementId);
|
|
1529
|
-
|
|
1530
|
-
else this.playAnimationToTarget(this.initial);
|
|
1639
|
+
this.endGesture("tap", this.whileTap);
|
|
1531
1640
|
window.removeEventListener(endEvent, endListener);
|
|
1532
1641
|
};
|
|
1533
1642
|
this.ngZone.runOutsideAngular(() => window.addEventListener(endEvent, endListener));
|
|
@@ -1536,17 +1645,16 @@ var MotionOneDirective = class MotionOneDirective {
|
|
|
1536
1645
|
}
|
|
1537
1646
|
}
|
|
1538
1647
|
onFocus() {
|
|
1539
|
-
if (
|
|
1648
|
+
if (this.hasKeys(this.whileFocus)) {
|
|
1540
1649
|
if (!this.gestureService.shouldAllowGesture(this.elementId, "focus")) return;
|
|
1541
1650
|
this.gestureService.cancelGesture(this.elementId);
|
|
1542
|
-
this.
|
|
1651
|
+
this.beginGesture("focus", this.whileFocus);
|
|
1543
1652
|
}
|
|
1544
1653
|
}
|
|
1545
1654
|
onBlur() {
|
|
1546
|
-
if (
|
|
1655
|
+
if (this.hasKeys(this.whileFocus)) {
|
|
1547
1656
|
this.gestureService.cancelGesture(this.elementId);
|
|
1548
|
-
|
|
1549
|
-
else this.playAnimationToTarget(this.initial);
|
|
1657
|
+
this.endGesture("focus", this.whileFocus);
|
|
1550
1658
|
}
|
|
1551
1659
|
}
|
|
1552
1660
|
applyInitialValues() {
|