Skip to content

Commit 6572b3f

Browse files
committed
feat(core): named export and global defaults (#194, #216)
- Export as named in addition to default - Add and use it for global defaults - Vue re-exports setter; Nuxt plugin applies to defaults
1 parent 0fadd56 commit 6572b3f

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

src/index.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ const DEL = "__aa_del"
100100
*/
101101
const NEW = "__aa_new"
102102

103+
/**
104+
* Track elements that have had an off-viewport initialization update.
105+
*/
106+
const seen = new WeakSet<Element>()
107+
103108
/**
104109
* Callback for handling all mutations.
105110
* @param mutations - A mutation list
@@ -148,7 +153,13 @@ function observePosition(el: Element) {
148153
.map((px) => `${-1 * Math.floor(px)}px`)
149154
.join(" ")
150155
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+
}
152163
++invocations > 1 && updatePos(el)
153164
},
154165
{
@@ -442,7 +453,21 @@ export function getTransitionSizes(
442453
function getOptions(el: Element): AutoAnimateOptions | AutoAnimationPlugin {
443454
return TGT in el && options.has((el as Element & { __aa_tgt: Element })[TGT])
444455
? 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 }
446471
}
447472

448473
/**
@@ -812,7 +837,7 @@ export interface AutoAnimationPlugin {
812837
* @param el - A parent element to add animations to.
813838
* @param options - An optional object of options.
814839
*/
815-
export default function autoAnimate(
840+
export function autoAnimate(
816841
el: HTMLElement,
817842
config: Partial<AutoAnimateOptions> | AutoAnimationPlugin = {}
818843
): AnimationController {
@@ -831,7 +856,7 @@ export default function autoAnimate(
831856
if (isPlugin(config)) {
832857
options.set(el, config)
833858
} else {
834-
options.set(el, { duration: 250, easing: "ease-in-out", ...config })
859+
options.set(el, { ...defaultOptions, ...config })
835860
}
836861
mutations.observe(el, { childList: true })
837862
parents.add(el)
@@ -844,6 +869,11 @@ export default function autoAnimate(
844869
},
845870
disable: () => {
846871
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+
})
847877
},
848878
isEnabled: () => enabled.has(el),
849879
})

src/nuxt/runtime/plugin.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { defineNuxtPlugin } from "#imports"
1+
import { defineNuxtPlugin, useRuntimeConfig } from "#imports"
22
import { vAutoAnimate } from "@formkit/auto-animate/vue"
3+
import { setAutoAnimateDefaults } from "@formkit/auto-animate"
34

45
export default defineNuxtPlugin((app) => {
6+
const config = useRuntimeConfig().public?.autoAnimate
7+
if (config && typeof config === "object") {
8+
setAutoAnimateDefaults(config as any)
9+
}
510
// Register the directive
611
app.vueApp.directive("auto-animate", vAutoAnimate)
712
})

src/vue/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ import autoAnimate, {
55
AutoAnimateOptions,
66
AutoAnimationPlugin,
77
AnimationController,
8+
setAutoAnimateDefaults,
89
} from "../index"
910

11+
export { setAutoAnimateDefaults }
12+
1013
export const vAutoAnimate: Directive<
1114
HTMLElement,
1215
Partial<AutoAnimateOptions>

0 commit comments

Comments
 (0)