@hilux/ngx-motion 1.0.4 → 1.0.6

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.4",
3
+ "version": "1.0.6",
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,50 +38,55 @@ export class MotionDirective implements OnDestroy, AfterViewInit {
36
38
  constructor(private builder: AnimationBuilder, private el: ElementRef) {}
37
39
 
38
40
  ngAfterViewInit() {
39
- this.applyInitialStyles();
40
- this.runAnimation(this.animate);
41
- if (this.onHover) {
42
- this.runAnimation(this.onHover);
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();
46
+ this.runAnimation(this.animate);
43
47
  }
48
+ this.setupHoverAnimations();
44
49
  }
45
50
 
46
51
  ngOnDestroy() {
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
  }
53
79
 
54
80
  runAnimation(styles: any) {
55
81
  let animationMetadata = [];
82
+ const currentStyles = this.variant === 'initial' ? this.initial :
83
+ this.variant === 'animate' ? this.animate :
84
+ this.variant === 'exit' ? this.exit : styles;
56
85
 
57
- switch (this.variant) {
58
- case 'initial':
59
- animationMetadata = [
60
- style(this.initial),
61
- animate(`${this.duration} ${this.easing}`, style(this.animate)),
62
- ];
63
- break;
64
- case 'animate':
65
- animationMetadata = [
66
- style(this.initial),
67
- animate(`${this.duration} ${this.easing}`, style(this.animate)),
68
- ];
69
- break;
70
- case 'exit':
71
- animationMetadata = [
72
- style(this.animate),
73
- animate(`${this.duration} ${this.easing}`, style(this.exit)),
74
- ];
75
- break;
76
- default:
77
- animationMetadata = [
78
- style(this.initial),
79
- animate(`${this.duration} ${this.easing}`, style(this.animate)),
80
- ];
81
- break;
82
- }
86
+ animationMetadata = [
87
+ style(this.variant === 'exit' ? this.animate : this.initial),
88
+ animate(`${this.duration} ${this.easing}`, style(currentStyles))
89
+ ];
83
90
 
84
91
  const animation = this.builder.build(animationMetadata);
85
92
  if (this.player) {
@@ -122,4 +129,15 @@ export class MotionDirective implements OnDestroy, AfterViewInit {
122
129
  private applyInitialStyles() {
123
130
  Object.assign(this.el.nativeElement.style, this.initial);
124
131
  }
132
+
133
+ private setupHoverAnimations() {
134
+ if (this.onHover) {
135
+ this.el.nativeElement.addEventListener('mouseenter', () => {
136
+ this.runAnimation(this.onHover);
137
+ });
138
+ this.el.nativeElement.addEventListener('mouseleave', () => {
139
+ this.runAnimation(this.animate);
140
+ });
141
+ }
142
+ }
125
143
  }