@hilux/ngx-motion 2.1.0 → 2.2.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 CHANGED
@@ -54,6 +54,7 @@ export class CardComponent {}
54
54
  - **Drag:** `motionDrag` with constraints, elastic edges, momentum and snap points (carousels, sliders, card stacks).
55
55
  - **Pan and swipe:** `motionPan` reports pointer movement and swipe directions, with `whilePan` styles.
56
56
  - **Reorder:** `motionReorderGroup` and `motionReorderItem` for drag-to-reorder lists, with keyboard support.
57
+ - **Layout animations:** `motionLayout` glides elements to their new place and size after any layout change, and `motionLayoutId` makes shared-element transitions.
57
58
  - **Exit animations:** `*motionIf` and `*motionPresence` keep elements in the DOM until their exit animation finishes. If shown again mid-exit, they animate back in.
58
59
  - **Variants:** named, reusable animation states.
59
60
  - **Timelines:** sequence several steps on one element.
@@ -328,6 +329,36 @@ Drag-to-reorder lists, like Framer Motion's `Reorder.Group` and `Reorder.Item`.
328
329
  - Keyboard: on a focused item (give it `tabindex="0"`), Alt + arrow keys move it one place.
329
330
  - Track items by identity in `@for` (not `$index`) so elements move with their values. Items can be different sizes.
330
331
 
332
+ ### Layout Animations
333
+ Like Framer Motion's `layout` and `layoutId`. Change the layout however you like (reorder or filter a list, switch flex to grid, toggle a class) and `motionLayout` animates each element from where it was drawn to where it is now. No positions to calculate.
334
+
335
+ ```html
336
+ <!-- Glides to its new place in the list -->
337
+ @for (item of items(); track item.id) {
338
+ <li motionLayout="position">{{ item.name }}</li>
339
+ }
340
+
341
+ <!-- Moved by justify-content alone -->
342
+ <button [class.justify-end]="on()" (click)="on.set(!on())">
343
+ <span class="knob" motionLayout></span>
344
+ </button>
345
+
346
+ <!-- Shared element: each tab renders its own pill, and the new pill starts where the old one was -->
347
+ @for (tab of tabs; track tab) {
348
+ <button (click)="active.set(tab)">
349
+ @if (active() === tab) { <span class="pill" motionLayoutId="tab-pill"></span> }
350
+ {{ tab }}
351
+ </button>
352
+ }
353
+ ```
354
+
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`.
357
+ - `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.
359
+ - Layout animations use the element's `transform`, so don't give it a CSS transform of its own; wrap it instead.
360
+ - Interrupted animations continue from where the element is drawn. Resizing the window re-measures without animating, and with reduced motion nothing animates.
361
+
331
362
  ## Angular Control Flow Integration
332
363
 
333
364
  Angular's control flow (`@if`, `@for`, etc.) removes elements immediately from the DOM when conditions change. To support exit animations, `@hilux/ngx-motion` provides structural directives that manage the animation lifecycle.
@@ -420,9 +451,14 @@ The directives work seamlessly with Angular's new control flow syntax:
420
451
  | `drag`, `dragConstraints`, `dragElastic`, `dragMomentum` | `motionDrag`, `dragConstraints`, `dragElastic`, `dragMomentum` |
421
452
  | `onPan`, `whilePan` | `motionPan` with `(pan)`, `[whilePan]` |
422
453
  | `<Reorder.Group values onReorder>` / `<Reorder.Item value>` | `[(motionReorderGroup)]` / `[motionReorderItem]` |
454
+ | `layout`, `layout="position"` | `motionLayout`, `motionLayout="position"` |
455
+ | `layoutId` | `motionLayoutId` |
423
456
 
424
457
  ## Changelog
425
458
 
459
+ **2.2.0**
460
+ - 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
+
426
462
  **2.1.0**
