@hilux/ngx-motion 1.0.0 → 1.0.2

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.0",
3
+ "version": "1.0.2",
4
4
  "description": "A library for Angular motion directives",
5
5
  "main": "bundles/ngx-motion.umd.js",
6
6
  "module": "fesm5/ngx-motion.js",
@@ -0,0 +1,44 @@
1
+ import {
2
+ Directive,
3
+ ContentChildren,
4
+ QueryList,
5
+ Output,
6
+ EventEmitter,
7
+ Input,
8
+ } from "@angular/core";
9
+ import { MotionDirective } from "./ngx-motion.directive";
10
+
11
+ @Directive({
12
+ selector: "[animatePresence]",
13
+ standalone: true,
14
+ })
15
+ export class MotionHostDirective {
16
+ @ContentChildren(MotionDirective) motionElements!: QueryList<MotionDirective>;
17
+ @Output() exitComplete = new EventEmitter<void>();
18
+ @Input() mode: "sync" | "popLayout" | "wait" = "wait";
19
+ @Input() initial = true;
20
+
21
+ private animatingElements = new Set<MotionDirective>();
22
+
23
+ ngAfterContentInit() {
24
+ this.motionElements.forEach((motion) => {
25
+ motion.registerPresence(this);
26
+ });
27
+ }
28
+
29
+ startExitAnimation(motion: MotionDirective) {
30
+ this.animatingElements.add(motion);
31
+ }
32
+
33
+ completeExitAnimation(motion: MotionDirective) {
34
+ this.animatingElements.delete(motion);
35
+ if (this.animatingElements.size === 0) {
36
+ this.exitComplete.emit();
37
+ }
38
+ }
39
+
40
+ getLongestDuration(): number {
41
+ return Math.max(...this.motionElements.map((m) => m.getDuration()));
42
+ }
43
+ }
44
+
@@ -2,164 +2,97 @@ import {
2
2
  Directive,
3
3
  Input,
4
4
  ElementRef,
5
- OnChanges,
6
- SimpleChanges,
7
- AfterViewInit,
8
- OnInit,
9
5
  OnDestroy,
10
- } from '@angular/core';
6
+ Output,
7
+ EventEmitter,
8
+ } from "@angular/core";
11
9
  import {
12
10
  AnimationBuilder,
13
- animate,
14
11
  style,
15
- AnimationMetadata,
12
+ animate,
16
13
  AnimationPlayer,
17
- } from '@angular/animations';
14
+ } from "@angular/animations";
15
+ import { MotionHostDirective} from "./ngx-motion-container.directive";
18
16
 
19
17
  @Directive({
20
- selector: '[motion]',
18
+ selector: "[motion]",
21
19
  standalone: true,
22
20
  })
