@hilux/ngx-motion 1.0.14 → 1.1.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 CHANGED
@@ -1,151 +1,414 @@
1
- # NgxMotion
1
+ # @hilux/ngx-motion
2
2
 
3
- WIP - Trigger and control angular animations with a Framer Motion like workflow in template HTML.
4
- Generated with [Angular CLI](https://github.com/angular/angular-cli) version 19.1.0.
3
+ Angular directive for Motion One animations with Framer Motion-like API.
5
4
 
5
+ ## Installation
6
6
 
7
- ## Setup
7
+ ```bash
8
+ npm install @hilux/ngx-motion motion
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ Import the directive in your component:
14
+
15
+ ```typescript
16
+ import { MotionOneDirective } from '@hilux/ngx-motion';
17
+
18
+ @Component({
19
+ selector: 'app-example',
20
+ standalone: true,
21
+ imports: [MotionOneDirective],
22
+ template: `
23
+ <div
24
+ motionone
25
+ [initial]="{ opacity: 0, y: 50 }"
26
+ [animate]="{ opacity: 1, y: 0 }"
27
+ [transition]="{ duration: 0.5 }"
28
+ >
29
+ Animated content
30
+ </div>
31
+ `
32
+ })
33
+ export class ExampleComponent {}
34
+ ```
35
+
36
+ ## Features
37
+
38
+ - ✅ **Motion One Integration**: Built on top of the performant Motion One animation library
39
+ - ✅ **Framer Motion API**: Familiar API for React developers
40
+ - ✅ **TypeScript Support**: Full type safety with TypeScript
41
+ - ✅ **Server-Side Rendering**: Compatible with Angular Universal
42
+ - ✅ **Stagger Animations**: Built-in support for staggered animations
43
+ - ✅ **Timeline Support**: Create complex sequential animations
44
+ - ✅ **Conditional Rendering**: `motionIf` directive for animated presence
45
+ - ✅ **Gesture Support**: whileHover, whileTap, whileFocus animations
46
+
47
+ ## API Reference
48
+
49
+ ### Animation State Inputs
50
+
51
+ | Input | Type | Default | Description |
52
+ |-------|------|---------|-------------|
53
+ | `initial` | `VariantWithTransition \| string` | `{}` | Initial animation state when element mounts |
54
+ | `animate` | `VariantWithTransition \| string` | `{}` | Target animation state to animate to |
55
+ | `exit` | `VariantWithTransition \| string` | `{}` | Animation state when element unmounts |
56
+ | `inView` | `VariantWithTransition` | `{}` | Animation state when element enters viewport |
57
+ | `whileHover` | `VariantWithTransition` | `{}` | Animation state while hovering over element |
58
+ | `whileTap` | `VariantWithTransition` | `{}` | Animation state while pressing/tapping element |
59
+ | `whileFocus` | `VariantWithTransition` | `{}` | Animation state while element is focused |
60
+ | `whileInView` | `VariantWithTransition` | `{}` | Animation state while element is in viewport |
61
+
62
+ ### Timing & Easing Inputs
63
+
64
+ | Input | Type | Default | Description |
65
+ |-------|------|---------|-------------|
66
+ | `duration` | `number` | `0.3` | Default animation duration in seconds |
67
+ | `delay` | `number` | `0` | Default delay before animation starts in seconds |
68
+ | `exitDelay` | `number` | `0` | Delay before exit animation starts in seconds |
69
+ | `easing` | `string \| number[] \| SpringConfig` | `'ease'` | Default easing function for animations |
70
+
71
+ ### Children Animation Inputs
72
+
73
+ | Input | Type | Default | Description |
74
+ |-------|------|---------|-------------|
75
+ | `delayChildren` | `number` | `0` | Delay before children animations start in seconds |
76
+ | `staggerChildren` | `number` | `1` | Time between each child animation start in seconds |
77
+ | `staggerDirection` | `number` | `1` | Direction of stagger (1 = forward, -1 = reverse) |
78
+
79
+ ### Advanced Inputs
80
+
81
+ | Input | Type | Default | Description |
82
+ |-------|------|---------|-------------|
83
+ | `variants` | `Record<string, VariantWithTransition>` | `{}` | Named animation variants for reusable states |
84
+ | `transition` | `TransitionOptions \| Record<string, TransitionOptions>` | `{}` | Transition configuration (overrides individual timing inputs) |
85
+ | `timeline` | `TimelineStep[]` | `undefined` | Sequential animation timeline for complex animations |
86
+ | `repeat` | `boolean` | `false` | Whether to repeat the animation infinitely |
87
+ | `offset` | `string` | `'0px'` | Intersection observer margin for inView detection |
8
88
 
9
- Make sure you have provideAnimations() in app config
89
+ ### Output Events
10
90
 
91
+ | Output | Type | Description |
92
+ |--------|------|-------------|
93
+ | `animationComplete` | `EventEmitter<void>` | Emitted when animation completes |
94
+
95
+ ## Type Definitions
96
+
97
+ ### VariantWithTransition
98
+ ```typescript
99
+ interface VariantWithTransition {
100
+ [key: string]: any;
101
+ transition?: TransitionOptions;
102
+ at?: number; // Delay in seconds before this variant triggers
103
+ }
104
+ ```
105
+
106
+ ### TransitionOptions
107
+ ```typescript
108
+ interface TransitionOptions {
109
+ duration?: number;
110
+ delay?: number;
111
+ ease?: string | number[] | { type: 'spring'; stiffness?: number; damping?: number };
112
+ repeat?: number | boolean;
113
+ repeatType?: 'loop' | 'mirror' | 'reverse' | 'forwards' | 'pingPong';
114
+ repeatDelay?: number;
115
+ staggerChildren?: number;
116
+ staggerDirection?: number;
117
+ when?: 'beforeChildren' | 'afterChildren' | 'together';
118
+ delayChildren?: number;
119
+ times?: number[]; // Keyframe timing array (0-1)
120
+ }
11
121
  ```
12
- import { ApplicationConfig, provideZoneChangeDetection } from '@angular/core';
13
- import { provideRouter, Router, withViewTransitions } from '@angular/router';
14
- import { provideAnimations } from '@angular/platform-browser/animations';
15
- import { routes } from './app.routes';
16
- import { provideClientHydration, withEventReplay } from '@angular/platform-browser';
17
122
 
18
- export const appConfig: ApplicationConfig = {
19
- providers: [
20
- provideZoneChangeDetection({ eventCoalescing: true }),
21
- provideRouter(routes , withViewTransitions()),
22
- provideClientHydration(withEventReplay()),
23
- provideAnimations(),
24
- ]
25
- };
123
+ ### TimelineStep
124
+ ```typescript
125
+ interface TimelineStep {
126
+ prop: string; // CSS property to animate
127
+ keyframes?: any[] | any; // Keyframe values
128
+ to?: any; // End value
129
+ duration: number; // Duration in seconds
130
+ delay?: number; // Delay in seconds
131
+ ease?: string | number[] | SpringConfig;
132
+ atTime?: number; // Absolute start time relative to element start
133
+ times?: number[]; // Keyframe timing array (0-1)
134
+ repeat?: number | boolean;
135
+ repeatType?: 'loop' | 'mirror' | 'reverse' | 'forwards' | 'pingPong';
136
+ repeatDelay?: number;
137
+ }
138
+ ```
26
139
 
140
+ ## Examples
27
141
 
142
+ ### Basic Animation
143
+ ```html
144
+ <div
145
+ motionone
146
+ [initial]="{ opacity: 0, scale: 0.8 }"
147
+ [animate]="{ opacity: 1, scale: 1 }"
148
+ [duration]="0.5"
149
+ [easing]="'ease-out'"
150
+ >
151
+ Fade and scale in
152
+ </div>
28
153
  ```
29
154
 
30
- Import motin directive in your component
155
+ ### Hover Interactions
156
+ ```html
157
+ <button
158
+ motionone
159
+ [initial]="{ scale: 1 }"
160
+ [whileHover]="{ scale: 1.05 }"
161
+ [whileTap]="{ scale: 0.95 }"
162
+ [transition]="{ type: 'spring', stiffness: 400, damping: 17 }"
163
+ >
164
+ Interactive Button
165
+ </button>
166
+ ```
31
167
 
168
+ ### Stagger Children
169
+ ```html
170
+ <div
171
+ motionone
172
+ [initial]="{ opacity: 0 }"
173
+ [animate]="{ opacity: 1 }"
174
+ [staggerChildren]="0.1"
175
+ [delayChildren]="0.3"
176
+ >
177
+ <div motionone [initial]="{ x: -50, opacity: 0 }" [animate]="{ x: 0, opacity: 1 }">Item 1</div>
178
+ <div motionone [initial]="{ x: -50, opacity: 0 }" [animate]="{ x: 0, opacity: 1 }">Item 2</div>
179
+ <div motionone [initial]="{ x: -50, opacity: 0 }" [animate]="{ x: 0, opacity: 1 }">Item 3</div>
180
+ </div>
32
181
  ```
33
- import { Component } from '@angular/core';
34
- import { RouterOutlet, RouterLink, RouterLinkActive, ActivatedRoute, Router, NavigationEnd , NavigationStart} from '@angular/router';
35
- import { MotionDirective } from '../../../../dist/ngx-motion/';
182
+
183
+ ### Using Variants
184
+ ```typescript
36
185
  @Component({
37
- selector: 'app-root',
38
- imports: [
39
- RouterOutlet,
40
- MotionDirective
41
- ],
42
- templateUrl: './app.component.html',
43
- styleUrl: './app.component.scss',
44
- standalone: true
186
+ template: `
187
+ <div
188
+ motionone
189
+ [variants]="containerVariants"
190
+ [initial]="'hidden'"
191
+ [animate]="'visible'"
192
+ >
193
+ <div motionone [variants]="itemVariants">Item 1</div>
194
+ <div motionone [variants]="itemVariants">Item 2</div>
195
+ </div>
196
+ `
45
197
  })
46
- export class AppComponent {
47
- title = 'ngx-motion-demo';
48
- }
198
+ export class VariantsExample {
199
+ containerVariants = {
200
+ hidden: { opacity: 0 },
201
+ visible: {
202
+ opacity: 1,
203
+ transition: {
204
+ staggerChildren: 0.2,
205
+ delayChildren: 0.1
206
+ }
207
+ }
208
+ };
49
209
 
210
+ itemVariants = {
211
+ hidden: { x: -20, opacity: 0 },
212
+ visible: {
213
+ x: 0,
214
+ opacity: 1,
215
+ transition: {
216
+ duration: 0.6,
217
+ ease: [0.33, 1, 0.68, 1]
218
+ }
219
+ }
220
+ };
221
+ }
50
222
  ```
51
223
 
224
+ ### Timeline Animation
52
225
 
53
- ## Basic Example
226
+ Timeline animations allow you to orchestrate multiple properties with precise timing control. Each timeline step can have different durations, delays, and easing functions.
54
227
 
228
+ ```typescript
229
+ @Component({
230
+ template: `
231
+ <div
232
+ motionone
233
+ [timeline]="complexTimeline"
234
+ [animate]="{ opacity: 1 }"
235
+ (animationComplete)="onAnimationComplete()"
236
+ >
237
+ Complex sequenced animation
238
+ </div>
239
+ `
240
+ })
241
+ export class TimelineExample {
242
+ complexTimeline: TimelineStep[] = [
243
+ { prop: 'x', to: 100, duration: 1, atTime: 0 },
244
+ { prop: 'rotate', to: 180, duration: 0.5, atTime: 0.5 },
245
+ { prop: 'scale', to: 1.2, duration: 0.3, atTime: 1.2 },
246
+ { prop: 'opacity', keyframes: [0, 1, 0.5, 1], duration: 2, atTime: 0.8 }
247
+ ];
248
+
249
+ onAnimationComplete() {
250
+ console.log('Timeline animation completed!');
251
+ }
252
+ }
55
253
  ```
56
- <div
57
- motion
58
- [initial]="{
59
- backgroundColor: 'red',
60
- scale: 1,
61
- transform: 'translateX(0px)'
62
- }"
63
- [animate]="{
64
- backgroundColor: 'blue',
65
- scale: 1,
66
- transform: 'translateX(100px)'
67
- }"
68
- [duration]="'.5s'"
69
- style="height: 100px; width: 100px; background-color: red"
70
- >
71
- Hello World
254
+
255
+ ### Advanced Timeline Example (From Demo)
256
+
257
+ This example demonstrates a complex multi-property timeline animation with overlapping animations:
258
+
259
+ ```html
260
+ <div class="h-12 bg-blue-600" style="width:40px"
261
+ motionone
262
+ [timeline]="[
263
+ {
264
+ prop: 'width',
265
+ keyframes: ['40px','160px','80px'],
266
+ duration: 1.2,
267
+ atTime: 0.0,
268
+ ease: 'ease-out',
269
+ times: [0, 0.7, 1]
270
+ },
271
+ {
272
+ prop: 'backgroundColor',
273
+ keyframes: ['#f00','#0f0','#00f'],
274
+ duration: 5.0,
275
+ atTime: 0.5,
276
+ ease: 'ease-in',
277
+ times: [0, 0.4, 1]
278
+ },
279
+ {
280
+ prop: 'transform',
281
+ keyframes: ['scaleY(0.5)','scaleY(1)','scaleY(10)'],
282
+ duration: 6.0,
283
+ atTime: 0,
284
+ ease: { type:'spring', stiffness:100, damping:10 },
285
+ times: [0, 4, 5]
286
+ }
287
+ ]">
72
288
  </div>