427
463
  - New `motionPan` directive: pan events with offset, delta and velocity, `whilePan` styles, and `swipe` with a direction. See [Pan and Swipe](#pan-and-swipe).
428
464
  - New drag-to-reorder lists: `motionReorderGroup` and `motionReorderItem`, with two-way bound order and keyboard support. See [Reorder](#reorder).
@@ -1,5 +1,5 @@
1
1
  import * as i0 from "@angular/core";
2
- import { ChangeDetectorRef, DOCUMENT, DestroyRef, Directive, ElementRef, EventEmitter, HostListener, Inject, Injectable, Injector, Input, NgZone, Output, PLATFORM_ID, TemplateRef, ViewContainerRef, afterNextRender, afterRenderEffect, effect, forwardRef, inject, input, isDevMode, model, output, signal, untracked } from "@angular/core";
2
+ import { ChangeDetectorRef, DOCUMENT, DestroyRef, Directive, ElementRef, EventEmitter, HostListener, Inject, Injectable, Injector, Input, NgZone, Output, PLATFORM_ID, TemplateRef, ViewContainerRef, afterEveryRender, afterNextRender, afterRenderEffect, effect, forwardRef, inject, input, isDevMode, model, output, signal, untracked } from "@angular/core";
3
3
  import { isPlatformBrowser } from "@angular/common";
4
4
  import { Subject, timer } from "rxjs";
5
5
  import { map, take } from "rxjs/operators";
@@ -3490,7 +3490,7 @@ i0.ɵɵngDeclareClassMetadata({
3490
3490
  }]
3491
3491
  }
3492
3492
  });
3493
- const LAYOUT_SPRING = {
3493
+ const LAYOUT_SPRING$1 = {
3494
3494
  type: "spring",
3495
3495
  stiffness: 550,
3496
3496
  damping: 45
@@ -3498,7 +3498,7 @@ const LAYOUT_SPRING = {
3498
3498
  var MotionReorderGroupDirective = class MotionReorderGroupDirective {
3499
3499
  motionReorderGroup = model.required(...ngDevMode ? [{ debugName: "motionReorderGroup" }] : /* istanbul ignore next */ []);
3500
3500
  reorderAxis = input("y", ...ngDevMode ? [{ debugName: "reorderAxis" }] : /* istanbul ignore next */ []);
3501
- reorderTransition = input(LAYOUT_SPRING, ...ngDevMode ? [{ debugName: "reorderTransition" }] : /* istanbul ignore next */ []);
3501
+ reorderTransition = input(LAYOUT_SPRING$1, ...ngDevMode ? [{ debugName: "reorderTransition" }] : /* istanbul ignore next */ []);
3502
3502
  reorderEnd = output();
3503
3503
  items = /* @__PURE__ */ new Set();
3504
3504
  motion = inject(MotionAnimationService);
@@ -3528,7 +3528,6 @@ var MotionReorderGroupDirective = class MotionReorderGroupDirective {
3528
3528
  const next = [...values];
3529
3529
  next.splice(to, 0, ...next.splice(from, 1));
3530
3530
  this.pending = true;
3531
- this.zone.run(() => this.motionReorderGroup.set(next));
3532
3531
  afterNextRender(() => {
3533
3532
  this.pending = false;
3534
3533
  const axis = this.reorderAxis();
@@ -3546,6 +3545,7 @@ var MotionReorderGroupDirective = class MotionReorderGroupDirective {
3546
3545
  }
3547
3546
  }
3548
3547
  }, { injector: this.injector });
3548
+ this.zone.run(() => this.motionReorderGroup.set(next));
3549
3549
  return true;
3550
3550
  }
3551
3551
  ended() {
@@ -3786,6 +3786,306 @@ i0.ɵɵngDeclareClassMetadata({
3786
3786
  }]
3787
3787
  }] }
3788
3788
  });
