@hilux/ngx-motion 1.0.0 → 1.0.1

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.1",
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,43 @@
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
+ })
14
+ export class MotionHostDirective {
15
+ @ContentChildren(MotionDirective) motionElements!: QueryList<MotionDirective>;
16
+ @Output() exitComplete = new EventEmitter<void>();
17
+ @Input() mode: "sync" | "popLayout" | "wait" = "wait";
18
+ @Input() initial = true;
19
+
20
+ private animatingElements = new Set<MotionDirective>();
21
+
22
+ ngAfterContentInit() {
23
+ this.motionElements.forEach((motion) => {
24
+ motion.registerPresence(this);
25
+ });
26
+ }
27
+
28
+ startExitAnimation(motion: MotionDirective) {
29
+ this.animatingElements.add(motion);
30
+ }
31
+
32
+ completeExitAnimation(motion: MotionDirective) {
33
+ this.animatingElements.delete(motion);
34
+ if (this.animatingElements.size === 0) {
35
+ this.exitComplete.emit();
36
+ }
37
+ }
38
+
39
+ getLongestDuration(): number {
40
+ return Math.max(...this.motionElements.map((m) => m.getDuration()));
41
+ }
42
+ }
43
+
@@ -2,164 +2,96 @@ 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]',
21
- standalone: true,
18
+ selector: "[motion]",
22
19
  })
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;
20
+ export class MotionDirective implements OnDestroy {
21
+ @Input() initial: any = {};
22
+ @Input() animate: any = {};
23
+ @Input() exit: any = {};
24
+ @Input() duration = "0.3s";
25
+ @Input() easing = "ease";
26
+
27
+ @Output() animationComplete = new EventEmitter<void>();
28
+
36
29
  private player: AnimationPlayer | null = null;
37
- private mouseEnterListener: (() => void) | null = null;
38
- private mouseLeaveListener: (() => void) | null = null;
30
+ private presence: MotionHostDirective | null = null;
39
31
 
40
32
  constructor(private builder: AnimationBuilder, private el: ElementRef) {}
41
33
 
42
34
  ngOnInit() {
43
- this.setupHoverListeners();
44
- }
45
-
46
- ngAfterViewInit(): void {
47
- this.applyInitialStyles();
48
- if (this.variant === 'animate') {
49
- this.runAnimation();
35
+ if (this.presence && !this.presence.initial) {
36
+ this.runAnimation(this.animate);
37
+ } else {
38
+ this.runAnimation(this.initial);
50
39
  }
51
40
  }
52
41
 
53
- ngOnChanges(changes: SimpleChanges): void {
54
- if (changes['variant'] && !changes['variant'].firstChange) {
55
- this.runAnimation();
56
- }
57
- if (changes['onHover']) {
58
- this.setupHoverListeners();
42
+ ngOnDestroy() {
43
+ if (this.exit) {
44
+ this.runExitAnimation();
45
+ } else {
46
+ this.completeAnimation();
59
47
  }
60
48
  }
61
49
 
62
- ngOnDestroy() {
63
- this.removeHoverListeners();
50
+ registerPresence(presence: MotionHostDirective) {
51
+ this.presence = presence;
64
52
  }
65
53
 
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
- }
54
+ runAnimation(styles: any) {
55
+ const animation = this.builder.build([
56
+ style(this.initial),
57
+ animate(`${this.duration} ${this.easing}`, style(styles)),
58
+ ]);
82
59
 
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;
60
+ if (this.player) {
61
+ this.player.destroy();
97
62
  }
98
- }
99
63
 
100
- private onMouseEnter() {
101
- if (this.onHover) {
102
- this.isHovered = true;
103
- this.updateAnimation();
104
- }
64
+ this.player = animation.create(this.el.nativeElement);
65
+ this.player.play();
105
66
  }
106
67
 
107
- private onMouseLeave() {
108
- if (this.onHover) {
109
- this.isHovered = false;
110
- this.updateAnimation();
68
+ runExitAnimation() {
69
+ if (this.presence) {
70
+ this.presence.startExitAnimation(this);
111
71
  }
112
- }
113
72
 
114
- private applyInitialStyles() {
115
- Object.assign(this.el.nativeElement.style, this.initial);
116
- }
73
+ const animation = this.builder.build([
74
+ style(this.animate),
75
+ animate(`${this.duration} ${this.easing}`, style(this.exit)),
76
+ ]);
117
77
 
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
- ];
78
+ if (this.player) {
79
+ this.player.destroy();
136
80
  }
137
81
 
138
- if (animationMetadata.length > 0) {
139
- this.playAnimation(animationMetadata);
140
- this.currentVariant = this.variant;
141
- }
82
+ this.player = animation.create(this.el.nativeElement);
83
+ this.player.onDone(() => this.completeAnimation());
84
+ this.player.play();
142
85
  }
143
86
 
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);
87
+ private completeAnimation() {
88
+ this.animationComplete.emit();
89
+ if (this.presence) {
90
+ this.presence.completeExitAnimation(this);
154
91
  }
155
92
  }
156
93
 
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();
94
+ getDuration(): number {
95
+ return parseFloat(this.duration) * 1000; // Convert to milliseconds
164
96
  }
165
97
  }
@@ -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';