@hilux/ngx-motion 1.0.14 → 1.0.15

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.
@@ -1,126 +0,0 @@
1
- import {
2
- Injectable,
3
- Inject,
4
- signal,
5
- } from '@angular/core';
6
- import { DOCUMENT } from '@angular/common';
7
- import { MotionDirective } from './ngx-motion.directive';
8
- import { fromEvent, Subscription } from 'rxjs';
9
- import { throttleTime, debounceTime } from 'rxjs/operators';
10
- import { Router } from '@angular/router';
11
-
12
- @Injectable({
13
- providedIn: 'root',
14
- })
15
- export class MotionService {
16
- private scrollSubscription: Subscription;
17
- private motionElements: MotionDirective[] = [];
18
- scrollYProgress = signal(0);
19
- totalExitDuration: number = 0;
20
-
21
- constructor(
22
- private router: Router,
23
- @Inject(DOCUMENT) private document: Document,
24
- ) {
25
- this.setupScrollListener();
26
- this.scrollSubscription = new Subscription();
27
- }
28
-
29
- ngOnDestroy() {
30
- if (this.scrollSubscription) {
31
- this.scrollSubscription.unsubscribe();
32
- }
33
- }
34
-
35
- private updateTotalExitDuration() {
36
- this.totalExitDuration = this.motionElements.reduce((total, element) => {
37
- return total + element.getDuration();
38
- }, 0);
39
- }
40
-
41
- private setupScrollListener() {
42
- if (typeof window !== 'undefined') {
43
- this.scrollSubscription = fromEvent(window, 'scroll')
44
- .pipe(
45
- throttleTime(16), // Limit to about 60fps
46
- debounceTime(1), // Wait for scroll to settle
47
- )
48
- .subscribe(() => {
49
- this.updateScrollProgress();
50
- });
51
- this.updateScrollProgress();
52
- }
53
- }
54
-
55
- private updateScrollProgress() {
56
- const doc = this.document.documentElement;
57
- const scrollTop = window.scrollY || doc.scrollTop;
58
- const scrollHeight = doc.scrollHeight - doc.clientHeight;
59
- const progress =
60
- scrollHeight > 0 ? Math.round((scrollTop / scrollHeight) * 100) : 0;
61
- this.scrollYProgress.set(progress);
62
- }
63
-
64
- registerMotionElement(element: MotionDirective) {
65
- this.motionElements.push(element);
66
- this.updateTotalExitDuration();
67
- }
68
-
69
- unregisterMotionElement(element: MotionDirective) {
70
- this.motionElements = this.motionElements.filter((e) => e !== element);
71
- this.updateTotalExitDuration();
72
- }
73
-
74
- getMotionElements(): MotionDirective[] {
75
- return this.motionElements;
76
- }
77
-
78
- getAllElementsByRoute(route: string): MotionDirective[] {
79
- return this.motionElements;
80
- }
81
-
82
- cancelAllAnimations() {
83
- this.motionElements.forEach((motion) => {
84
- motion.cancel();
85
- });
86
- }
87
-
88
- runAllEnterAnimations(): void {
89
- const enterAnimations = this.motionElements.map((motion) => {
90
- // motion.runInitAnimation();
91
- });
92
- }
93
-
94
- runAllExitAnimations(): void {
95
- // Proceed with the exit animations
96
- this.motionElements.map((motion) => {
97
- //motion.runExitAnimation();
98
- });
99
- }
100
-
101
- runAllEnterAnimationsForRoute(route: string): MotionDirective[] {
102
- const enterAnimations = this.getAllElementsByRoute(route).map((motion) => {
103
- motion.runInitAnimation();
104
- return motion;
105
- });
106
- return enterAnimations;
107
- }
108
-
109
- getLongestExitDuration(): number {
110
- const durations = this.motionElements.map((motion) => motion.getDuration());
111
- return durations.length > 0 ? Math.max(...durations) : 0;
112
- }
113
-
114
- getLongestEnterDuration(): number {
115
- const durations = this.motionElements.map((motion) => motion.getDuration());
116
- return durations.length > 0 ? Math.max(...durations) : 0;
117
- }
118
-
119
- getLongestDurationForRoute(): number {
120
- const route = this.router.url;
121
- const elements = this.getAllElementsByRoute(route);
122
- const durations = elements.map((motion) => motion.getDuration());
123
- console.log('durations', durations.length > 0 ? Math.max(...durations) : 0);
124
- return durations.length > 0 ? Math.max(...durations) : 0;
125
- }
126
- }
@@ -1,311 +0,0 @@
1
- import {
2
- Directive,
3
- Input,
4
- ElementRef,
5
- OnDestroy,
6
- Output,
7
- EventEmitter,
8
- OnChanges,
9
- SimpleChanges,
10
- HostListener,
11
- } from '@angular/core';
12
- import {
13
- AnimationBuilder,
14
- style,
15
- animate,
16
- AnimationPlayer,
17
- AnimationMetadata,
18
- } from '@angular/animations';
19
- import { MotionService } from './services/ngx-motion.service';
20
-
21
- let uniqueIdCounter = 0;
22
-
23
- @Directive({
24
- selector: '[motion]'
25
- })
26
-
27
- export class MotionDirective implements OnDestroy, OnChanges {
28
- @Input() initial: any = {};
29
- @Input() animate: any = {};
30
- @Input() exit: any = {};
31
- @Input() inView: any = {};
32
- @Input() duration = '0.3s';
33
- @Input() delay = '0s';
34
- @Input() repeat = false;
35
- @Input() exitDelay = '0s';
36
- @Input() easing = 'ease';
37
- @Input() offset = '0px';
38
- @Input() whileHover: any = {};
39
- @Input() staggerChildren = '0s';
40
- @Input() whileTap: any = {};
41
- @Input() whileFocus: any = {};
42
- @Input() whileInView: any = {};
43
- @Input() variants: any = {};
44
- @Output() animationComplete = new EventEmitter<void>();
45
-
46
- private player: AnimationPlayer | null = null;
47
- private elementId: string;
48
- private hoverTimeout: any;
49
- private observer?: IntersectionObserver;
50
- private routeDelay: number = 0;
51
-
52
- get scrollProgress() {
53
- return this.motionService.scrollYProgress();
54
- }
55
-
56
- constructor(
57
- private builder: AnimationBuilder,
58
- private el: ElementRef,
59
- private motionService: MotionService,
60
-
61
- ) {
62
-
63
- this.elementId = `motion-${uniqueIdCounter++}`;
64
- this.el.nativeElement.setAttribute('id', this.elementId);
65
-
66
- this.motionService.registerMotionElement(this);
67
- this.applyInitialStyles();
68
- this.routeDelay = this.motionService.getLongestExitDuration();
69
- }
70
-
71
-
72
- ngOnInit() {
73
- if (Object.keys(this.inView).length > 0) {
74
- this.runInitAnimationsWhenInViewport(this, this.offset, this.repeat);
75
- }
76
- setTimeout(() => {
77
- this.playAnimation(this.initial, this.animate);
78
- }, this.routeDelay);
79
- }
80
-
81
- ngOnDestroy() {
82
- if (this.exit && Object.keys(this.exit).length > 0) {
83
- this.runExitAnimation();
84
- }
85
-
86
- //TODO: Add options to select reset behaviour , e.g either reset tto inintial or allow anim to run
87
- // this.resetPlayer(); // Ensure the animation is properly cleaned up
88
- // this.reset();
89
-
90
- if (this.observer) {
91
- this.observer.disconnect();
92
- }
93
- }
94
-
95
-
96
- ngOnChanges(changes: SimpleChanges) {
97
- if (changes['animate'] && changes['animate'].previousValue !== undefined) {
98
- const oldState = changes['animate'].previousValue;
99
- let newState = changes['animate'].currentValue;
100
-
101
- if (typeof newState === 'object') {
102
- this.playAnimation(oldState, newState);
103
- } else if (typeof newState === 'string' && this.variants?.[newState]) {
104
- newState = this.variants[newState];
105
- this.playAnimation(oldState, newState);
106
- }
107
- }
108
- }
109
-
110
-
111
- @HostListener('mouseenter')
112
- onMouseEnter() {
113
- if (Object.keys(this.whileHover).length > 0) {
114
- clearTimeout(this.hoverTimeout);
115
- if (Object.keys(this.animate).length > 0) {
116
- this.playAnimation(this.animate, this.whileHover);
117
- } else {
118
- this.playAnimation(this.initial, this.whileHover);
119
- }
120
- }
121
- }
122
-
123
- @HostListener('mouseleave')
124
- onMouseLeave() {
125
- if (Object.keys(this.whileHover).length > 0) {
126
- this.hoverTimeout = setTimeout(() => {
127
- if (Object.keys(this.animate).length > 0) {
128
- this.playAnimation(this.whileHover, this.animate);
129
- } else {
130
- this.playAnimation(this.whileHover, this.initial);
131
- }
132
- }, 100);
133
- }
134
- }
135
-
136
- @HostListener('focus')
137
- @HostListener('mousedown', ['$event'])
138
- @HostListener('touchstart', ['$event'])
139
- onPress(event: Event) {
140
- if (Object.keys(this.whileTap).length > 0) {
141
- if (Object.keys(this.animate).length > 0) {
142
- this.playAnimation(this.animate, this.whileTap);
143
- } else {
144
- this.playAnimation(this.initial, this.whileTap);
145
- }
146
- const endEvent = event.type === 'mousedown' ? 'mouseup' : 'touchend';
147
- const endListener = () => {
148
- if (Object.keys(this.animate).length > 0) {
149
- this.playAnimation(this.whileTap, this.animate);
150
- } else {
151
- this.playAnimation(this.whileTap, this.initial);
152
- }
153
-
154
- window.removeEventListener(endEvent, endListener);
155
- };
156
-
157
- window.addEventListener(endEvent, endListener);
158
- }
159
- }
160
-
161
- @HostListener('focus')
162
- onFocus() {
163
- if (Object.keys(this.whileFocus).length > 0) {
164
- if (Object.keys(this.animate).length > 0) {
165
- this.playAnimation(this.animate, this.whileFocus);
166
- } else {
167
- this.playAnimation(this.initial, this.whileFocus);
168
- }
169
- }
170
- }
171
-
172
- @HostListener('blur')
173
- onBlur() {
174
- if (Object.keys(this.whileFocus).length > 0) {
175
- if (Object.keys(this.animate).length > 0) {
176
- this.playAnimation(this.whileFocus, this.animate);
177
- } else {
178
- this.playAnimation(this.whileFocus, this.initial);
179
- }
180
- }
181
- }
182
-
183
- applyInitialValues() {
184
- Object.assign(this.el.nativeElement.style, this.initial);
185
- }
186
-
187
- private resetPlayer() {
188
- if (this.player) {
189
- this.player.finish();
190
- this.player.destroy();
191
- }
192
- }
193
-
194
- private reset() {
195
- this.resetPlayer();
196
- this.el.nativeElement.removeAttribute('style');
197
- }
198
-
199
- private applyInitialStyles() {
200
- Object.assign(this.el.nativeElement.style, this.initial);
201
- }
202
-
203
- private applyAnimateStyles() {
204
- Object.assign(this.el.nativeElement.style, this.animate);
205
- }
206
-
207
- private playAnimation(startState: any, targetState: any) {
208
- if (!targetState) return;
209
-
210
- if (startState === undefined) {
211
- startState = this.initial;
212
- }
213
-
214
- if (targetState === undefined) {
215
- targetState = this.animate;
216
- }
217
-
218
- const delay = this.delay || '0ms';
219
- const duration = this.duration || '1s';
220
- const easing = this.easing || 'ease';
221
-
222
- if (typeof targetState === 'string') {
223
- targetState = this.variants[targetState];
224
- }
225
-
226
- if (typeof startState === 'string') {
227
- startState = this.variants[startState];
228
- }
229
-
230
- const timing = `${duration} ${delay} ${easing}`;
231
-
232
- const parentAnimation: AnimationMetadata[] = [
233
- style(startState || {}),
234
- animate(timing, style(targetState)),
235
- ];
236
-
237
- const animationBuilder = this.builder.build(parentAnimation);
238
-
239
- if (this.player && this.player.hasStarted()) {
240
- this.player.destroy();
241
- }
242
-
243
- this.player = animationBuilder.create(this.el.nativeElement);
244
- this.player.onDone(() => {
245
- this.animationComplete.emit();
246
- });
247
- this.player.play();
248
- }
249
-
250
- runInViewAnimation() {
251
- if (this.inView && this.animate) {
252
- this.playAnimation(this.initial, this.inView);
253
- //this.animationComplete.emit();
254
- }
255
- }
256
-
257
- runInitAnimation() {
258
- if (this.initial && this.animate) {
259
- this.playAnimation(this.initial, this.animate);
260
- //this.animationComplete.emit();
261
- }
262
- }
263
-
264
- runExitAnimation() {
265
- if (this.exit) {
266
- this.playAnimation(this.animate, this.exit);
267
- //this.animationComplete.emit();
268
- }
269
- }
270
-
271
- cancel() {
272
- this.applyAnimateStyles();
273
- }
274
-
275
- getDuration(): number {
276
- const durationSeconds = parseFloat(this.duration);
277
- const delaySeconds = parseFloat(this.delay);
278
- return (durationSeconds + delaySeconds) * 1000;
279
- }
280
-
281
- getKeyByValue(object: any, key: any) {
282
- return Object.keys(object).find((key) => object[key] === key);
283
- }
284
-
285
- //TODO:Add logic for exiting elements
286
- runInitAnimationsWhenInViewport(
287
- element: MotionDirective,
288
- offset: string = '0px',
289
- repeat: boolean,
290
- ) {
291
- this.observer = new IntersectionObserver(
292
- (entries) => {
293
- entries.forEach((entry) => {
294
- if (entry.isIntersecting) {
295
- // console.log('In view');
296
- element.runInViewAnimation();
297
-
298
- if (!repeat) {
299
- this.observer?.unobserve(entry.target);
300
- }
301
- } else if (!entry.isIntersecting && repeat) {
302
- //console.log('Leaving view');
303
- }
304
- });
305
- },
306
- { rootMargin: offset },
307
- );
308
-
309
- this.observer.observe(element.el.nativeElement);
310
- }
311
- }
@@ -1,16 +0,0 @@
1
- import { NgModule } from '@angular/core';
2
- import { MotionDirective } from './ngx-motion.directive';
3
- import { MotionHostComponent } from './motion-host/motion-host.component';
4
- @NgModule({
5
- declarations: [
6
- MotionDirective,
7
- ],
8
- imports: [
9
- MotionHostComponent
10
- ],
11
- exports: [
12
- MotionDirective,
13
- MotionHostComponent
14
- ]
15
- })
16
- export class NgxMotionModule { }
@@ -1,128 +0,0 @@
1
- import {
2
- Injectable,
3
- Inject,
4
- signal,
5
- } from '@angular/core';
6
- import { DOCUMENT } from '@angular/common';
7
- import { MotionDirective } from '../ngx-motion.directive';
8
-
9
- import { fromEvent, Subscription } from 'rxjs';
10
- import { throttleTime, debounceTime } from 'rxjs/operators';
11
- import { Router } from '@angular/router';
12
-
13
- @Injectable({
14
- providedIn: 'root',
15
- })
16
- export class MotionService {
17
- private scrollSubscription: Subscription;
18
- private motionElements: MotionDirective[] = [];
19
- scrollYProgress = signal(0);
20
- totalExitDuration: number = 0;
21
-
22
- constructor(
23
- private router: Router,
24
- @Inject(DOCUMENT) private document: Document,
25
- ) {
26
- this.setupScrollListener();
27
- this.scrollSubscription = new Subscription();
28
- }
29
-
30
- ngOnDestroy() {
31
- if (this.scrollSubscription) {
32
- this.scrollSubscription.unsubscribe();
33
- }
34
- }
35
-
36
- private updateTotalExitDuration() {
37
- this.totalExitDuration = this.motionElements.reduce((total, element) => {
38
- return total + element.getDuration();
39
- }, 0);
40
- }
41
-
42
- private setupScrollListener() {
43
- if (typeof window !== 'undefined') {
44
- this.scrollSubscription = fromEvent(window, 'scroll')
45
- .pipe(
46
- throttleTime(16), // Limit to about 60fps
47
- debounceTime(1), // Wait for scroll to settle
48
- )
49
- .subscribe(() => {
50
- this.updateScrollProgress();
51
- });
52
- this.updateScrollProgress();
53
- }
54
- }
55
-
56
- private updateScrollProgress() {
57
- const doc = this.document.documentElement;
58
- const scrollTop = window.scrollY || doc.scrollTop;
59
- const scrollHeight = doc.scrollHeight - doc.clientHeight;
60
- const progress =
61
- scrollHeight > 0 ? Math.round((scrollTop / scrollHeight) * 100) : 0;
62
- this.scrollYProgress.set(progress);
63
- }
64
-
65
- registerMotionElement(element: MotionDirective) {
66
- this.motionElements.push(element);
67
- this.updateTotalExitDuration();
68
- }
69
-
70
- unregisterMotionElement(element: MotionDirective) {
71
- this.motionElements = this.motionElements.filter((e) => e !== element);
72
- this.updateTotalExitDuration();
73
- }
74
-
75
- getMotionElements(): MotionDirective[] {
76
- return this.motionElements;
77
- }
78
-
79
- getAllElementsByRoute(route: string): MotionDirective[] {
80
- return this.motionElements;
81
- }
82
-
83
- cancelAllAnimations() {
84
- this.motionElements.forEach((motion) => {
85
- motion.cancel();
86
- });
87
- }
88
-
89
- runAllEnterAnimations(): void {
90
- const enterAnimations = this.motionElements.map((motion) => {
91
- // motion.runInitAnimation();
92
- });
93
- }
94
-
95
- runAllExitAnimations(): void {
96
- // Proceed with the exit animations
97
- this.motionElements.map((motion) => {
98
- motion.runExitAnimation();
99
- });
100
- }
101
-
102
- runAllEnterAnimationsForRoute(route: string): MotionDirective[] {
103
- const enterAnimations = this.getAllElementsByRoute(route).map((motion) => {
104
- motion.runInitAnimation();
105
- return motion;
106
- });
107
- return enterAnimations;
108
- }
109
-
110
- getLongestExitDuration(): number {
111
- const durations = this.motionElements.map((motion) => motion.getDuration());
112
- return durations.length > 0 ? Math.max(...durations) : 0;
113
- }
114
-
115
- getLongestEnterDuration(): number {
116
- const durations = this.motionElements.map((motion) => motion.getDuration());
117
- return durations.length > 0 ? Math.max(...durations) : 0;
118
- }
119
-
120
- getLongestDurationForRoute(): number {
121
- const route = this.router.url;
122
- const elements = this.getAllElementsByRoute(route);
123
- const durations = elements.map((motion) => motion.getDuration());
124
- console.log('durations', durations.length > 0 ? Math.max(...durations) : 0);
125
- return durations.length > 0 ? Math.max(...durations) : 0;
126
- }
127
- }
128
-
package/src/public-api.ts DELETED
@@ -1,6 +0,0 @@
1
- export * from './lib/ngx-motion.directive';
2
- export * from './lib/services/ngx-motion.service';
3
- export * from './lib/motion-host/motion-host.component';
4
- export * from './lib/motion-host/motion-page-transition';
5
-
6
- // export * from './lib/ngx-motion-host.directive';
package/tsconfig.lib.json DELETED
@@ -1,14 +0,0 @@
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
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,14 +0,0 @@
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
- }