@hilux/ngx-motion 1.0.3 → 1.0.5

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.3",
3
+ "version": "1.0.5",
4
4
  "description": "A library for Angular motion directives",
5
5
  "main": "bundles/ngx-motion.umd.js",
6
6
  "module": "fesm5/ngx-motion.js",
@@ -5,6 +5,7 @@ import {
5
5
  OnDestroy,
6
6
  Output,
7
7
  EventEmitter,
8
+ AfterViewInit,
8
9
  } from "@angular/core";
9
10
  import {
10
11
  AnimationBuilder,
@@ -18,7 +19,7 @@ import { MotionHostDirective} from "./ngx-motion-container.directive";
18
19
  selector: "[motion]",
19
20
  standalone: true,
20
21
  })
21
- export class MotionDirective implements OnDestroy {
22
+ export class MotionDirective implements OnDestroy, AfterViewInit {
22
23
  @Input() initial: any = {};
23
24
  @Input() animate: any = {};
24
25
  @Input() exit: any = {};
@@ -34,23 +35,16 @@ export class MotionDirective implements OnDestroy {
34
35
 
35
36
  constructor(private builder: AnimationBuilder, private el: ElementRef) {}
36
37
 
37
- ngOnInit() {
38
- if (this.presence && !this.presence.initial) {
38
+ ngAfterViewInit() {
39
+ this.applyInitialStyles();
40
+ if (!this.variant) {
39
41
  this.runAnimation(this.animate);
40
- } else {
41
- this.runAnimation(this.initial);
42
- }
43
- if (this.onHover) {
44
- this.runAnimation(this.onHover);
45
42
  }
43
+ this.setupHoverAnimations();
46
44
  }
47
45
 
48
46
  ngOnDestroy() {
49
- if (this.exit) {
50
- this.runExitAnimation();
51
- } else {
52
- this.completeAnimation();
53
- }
47
+ this.runExitAnimation();
54
48
  }
55
49
 
56
50
  registerPresence(presence: MotionHostDirective) {
@@ -58,19 +52,20 @@ export class MotionDirective implements OnDestroy {
58
52
  }
59
53
 
60
54
  runAnimation(styles: any) {
61
- const animation = this.builder.build([
62
- style(this.initial),
63
- animate(`${this.duration} ${this.easing}`, style(styles)),
64
- ]);
55
+ let animationMetadata = [];
56
+ const currentStyles = this.variant === 'initial' ? this.initial :
57
+ this.variant === 'animate' ? this.animate :
58
+ this.variant === 'exit' ? this.exit : styles;
65
59
 
66
- if (this.variant) {
67
- styles = { ...styles, ...this.getVariantStyles(this.variant) };
68
- }
60
+ animationMetadata = [
61
+ style(this.variant === 'initial' ? {} : this.initial),
62
+ animate(`${this.duration} ${this.easing}`, style(currentStyles))
63
+ ];
69
64
 
65
+ const animation = this.builder.build(animationMetadata);
70
66
  if (this.player) {
71
67
  this.player.destroy();
72
68
  }
73
-
74
69
  this.player = animation.create(this.el.nativeElement);
75
70
  this.player.play();
76
71
  }
@@ -105,14 +100,18 @@ export class MotionDirective implements OnDestroy {
105
100
  return parseFloat(this.duration) * 1000; // Convert to milliseconds
106
101
  }
107
102
 
108
- private getVariantStyles(variant: string): any {
109
- switch (variant) {
110
- case 'fade':
111
- return { opacity: 0 };
112
- case 'scale':
113
- return { transform: 'scale(1.2)' };
114
- default:
115
- return {};
103
+ private applyInitialStyles() {
104
+ Object.assign(this.el.nativeElement.style, this.initial);
105
+ }
106
+
107
+ private setupHoverAnimations() {
108
+ if (this.onHover) {
109
+ this.el.nativeElement.addEventListener('mouseenter', () => {
110
+ this.runAnimation(this.onHover);
111
+ });
112
+ this.el.nativeElement.addEventListener('mouseleave', () => {
113
+ this.runAnimation(this.animate);
114
+ });
116
115
  }
117
116
  }
118
117
  }