@hilux/ngx-motion 1.1.0 → 1.1.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/README.md +1 -1
- package/ng-package.json +7 -0
- package/package.json +21 -14
- package/src/lib/components/motion-one-presence.component.ts +116 -0
- package/src/lib/constants/motion-defaults.constants.ts +41 -0
- package/src/lib/directives/motion-animate.directive.ts +55 -0
- package/src/lib/directives/motion-if.directive.ts +138 -0
- package/src/lib/directives/motion-one.directive.backup.ts +1591 -0
- package/src/lib/directives/motion-one.directive.ts +1154 -0
- package/src/lib/directives/motion-presence.directive.ts +145 -0
- package/src/lib/services/motion-animation-options.service.ts +142 -0
- package/src/lib/services/motion-animation.service.ts +86 -0
- package/src/lib/services/motion-browser-safe.ts +64 -0
- package/src/lib/services/motion-gesture.service.ts +141 -0
- package/src/lib/services/motion-inview.service.ts +91 -0
- package/src/lib/services/motion-one.service.ts +424 -0
- package/src/lib/services/motion-stagger.service.ts +70 -0
- package/src/lib/services/motion-timeline.service.ts +182 -0
- package/src/lib/types/motion-animation.types.ts +46 -0
- package/src/lib/utils/color.utils.ts +54 -0
- package/src/lib/utils/easing.utils.ts +77 -0
- package/src/lib/utils/index.ts +7 -0
- package/src/lib/utils/style-extraction.utils.ts +70 -0
- package/src/lib/utils/timing.utils.ts +61 -0
- package/src/lib/utils/transform.utils.ts +112 -0
- package/src/lib/utils/variant.utils.ts +55 -0
- package/src/public-api.ts +21 -0
- package/tsconfig.lib.json +17 -0
- package/tsconfig.lib.prod.json +11 -0
- package/tsconfig.spec.json +15 -0
- package/fesm2022/hilux-ngx-motion.mjs +0 -1562
- package/fesm2022/hilux-ngx-motion.mjs.map +0 -1
- package/index.d.ts +0 -5
- package/lib/directives/motion-if.directive.d.ts +0 -14
- package/lib/directives/motion-one.directive.d.ts +0 -142
- package/lib/services/motion-animation.service.d.ts +0 -20
- package/lib/services/motion-browser-safe.d.ts +0 -10
- package/lib/services/motion-one.service.d.ts +0 -87
- package/public-api.d.ts +0 -6
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Directive,
|
|
3
|
+
Input,
|
|
4
|
+
TemplateRef,
|
|
5
|
+
ViewContainerRef,
|
|
6
|
+
EmbeddedViewRef,
|
|
7
|
+
ElementRef,
|
|
8
|
+
effect,
|
|
9
|
+
signal,
|
|
10
|
+
DestroyRef,
|
|
11
|
+
inject,
|
|
12
|
+
ChangeDetectorRef,
|
|
13
|
+
Renderer2
|
|
14
|
+
} from '@angular/core';
|
|
15
|
+
import { MotionOneDirective } from './motion-one.directive';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Enhanced structural directive for animated presence with proper lifecycle management.
|
|
19
|
+
* Automatically handles enter and exit animations for elements added/removed from DOM.
|
|
20
|
+
*
|
|
21
|
+
* Supports multiple integration patterns:
|
|
22
|
+
*
|
|
23
|
+
* Pattern 1 - Direct with motionone directive:
|
|
24
|
+
* ```html
|
|
25
|
+
* <div *motionPresence="show">
|
|
26
|
+
* <div motionone
|
|
27
|
+
* [initial]="{ opacity: 0 }"
|
|
28
|
+
* [animate]="{ opacity: 1 }"
|
|
29
|
+
* [exit]="{ opacity: 0 }">
|
|
30
|
+
* Content
|
|
31
|
+
* </div>
|
|
32
|
+
* </div>
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* Pattern 2 - With animation callbacks:
|
|
36
|
+
* ```html
|
|
37
|
+
* <div *motionPresence="show; onEnter: handleEnter; onExit: handleExit">
|
|
38
|
+
* Content
|
|
39
|
+
* </div>
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
@Directive({
|
|
43
|
+
selector: '[motionPresence]',
|
|
44
|
+
standalone: true
|
|
45
|
+
})
|
|
46
|
+
export class MotionPresenceDirective {
|
|
47
|
+
private hasView = false;
|
|
48
|
+
private currentView: EmbeddedViewRef<any> | null = null;
|
|
49
|
+
private exitTimeout: any;
|
|
50
|
+
private destroyRef = inject(DestroyRef);
|
|
51
|
+
|
|
52
|
+
@Input() motionPresenceOnEnter?: () => void;
|
|
53
|
+
@Input() motionPresenceOnExit?: () => Promise<void>;
|
|
54
|
+
|
|
55
|
+
constructor(
|
|
56
|
+
private templateRef: TemplateRef<any>,
|
|
57
|
+
private viewContainer: ViewContainerRef,
|
|
58
|
+
private cdr: ChangeDetectorRef
|
|
59
|
+
) {
|
|
60
|
+
this.destroyRef.onDestroy(() => {
|
|
61
|
+
if (this.exitTimeout) {
|
|
62
|
+
clearTimeout(this.exitTimeout);
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
@Input() set motionPresence(condition: boolean) {
|
|
68
|
+
if (condition && !this.hasView) {
|
|
69
|
+
this.enter();
|
|
70
|
+
} else if (!condition && this.hasView) {
|
|
71
|
+
this.exit();
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
private enter() {
|
|
76
|
+
this.currentView = this.viewContainer.createEmbeddedView(this.templateRef);
|
|
77
|
+
this.hasView = true;
|
|
78
|
+
this.cdr.detectChanges();
|
|
79
|
+
|
|
80
|
+
// Wait for view to be rendered
|
|
81
|
+
setTimeout(() => {
|
|
82
|
+
if (this.motionPresenceOnEnter) {
|
|
83
|
+
this.motionPresenceOnEnter();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Find and trigger motion directive if present
|
|
87
|
+
const motionElements = this.findMotionDirectives(this.currentView);
|
|
88
|
+
motionElements.forEach(directive => {
|
|
89
|
+
if (directive.runInitAnimation) {
|
|
90
|
+
directive.runInitAnimation();
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}, 0);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
private async exit() {
|
|
97
|
+
if (!this.hasView || !this.currentView) return;
|
|
98
|
+
|
|
99
|
+
// Find motion directives for exit animation
|
|
100
|
+
const motionElements = this.findMotionDirectives(this.currentView);
|
|
101
|
+
|
|
102
|
+
if (motionElements.length > 0) {
|
|
103
|
+
// Wait for exit animations
|
|
104
|
+
const exitPromises = motionElements
|
|
105
|
+
.filter(directive => directive.exit && Object.keys(directive.exit).length > 0)
|
|
106
|
+
.map(directive => directive.runExitAnimation());
|
|
107
|
+
|
|
108
|
+
if (exitPromises.length > 0) {
|
|
109
|
+
await Promise.all(exitPromises);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (this.motionPresenceOnExit) {
|
|
114
|
+
await this.motionPresenceOnExit();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
this.viewContainer.clear();
|
|
118
|
+
this.hasView = false;
|
|
119
|
+
this.currentView = null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private findMotionDirectives(view: EmbeddedViewRef<any> | null): MotionOneDirective[] {
|
|
123
|
+
if (!view) return [];
|
|
124
|
+
|
|
125
|
+
const directives: MotionOneDirective[] = [];
|
|
126
|
+
|
|
127
|
+
view.rootNodes.forEach((node: any) => {
|
|
128
|
+
if (node?._motionDirective) {
|
|
129
|
+
directives.push(node._motionDirective);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Search children
|
|
133
|
+
if (node?.querySelectorAll) {
|
|
134
|
+
const elements = node.querySelectorAll('[motionone]');
|
|
135
|
+
elements.forEach((el: any) => {
|
|
136
|
+
if (el._motionDirective) {
|
|
137
|
+
directives.push(el._motionDirective);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
return directives;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
import type { AnimationOptions } from 'motion';
|
|
3
|
+
import { TransitionOptions, VariantWithTransition } from '../types/motion-animation.types';
|
|
4
|
+
import { MotionAnimationService } from './motion-animation.service';
|
|
5
|
+
import { getEasing, getRepeatValue, mapRepeatTypeToDirection } from '../utils/easing.utils';
|
|
6
|
+
|
|
7
|
+
export interface AnimationOptionsConfig {
|
|
8
|
+
targetState?: VariantWithTransition;
|
|
9
|
+
animateState?: VariantWithTransition;
|
|
10
|
+
staggerDelay?: number;
|
|
11
|
+
transition: TransitionOptions | Record<string, TransitionOptions>;
|
|
12
|
+
delay: number;
|
|
13
|
+
easing: string | number[] | { type: 'spring'; stiffness?: number; damping?: number };
|
|
14
|
+
duration: number;
|
|
15
|
+
repeat: boolean;
|
|
16
|
+
onComplete?: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
@Injectable({
|
|
20
|
+
providedIn: 'root'
|
|
21
|
+
})
|
|
22
|
+
export class MotionAnimationOptionsService {
|
|
23
|
+
|
|
24
|
+
constructor(private motionAnimation: MotionAnimationService) {}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Build animation options from configuration
|
|
28
|
+
* Handles priority: stagger delay > state transition > global transition > defaults
|
|
29
|
+
*/
|
|
30
|
+
getAnimationOptions(config: AnimationOptionsConfig): AnimationOptions {
|
|
31
|
+
// Priority: 1. Stagger delay, 2. State-specific transition, 3. Global transition, 4. Individual properties
|
|
32
|
+
const stateTransition = (config.targetState?.transition as TransitionOptions) ??
|
|
33
|
+
(config.animateState?.transition as TransitionOptions);
|
|
34
|
+
const globalTransition = (config.transition as TransitionOptions);
|
|
35
|
+
|
|
36
|
+
const transitionObj: TransitionOptions = stateTransition || globalTransition || {};
|
|
37
|
+
|
|
38
|
+
// Always prioritize stagger delay over any other delay settings (including 0)
|
|
39
|
+
const delay = config.staggerDelay !== undefined && config.staggerDelay >= 0
|
|
40
|
+
? config.staggerDelay
|
|
41
|
+
: (transitionObj.delay ?? config.delay);
|
|
42
|
+
|
|
43
|
+
const easeInput = transitionObj.ease ?? config.easing;
|
|
44
|
+
|
|
45
|
+
// Detect if target contains keyframe arrays (avoid native spring in that case)
|
|
46
|
+
let targetHasKeyframeArrays = false;
|
|
47
|
+
if (config.targetState) {
|
|
48
|
+
for (const k of Object.keys(config.targetState)) {
|
|
49
|
+
if (k === 'transition' || k === 'at') continue;
|
|
50
|
+
const v: any = config.targetState[k];
|
|
51
|
+
if (Array.isArray(v)) {
|
|
52
|
+
targetHasKeyframeArrays = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// If spring config and no keyframe arrays, prefer native spring options (no fixed duration)
|
|
59
|
+
if (easeInput && typeof easeInput === 'object' && 'type' in easeInput &&
|
|
60
|
+
easeInput.type === 'spring' && !targetHasKeyframeArrays) {
|
|
61
|
+
const springOptions: any = {
|
|
62
|
+
type: 'spring',
|
|
63
|
+
stiffness: easeInput.stiffness ?? 100,
|
|
64
|
+
damping: easeInput.damping ?? 10,
|
|
65
|
+
delay,
|
|
66
|
+
repeat: getRepeatValue(transitionObj, config.repeat),
|
|
67
|
+
repeatDelay: transitionObj.repeatDelay,
|
|
68
|
+
onComplete: config.onComplete
|
|
69
|
+
};
|
|
70
|
+
const direction = mapRepeatTypeToDirection(transitionObj.repeatType);
|
|
71
|
+
if (direction) springOptions.direction = direction;
|
|
72
|
+
if (transitionObj.repeatType) springOptions.repeatType = transitionObj.repeatType;
|
|
73
|
+
return springOptions as AnimationOptions;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const ease = getEasing(easeInput, this.motionAnimation);
|
|
77
|
+
|
|
78
|
+
const options: any = {
|
|
79
|
+
duration: transitionObj.duration ?? config.duration,
|
|
80
|
+
delay,
|
|
81
|
+
repeat: getRepeatValue(transitionObj, config.repeat),
|
|
82
|
+
repeatDelay: transitionObj.repeatDelay,
|
|
83
|
+
onComplete: config.onComplete
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const direction = mapRepeatTypeToDirection(transitionObj.repeatType);
|
|
87
|
+
if (direction) options.direction = direction;
|
|
88
|
+
if (transitionObj.repeatType) options.repeatType = transitionObj.repeatType;
|
|
89
|
+
|
|
90
|
+
options.easing = ease; // Motion 11+
|
|
91
|
+
options.ease = ease; // Back-compat
|
|
92
|
+
|
|
93
|
+
return options as AnimationOptions;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Get stagger value from transition configuration
|
|
98
|
+
*/
|
|
99
|
+
getStaggerValue(
|
|
100
|
+
animateState?: VariantWithTransition,
|
|
101
|
+
transition?: TransitionOptions | Record<string, TransitionOptions>,
|
|
102
|
+
defaultStaggerChildren: number = 1
|
|
103
|
+
): number {
|
|
104
|
+
const stateTransition = animateState?.transition as TransitionOptions;
|
|
105
|
+
const globalTransition = transition as TransitionOptions;
|
|
106
|
+
|
|
107
|
+
const transitionObj: TransitionOptions = stateTransition || globalTransition || {};
|
|
108
|
+
|
|
109
|
+
return transitionObj.staggerChildren ?? defaultStaggerChildren;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Get stagger direction from transition configuration
|
|
114
|
+
*/
|
|
115
|
+
getStaggerDirection(
|
|
116
|
+
animateState?: VariantWithTransition,
|
|
117
|
+
transition?: TransitionOptions | Record<string, TransitionOptions>,
|
|
118
|
+
defaultStaggerDirection: number = 1
|
|
119
|
+
): number {
|
|
120
|
+
const stateTransition = animateState?.transition as TransitionOptions;
|
|
121
|
+
const globalTransition = transition as TransitionOptions;
|
|
122
|
+
|
|
123
|
+
const transitionObj: TransitionOptions = stateTransition || globalTransition || {};
|
|
124
|
+
|
|
125
|
+
return transitionObj.staggerDirection ?? defaultStaggerDirection;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Get 'when' property from transition configuration
|
|
130
|
+
*/
|
|
131
|
+
getWhen(
|
|
132
|
+
animateState?: VariantWithTransition,
|
|
133
|
+
transition?: TransitionOptions | Record<string, TransitionOptions>
|
|
134
|
+
): 'beforeChildren' | 'afterChildren' | 'together' {
|
|
135
|
+
const stateTransition = animateState?.transition as TransitionOptions;
|
|
136
|
+
const globalTransition = transition as TransitionOptions;
|
|
137
|
+
|
|
138
|
+
const transitionObj: TransitionOptions = stateTransition || globalTransition || {};
|
|
139
|
+
|
|
140
|
+
return (transitionObj.when ?? 'together') as 'beforeChildren' | 'afterChildren' | 'together';
|
|
141
|
+
}
|
|
142
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
|
|
2
|
+
import { isPlatformBrowser } from '@angular/common';
|
|
3
|
+
import * as motion from 'motion';
|
|
4
|
+
import type { AnimationOptions, Target, ValueAnimationOptions, SpringOptions } from 'motion';
|
|
5
|
+
|
|
6
|
+
@Injectable({
|
|
7
|
+
providedIn: 'root'
|
|
8
|
+
})
|
|
9
|
+
export class MotionAnimationService {
|
|
10
|
+
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
|
|
11
|
+
|
|
12
|
+
// Mock implementations for server environment
|
|
13
|
+
private mockAnimate = () => ({ stop: () => {} });
|
|
14
|
+
private mockEasing = (t: number) => t;
|
|
15
|
+
private mockStagger = () => 0;
|
|
16
|
+
private mockSpring = () => motion.easeInOut;
|
|
17
|
+
|
|
18
|
+
// Wrapper for animate function
|
|
19
|
+
animate(target: any, keyframes: any, options?: any): any {
|
|
20
|
+
if (!isPlatformBrowser(this.platformId)) {
|
|
21
|
+
return this.mockAnimate();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
try {
|
|
25
|
+
return motion.animate(target, keyframes, options);
|
|
26
|
+
} catch (e) {
|
|
27
|
+
console.error('Error in animate:', e);
|
|
28
|
+
return this.mockAnimate();
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Wrapper for easing functions
|
|
33
|
+
get easeIn() {
|
|
34
|
+
return isPlatformBrowser(this.platformId) ? (motion as any).easeIn : this.mockEasing;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
get easeOut() {
|
|
38
|
+
return isPlatformBrowser(this.platformId) ? (motion as any).easeOut : this.mockEasing;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
get easeInOut() {
|
|
42
|
+
return isPlatformBrowser(this.platformId) ? (motion as any).easeInOut : this.mockEasing;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Wrapper for stagger function
|
|
46
|
+
stagger(duration: number, options?: any): any {
|
|
47
|
+
if (!isPlatformBrowser(this.platformId)) {
|
|
48
|
+
return this.mockStagger();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
return motion.stagger(duration, options);
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.error('Error in stagger:', e);
|
|
55
|
+
return this.mockStagger();
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get spring() {
|
|
60
|
+
if (!isPlatformBrowser(this.platformId)) {
|
|
61
|
+
return this.mockSpring;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return (options?: { stiffness?: number; damping?: number }) => {
|
|
65
|
+
const m: any = motion as any;
|
|
66
|
+
try {
|
|
67
|
+
// Prefer Motion One spring factory if available
|
|
68
|
+
if (typeof m.spring === 'function') {
|
|
69
|
+
return m.spring({
|
|
70
|
+
stiffness: options?.stiffness ?? 100,
|
|
71
|
+
damping: options?.damping ?? 10
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
// Motion 10 shim: use m.createGenerator?.spring
|
|
75
|
+
if (m?.createGenerator?.spring) {
|
|
76
|
+
return m.createGenerator.spring({
|
|
77
|
+
stiffness: options?.stiffness ?? 100,
|
|
78
|
+
damping: options?.damping ?? 10
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
} catch {}
|
|
82
|
+
// Fallback to an ease function if no spring present
|
|
83
|
+
return (m?.easeInOut as any) ?? ((t: number) => t);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Browser-safe wrapper for Motion One
|
|
3
|
+
* Prevents "window is not defined" errors during SSR
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Create mock implementations for server environment
|
|
7
|
+
const mockAnimate = () => ({ stop: () => {} });
|
|
8
|
+
const mockEasing = { ease: [0, 0, 1, 1] };
|
|
9
|
+
const mockStagger = () => 0;
|
|
10
|
+
const mockInView = () => ({ stop: () => {} });
|
|
11
|
+
|
|
12
|
+
// Check if we're in a browser environment
|
|
13
|
+
const isBrowser = typeof window !== 'undefined';
|
|
14
|
+
|
|
15
|
+
// Define the exports
|
|
16
|
+
let motionExports: any = {
|
|
17
|
+
animate: mockAnimate,
|
|
18
|
+
easeIn: mockEasing,
|
|
19
|
+
easeOut: mockEasing,
|
|
20
|
+
easeInOut: mockEasing,
|
|
21
|
+
stagger: mockStagger,
|
|
22
|
+
inView: mockInView
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Only try to load motion in browser environment
|
|
26
|
+
if (isBrowser) {
|
|
27
|
+
try {
|
|
28
|
+
// Use dynamic import syntax
|
|
29
|
+
import('motion').then(motion => {
|
|
30
|
+
motionExports.animate = motion.animate;
|
|
31
|
+
motionExports.easeIn = motion.easeIn;
|
|
32
|
+
motionExports.easeOut = motion.easeOut;
|
|
33
|
+
motionExports.easeInOut = motion.easeInOut;
|
|
34
|
+
motionExports.stagger = motion.stagger;
|
|
35
|
+
motionExports.inView = motion.inView;
|
|
36
|
+
}).catch(e => {
|
|
37
|
+
console.warn('Failed to import motion library, using mock implementations', e);
|
|
38
|
+
});
|
|
39
|
+
} catch (e) {
|
|
40
|
+
console.warn('Failed to import motion library, using mock implementations', e);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Export the implementations (real or mock)
|
|
45
|
+
export const animate = (...args: any[]) => {
|
|
46
|
+
return motionExports.animate(...args);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export const easeIn = motionExports.easeIn;
|
|
50
|
+
export const easeOut = motionExports.easeOut;
|
|
51
|
+
export const easeInOut = motionExports.easeInOut;
|
|
52
|
+
export const stagger = (...args: any[]) => {
|
|
53
|
+
return motionExports.stagger(...args);
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export const inView = (...args: any[]) => {
|
|
57
|
+
return motionExports.inView(...args);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Re-export types
|
|
61
|
+
export type {
|
|
62
|
+
AnimationOptions,
|
|
63
|
+
Target
|
|
64
|
+
} from 'motion';
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Injectable } from '@angular/core';
|
|
2
|
+
|
|
3
|
+
export interface GestureAnimationState {
|
|
4
|
+
isAnimating: boolean;
|
|
5
|
+
gestureType: 'hover' | 'tap' | 'focus' | null;
|
|
6
|
+
targetState: string | null;
|
|
7
|
+
controls: any;
|
|
8
|
+
startTime: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@Injectable({
|
|
12
|
+
providedIn: 'root'
|
|
13
|
+
})
|
|
14
|
+
export class MotionGestureService {
|
|
15
|
+
private gestureStates = new Map<string, GestureAnimationState>();
|
|
16
|
+
private readonly MIN_GESTURE_INTERVAL = 50; // Minimum ms between gesture triggers
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Check if a gesture should be allowed based on timing
|
|
20
|
+
* Prevents rapid-fire gesture triggers that cause aggressive spring behavior
|
|
21
|
+
*/
|
|
22
|
+
shouldAllowGesture(elementId: string, gestureType: 'hover' | 'tap' | 'focus'): boolean {
|
|
23
|
+
const state = this.gestureStates.get(elementId);
|
|
24
|
+
|
|
25
|
+
if (!state) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// Allow gestures of different types immediately
|
|
30
|
+
if (state.gestureType !== gestureType) {
|
|
31
|
+
return true;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Check if enough time has passed since last gesture of same type
|
|
35
|
+
const timeSinceLastGesture = Date.now() - state.startTime;
|
|
36
|
+
return timeSinceLastGesture >= this.MIN_GESTURE_INTERVAL;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Register that a gesture animation is starting
|
|
41
|
+
*/
|
|
42
|
+
startGesture(
|
|
43
|
+
elementId: string,
|
|
44
|
+
gestureType: 'hover' | 'tap' | 'focus',
|
|
45
|
+
targetState: string,
|
|
46
|
+
controls: any
|
|
47
|
+
): void {
|
|
48
|
+
const existingState = this.gestureStates.get(elementId);
|
|
49
|
+
|
|
50
|
+
// Stop any existing animation for this element
|
|
51
|
+
if (existingState?.controls) {
|
|
52
|
+
try {
|
|
53
|
+
existingState.controls.stop();
|
|
54
|
+
} catch (e) {
|
|
55
|
+
// Animation may have already finished
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
this.gestureStates.set(elementId, {
|
|
60
|
+
isAnimating: true,
|
|
61
|
+
gestureType,
|
|
62
|
+
targetState,
|
|
63
|
+
controls,
|
|
64
|
+
startTime: Date.now()
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Register that a gesture animation has ended
|
|
70
|
+
*/
|
|
71
|
+
endGesture(elementId: string): void {
|
|
72
|
+
const state = this.gestureStates.get(elementId);
|
|
73
|
+
if (state) {
|
|
74
|
+
state.isAnimating = false;
|
|
75
|
+
state.gestureType = null;
|
|
76
|
+
state.targetState = null;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Get the current gesture state for an element
|
|
82
|
+
*/
|
|
83
|
+
getGestureState(elementId: string): GestureAnimationState | undefined {
|
|
84
|
+
return this.gestureStates.get(elementId);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Cancel any ongoing gesture animation for an element
|
|
89
|
+
* Returns true if an animation was cancelled
|
|
90
|
+
*/
|
|
91
|
+
cancelGesture(elementId: string): boolean {
|
|
92
|
+
const state = this.gestureStates.get(elementId);
|
|
93
|
+
|
|
94
|
+
if (state?.controls && state.isAnimating) {
|
|
95
|
+
try {
|
|
96
|
+
state.controls.stop();
|
|
97
|
+
this.endGesture(elementId);
|
|
98
|
+
return true;
|
|
99
|
+
} catch (e) {
|
|
100
|
+
// Animation may have already finished
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Check if an element currently has an active gesture animation
|
|
109
|
+
*/
|
|
110
|
+
isGestureActive(elementId: string, gestureType?: 'hover' | 'tap' | 'focus'): boolean {
|
|
111
|
+
const state = this.gestureStates.get(elementId);
|
|
112
|
+
|
|
113
|
+
if (!state || !state.isAnimating) {
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (gestureType) {
|
|
118
|
+
return state.gestureType === gestureType;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return true;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Clear all gesture state (useful for cleanup)
|
|
126
|
+
*/
|
|
127
|
+
clearElement(elementId: string): void {
|
|
128
|
+
this.cancelGesture(elementId);
|
|
129
|
+
this.gestureStates.delete(elementId);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Clear all gesture states
|
|
134
|
+
*/
|
|
135
|
+
clearAll(): void {
|
|
136
|
+
this.gestureStates.forEach((_, elementId) => {
|
|
137
|
+
this.cancelGesture(elementId);
|
|
138
|
+
});
|
|
139
|
+
this.gestureStates.clear();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { Injectable, PLATFORM_ID, Inject } from '@angular/core';
|
|
2
|
+
import { isPlatformBrowser } from '@angular/common';
|
|
3
|
+
import { MOTION_DEFAULTS } from '../constants/motion-defaults.constants';
|
|
4
|
+
|
|
5
|
+
export interface IntersectionObserverConfig {
|
|
6
|
+
root?: Element | null;
|
|
7
|
+
rootMargin?: string;
|
|
8
|
+
threshold?: number | number[];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@Injectable({
|
|
12
|
+
providedIn: 'root'
|
|
13
|
+
})
|
|
14
|
+
export class MotionInViewService {
|
|
15
|
+
|
|
16
|
+
constructor(@Inject(PLATFORM_ID) private platformId: Object) {}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Create an IntersectionObserver with the given configuration
|
|
20
|
+
*/
|
|
21
|
+
createObserver(
|
|
22
|
+
callback: IntersectionObserverCallback,
|
|
23
|
+
config: IntersectionObserverConfig = {}
|
|
24
|
+
): IntersectionObserver | null {
|
|
25
|
+
if (!isPlatformBrowser(this.platformId)) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const observerConfig: IntersectionObserverInit = {
|
|
30
|
+
root: config.root ?? null,
|
|
31
|
+
rootMargin: config.rootMargin ?? MOTION_DEFAULTS.INVIEW_ROOT_MARGIN,
|
|
32
|
+
threshold: config.threshold ?? MOTION_DEFAULTS.INVIEW_THRESHOLD
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return new IntersectionObserver(callback, observerConfig);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Create a standard IntersectionObserver for runInView functionality
|
|
40
|
+
*/
|
|
41
|
+
createRunInViewObserver(
|
|
42
|
+
element: HTMLElement,
|
|
43
|
+
onEntry: () => void,
|
|
44
|
+
onExit: () => void
|
|
45
|
+
): IntersectionObserver | null {
|
|
46
|
+
return this.createObserver(
|
|
47
|
+
(entries) => {
|
|
48
|
+
entries.forEach((entry) => {
|
|
49
|
+
if (entry.isIntersecting) {
|
|
50
|
+
onEntry();
|
|
51
|
+
} else {
|
|
52
|
+
onExit();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
root: null,
|
|
58
|
+
rootMargin: MOTION_DEFAULTS.INVIEW_ROOT_MARGIN,
|
|
59
|
+
threshold: MOTION_DEFAULTS.INVIEW_THRESHOLD
|
|
60
|
+
}
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Parse margin string (e.g., "0px", "10px 20px")
|
|
66
|
+
*/
|
|
67
|
+
parseMargin(margin: string): string {
|
|
68
|
+
// Ensure margin is in valid format for IntersectionObserver
|
|
69
|
+
if (!margin) return MOTION_DEFAULTS.INVIEW_MARGIN;
|
|
70
|
+
return margin;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Check if IntersectionObserver is supported
|
|
75
|
+
*/
|
|
76
|
+
isSupported(): boolean {
|
|
77
|
+
if (!isPlatformBrowser(this.platformId)) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return 'IntersectionObserver' in window;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Safely disconnect an observer
|
|
85
|
+
*/
|
|
86
|
+
disconnect(observer: IntersectionObserver | null | undefined): void {
|
|
87
|
+
if (observer) {
|
|
88
|
+
observer.disconnect();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|