@@ -100,6 +100,11 @@ const DEL = "__aa_del"
100
100
*/
101
101
const NEW = "__aa_new"
102
102
103
+ /**
104
+ * Track elements that have had an off-viewport initialization update.
105
+ */
106
+ const seen = new WeakSet < Element > ( )
107
+
103
108
/**
104
109
* Callback for handling all mutations.
105
110
* @param mutations - A mutation list
@@ -148,7 +153,13 @@ function observePosition(el: Element) {
148
153
. map ( ( px ) => `${ - 1 * Math . floor ( px ) } px` )
149
154
. join ( " " )
150
155
const observer = new IntersectionObserver (
151
- ( ) => {
156
+ ( entries ) => {
157
+ const entry = entries [ 0 ]
158
+ if ( entry && entry . target === el && ! entry . isIntersecting && ! seen . has ( el ) ) {
159
+ seen . add ( el )
160
+ updatePos ( el )
161
+ return
162
+ }
152
163
++ invocations > 1 && updatePos ( el )
153
164
} ,
154
165
{
@@ -442,7 +453,21 @@ export function getTransitionSizes(
442
453
function getOptions ( el : Element ) : AutoAnimateOptions | AutoAnimationPlugin {
443
454
return TGT in el && options . has ( ( el as Element & { __aa_tgt : Element } ) [ TGT ] )
444
455
? options . get ( ( el as Element & { __aa_tgt : Element } ) [ TGT ] ) !
445
- : { duration : 250 , easing : "ease-in-out" }
456
+ : defaultOptions
457
+ }
458
+
459
+ /**
460
+ * Global default options used when none are provided.
461
+ */
462
+ let defaultOptions : AutoAnimateOptions = { duration : 250 , easing : "ease-in-out" }
463
+
464
+ /**
465
+ * Allows consumers to configure library-wide default options.
466
+ */
467
+ export function setAutoAnimateDefaults (
468
+ opts : Partial < AutoAnimateOptions >
469
+ ) : void {
470
+ defaultOptions = { ...defaultOptions , ...opts }
446
471
}
447
472
448
473
/**
@@ -812,7 +837,7 @@ export interface AutoAnimationPlugin {
812
837
* @param el - A parent element to add animations to.
813
838
* @param options - An optional object of options.
814
839
*/
815
- export default function autoAnimate (
840
+ export function autoAnimate (
816
841
el : HTMLElement ,
817
842
config : Partial < AutoAnimateOptions > | AutoAnimationPlugin = { }
818
843
) : AnimationController {
@@ -831,7 +856,7 @@ export default function autoAnimate(
831
856
if ( isPlugin ( config ) ) {
832
857
options . set ( el , config )
833
858
} else {
834
- options . set ( el , { duration : 250 , easing : "ease-in-out" , ...config } )
859
+ options . set ( el , { ... defaultOptions , ...config } )
835
860
}
836
861
mutations . observe ( el , { childList : true } )
837
862
parents . add ( el )
@@ -844,6 +869,11 @@ export default function autoAnimate(
844
869
} ,
845
870
disable : ( ) => {
846
871
enabled . delete ( el )
872
+ // Cancel in-flight animations and immediate timers
873
+ forEach ( el , ( child ) => {
874
+ animations . get ( child ) ?. cancel ( )
875
+ clearTimeout ( debounces . get ( child ) )
876
+ } )
847
877
} ,
848
878
isEnabled : ( ) => enabled . has ( el ) ,
849
879
} )
0 commit comments