@hilux/ngx-motion 1.0.5 → 1.0.7

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hilux/ngx-motion",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "A library for Angular motion directives",
5
5
  "main": "bundles/ngx-motion.umd.js",
6
6
  "module": "fesm5/ngx-motion.js",
@@ -6,6 +6,8 @@ import {
6
6
  Output,
7
7
  EventEmitter,
8
8
  AfterViewInit,
9
+ OnChanges,
10
+ SimpleChanges
9
11
  } from "@angular/core";
10
12
  import {
11
13
  AnimationBuilder,
@@ -19,7 +21,7 @@ import { MotionHostDirective} from "./ngx-motion-container.directive";
19
21
  selector: "[motion]",
20
22
  standalone: true,
21
23
  })
22
- export class MotionDirective implements OnDestroy, AfterViewInit {
24
+ export class MotionDirective implements OnDestroy, AfterViewInit, OnChanges {
23
25
  @Input() initial: any = {};
24
26
  @Input() animate: any = {};
25
27
  @Input() exit: any = {};
@@ -36,8 +38,11 @@ export class MotionDirective implements OnDestroy, AfterViewInit {
36
38
  constructor(private builder: AnimationBuilder, private el: ElementRef) {}
37
39
 
38
40
  ngAfterViewInit() {
39
- this.applyInitialStyles();
40
- if (!this.variant) {
41
+ if (this.variant) {
42
+ const initialState = this.variant === 'animate' ? this.animate : this.initial;
43
+ Object.assign(this.el.nativeElement.style, initialState);
44
+ } else {
45
+ this.applyInitialStyles();
41
46
  this.runAnimation(this.animate);
42
47
  }
43
48
  this.setupHoverAnimations();
@@ -47,6 +52,27 @@ export class MotionDirective implements OnDestroy, AfterViewInit {
47
52
  this.runExitAnimation();
48
53
  }
49
54
 
55
+ ngOnChanges(changes: SimpleChanges) {
56
+ if (changes['variant'] && !changes['variant'].firstChange) {
57
+ const targetState = this.variant === 'animate' ? this.animate :
58
+ this.variant === 'exit' ? this.exit :
59
+ this.initial;
60
+
61
+ const startState = this.variant === 'exit' ? this.animate : this.initial;
62
+
63
+ const animation = this.builder.build([
64
+ style(startState),
65
+ animate(`${this.duration} ${this.easing}`, style(targetState))
66
+ ]);
67
+
68
+ if (this.player) {
69
+ this.player.destroy();
70
+ }
71
+ this.player = animation.create(this.el.nativeElement);
72
+ this.player.play();
73
+ }
74
+ }
75
+
50
76
  registerPresence(presence: MotionHostDirective) {
51
77
  this.presence = presence;
52
78
  }
@@ -58,7 +84,7 @@ export class MotionDirective implements OnDestroy, AfterViewInit {
58
84
  this.variant === 'exit' ? this.exit : styles;
59
85
 
60
86
  animationMetadata = [
61
- style(this.variant === 'initial' ? {} : this.initial),
87
+ style(this.variant === 'exit' ? this.animate : this.initial),
62
88
  animate(`${this.duration} ${this.easing}`, style(currentStyles))
63
89
  ];
64
90
 
@@ -107,10 +133,43 @@ export class MotionDirective implements OnDestroy, AfterViewInit {
107
133
  private setupHoverAnimations() {
108
134
  if (this.onHover) {
109
135
  this.el.nativeElement.addEventListener('mouseenter', () => {
110
- this.runAnimation(this.onHover);
136
+ const currentStyles = getComputedStyle(this.el.nativeElement);
137
+ const currentState: { [key: string]: any } = {};
138
+
139
+ Object.keys(this.onHover).forEach((key: string) => {
140
+ currentState[key] = currentStyles[key as keyof CSSStyleDeclaration];
141
+ });
142
+
143
+ const animation = this.builder.build([
144
+ style(currentState),
145
+ animate(`${this.duration} ${this.easing}`, style(this.onHover))
146
+ ]);
147
+
148
+ if (this.player) {
149
+ this.player.destroy();
150
+ }
151
+ this.player = animation.create(this.el.nativeElement);
152
+ this.player.play();
111
153
  });
154
+
112
155
  this.el.nativeElement.addEventListener('mouseleave', () => {
113
- this.runAnimation(this.animate);
156
+ const currentStyles = getComputedStyle(this.el.nativeElement);
157
+ const currentState: { [key: string]: any } = {};
158
+
159
+ Object.keys(this.onHover).forEach((key: string) => {
160
+ currentState[key] = currentStyles[key as keyof CSSStyleDeclaration];
161
+ });
162
+
163
+ const animation = this.builder.build([
164
+ style(currentState),
165
+ animate(`${this.duration} ${this.easing}`, style(this.animate || this.initial))
166
+ ]);
167
+
168
+ if (this.player) {
169
+ this.player.destroy();
170
+ }
171
+ this.player = animation.create(this.el.nativeElement);
172
+ this.player.play();
114
173
  });
115
174
  }
116
175
  }