@hilux/ngx-motion 2.2.0 → 2.3.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 +49 -2
- package/fesm2022/hilux-ngx-motion.mjs +79 -11
- package/fesm2022/hilux-ngx-motion.mjs.map +1 -1
- package/package.json +1 -1
- package/types/hilux-ngx-motion.d.ts +47 -13
package/README.md
CHANGED
|
@@ -353,9 +353,21 @@ Like Framer Motion's `layout` and `layoutId`. Change the layout however you like
|
|
|
353
353
|
```
|
|
354
354
|
|
|
355
355
|
- `motionLayout`: what animates. Empty (the default) animates position and size, `"position"` only moves (size changes jump, so text never stretches), and `"size"` only resizes.
|
|
356
|
-
- `motionLayoutId`: a shared id. When an element with the same id appears (in the same render or soon after the old one is removed), it starts from the old one's position and size. Works on its own or with `motionLayout
|
|
356
|
+
- `motionLayoutId`: a shared id. When an element with the same id appears (in the same render or soon after the old one is removed), it starts from the old one's position and size. Works on its own or with `motionLayout`, and across scroll containers.
|
|
357
|
+
- Shared elements that stay mounted, like a thumbnail and its lightbox: while both are mounted the newer one is shown and the older one hidden (`visibility: hidden`). Remove the newer one and the older one reappears and flies back from where it was:
|
|
358
|
+
|
|
359
|
+
```html
|
|
360
|
+
@for (p of photos; track p.id) {
|
|
361
|
+
<button class="thumb" [motionLayoutId]="p.id" (click)="open.set(p)"><img [src]="p.src" /></button>
|
|
362
|
+
}
|
|
363
|
+
@if (open(); as p) {
|
|
364
|
+
<div class="lightbox" [motionLayoutId]="p.id" (click)="open.set(null)"><img [src]="p.src" /></div>
|
|
365
|
+
}
|
|
366
|
+
```
|
|
357
367
|
- `layoutTransition`: the transition (a spring by default). `layoutAnimationStart` and `layoutAnimationComplete` are emitted, and `animating()` is a signal, via `exportAs: 'motionLayout'`.
|
|
358
|
-
- Size is animated with scale. Mark content inside with `motionLayoutCorrect` and it is counter-scaled, so text keeps its size while the box grows or shrinks: `<li motionLayout><span motionLayoutCorrect>{{ item }}</span></li>`. Or use `"position"` to animate position only.
|
|
368
|
+
- Size is animated with scale. Corner radii (in px) are corrected so they stay round. Mark content inside with `motionLayoutCorrect` and it is counter-scaled, so text keeps its size while the box grows or shrinks: `<li motionLayout><span motionLayoutCorrect>{{ item }}</span></li>`. Or use `"position"` to animate position only.
|
|
369
|
+
- Every layout element is measured after every render, so elements whose own component wasn't re-rendered (OnPush siblings) still animate when something else pushes them. There is no `LayoutGroup` to add.
|
|
370
|
+
- While it moves the element has the class `motion-layout-animating`, for example to raise it above its neighbours with `z-index`.
|
|
359
371
|
- Layout animations use the element's `transform`, so don't give it a CSS transform of its own; wrap it instead.
|
|
360
372
|
- Interrupted animations continue from where the element is drawn. Resizing the window re-measures without animating, and with reduced motion nothing animates.
|
|
361
373
|
|
|
@@ -453,9 +465,41 @@ The directives work seamlessly with Angular's new control flow syntax:
|
|
|
453
465
|
| `<Reorder.Group values onReorder>` / `<Reorder.Item value>` | `[(motionReorderGroup)]` / `[motionReorderItem]` |
|
|
454
466
|
| `layout`, `layout="position"` | `motionLayout`, `motionLayout="position"` |
|
|
455
467
|
| `layoutId` | `motionLayoutId` |
|
|
468
|
+
| `<LayoutGroup>` | not needed: every layout element is measured after every render |
|
|
469
|
+
|
|
470
|
+
## CSS or ngx-motion?
|
|
471
|
+
A lot of animation is plain CSS now, and where CSS is enough it is the better choice: no JavaScript, and it runs off the main thread. ngx-motion is for what CSS can't do: physics, the pointer's position and speed, smooth interruption, removing elements after they animate out, and layout animations on a page that stays interactive. Browser support below is as of mid-2026.
|
|
472
|
+
|
|
473
|
+
| Feature | Plain CSS | What ngx-motion adds |
|
|
474
|
+
|---|---|---|
|
|
475
|
+
| Transitions and keyframes | ✅ `transition`, `@keyframes`, separate `translate` / `scale` / `rotate` | Plain objects in the template, values that can change at runtime (signals) |
|
|
476
|
+
| Easing and springs | ⚠️ `linear()` can draw a spring-shaped curve, but it has a fixed duration and no physics | Real springs: an interrupted spring carries its speed into the next one |
|
|
477
|
+
| Interrupting an animation | ⚠️ Transitions re-aim from the current value but lose their speed; keyframes restart | Continues from where the element is and how fast it's moving |
|
|
478
|
+
| Hover, tap, focus | ✅ `:hover`, `:active`, `:focus-visible` | Physics spring-back; `whileTap` on touch screens; composes with drag and pointer effects |
|
|
479
|
+
| Enter animations | ✅ `@starting-style`; Angular's `animate.enter` | Declarative `initial` → `animate` states |
|
|
480
|
+
| Exit animations | ⚠️ Only for elements that stay in the DOM (`allow-discrete`, popovers, dialogs); Angular's `animate.leave` waits for a CSS animation | `*motionIf` / `*motionPresence` with `exit` states; shown again mid-exit, it animates back in |
|
|
481
|
+
| Stagger | ⚠️ A delay per item via custom properties; `sibling-index()` only in Chrome | `staggerChildren`, `delayChildren`, direction, nested parents |
|
|
482
|
+
| Sequences | ⚠️ Percentages in one `@keyframes`; several elements need hand-tuned delays | Timelines, `repeat` with `mirror` / `reverse`, per-property transitions |
|
|
483
|
+
| Play once when scrolled into view | ❌ No trigger yet (`animation-trigger` is experimental); needs IntersectionObserver | `runInView`, `whileInView` |
|
|
484
|
+
| Scroll-linked values | ✅ / ⚠️ `animation-timeline: scroll()` / `view()` in Chrome and Safari 26, not in Firefox by default | Same API everywhere (uses the native timeline where available), plus `progress()` and `velocity()` signals |
|
|
485
|
+
| Magnetic, tilt, follow-pointer | ❌ CSS can't read the pointer position | `motionMagnetic`, `motionTilt` (sets `--motion-pointer-x/y` for CSS), `motionFollowPointer` |
|
|
486
|
+
| Drag | ❌ | Constraints, elastic edges, momentum, snap points |
|
|
487
|
+
| Pan and swipe | ❌ Only `touch-action` and scroll snap | Offset, delta, velocity and swipe direction |
|
|
488
|
+
| Drag to reorder | ❌ | Two-way bound order, keyboard moves, neighbours glide aside |
|
|
489
|
+
| Layout changes | ⚠️ View Transitions animate layout changes, but snapshot the whole page as an image, run one at a time and restart if interrupted; `interpolate-size` animates `height: auto` (Chrome only) | `motionLayout`: the real elements animate and stay clickable, several at once, interruptible, on springs; text can keep its size and corners stay round |
|
|
490
|
+
| Shared elements | ✅ / ⚠️ `view-transition-name` covers a thumbnail growing into a detail view | `motionLayoutId`: across scroll containers, while the page stays interactive, no `startViewTransition` call |
|
|
491
|
+
| Reduced motion | ✅ `@media (prefers-reduced-motion)` | Respected by every directive automatically |
|
|
492
|
+
| Change detection and SSR | No JavaScript | Runs outside Angular's change detection; does nothing on the server |
|
|
493
|
+
|
|
494
|
+
**Use CSS for** hover and focus styling, simple enter animations, looping decorations and fixed-length transitions. **Use ngx-motion for** gestures and pointer effects, interruptible springs, exits from `@if` and `@for`, scroll triggers, and layout animations inside a page. For full-page route changes, View Transitions are often the better tool (the demo's Route Transitions examples use them).
|
|
456
495
|
|
|
457
496
|
## Changelog
|
|
458
497
|
|
|
498
|
+
**2.3.0**
|
|
499
|
+
- Shared layout elements that stay mounted: while two elements with the same `motionLayoutId` are mounted, the newer one is shown and the older hidden, and when the newer one is removed the older one flies back from it (thumbnail and lightbox).
|
|
500
|
+
- Layout animations keep corner radii round while scaling, and add the class `motion-layout-animating` while an element moves.
|
|
501
|
+
- Fixed: a window resize no longer leaves a shared element starting from its pre-resize position, and a late frame after a layout animation finished can no longer leave a small offset behind.
|
|
502
|
+
|
|
459
503
|
**2.2.0**
|
|
460
504
|
- New layout animations: `motionLayout` animates elements to their new position and size after any layout change (with `motionLayoutCorrect` to keep content from stretching), and `motionLayoutId` makes shared-element transitions. See [Layout Animations](#layout-animations).
|
|
461
505
|
|
|
@@ -574,6 +618,9 @@ It's the closest equivalent. Framer Motion (now Motion for React) is React-only.
|
|
|
574
618
|
### Which Angular versions are supported?
|
|
575
619
|
2.x supports Angular 22 and later. 1.x supports Angular 19.
|
|
576
620
|
|
|
621
|
+
### Do I need it, or is CSS enough?
|
|
622
|
+
Often CSS is enough. See [CSS or ngx-motion?](#css-or-ngx-motion) for a feature-by-feature table.
|
|
623
|
+
|
|
577
624
|
### How is it different from Angular's built-in animations?
|
|
578
625
|
Angular's `animate.enter` / `animate.leave` (and the older `@angular/animations`) are built around CSS classes and state-transition triggers. ngx-motion adds things those don't cover:
|
|
579
626
|
- physics springs
|
|
@@ -3798,8 +3798,15 @@ const LAYOUT_SPRING = {
|
|
|
3798
3798
|
damping: 34,
|
|
3799
3799
|
mass: 1
|
|
3800
3800
|
};
|
|
3801
|
+
const CORNERS = [
|
|
3802
|
+
"TopLeft",
|
|
3803
|
+
"TopRight",
|
|
3804
|
+
"BottomRight",
|
|
3805
|
+
"BottomLeft"
|
|
3806
|
+
];
|
|
3801
3807
|
var MotionLayoutRegistry = class MotionLayoutRegistry {
|
|
3802
3808
|
seen = /* @__PURE__ */ new Map();
|
|
3809
|
+
stacks = /* @__PURE__ */ new Map();
|
|
3803
3810
|
record(id, owner, box) {
|
|
3804
3811
|
this.seen.set(id, {
|
|
3805
3812
|
owner,
|
|
@@ -3810,10 +3817,22 @@ var MotionLayoutRegistry = class MotionLayoutRegistry {
|
|
|
3810
3817
|
const s = this.seen.get(id);
|
|
3811
3818
|
return s && s.owner !== owner ? s.box : void 0;
|
|
3812
3819
|
}
|
|
3813
|
-
|
|
3814
|
-
const
|
|
3815
|
-
|
|
3816
|
-
|
|
3820
|
+
join(id, m) {
|
|
3821
|
+
const stack = this.stacks.get(id) ?? [];
|
|
3822
|
+
stack.at(-1)?.setHidden(true);
|
|
3823
|
+
stack.push(m);
|
|
3824
|
+
this.stacks.set(id, stack);
|
|
3825
|
+
}
|
|
3826
|
+
leave(id, m) {
|
|
3827
|
+
const stack = this.stacks.get(id) ?? [];
|
|
3828
|
+
const wasLead = stack.at(-1) === m;
|
|
3829
|
+
const i = stack.indexOf(m);
|
|
3830
|
+
if (i >= 0) stack.splice(i, 1);
|
|
3831
|
+
if (!stack.length) this.stacks.delete(id);
|
|
3832
|
+
const last = this.seen.get(id);
|
|
3833
|
+
if (wasLead && stack.length) stack.at(-1).resume(last?.owner === m ? last.box : void 0);
|
|
3834
|
+
else if (last?.owner === m) setTimeout(() => {
|
|
3835
|
+
if (this.seen.get(id) === last) this.seen.delete(id);
|
|
3817
3836
|
}, 1e3);
|
|
3818
3837
|
}
|
|
3819
3838
|
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
@@ -3860,7 +3879,12 @@ var MotionLayoutDirective = class MotionLayoutDirective {
|
|
|
3860
3879
|
};
|
|
3861
3880
|
cur = NONE;
|
|
3862
3881
|
controls;
|
|
3882
|
+
run;
|
|
3863
3883
|
resnap = false;
|
|
3884
|
+
joined = null;
|
|
3885
|
+
resumeFrom;
|
|
3886
|
+
radii = null;
|
|
3887
|
+
inlineRadius = "";
|
|
3864
3888
|
reduced = typeof window !== "undefined" && !!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
3865
3889
|
constructor() {
|
|
3866
3890
|
const destroyRef = inject(DestroyRef);
|
|
@@ -3874,8 +3898,14 @@ var MotionLayoutDirective = class MotionLayoutDirective {
|
|
|
3874
3898
|
this.resnap = true;
|
|
3875
3899
|
this.controls?.stop();
|
|
3876
3900
|
this.controls = void 0;
|
|
3901
|
+
if (this.run) {
|
|
3902
|
+
this.run = void 0;
|
|
3903
|
+
this._animating.set(false);
|
|
3904
|
+
}
|
|
3877
3905
|
if (this.cur !== NONE) this.apply(NONE);
|
|
3878
3906
|
this.prev = this.measure() ?? this.prev;
|
|
3907
|
+
const id = this.motionLayoutId();
|
|
3908
|
+
if (id && this.prev) this.registry.record(id, this, this.shifted(this.visual(this.prev)));
|
|
3879
3909
|
cancelAnimationFrame(raf);
|
|
3880
3910
|
raf = requestAnimationFrame(() => this.resnap = false);
|
|
3881
3911
|
};
|
|
@@ -3884,10 +3914,16 @@ var MotionLayoutDirective = class MotionLayoutDirective {
|
|
|
3884
3914
|
window.removeEventListener("resize", resize);
|
|
3885
3915
|
cancelAnimationFrame(raf);
|
|
3886
3916
|
this.controls?.stop();
|
|
3887
|
-
|
|
3888
|
-
if (id) this.registry.release(id, this);
|
|
3917
|
+
if (this.joined) this.registry.leave(this.joined, this);
|
|
3889
3918
|
});
|
|
3890
3919
|
}
|
|
3920
|
+
setHidden(hidden) {
|
|
3921
|
+
this.el.style.visibility = hidden ? "hidden" : "";
|
|
3922
|
+
}
|
|
3923
|
+
resume(from) {
|
|
3924
|
+
this.setHidden(false);
|
|
3925
|
+
this.resumeFrom = from;
|
|
3926
|
+
}
|
|
3891
3927
|
mode() {
|
|
3892
3928
|
const m = this.motionLayout();
|
|
3893
3929
|
return m === "position" || m === "size" ? m : "both";
|
|
@@ -3932,10 +3968,18 @@ var MotionLayoutDirective = class MotionLayoutDirective {
|
|
|
3932
3968
|
};
|
|
3933
3969
|
}
|
|
3934
3970
|
update(box) {
|
|
3971
|
+
const id = this.motionLayoutId() || null;
|
|
3972
|
+
if (id !== this.joined) {
|
|
3973
|
+
if (this.joined) this.registry.leave(this.joined, this);
|
|
3974
|
+
if (id) this.registry.join(id, this);
|
|
3975
|
+
this.joined = id;
|
|
3976
|
+
}
|
|
3935
3977
|
if (!box) return;
|
|
3936
|
-
const id = this.motionLayoutId();
|
|
3937
3978
|
let from;
|
|
3938
|
-
if (
|
|
3979
|
+
if (this.resumeFrom) {
|
|
3980
|
+
from = this.shifted(this.resumeFrom, -1);
|
|
3981
|
+
this.resumeFrom = void 0;
|
|
3982
|
+
} else if (!this.prev) {
|
|
3939
3983
|
const seen = id && this.registry.from(id, this);
|
|
3940
3984
|
if (seen) from = this.shifted(seen, -1);
|
|
3941
3985
|
} else if (!same(this.prev, box)) from = this.visual(this.prev);
|
|
@@ -3953,33 +3997,57 @@ var MotionLayoutDirective = class MotionLayoutDirective {
|
|
|
3953
3997
|
};
|
|
3954
3998
|
if (Math.abs(start.x) < .5 && Math.abs(start.y) < .5 && Math.abs(start.sx - 1) < .005 && Math.abs(start.sy - 1) < .005) return;
|
|
3955
3999
|
this.controls?.stop();
|
|
4000
|
+
if (!this.radii) this.readRadii();
|
|
3956
4001
|
this.apply(start);
|
|
3957
4002
|
if (!this._animating()) {
|
|
3958
4003
|
this._animating.set(true);
|
|
3959
4004
|
this.layoutAnimationStart.emit();
|
|
3960
4005
|
}
|
|
3961
4006
|
const id = this.motionLayoutId();
|
|
4007
|
+
const run = {};
|
|
4008
|
+
this.run = run;
|
|
3962
4009
|
const controls = this.motion.animate(0, 1, {
|
|
3963
4010
|
...this.layoutTransition(),
|
|
3964
4011
|
onUpdate: (p) => {
|
|
4012
|
+
if (this.run !== run) return;
|
|
3965
4013
|
this.apply(lerp(start, NONE, p));
|
|
3966
4014
|
if (id && this.prev) this.registry.record(id, this, this.shifted(this.visual(this.prev)));
|
|
3967
4015
|
}
|
|
3968
4016
|
});
|
|
3969
4017
|
this.controls = controls;
|
|
3970
4018
|
Promise.resolve(controls).then(() => {
|
|
3971
|
-
if (this.
|
|
4019
|
+
if (this.run !== run) return;
|
|
4020
|
+
this.run = void 0;
|
|
3972
4021
|
this.controls = void 0;
|
|
3973
4022
|
this.apply(NONE);
|
|
3974
4023
|
this._animating.set(false);
|
|
3975
4024
|
this.layoutAnimationComplete.emit();
|
|
3976
4025
|
}, () => {});
|
|
3977
4026
|
}
|
|
4027
|
+
readRadii() {
|
|
4028
|
+
if (this.mode() === "position") return;
|
|
4029
|
+
const cs = getComputedStyle(this.el);
|
|
4030
|
+
const r = CORNERS.map((c) => cs[`border${c}Radius`]);
|
|
4031
|
+
if (r.some((v) => v.includes("%"))) return;
|
|
4032
|
+
const px = r.map((v) => parseFloat(v) || 0);
|
|
4033
|
+
if (px.some((v) => v > 0)) {
|
|
4034
|
+
this.radii = px;
|
|
4035
|
+
this.inlineRadius = this.el.style.borderRadius;
|
|
4036
|
+
}
|
|
4037
|
+
}
|
|
3978
4038
|
apply(o) {
|
|
3979
4039
|
this.cur = o;
|
|
3980
|
-
|
|
4040
|
+
const still = o === NONE;
|
|
4041
|
+
this.el.style.transform = still ? "" : `translate(${o.x}px, ${o.y}px) scale(${o.sx}, ${o.sy})`;
|
|
4042
|
+
this.el.classList.toggle("motion-layout-animating", !still);
|
|
3981
4043
|
if (this.mode() === "position") return;
|
|
3982
|
-
const
|
|
4044
|
+
const flat = still || o.sx === 1 && o.sy === 1;
|
|
4045
|
+
if (this.radii) {
|
|
4046
|
+
const r = this.radii;
|
|
4047
|
+
this.el.style.borderRadius = flat ? this.inlineRadius : `${r.map((v) => `${v / o.sx}px`).join(" ")} / ${r.map((v) => `${v / o.sy}px`).join(" ")}`;
|
|
4048
|
+
if (still) this.radii = null;
|
|
4049
|
+
}
|
|
4050
|
+
const inverse = flat ? "" : `scale(${1 / o.sx}, ${1 / o.sy})`;
|
|
3983
4051
|
this.el.querySelectorAll("[motionLayoutCorrect]").forEach((c) => c.style.transform = inverse);
|
|
3984
4052
|
}
|
|
3985
4053
|
static ɵfac = i0.ɵɵngDeclareFactory({
|