73
289
  ```
74
290
 
291
+ **This timeline creates:**
292
+ 1. **Width Animation** (0.0s → 1.2s): Element width morphs from 40px → 160px → 80px with ease-out timing
293
+ 2. **Color Transition** (0.5s → 5.5s): Background color cycles through red → green → blue starting 0.5s later
294
+ 3. **Scale Transform** (0.0s → 6.0s): Vertical scaling from 0.5 → 1 → 10 with spring physics
295
+
296
+ **Key Timeline Features:**
297
+ - **Overlapping Animations**: Multiple properties animate simultaneously with different timings
298
+ - **Custom Keyframe Timing**: `times` array controls when each keyframe occurs (0-1 normalized)
299
+ - **Mixed Easing**: Different easing functions per property (ease-out, ease-in, spring)
300
+ - **Absolute Timing**: `atTime` sets when each animation begins relative to timeline start
301
+ - **Duration Independence**: Each property can have completely different durations
75
302
 
76
- ## Variant Example
303
+ ### Conditional Rendering with motionIf
304
+ ```html
305
+ <div *motionIf="isVisible">
306
+ <div
307
+ motionone
308
+ [initial]="{ scale: 0, opacity: 0 }"
309
+ [animate]="{ scale: 1, opacity: 1 }"
310
+ [exit]="{ scale: 0, opacity: 0, transition: { duration: 0.2 } }"
311
+ [transition]="{ duration: 0.3, ease: 'backOut' }"
312
+ >
313
+ This will animate in and out smoothly!
314
+ </div>
315
+ </div>
316
+ ```
77
317
 
318
+ ### Spring Physics
319
+ ```html
320
+ <div
321
+ motionone
322
+ [initial]="{ y: -100 }"
323
+ [animate]="{ y: 0 }"
324
+ [easing]="{ type: 'spring', stiffness: 300, damping: 20 }"
325
+ >
326
+ Bouncy spring animation
327
+ </div>
78
328
  ```
79
- this.foodVariants = {
80
- egg: { opacity: 1, transform: 'translateX(0px)', backgroundColor: 'blue' },
81
- apple: { opacity: 1, transform: 'translateX(100px)', backgroundColor: 'green' },
82
- pasta: { opacity: 0.5, transform: 'scale(0.8)', backgroundColor: 'yellow' }
83
- };
84
329
 
330
+ ### Scroll-triggered Animation
331
+ ```html
85
332
  <div
