@hilux/ngx-motion 1.0.0

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/README.md ADDED
@@ -0,0 +1,24 @@
1
+ # NgxMotion
2
+
3
+ This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 15.1.0.
4
+
5
+ ## Code scaffolding
6
+
7
+ Run `ng generate component component-name --project ngx-motion` to generate a new component. You can also use `ng generate directive|pipe|service|class|guard|interface|enum|module --project ngx-motion`.
8
+ > Note: Don't forget to add `--project ngx-motion` or else it will be added to the default project in your `angular.json` file.
9
+
10
+ ## Build
11
+
12
+ Run `ng build ngx-motion` to build the project. The build artifacts will be stored in the `dist/` directory.
13
+
14
+ ## Publishing
15
+
16
+ After building your library with `ng build ngx-motion`, go to the dist folder `cd dist/ngx-motion` and run `npm publish`.
17
+
18
+ ## Running unit tests
19
+
20
+ Run `ng test ngx-motion` to execute the unit tests via [Karma](https://karma-runner.github.io).
21
+
22
+ ## Further help
23
+
24
+ To get more help on the Angular CLI use `ng help` or go check out the [Angular CLI Overview and Command Reference](https://angular.io/cli) page.
@@ -0,0 +1,7 @@
1
+ {
2
+ "$schema": "../../node_modules/ng-packagr/ng-package.schema.json",
3
+ "dest": "../../dist/ngx-motion",
4
+ "lib": {
5
+ "entryFile": "src/public-api.ts"
6
+ }
7
+ }
package/package.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "name": "@hilux/ngx-motion",
3
+ "version": "1.0.0",
4
+ "description": "A library for Angular motion directives",
5
+ "main": "bundles/ngx-motion.umd.js",
6
+ "module": "fesm5/ngx-motion.js",
7
+ "types": "ngx-motion.d.ts",
8
+ "peerDependencies": {
9
+ "@angular/core":"19.1.5",
10
+ "@angular/common":"19.1.5"
11
+ },
12
+ "scripts": {
13
+ "build": "ng build ngx-motion"
14
+ },
15
+ "author": "Riki Sommers <riki.sommers@yahoo.co.nz>"
16
+ }
@@ -0,0 +1,165 @@
1
+ import {
2
+ Directive,
3
+ Input,
4
+ ElementRef,
5
+ OnChanges,
6
+ SimpleChanges,
7
+ AfterViewInit,
8
+ OnInit,
9
+ OnDestroy,
10
+ } from '@angular/core';
11
+ import {
12
+ AnimationBuilder,
13
+ animate,
14
+ style,
15
+ AnimationMetadata,
16
+ AnimationPlayer,
17
+ } from '@angular/animations';
18
+
19
+ @Directive({
20
+ selector: '[motion]',
21
+ standalone: true,
22
+ })
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;
36
+ private player: AnimationPlayer | null = null;
37
+ private mouseEnterListener: (() => void) | null = null;
38
+ private mouseLeaveListener: (() => void) | null = null;
39
+
40
+ constructor(private builder: AnimationBuilder, private el: ElementRef) {}
41
+
42
+ ngOnInit() {
43
+ this.setupHoverListeners();
44
+ }
45
+
46
+ ngAfterViewInit(): void {
47
+ this.applyInitialStyles();
48
+ if (this.variant === 'animate') {
49
+ this.runAnimation();
50
+ }
51
+ }
52
+
53
+ ngOnChanges(changes: SimpleChanges): void {
54
+ if (changes['variant'] && !changes['variant'].firstChange) {
55
+ this.runAnimation();
56
+ }
57
+ if (changes['onHover']) {
58
+ this.setupHoverListeners();
59
+ }
60
+ }
61
+
62
+ ngOnDestroy() {
63
+ this.removeHoverListeners();
64
+ }
65
+
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
+ }
82
+
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;
97
+ }
98
+ }
99
+
100
+ private onMouseEnter() {
101
+ if (this.onHover) {
102
+ this.isHovered = true;
103
+ this.updateAnimation();
104
+ }
105
+ }
106
+
107
+ private onMouseLeave() {
108
+ if (this.onHover) {
109
+ this.isHovered = false;
110
+ this.updateAnimation();
111
+ }
112
+ }
113
+
114
+ private applyInitialStyles() {
115
+ Object.assign(this.el.nativeElement.style, this.initial);
116
+ }
117
+
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
+ ];
136
+ }
137
+
138
+ if (animationMetadata.length > 0) {
139
+ this.playAnimation(animationMetadata);
140
+ this.currentVariant = this.variant;
141
+ }
142
+ }
143
+
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);
154
+ }
155
+ }
156
+
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();
164
+ }
165
+ }
@@ -0,0 +1,14 @@
1
+ import { NgModule } from '@angular/core';
2
+ import { NgxMotionDirective } from './ngx-motion.directive';
3
+
4
+ @NgModule({
5
+ declarations: [
6
+ NgxMotionDirective
7
+ ],
8
+ imports: [
9
+ ],
10
+ exports: [
11
+ NgxMotionDirective
12
+ ]
13
+ })
14
+ export class NgxMotionModule { }
@@ -0,0 +1 @@
1
+ export * from './lib/ngx-motion.directive';
@@ -0,0 +1,14 @@
1
+ /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+ {
3
+ "extends": "../../tsconfig.json",
4
+ "compilerOptions": {
5
+ "outDir": "../../out-tsc/lib",
6
+ "declaration": true,
7
+ "declarationMap": true,
8
+ "inlineSources": true,
9
+ "types": []
10
+ },
11
+ "exclude": [
12
+ "**/*.spec.ts"
13
+ ]
14
+ }
@@ -0,0 +1,10 @@
1
+ /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+ {
3
+ "extends": "./tsconfig.lib.json",
4
+ "compilerOptions": {
5
+ "declarationMap": false
6
+ },
7
+ "angularCompilerOptions": {
8
+ "compilationMode": "partial"
9
+ }
10
+ }
@@ -0,0 +1,14 @@
1
+ /* To learn more about this file see: https://angular.io/config/tsconfig. */
2
+ {
3
+ "extends": "../../tsconfig.json",
4
+ "compilerOptions": {
5
+ "outDir": "../../out-tsc/spec",
6
+ "types": [
7
+ "jasmine"
8
+ ]
9
+ },
10
+ "include": [
11
+ "**/*.spec.ts",
12
+ "**/*.d.ts"
13
+ ]
14
+ }