AnimatePresence custom
AnimatePresence accepts a custom prop for data that exiting children still
need after they have been removed from Svelte state. This mirrors Motion’s
presence context: exit variants resolve with the latest parent custom value, falling back to the motion element’s own custom only when the
parent doesn’t provide one.
<script lang="ts">
import { AnimatePresence, motion, type Variants } from '@humanspeak/svelte-motion'
let index = $state(0)
let direction = $state(1)
const variants: Variants = {
enter: (direction) => ({ x: direction > 0 ? 120 : -120, opacity: 0 }),
center: { x: 0, opacity: 1 },
exit: (direction) => ({ x: direction > 0 ? -120 : 120, opacity: 0 })
}
</script>
<AnimatePresence custom={direction} initial={false}>
<motion.div
key={index}
{variants}
initial="enter"
animate="center"
exit="exit"
/>
</AnimatePresence><script lang="ts">
import { AnimatePresence, motion, type Variants } from '@humanspeak/svelte-motion'
let index = $state(0)
let direction = $state(1)
const variants: Variants = {
enter: (direction) => ({ x: direction > 0 ? 120 : -120, opacity: 0 }),
center: { x: 0, opacity: 1 },
exit: (direction) => ({ x: direction > 0 ? -120 : 120, opacity: 0 })
}
</script>
<AnimatePresence custom={direction} initial={false}>
<motion.div
key={index}
{variants}
initial="enter"
animate="center"
exit="exit"
/>
</AnimatePresence>Why parent custom matters
When a child is removed, its props are already stale. A directional carousel
needs the new navigation direction to tell the old slide where to exit. Passing custom={direction} to AnimatePresence makes that direction available to the
exiting variant at the exact moment the exit animation is resolved.
API Reference
<AnimatePresence custom={value}>
custom— any value forwarded to dynamic exit variants.- Exit variants receive the
AnimatePresencecustom value before the element’s owncustomvalue. - If
AnimatePresence customisundefined, the element’s inheritedcustomvalue continues to drive variants.
Related
AnimatePresence— coordinate enter and exit animationsVariants— named animation states and function-form variants
Based on Motion’s AnimatePresence custom API.