3789
- export { MotionAnimationService, MotionDragDirective, MotionFollowPointerDirective, MotionIfDirective, MotionMagneticDirective, MotionOneDirective, MotionOneService, MotionPanDirective, MotionPresenceDirective, MotionReorderGroupDirective, MotionReorderItemDirective, MotionScrollDirective, MotionTiltDirective, POINTER_SPRING, SCROLL_THROUGH, SCROLL_WHILE_VISIBLE };
3789
+ const NONE = {
3790
+ x: 0,
3791
+ y: 0,
3792
+ sx: 1,
3793
+ sy: 1
3794
+ };
3795
+ const LAYOUT_SPRING = {
3796
+ type: "spring",
3797
+ stiffness: 300,
3798
+ damping: 34,
3799
+ mass: 1
3800
+ };
3801
+ var MotionLayoutRegistry = class MotionLayoutRegistry {
3802
+ seen = /* @__PURE__ */ new Map();
3803
+ record(id, owner, box) {
3804
+ this.seen.set(id, {
3805
+ owner,
3806
+ box
3807
+ });
3808
+ }
3809
+ from(id, owner) {
3810
+ const s = this.seen.get(id);
3811
+ return s && s.owner !== owner ? s.box : void 0;
3812
+ }
3813
+ release(id, owner) {
3814
+ const s = this.seen.get(id);
3815
+ if (s?.owner === owner) setTimeout(() => {
3816
+ if (this.seen.get(id) === s) this.seen.delete(id);
3817
+ }, 1e3);
3818
+ }
3819
+ static ɵfac = i0.ɵɵngDeclareFactory({
3820
+ minVersion: "12.0.0",
3821
+ version: "22.3.2",
3822
+ ngImport: i0,
3823
+ type: MotionLayoutRegistry,
3824
+ deps: [],
3825
+ target: i0.ɵɵFactoryTarget.Injectable
3826
+ });
3827
+ static ɵprov = i0.ɵɵngDeclareInjectable({
3828
+ minVersion: "12.0.0",
3829
+ version: "22.3.2",
3830
+ ngImport: i0,
3831
+ type: MotionLayoutRegistry,
3832
+ providedIn: "root"
3833
+ });
3834
+ };
3835
+ i0.ɵɵngDeclareClassMetadata({
3836
+ minVersion: "12.0.0",
3837
+ version: "22.3.2",
3838
+ ngImport: i0,
3839
+ type: MotionLayoutRegistry,
3840
+ decorators: [{
3841
+ type: Injectable,
3842
+ args: [{ providedIn: "root" }]
3843
+ }]
3844
+ });
3845
+ var MotionLayoutDirective = class MotionLayoutDirective {
3846
+ motionLayout = input("", ...ngDevMode ? [{ debugName: "motionLayout" }] : /* istanbul ignore next */ []);
3847
+ motionLayoutId = input(null, ...ngDevMode ? [{ debugName: "motionLayoutId" }] : /* istanbul ignore next */ []);
3848
+ layoutTransition = input(LAYOUT_SPRING, ...ngDevMode ? [{ debugName: "layoutTransition" }] : /* istanbul ignore next */ []);
3849
+ layoutAnimationStart = output();
3850
+ layoutAnimationComplete = output();
3851
+ _animating = signal(false, ...ngDevMode ? [{ debugName: "_animating" }] : /* istanbul ignore next */ []);
3852
+ animating = this._animating.asReadonly();
3853
+ el = inject(ElementRef).nativeElement;
3854
+ motion = inject(MotionAnimationService);
3855
+ registry = inject(MotionLayoutRegistry);
3856
+ prev = null;
3857
+ shift = {
3858
+ x: 0,
3859
+ y: 0
3860
+ };
3861
+ cur = NONE;
3862
+ controls;
3863
+ resnap = false;
3864
+ reduced = typeof window !== "undefined" && !!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
3865
+ constructor() {
3866
+ const destroyRef = inject(DestroyRef);
3867
+ if (!isPlatformBrowser(inject(PLATFORM_ID))) return;
3868
+ afterEveryRender({
3869
+ earlyRead: () => this.measure(),
3870
+ write: (box) => this.update(box)
3871
+ });
3872
+ let raf = 0;
3873
+ const resize = () => {
3874
+ this.resnap = true;
3875
+ this.controls?.stop();
3876
+ this.controls = void 0;
3877
+ if (this.cur !== NONE) this.apply(NONE);
3878
+ this.prev = this.measure() ?? this.prev;
3879
+ cancelAnimationFrame(raf);
3880
+ raf = requestAnimationFrame(() => this.resnap = false);
3881
+ };
3882
+ inject(NgZone).runOutsideAngular(() => window.addEventListener("resize", resize));
3883
+ destroyRef.onDestroy(() => {
3884
+ window.removeEventListener("resize", resize);
3885
+ cancelAnimationFrame(raf);
3886
+ this.controls?.stop();
3887
+ const id = this.motionLayoutId();
3888
+ if (id) this.registry.release(id, this);
3889
+ });
3890
+ }
3891
+ mode() {
3892
+ const m = this.motionLayout();
3893
+ return m === "position" || m === "size" ? m : "both";
3894
+ }
3895
+ measure() {
3896
+ const el = this.el;
3897
+ if (!el.isConnected || !el.offsetWidth && !el.offsetHeight && !el.offsetParent) return null;
3898
+ let x = 0, y = 0;
3899
+ for (let n = el; n; n = n.offsetParent) {
3900
+ x += n.offsetLeft;
3901
+ y += n.offsetTop;
3902
+ }
3903
+ const w = el.offsetWidth, h = el.offsetHeight;
3904
+ const box = {
3905
+ cx: x + w / 2,
3906
+ cy: y + h / 2,
3907
+ w,
3908
+ h
3909
+ };
3910
+ if (this.motionLayoutId()) {
3911
+ const r = el.getBoundingClientRect();
3912
+ this.shift = {
3913
+ x: r.left + r.width / 2 - this.cur.x - box.cx,
3914
+ y: r.top + r.height / 2 - this.cur.y - box.cy
3915
+ };
3916
+ }
3917
+ return box;
3918
+ }
3919
+ shifted(b, sign = 1) {
3920
+ return {
3921
+ ...b,
3922
+ cx: b.cx + sign * this.shift.x,
3923
+ cy: b.cy + sign * this.shift.y
3924
+ };
3925
+ }
3926
+ visual(b, o = this.cur) {
3927
+ return {
3928
+ cx: b.cx + o.x,
3929
+ cy: b.cy + o.y,
3930
+ w: b.w * o.sx,
3931
+ h: b.h * o.sy
3932
+ };
3933
+ }
3934
+ update(box) {
3935
+ if (!box) return;
3936
+ const id = this.motionLayoutId();
3937
+ let from;
3938
+ if (!this.prev) {
3939
+ const seen = id && this.registry.from(id, this);
3940
+ if (seen) from = this.shifted(seen, -1);
3941
+ } else if (!same(this.prev, box)) from = this.visual(this.prev);
3942
+ this.prev = box;
3943
+ if (from && !this.resnap && !this.reduced) this.animateFrom(from, box);
3944
+ if (id) this.registry.record(id, this, this.shifted(this.visual(box)));
3945
+ }
3946
+ animateFrom(from, to) {
3947
+ const mode = this.mode();
3948
+ const start = {
3949
+ x: mode === "size" ? 0 : from.cx - to.cx,
3950
+ y: mode === "size" ? 0 : from.cy - to.cy,
3951
+ sx: mode === "position" || !to.w ? 1 : from.w / to.w,
3952
+ sy: mode === "position" || !to.h ? 1 : from.h / to.h
3953
+ };
3954
+ 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
+ this.controls?.stop();
3956
+ this.apply(start);
3957
+ if (!this._animating()) {
3958
+ this._animating.set(true);
3959
+ this.layoutAnimationStart.emit();
3960
+ }
3961
+ const id = this.motionLayoutId();
3962
+ const controls = this.motion.animate(0, 1, {
3963
+ ...this.layoutTransition(),
3964
+ onUpdate: (p) => {
3965
+ this.apply(lerp(start, NONE, p));
3966
+ if (id && this.prev) this.registry.record(id, this, this.shifted(this.visual(this.prev)));
3967
+ }
3968
+ });
3969
+ this.controls = controls;
3970
+ Promise.resolve(controls).then(() => {
3971
+ if (this.controls !== controls) return;
3972
+ this.controls = void 0;
3973
+ this.apply(NONE);
3974
+ this._animating.set(false);
3975
+ this.layoutAnimationComplete.emit();
3976
+ }, () => {});
3977
+ }
3978
+ apply(o) {
3979
+ this.cur = o;
3980
+ this.el.style.transform = o === NONE ? "" : `translate(${o.x}px, ${o.y}px) scale(${o.sx}, ${o.sy})`;
3981
+ if (this.mode() === "position") return;
3982
+ const inverse = o === NONE || o.sx === 1 && o.sy === 1 ? "" : `scale(${1 / o.sx}, ${1 / o.sy})`;
3983
+ this.el.querySelectorAll("[motionLayoutCorrect]").forEach((c) => c.style.transform = inverse);
3984
+ }
3985
+ static ɵfac = i0.ɵɵngDeclareFactory({
3986
+ minVersion: "12.0.0",
3987
+ version: "22.3.2",
3988
+ ngImport: i0,
3989
+ type: MotionLayoutDirective,
3990
+ deps: [],
3991
+ target: i0.ɵɵFactoryTarget.Directive
3992
+ });
3993
+ static ɵdir = i0.ɵɵngDeclareDirective({
3994
+ minVersion: "17.1.0",
3995
+ version: "22.3.2",
3996
+ type: MotionLayoutDirective,
3997
+ isStandalone: true,
3998
+ selector: "[motionLayout], [motionLayoutId]",
3999
+ inputs: {
4000
+ motionLayout: {
4001
+ classPropertyName: "motionLayout",
4002
+ publicName: "motionLayout",
4003
+ isSignal: true,
4004
+ isRequired: false,
4005
+ transformFunction: null
4006
+ },
4007
+ motionLayoutId: {
4008
+ classPropertyName: "motionLayoutId",
4009
+ publicName: "motionLayoutId",
4010
+ isSignal: true,
4011
+ isRequired: false,
4012
+ transformFunction: null
4013
+ },
4014
+ layoutTransition: {
4015
+ classPropertyName: "layoutTransition",
4016
+ publicName: "layoutTransition",
4017
+ isSignal: true,
4018
+ isRequired: false,
4019
+ transformFunction: null
4020
+ }
4021
+ },
4022
+ outputs: {
4023
+ layoutAnimationStart: "layoutAnimationStart",
4024
+ layoutAnimationComplete: "layoutAnimationComplete"
4025
+ },
4026
+ exportAs: ["motionLayout"],
4027
+ ngImport: i0
4028
+ });
4029
+ };
4030
+ i0.ɵɵngDeclareClassMetadata({
4031
+ minVersion: "12.0.0",
4032
+ version: "22.3.2",
4033
+ ngImport: i0,
4034
+ type: MotionLayoutDirective,
4035
+ decorators: [{
4036
+ type: Directive,
4037
+ args: [{
4038
+ selector: "[motionLayout], [motionLayoutId]",
4039
+ exportAs: "motionLayout"
4040
+ }]
4041
+ }],
4042
+ ctorParameters: () => [],
4043
+ propDecorators: {
4044
+ motionLayout: [{
4045
+ type: i0.Input,
4046
+ args: [{
4047
+ isSignal: true,
4048
+ alias: "motionLayout",
4049
+ required: false
4050
+ }]
4051
+ }],
4052
+ motionLayoutId: [{
4053
+ type: i0.Input,
4054
+ args: [{
4055
+ isSignal: true,
4056
+ alias: "motionLayoutId",
4057
+ required: false
4058
+ }]
4059
+ }],
4060
+ layoutTransition: [{
4061
+ type: i0.Input,
4062
+ args: [{
4063
+ isSignal: true,
4064
+ alias: "layoutTransition",
4065
+ required: false
4066
+ }]
4067
+ }],
4068
+ layoutAnimationStart: [{
4069
+ type: i0.Output,
4070
+ args: ["layoutAnimationStart"]
4071
+ }],
4072
+ layoutAnimationComplete: [{
4073
+ type: i0.Output,
4074
+ args: ["layoutAnimationComplete"]
4075
+ }]
4076
+ }
4077
+ });
4078
+ function same(a, b) {
4079
+ return Math.abs(a.cx - b.cx) < .5 && Math.abs(a.cy - b.cy) < .5 && Math.abs(a.w - b.w) < .5 && Math.abs(a.h - b.h) < .5;
4080
+ }
4081
+ function lerp(a, b, p) {
4082
+ return {
4083
+ x: a.x + (b.x - a.x) * p,
4084
+ y: a.y + (b.y - a.y) * p,
4085
+ sx: a.sx + (b.sx - a.sx) * p,
4086
+ sy: a.sy + (b.sy - a.sy) * p
4087
+ };
4088
+ }
4089
+ export { MotionAnimationService, MotionDragDirective, MotionFollowPointerDirective, MotionIfDirective, MotionLayoutDirective, MotionLayoutRegistry, MotionMagneticDirective, MotionOneDirective, MotionOneService, MotionPanDirective, MotionPresenceDirective, MotionReorderGroupDirective, MotionReorderItemDirective, MotionScrollDirective, MotionTiltDirective, POINTER_SPRING, SCROLL_THROUGH, SCROLL_WHILE_VISIBLE };
3790
4090
 
3791
4091
  //# sourceMappingURL=hilux-ngx-motion.mjs.map