@hilux/ngx-motion 2.2.0 → 2.4.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 +93 -2
- package/fesm2022/hilux-ngx-motion.mjs +404 -14
- package/fesm2022/hilux-ngx-motion.mjs.map +1 -1
- package/package.json +1 -1
- package/types/hilux-ngx-motion.d.ts +193 -14
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
|
+
- **Motion values:** `motionSignal`, `mapSignal`, `springSignal`, `velocitySignal` and `scrollSignal`: values that change every frame, as Angular signals, bound to styles with `[motionStyle]` without change detection.
|
|
57
58
|
- **Layout animations:** `motionLayout` glides elements to their new place and size after any layout change, and `motionLayoutId` makes shared-element transitions.
|
|
58
59
|
- **Exit animations:** `*motionIf` and `*motionPresence` keep elements in the DOM until their exit animation finishes. If shown again mid-exit, they animate back in.
|
|
59
60
|
- **Variants:** named, reusable animation states.
|
|
@@ -329,6 +330,44 @@ Drag-to-reorder lists, like Framer Motion's `Reorder.Group` and `Reorder.Item`.
|
|
|
329
330
|
- Keyboard: on a focused item (give it `tabindex="0"`), Alt + arrow keys move it one place.
|
|
330
331
|
- Track items by identity in `@for` (not `$index`) so elements move with their values. Items can be different sizes.
|
|
331
332
|
|
|
333
|
+
### Motion Values
|
|
334
|
+
Values that change every frame, like Framer Motion's `useMotionValue`, `useTransform`, `useSpring`, `useVelocity` and `useScroll`, as Angular signals. Read one in a template like any signal, or bind it with `[motionStyle]`: the directive writes each change straight to the element, so nothing re-renders while it moves.
|
|
335
|
+
|
|
336
|
+
```typescript
|
|
337
|
+
import { MotionStyleDirective, mapSignal, motionSignal, scrollSignal, springSignal, velocitySignal } from '@hilux/ngx-motion';
|
|
338
|
+
|
|
339
|
+
@Component({
|
|
340
|
+
imports: [MotionStyleDirective, MotionPanDirective],
|
|
341
|
+
template: `
|
|
342
|
+
<div motionPan="x" (pan)="drag.set($event.offset.x)" (panEnd)="drag.set(0)"
|
|
343
|
+
[motionStyle]="{ x: x, backgroundColor: colour }">{{ label() }}</div>
|
|
344
|
+
<div class="bar" [motionStyle]="{ scaleX: scroll.progress, skewY: skew }"></div>
|
|
345
|
+
`,
|
|
346
|
+
})
|
|
347
|
+
export class Example {
|
|
348
|
+
readonly drag = motionSignal(0); // useMotionValue
|
|
349
|
+
readonly x = springSignal(this.drag, { stiffness: 500, damping: 40 }); // useSpring: follows drag
|
|
350
|
+
readonly colour = mapSignal(this.x, [-140, 0, 140], ['#f87171', '#3a3a3a', '#86efac']); // useTransform
|
|
351
|
+
readonly label = mapSignal(this.x, v => `${Math.round(v)}px`);
|
|
352
|
+
readonly scroll = scrollSignal(); // useScroll: progress, offset, velocity
|
|
353
|
+
readonly skew = mapSignal(velocitySignal(this.scroll.offset), [-3000, 3000], [-10, 10]); // useVelocity
|
|
354
|
+
}
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
| Function | What it gives you |
|
|
358
|
+
|---|---|
|
|
359
|
+
| `motionSignal(initial)` | A writable value: `x()` (tracked), `x.get()` (untracked), `x.set(v)`, `x.on(fn)` (every change, outside change detection), `x.getVelocity()` |
|
|
360
|
+
| `mapSignal(source, [in], [out], { clamp, ease })` | Maps a range to another: numbers, colours, or strings with units. Clamped by default |
|
|
361
|
+
| `mapSignal(source, fn)` / `mapSignal([a, b], fn)` | Any function of one or more values |
|
|
362
|
+
| `springSignal(target \| source, { stiffness, damping, mass })` | Springs to `set()` targets, or follows a source; keeps its speed when the target changes. `jump(v)` skips the spring |
|
|
363
|
+
| `velocitySignal(source)` | Speed per second; back to 0 when the source stops |
|
|
364
|
+
| `scrollSignal({ container, target, offset, axis })` | `progress` (0 to 1), `offset` (px) and `velocity` for the page or an element; `container` and `target` can be `viewChild` signals |
|
|
365
|
+
|
|
366
|
+
- Sources can be motion signals or any Angular signal (those update with change detection).
|
|
367
|
+
- `springSignal`, `velocitySignal` and `scrollSignal` must be created in an injection context (a field or the constructor), or given `{ injector }`. They clean up when it's destroyed.
|
|
368
|
+
- `[motionStyle]`: transform shortcuts `x`, `y`, `z` (px), `scale`, `scaleX`, `scaleY`, `rotate`, `rotateX/Y/Z`, `skewX/Y` (deg) are combined into `transform`, so don't animate the same element's transform another way. Other styles use camelCase names; numbers get px except unitless ones such as `opacity`. CSS custom properties (`'--progress'`) take any value.
|
|
369
|
+
- With reduced motion, and on the server, springs go straight to their target.
|
|
370
|
+
|
|
332
371
|
### Layout Animations
|
|
333
372
|
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
373
|
|
|
@@ -353,9 +392,21 @@ Like Framer Motion's `layout` and `layoutId`. Change the layout however you like
|
|
|
353
392
|
```
|
|
354
393
|
|
|
355
394
|
- `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
|
|
395
|
+
- `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.
|
|
396
|
+
- 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:
|
|
397
|
+
|
|
398
|
+
```html
|
|
399
|
+
@for (p of photos; track p.id) {
|
|
400
|
+
<button class="thumb" [motionLayoutId]="p.id" (click)="open.set(p)"><img [src]="p.src" /></button>
|
|
401
|
+
}
|
|
402
|
+
@if (open(); as p) {
|
|
403
|
+
<div class="lightbox" [motionLayoutId]="p.id" (click)="open.set(null)"><img [src]="p.src" /></div>
|
|
404
|
+
}
|
|
405
|
+
```
|
|
357
406
|
- `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.
|
|
407
|
+
- 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.
|
|
408
|
+
- 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.
|
|
409
|
+
- While it moves the element has the class `motion-layout-animating`, for example to raise it above its neighbours with `z-index`.
|
|
359
410
|
- Layout animations use the element's `transform`, so don't give it a CSS transform of its own; wrap it instead.
|
|
360
411
|
- Interrupted animations continue from where the element is drawn. Resizing the window re-measures without animating, and with reduced motion nothing animates.
|
|
361
412
|
|
|
@@ -451,11 +502,48 @@ The directives work seamlessly with Angular's new control flow syntax:
|
|
|
451
502
|
| `drag`, `dragConstraints`, `dragElastic`, `dragMomentum` | `motionDrag`, `dragConstraints`, `dragElastic`, `dragMomentum` |
|
|
452
503
|
| `onPan`, `whilePan` | `motionPan` with `(pan)`, `[whilePan]` |
|
|
453
504
|
| `<Reorder.Group values onReorder>` / `<Reorder.Item value>` | `[(motionReorderGroup)]` / `[motionReorderItem]` |
|
|
505
|
+
| `useMotionValue`, `useTransform`, `useSpring`, `useVelocity`, `useScroll` | `motionSignal`, `mapSignal`, `springSignal`, `velocitySignal`, `scrollSignal` |
|
|
506
|
+
| `style={{ x, opacity }}` with motion values | `[motionStyle]="{ x: x, opacity: opacity }"` |
|
|
454
507
|
| `layout`, `layout="position"` | `motionLayout`, `motionLayout="position"` |
|
|
455
508
|
| `layoutId` | `motionLayoutId` |
|
|
509
|
+
| `<LayoutGroup>` | not needed: every layout element is measured after every render |
|
|
510
|
+
|
|
511
|
+
## CSS or ngx-motion?
|
|
512
|
+
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.
|
|
513
|
+
|
|
514
|
+
| Feature | Plain CSS | What ngx-motion adds |
|
|
515
|
+
|---|---|---|
|
|
516
|
+
| Transitions and keyframes | ✅ `transition`, `@keyframes`, separate `translate` / `scale` / `rotate` | Plain objects in the template, values that can change at runtime (signals) |
|
|
517
|
+
| 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 |
|
|
518
|
+
| 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 |
|
|
519
|
+
| Hover, tap, focus | ✅ `:hover`, `:active`, `:focus-visible` | Physics spring-back; `whileTap` on touch screens; composes with drag and pointer effects |
|
|
520
|
+
| Enter animations | ✅ `@starting-style`; Angular's `animate.enter` | Declarative `initial` → `animate` states |
|
|
521
|
+
| 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 |
|
|
522
|
+
| Stagger | ⚠️ A delay per item via custom properties; `sibling-index()` only in Chrome | `staggerChildren`, `delayChildren`, direction, nested parents |
|
|
523
|
+
| Sequences | ⚠️ Percentages in one `@keyframes`; several elements need hand-tuned delays | Timelines, `repeat` with `mirror` / `reverse`, per-property transitions |
|
|
524
|
+
| Play once when scrolled into view | ❌ No trigger yet (`animation-trigger` is experimental); needs IntersectionObserver | `runInView`, `whileInView` |
|
|
525
|
+
| 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 |
|
|
526
|
+
| Magnetic, tilt, follow-pointer | ❌ CSS can't read the pointer position | `motionMagnetic`, `motionTilt` (sets `--motion-pointer-x/y` for CSS), `motionFollowPointer` |
|
|
527
|
+
| Drag | ❌ | Constraints, elastic edges, momentum, snap points |
|
|
528
|
+
| Pan and swipe | ❌ Only `touch-action` and scroll snap | Offset, delta, velocity and swipe direction |
|
|
529
|
+
| Drag to reorder | ❌ | Two-way bound order, keyboard moves, neighbours glide aside |
|
|
530
|
+
| 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 |
|
|
531
|
+
| 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 |
|
|
532
|
+
| Reduced motion | ✅ `@media (prefers-reduced-motion)` | Respected by every directive automatically |
|
|
533
|
+
| Change detection and SSR | No JavaScript | Runs outside Angular's change detection; does nothing on the server |
|
|
534
|
+
|
|
535
|
+
**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
536
|
|
|
457
537
|
## Changelog
|
|
458
538
|
|
|
539
|
+
**2.4.0**
|
|
540
|
+
- Motion values as Angular signals: `motionSignal`, `mapSignal`, `springSignal`, `velocitySignal` and `scrollSignal`, and the `[motionStyle]` directive, which writes them to the element without change detection. See [Motion Values](#motion-values).
|
|
541
|
+
|
|
542
|
+
**2.3.0**
|
|
543
|
+
- 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).
|
|
544
|
+
- Layout animations keep corner radii round while scaling, and add the class `motion-layout-animating` while an element moves.
|
|
545
|
+
- 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.
|
|
546
|
+
|
|
459
547
|
**2.2.0**
|
|
460
548
|
- 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
549
|
|
|
@@ -574,6 +662,9 @@ It's the closest equivalent. Framer Motion (now Motion for React) is React-only.
|
|
|
574
662
|
### Which Angular versions are supported?
|
|
575
663
|
2.x supports Angular 22 and later. 1.x supports Angular 19.
|
|
576
664
|
|
|
665
|
+
### Do I need it, or is CSS enough?
|
|
666
|
+
Often CSS is enough. See [CSS or ngx-motion?](#css-or-ngx-motion) for a feature-by-feature table.
|
|
667
|
+
|
|
577
668
|
### How is it different from Angular's built-in animations?
|
|
578
669
|
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
670
|
- physics springs
|
|
@@ -1,9 +1,9 @@
|
|
|
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, afterEveryRender, 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, assertInInjectionContext, effect, forwardRef, inject, input, isDevMode, isSignal, 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";
|
|
6
|
-
import { animate, easeIn, easeInOut, easeOut, inView, scroll, spring, stagger } from "motion";
|
|
6
|
+
import { animate, easeIn, easeInOut, easeOut, inView, scroll, spring, stagger, transform } from "motion";
|
|
7
7
|
var MotionOneService = class MotionOneService {
|
|
8
8
|
document;
|
|
9
9
|
platformId;
|
|
@@ -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({
|
|
@@ -4086,6 +4154,328 @@ function lerp(a, b, p) {
|
|
|
4086
4154
|
sy: a.sy + (b.sy - a.sy) * p
|
|
4087
4155
|
};
|
|
4088
4156
|
}
|
|
4089
|
-
|
|
4157
|
+
const BRAND = Symbol("MotionSignal");
|
|
4158
|
+
function isMotionSignal(value) {
|
|
4159
|
+
return typeof value === "function" && value[BRAND] === true;
|
|
4160
|
+
}
|
|
4161
|
+
const SETTLE_MS = 50;
|
|
4162
|
+
const MIN_SPAN_MS = 16;
|
|
4163
|
+
function motionSignal(initial) {
|
|
4164
|
+
const state = signal(initial, ...ngDevMode ? [{ debugName: "state" }] : /* istanbul ignore next */ []);
|
|
4165
|
+
const listeners = /* @__PURE__ */ new Set();
|
|
4166
|
+
let samples = [];
|
|
4167
|
+
let lastTime = 0;
|
|
4168
|
+
const now = () => typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
4169
|
+
const read = (() => state());
|
|
4170
|
+
Object.defineProperty(read, BRAND, { value: true });
|
|
4171
|
+
read.get = () => untracked(state);
|
|
4172
|
+
read.on = (listener) => {
|
|
4173
|
+
listeners.add(listener);
|
|
4174
|
+
return () => listeners.delete(listener);
|
|
4175
|
+
};
|
|
4176
|
+
read.getVelocity = () => {
|
|
4177
|
+
if (now() - lastTime > SETTLE_MS || samples.length < 2) return 0;
|
|
4178
|
+
const last = samples[samples.length - 1];
|
|
4179
|
+
let ref = samples[0];
|
|
4180
|
+
for (let i = samples.length - 2; i >= 0; i--) if (last.t - samples[i].t >= MIN_SPAN_MS) {
|
|
4181
|
+
ref = samples[i];
|
|
4182
|
+
break;
|
|
4183
|
+
}
|
|
4184
|
+
const dt = last.t - ref.t;
|
|
4185
|
+
return dt >= MIN_SPAN_MS / 2 ? (last.v - ref.v) / dt * 1e3 : 0;
|
|
4186
|
+
};
|
|
4187
|
+
read.set = (value) => {
|
|
4188
|
+
const previous = untracked(state);
|
|
4189
|
+
if (Object.is(previous, value)) return;
|
|
4190
|
+
const t = now();
|
|
4191
|
+
if (typeof value === "number") {
|
|
4192
|
+
if (t - lastTime > SETTLE_MS) samples = [];
|
|
4193
|
+
samples.push({
|
|
4194
|
+
t,
|
|
4195
|
+
v: value
|
|
4196
|
+
});
|
|
4197
|
+
if (samples.length > 8) samples = samples.slice(-8);
|
|
4198
|
+
}
|
|
4199
|
+
lastTime = t;
|
|
4200
|
+
state.set(value);
|
|
4201
|
+
for (const listener of [...listeners]) listener(value);
|
|
4202
|
+
};
|
|
4203
|
+
return read;
|
|
4204
|
+
}
|
|
4205
|
+
function mapSignal(source, mapOrInput, outputOrOptions, rangeOptions) {
|
|
4206
|
+
const sources = Array.isArray(source) ? source : [source];
|
|
4207
|
+
let compute;
|
|
4208
|
+
let options;
|
|
4209
|
+
const value = (s) => isMotionSignal(s) ? s.get() : untracked(s);
|
|
4210
|
+
if (typeof mapOrInput === "function") {
|
|
4211
|
+
const map = mapOrInput;
|
|
4212
|
+
options = outputOrOptions;
|
|
4213
|
+
compute = Array.isArray(source) ? () => map(sources.map(value)) : () => map(value(sources[0]));
|
|
4214
|
+
} else {
|
|
4215
|
+
options = rangeOptions;
|
|
4216
|
+
const mapper = transform(mapOrInput, outputOrOptions, {
|
|
4217
|
+
clamp: rangeOptions?.clamp ?? true,
|
|
4218
|
+
ease: rangeOptions?.ease
|
|
4219
|
+
});
|
|
4220
|
+
compute = () => mapper(value(sources[0]));
|
|
4221
|
+
}
|
|
4222
|
+
const out = motionSignal(compute());
|
|
4223
|
+
connect(sources, () => out.set(compute()), options);
|
|
4224
|
+
return out;
|
|
4225
|
+
}
|
|
4226
|
+
function springSignal(source, options = {}) {
|
|
4227
|
+
const injector = injectorFor(springSignal, options);
|
|
4228
|
+
const zone = injector.get(NgZone);
|
|
4229
|
+
const browser = isPlatformBrowser(injector.get(PLATFORM_ID)) && typeof requestAnimationFrame !== "undefined";
|
|
4230
|
+
const reduced = browser && !!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches;
|
|
4231
|
+
const { stiffness = 300, damping = 30, mass = 1, restDelta = .01, restSpeed = .05 } = options;
|
|
4232
|
+
const start = typeof source === "number" ? source : isMotionSignal(source) ? source.get() : untracked(source);
|
|
4233
|
+
const value = motionSignal(start);
|
|
4234
|
+
let position = start;
|
|
4235
|
+
let velocity = 0;
|
|
4236
|
+
let target = start;
|
|
4237
|
+
let raf = 0;
|
|
4238
|
+
let last = 0;
|
|
4239
|
+
const stop = () => {
|
|
4240
|
+
if (raf) cancelAnimationFrame(raf);
|
|
4241
|
+
raf = 0;
|
|
4242
|
+
};
|
|
4243
|
+
const step = (time) => {
|
|
4244
|
+
let dt = Math.min(time > last ? time - last : 16, 64) / 1e3;
|
|
4245
|
+
last = time;
|
|
4246
|
+
while (dt > 0) {
|
|
4247
|
+
const h = Math.min(dt, .004);
|
|
4248
|
+
const accel = (-stiffness * (position - target) - damping * velocity) / mass;
|
|
4249
|
+
velocity += accel * h;
|
|
4250
|
+
position += velocity * h;
|
|
4251
|
+
dt -= h;
|
|
4252
|
+
}
|
|
4253
|
+
if (Math.abs(velocity) < restSpeed && Math.abs(position - target) < restDelta) {
|
|
4254
|
+
position = target;
|
|
4255
|
+
velocity = 0;
|
|
4256
|
+
raf = 0;
|
|
4257
|
+
} else raf = requestAnimationFrame(step);
|
|
4258
|
+
value.set(position);
|
|
4259
|
+
};
|
|
4260
|
+
const out = (() => value());
|
|
4261
|
+
Object.defineProperty(out, BRAND, { value: true });
|
|
4262
|
+
out.get = value.get;
|
|
4263
|
+
out.on = value.on;
|
|
4264
|
+
out.getVelocity = () => velocity;
|
|
4265
|
+
out.jump = (v) => {
|
|
4266
|
+
stop();
|
|
4267
|
+
position = target = v;
|
|
4268
|
+
velocity = 0;
|
|
4269
|
+
value.set(v);
|
|
4270
|
+
};
|
|
4271
|
+
out.set = (next) => {
|
|
4272
|
+
if (!browser || reduced) return out.jump(next);
|
|
4273
|
+
target = next;
|
|
4274
|
+
if (raf || position === target && velocity === 0) return;
|
|
4275
|
+
last = performance.now();
|
|
4276
|
+
zone.runOutsideAngular(() => raf = requestAnimationFrame(step));
|
|
4277
|
+
};
|
|
4278
|
+
if (typeof source !== "number") connect([source], () => out.set(isMotionSignal(source) ? source.get() : untracked(source)), options);
|
|
4279
|
+
injector.get(DestroyRef).onDestroy(stop);
|
|
4280
|
+
return out;
|
|
4281
|
+
}
|
|
4282
|
+
function velocitySignal(source, options = {}) {
|
|
4283
|
+
const injector = injectorFor(velocitySignal, options);
|
|
4284
|
+
const zone = injector.get(NgZone);
|
|
4285
|
+
const out = motionSignal(0);
|
|
4286
|
+
let raf = 0;
|
|
4287
|
+
const check = () => {
|
|
4288
|
+
raf = 0;
|
|
4289
|
+
const v = source.getVelocity();
|
|
4290
|
+
out.set(v);
|
|
4291
|
+
if (v !== 0) raf = requestAnimationFrame(check);
|
|
4292
|
+
};
|
|
4293
|
+
const stop = source.on(() => {
|
|
4294
|
+
out.set(source.getVelocity());
|
|
4295
|
+
if (!raf && typeof requestAnimationFrame !== "undefined") zone.runOutsideAngular(() => raf = requestAnimationFrame(check));
|
|
4296
|
+
});
|
|
4297
|
+
injector.get(DestroyRef).onDestroy(() => {
|
|
4298
|
+
stop();
|
|
4299
|
+
if (raf) cancelAnimationFrame(raf);
|
|
4300
|
+
});
|
|
4301
|
+
return out;
|
|
4302
|
+
}
|
|
4303
|
+
function scrollSignal(options = {}) {
|
|
4304
|
+
const injector = injectorFor(scrollSignal, options);
|
|
4305
|
+
const progress = motionSignal(0);
|
|
4306
|
+
const offset = motionSignal(0);
|
|
4307
|
+
const velocity = motionSignal(0);
|
|
4308
|
+
let cancel;
|
|
4309
|
+
if (isPlatformBrowser(injector.get(PLATFORM_ID))) afterNextRender(() => {
|
|
4310
|
+
const resolve = (e) => {
|
|
4311
|
+
const v = typeof e === "function" ? e() : e;
|
|
4312
|
+
return v instanceof ElementRef ? v.nativeElement : v ?? void 0;
|
|
4313
|
+
};
|
|
4314
|
+
const container = resolve(options.container);
|
|
4315
|
+
const target = resolve(options.target);
|
|
4316
|
+
const axis = options.axis ?? "y";
|
|
4317
|
+
injector.get(NgZone).runOutsideAngular(() => {
|
|
4318
|
+
cancel = scroll((p, info) => {
|
|
4319
|
+
progress.set(p);
|
|
4320
|
+
offset.set(info[axis].current);
|
|
4321
|
+
velocity.set(info[axis].velocity);
|
|
4322
|
+
}, {
|
|
4323
|
+
container,
|
|
4324
|
+
target,
|
|
4325
|
+
offset: options.offset,
|
|
4326
|
+
axis
|
|
4327
|
+
});
|
|
4328
|
+
});
|
|
4329
|
+
}, { injector });
|
|
4330
|
+
injector.get(DestroyRef).onDestroy(() => cancel?.());
|
|
4331
|
+
return {
|
|
4332
|
+
progress,
|
|
4333
|
+
offset,
|
|
4334
|
+
velocity
|
|
4335
|
+
};
|
|
4336
|
+
}
|
|
4337
|
+
function connect(sources, update, options) {
|
|
4338
|
+
const stops = sources.filter(isMotionSignal).map((s) => s.on(update));
|
|
4339
|
+
const plain = sources.filter((s) => !isMotionSignal(s));
|
|
4340
|
+
const injector = options?.injector ?? tryInject();
|
|
4341
|
+
if (plain.length) {
|
|
4342
|
+
if (!injector) throw new Error("ngx-motion: a plain Angular signal source needs an injection context or the `injector` option.");
|
|
4343
|
+
effect(() => {
|
|
4344
|
+
for (const s of plain) s();
|
|
4345
|
+
untracked(update);
|
|
4346
|
+
}, { injector });
|
|
4347
|
+
}
|
|
4348
|
+
if (stops.length && injector) injector.get(DestroyRef).onDestroy(() => stops.forEach((stop) => stop()));
|
|
4349
|
+
}
|
|
4350
|
+
function tryInject() {
|
|
4351
|
+
try {
|
|
4352
|
+
return inject(Injector);
|
|
4353
|
+
} catch {
|
|
4354
|
+
return;
|
|
4355
|
+
}
|
|
4356
|
+
}
|
|
4357
|
+
function injectorFor(fn, options) {
|
|
4358
|
+
if (options.injector) return options.injector;
|
|
4359
|
+
assertInInjectionContext(fn);
|
|
4360
|
+
return inject(Injector);
|
|
4361
|
+
}
|
|
4362
|
+
const TRANSFORMS = [
|
|
4363
|
+
"x",
|
|
4364
|
+
"y",
|
|
4365
|
+
"z",
|
|
4366
|
+
"scale",
|
|
4367
|
+
"scaleX",
|
|
4368
|
+
"scaleY",
|
|
4369
|
+
"rotate",
|
|
4370
|
+
"rotateX",
|
|
4371
|
+
"rotateY",
|
|
4372
|
+
"rotateZ",
|
|
4373
|
+
"skewX",
|
|
4374
|
+
"skewY"
|
|
4375
|
+
];
|
|
4376
|
+
const TRANSFORM_FN = {
|
|
4377
|
+
x: "translateX",
|
|
4378
|
+
y: "translateY",
|
|
4379
|
+
z: "translateZ"
|
|
4380
|
+
};
|
|
4381
|
+
const UNITLESS = /* @__PURE__ */ new Set([
|
|
4382
|
+
"opacity",
|
|
4383
|
+
"zIndex",
|
|
4384
|
+
"fontWeight",
|
|
4385
|
+
"lineHeight",
|
|
4386
|
+
"flex",
|
|
4387
|
+
"flexGrow",
|
|
4388
|
+
"flexShrink",
|
|
4389
|
+
"order",
|
|
4390
|
+
"zoom",
|
|
4391
|
+
"fillOpacity",
|
|
4392
|
+
"strokeOpacity"
|
|
4393
|
+
]);
|
|
4394
|
+
var MotionStyleDirective = class MotionStyleDirective {
|
|
4395
|
+
motionStyle = input(null, ...ngDevMode ? [{ debugName: "motionStyle" }] : /* istanbul ignore next */ []);
|
|
4396
|
+
el = inject(ElementRef).nativeElement;
|
|
4397
|
+
transform = /* @__PURE__ */ new Map();
|
|
4398
|
+
constructor() {
|
|
4399
|
+
effect((onCleanup) => {
|
|
4400
|
+
const styles = this.motionStyle() ?? {};
|
|
4401
|
+
const stops = [];
|
|
4402
|
+
for (const [key, v] of Object.entries(styles)) if (isMotionSignal(v)) {
|
|
4403
|
+
this.write(key, v.get());
|
|
4404
|
+
stops.push(v.on((value) => this.write(key, value)));
|
|
4405
|
+
} else if (isSignal(v) || typeof v === "function") {
|
|
4406
|
+
const value = v();
|
|
4407
|
+
untracked(() => this.write(key, value));
|
|
4408
|
+
} else this.write(key, v);
|
|
4409
|
+
onCleanup(() => {
|
|
4410
|
+
stops.forEach((stop) => stop());
|
|
4411
|
+
for (const key of Object.keys(styles)) if (!(key in (untracked(this.motionStyle) ?? {}))) this.write(key, null);
|
|
4412
|
+
});
|
|
4413
|
+
});
|
|
4414
|
+
}
|
|
4415
|
+
write(key, value) {
|
|
4416
|
+
if (TRANSFORMS.includes(key)) {
|
|
4417
|
+
if (value == null) this.transform.delete(key);
|
|
4418
|
+
else this.transform.set(key, value);
|
|
4419
|
+
this.el.style.transform = this.transformString();
|
|
4420
|
+
return;
|
|
4421
|
+
}
|
|
4422
|
+
const css = value == null ? "" : typeof value === "number" && !UNITLESS.has(key) && !key.startsWith("--") ? `${value}px` : String(value);
|
|
4423
|
+
if (key.startsWith("--")) this.el.style.setProperty(key, css);
|
|
4424
|
+
else this.el.style[key] = css;
|
|
4425
|
+
}
|
|
4426
|
+
transformString() {
|
|
4427
|
+
let out = "";
|
|
4428
|
+
for (const key of TRANSFORMS) {
|
|
4429
|
+
const v = this.transform.get(key);
|
|
4430
|
+
if (v === void 0) continue;
|
|
4431
|
+
const unit = typeof v !== "number" ? "" : key === "x" || key === "y" || key === "z" ? "px" : key.startsWith("scale") ? "" : "deg";
|
|
4432
|
+
out += `${TRANSFORM_FN[key] ?? key}(${v}${unit}) `;
|
|
4433
|
+
}
|
|
4434
|
+
return out.trim();
|
|
4435
|
+
}
|
|
4436
|
+
static ɵfac = i0.ɵɵngDeclareFactory({
|
|
4437
|
+
minVersion: "12.0.0",
|
|
4438
|
+
version: "22.3.2",
|
|
4439
|
+
ngImport: i0,
|
|
4440
|
+
type: MotionStyleDirective,
|
|
4441
|
+
deps: [],
|
|
4442
|
+
target: i0.ɵɵFactoryTarget.Directive
|
|
4443
|
+
});
|
|
4444
|
+
static ɵdir = i0.ɵɵngDeclareDirective({
|
|
4445
|
+
minVersion: "17.1.0",
|
|
4446
|
+
version: "22.3.2",
|
|
4447
|
+
type: MotionStyleDirective,
|
|
4448
|
+
isStandalone: true,
|
|
4449
|
+
selector: "[motionStyle]",
|
|
4450
|
+
inputs: { motionStyle: {
|
|
4451
|
+
classPropertyName: "motionStyle",
|
|
4452
|
+
publicName: "motionStyle",
|
|
4453
|
+
isSignal: true,
|
|
4454
|
+
isRequired: false,
|
|
4455
|
+
transformFunction: null
|
|
4456
|
+
} },
|
|
4457
|
+
ngImport: i0
|
|
4458
|
+
});
|
|
4459
|
+
};
|
|
4460
|
+
i0.ɵɵngDeclareClassMetadata({
|
|
4461
|
+
minVersion: "12.0.0",
|
|
4462
|
+
version: "22.3.2",
|
|
4463
|
+
ngImport: i0,
|
|
4464
|
+
type: MotionStyleDirective,
|
|
4465
|
+
decorators: [{
|
|
4466
|
+
type: Directive,
|
|
4467
|
+
args: [{ selector: "[motionStyle]" }]
|
|
4468
|
+
}],
|
|
4469
|
+
ctorParameters: () => [],
|
|
4470
|
+
propDecorators: { motionStyle: [{
|
|
4471
|
+
type: i0.Input,
|
|
4472
|
+
args: [{
|
|
4473
|
+
isSignal: true,
|
|
4474
|
+
alias: "motionStyle",
|
|
4475
|
+
required: false
|
|
4476
|
+
}]
|
|
4477
|
+
}] }
|
|
4478
|
+
});
|
|
4479
|
+
export { MotionAnimationService, MotionDragDirective, MotionFollowPointerDirective, MotionIfDirective, MotionLayoutDirective, MotionLayoutRegistry, MotionMagneticDirective, MotionOneDirective, MotionOneService, MotionPanDirective, MotionPresenceDirective, MotionReorderGroupDirective, MotionReorderItemDirective, MotionScrollDirective, MotionStyleDirective, MotionTiltDirective, POINTER_SPRING, SCROLL_THROUGH, SCROLL_WHILE_VISIBLE, isMotionSignal, mapSignal, motionSignal, scrollSignal, springSignal, velocitySignal };
|
|
4090
4480
|
|
|
4091
4481
|
//# sourceMappingURL=hilux-ngx-motion.mjs.map
|