86
- motion
87
- [variants]="foodVariants"
88
- [animate]="currentFood"
89
- style="
90
- height: 100px;
91
- width: 100px;
92
- background-color: red;
93
- position: absolute;
94
- top: 0;
95
- left: 0;
96
- z-index: 100;
97
- "
98
- >
99
- {{currentFood}}
333
+ motionone
334
+ [initial]="{ opacity: 0, y: 50 }"
335
+ [whileInView]="{ opacity: 1, y: 0 }"
336
+ [transition]="{ duration: 0.6 }"
337
+ [offset]="'-100px'"
338
+ >
339
+ Animates when scrolled into view
340
+ </div>
341
+ ```
342
+
343
+ ## Directives
344
+
345
+ ### motionone
346
+ Main animation directive for creating smooth, performant animations.
347
+
348
+ ### motionIf
349
+ Structural directive for conditional rendering with enter/exit animations:
350
+
351
+ ```html
352
+ <div *motionIf="condition">
353
+ Content with enter/exit animations
100
354
  </div>
355
+ ```
356
+
357
+ ## Easing Options
358
+
359
+ ### Built-in Easings
360
+ - `'ease'` - Default CSS ease
361
+ - `'ease-in'` - CSS ease-in
362
+ - `'ease-out'` - CSS ease-out
363
+ - `'ease-in-out'` - CSS ease-in-out
364
+ - `'linear'` - Linear timing
365
+
366
+ ### Custom Cubic Bezier
367
+ ```typescript
368
+ [0.4, 0, 0.2, 1] // Custom cubic bezier curve
369
+ ```
370
+
371
+ ### Spring Physics
372
+ ```typescript
373
+ {
374
+ type: 'spring',
375
+ stiffness: 400, // Spring stiffness (higher = snappier)
376
+ damping: 17 // Spring damping (higher = less bouncy)
377
+ }
378
+ ```
379
+
380
+ ## Transform Properties
381
+
382
+ The directive supports convenient transform shortcuts:
383
+ - `x`, `y` - Translate in pixels
384
+ - `scale`, `scaleX`, `scaleY` - Scale transforms
385
+ - `rotate` - Rotation in degrees
386
+ - `transform` - Raw transform string (for advanced use)
101
387
 
388
+ ## Color Animation
389
+
390
+ Supports smooth color transitions:
391
+ ```typescript
392
+ {
393
+ backgroundColor: ['#ff0000', '#00ff00', '#0000ff'], // Keyframe colors
394
+ color: '#ffffff' // Single color value
395
+ }
102
396
  ```
103
397
 
104
- ## MotionDirective
105
-
106
- - @Input() initial: any = {};
107
- - @Input() animate: any = {};
108
- - @Input() exit: any = {};
109
- - @Input() inView: any = {};
110
- - @Input() duration = '0.3s';
111
- - @Input() delay = '0s';
112
- - @Input() repeat = false;
113
- - @Input() exitDelay = '0s';
114
- - @Input() easing = 'ease';
115
- - @Input() offset = '0px';
116
- - @Input() whileHover: any = {};
117
- - @Input() staggerChildren = '0s';
118
- - @Input() whileTap: any = {};
119
- - @Input() whileFocus: any = {};
120
- - @Input() whileInView: any = {};
121
- - @Input() variants: any = {};
122
- - @Output() animationComplete = new EventEmitter<void>();
123
-
124
- Current Limitations:
125
-
126
- - staggerChildren not complete.
127
- - Dynamic animate values must be generated in ts, you cant set directly in the animate value.
128
- - InView offset not complete.
129
- - Repeat & offset only apply to inView animations.
130
- - exitDelay not complete.
131
-
132
- ## MotionHostComponent
133
-
134
- WIP - Atempt at Angular version of AnimatePresence.
135
- So far unable to use @queue as expected using Animation Builder.
136
- Relying on router event and getLongestDuration to delay the [motion] entry delay.
137
-
138
- ## MotionService
139
-
140
- - scrollYProgress
141
- - registerMotionElement
142
- - unregisterMotionElement
143
- - getMotionElements
144
- - getAllElementsByRoute
145
- - cancelAllAnimations
146
- - runAllEnterAnimations
147
- - runAllExitAnimations
148
- - runAllEnterAnimationsForRoute
149
- - getLongestExitDuration
150
- - getLongestEnterDuration
151
- - getLongestDurationForRoute
398
+ ## Performance Notes
399
+
400
+ - Uses Motion One's optimized animation engine
401
+ - Automatically promotes animations to the GPU when beneficial
402
+ - Minimal bundle size impact
403
+ - Tree-shakeable exports
404
+
405
+ ## Browser Support
406
+
407
+ - Chrome 80+
408
+ - Firefox 76+
409
+ - Safari 13+
410
+ - Edge 80+
411
+
412
+ ## License
413
+
414
+ MIT