23
- export class NgxMotionDirective
24
- implements OnInit, OnChanges, AfterViewInit, OnDestroy
25
- {
26
- @Input() initial: { [key: string]: any } = {};
27
- @Input() animate: { [key: string]: any } = {};
28
- @Input() exit: { [key: string]: any } = {};
29
- @Input() onHover: { [key: string]: any } | undefined;
30
- @Input() variant: 'initial' | 'animate' | 'exit' = 'initial';
31
- @Input() duration: string = '0.5s';
32
- @Input() easing: string = 'ease-out';
33
-
34
- private currentVariant: 'initial' | 'animate' | 'exit' = 'initial';
35
- private isHovered: boolean = false;
21
+ export class MotionDirective implements OnDestroy {
22
+ @Input() initial: any = {};
23
+ @Input() animate: any = {};
24
+ @Input() exit: any = {};
25
+ @Input() duration = "0.3s";
26
+ @Input() easing = "ease";
27
+
28
+ @Output() animationComplete = new EventEmitter<void>();
29
+
36
30
  private player: AnimationPlayer | null = null;
37
- private mouseEnterListener: (() => void) | null = null;
38
- private mouseLeaveListener: (() => void) | null = null;
31
+ private presence: MotionHostDirective | null = null;
39
32
 
40
33
  constructor(private builder: AnimationBuilder, private el: ElementRef) {}
41
34
 
42
35
  ngOnInit() {
43
- this.setupHoverListeners();
44
- }
45
-
46
- ngAfterViewInit(): void {
47
- this.applyInitialStyles();
48
- if (this.variant === 'animate') {
49
- this.runAnimation();
36
+ if (this.presence && !this.presence.initial) {
37
+ this.runAnimation(this.animate);
38
+ } else {
39
+ this.runAnimation(this.initial);
50
40
  }
51
41
  }
52
42
 
53
- ngOnChanges(changes: SimpleChanges): void {
54
- if (changes['variant'] && !changes['variant'].firstChange) {
55
- this.runAnimation();
56
- }
57
- if (changes['onHover']) {
58
- this.setupHoverListeners();
43
+ ngOnDestroy() {
44
+ if (this.exit) {
45
+ this.runExitAnimation();
46
+ } else {
47
+ this.completeAnimation();
59
48
  }
60
49
  }
61
50
 
62
- ngOnDestroy() {
63
- this.removeHoverListeners();
51
+ registerPresence(presence: MotionHostDirective) {
52
+ this.presence = presence;
64
53
  }
65
54
 
66
- private setupHoverListeners() {
67
- this.removeHoverListeners();
68
-
69
- if (this.onHover && Object.keys(this.onHover).length > 0) {
70
- this.mouseEnterListener = () => this.onMouseEnter();
71
- this.mouseLeaveListener = () => this.onMouseLeave();
72
- this.el.nativeElement.addEventListener(
73
- 'mouseenter',
74
- this.mouseEnterListener
75
- );
76
- this.el.nativeElement.addEventListener(
77
- 'mouseleave',
78
- this.mouseLeaveListener
79
- );
80
- }
81
- }
55
+ runAnimation(styles: any) {
56
+ const animation = this.builder.build([
57
+ style(this.initial),
58
+ animate(`${this.duration} ${this.easing}`, style(styles)),
59
+ ]);
82
60
 
83
- private removeHoverListeners() {
84
- if (this.mouseEnterListener) {
85
- this.el.nativeElement.removeEventListener(
86
- 'mouseenter',
87
- this.mouseEnterListener
88
- );
89
- this.mouseEnterListener = null;
90
- }
91
- if (this.mouseLeaveListener) {
92
- this.el.nativeElement.removeEventListener(
93
- 'mouseleave',
94
- this.mouseLeaveListener
95
- );
96
- this.mouseLeaveListener = null;
61
+ if (this.player) {
62
+ this.player.destroy();
97
63
  }
98
- }
99
64
 
100
- private onMouseEnter() {
101
- if (this.onHover) {
102
- this.isHovered = true;
103
- this.updateAnimation();
104
- }
65
+ this.player = animation.create(this.el.nativeElement);
66
+ this.player.play();
105
67
  }
106
68
 
107
- private onMouseLeave() {
108
- if (this.onHover) {
109
- this.isHovered = false;
110
- this.updateAnimation();
69
+ runExitAnimation() {
70
+ if (this.presence) {
71
+ this.presence.startExitAnimation(this);
111
72
  }
112
- }
113
73
 
114
- private applyInitialStyles() {
115
- Object.assign(this.el.nativeElement.style, this.initial);
116
- }
74
+ const animation = this.builder.build([
75
+ style(this.animate),
76
+ animate(`${this.duration} ${this.easing}`, style(this.exit)),
77
+ ]);
117
78
 
118
- private runAnimation(): void {
119
- let animationMetadata: AnimationMetadata[] = [];
120
-
121
- if (this.variant === 'initial' && this.currentVariant !== 'initial') {
122
- animationMetadata = [
123
- style(this.animate),
124
- animate(`${this.duration} ${this.easing}`, style(this.initial)),
125
- ];
126
- } else if (this.variant === 'animate') {
127
- animationMetadata = [
128
- style(this.initial),
129
- animate(`${this.duration} ${this.easing}`, style(this.animate)),
130
- ];
131
- } else if (this.variant === 'exit') {
132
- animationMetadata = [
133
- style(this.animate),
134
- animate(`${this.duration} ${this.easing}`, style(this.exit)),
135
- ];
79
+ if (this.player) {
80
+ this.player.destroy();
136
81
  }
137
82
 
138
- if (animationMetadata.length > 0) {
139
- this.playAnimation(animationMetadata);
140
- this.currentVariant = this.variant;
141
- }
83
+ this.player = animation.create(this.el.nativeElement);
84
+ this.player.onDone(() => this.completeAnimation());
85
+ this.player.play();
142
86
  }
143
87
 
144
- private updateAnimation() {
145
- if (this.onHover) {
146
- let targetStyles = this.isHovered
147
- ? { ...this.animate, ...this.onHover }
148
- : this.animate;
149
- const animationMetadata: AnimationMetadata[] = [
150
- style(this.el.nativeElement.style),
151
- animate(`${this.duration} ${this.easing}`, style(targetStyles)),
152
- ];
153
- this.playAnimation(animationMetadata);
88
+ private completeAnimation() {
89
+ this.animationComplete.emit();
90
+ if (this.presence) {
91
+ this.presence.completeExitAnimation(this);
154
92
  }
155
93
  }
156
94
 
157
- private playAnimation(metadata: AnimationMetadata[]): void {
158
- if (this.player) {
159
- this.player.destroy();
160
- }
161
- const factory = this.builder.build(metadata);
162
- this.player = factory.create(this.el.nativeElement);
163
- this.player.play();
95
+ getDuration(): number {
96
+ return parseFloat(this.duration) * 1000; // Convert to milliseconds
164
97
  }
165
98
  }
@@ -1,14 +1,16 @@
1
1
  import { NgModule } from '@angular/core';
2
- import { NgxMotionDirective } from './ngx-motion.directive';
3
-
2
+ import { MotionDirective } from './ngx-motion.directive';
3
+ import { MotionHostDirective } from './ngx-motion-container.directive';
4
4
  @NgModule({
5
5
  declarations: [
6
- NgxMotionDirective
6
+ MotionDirective,
7
+ MotionHostDirective
7
8
  ],
8
9
  imports: [
9
10
  ],
10
11
  exports: [
11
- NgxMotionDirective
12
+ MotionDirective,
13
+ MotionHostDirective
12
14
  ]
13
15
  })
14
16
  export class NgxMotionModule { }
package/src/public-api.ts CHANGED
@@ -1 +1,2 @@
1
- export * from './lib/ngx-motion.directive';
1
+ export * from './lib/ngx-motion.directive';
2
+ export * from './lib/ngx-motion-container.directive';