Skip to content

Commit f57720c

Browse files
committed
fix: animatePresence custom prop not working
1 parent eaa7f0e commit f57720c

File tree

7 files changed

+35
-17
lines changed

7 files changed

+35
-17
lines changed

packages/motion/src/components/animate-presence/AnimatePresence.vue

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<script setup lang="ts">
2-
import { Transition, TransitionGroup, computed, onMounted, onUnmounted } from 'vue'
2+
import { Transition, TransitionGroup, computed, onUnmounted } from 'vue'
33
import { mountedStates } from '@/state'
4-
import { doneCallbacks, provideAnimatePresence, removeDoneCallback } from '@/components/presence'
4+
import { doneCallbacks, removeDoneCallback } from '@/components/animate-presence/presence'
55
import type { AnimatePresenceProps } from './types'
66
import { usePopLayout } from './use-pop-layout'
77
import { delay } from '@/utils/delay'
8+
import { useAnimatePresence } from './presence'
89
910
defineOptions({
1011
name: 'AnimatePresence',
@@ -17,15 +18,11 @@ const props = withDefaults(defineProps<AnimatePresenceProps>(), {
1718
anchorX: 'left',
1819
})
1920
20-
const presenceContext = {
21-
initial: props.initial,
22-
custom: props.custom,
23-
}
24-
provideAnimatePresence(presenceContext)
21+
/**
22+
* Provide the presence context to the children
23+
*/
24+
useAnimatePresence(props)
2525
26-
onMounted(() => {
27-
presenceContext.initial = undefined
28-
})
2926
const { addPopStyle, removePopStyle, styles } = usePopLayout(props)
3027
3128
function findMotionElement(el: Element): Element | null {

packages/motion/src/components/presence.ts renamed to packages/motion/src/components/animate-presence/presence.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import type { AnimatePresenceProps } from '@/components/animate-presence/types'
12
import { createContext } from '@/utils'
3+
import { onMounted, watch } from 'vue'
24

35
export const doneCallbacks = new WeakMap<Element, (v?: any, safeUnmount?: boolean) => void>()
46

@@ -16,3 +18,21 @@ export interface PresenceContext {
1618
}
1719

1820
export const [injectAnimatePresence, provideAnimatePresence] = createContext<PresenceContext>('AnimatePresenceContext')
21+
22+
export function useAnimatePresence(props: AnimatePresenceProps) {
23+
const presenceContext = {
24+
initial: props.initial,
25+
custom: props.custom,
26+
}
27+
watch(() => props.custom, (v) => {
28+
presenceContext.custom = v
29+
}, {
30+
flush: 'pre',
31+
})
32+
33+
provideAnimatePresence(presenceContext)
34+
35+
onMounted(() => {
36+
presenceContext.initial = undefined
37+
})
38+
}

packages/motion/src/components/motion/use-motion-state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { useLazyMotionContext } from '@/components/lazy-motion/context'
44
import { useMotionConfig } from '@/components/motion-config'
55
import type { MotionProps } from '@/components/motion/types'
66
import { checkMotionIsHidden } from '@/components/motion/utils'
7-
import { injectAnimatePresence } from '@/components/presence'
7+
import { injectAnimatePresence } from '@/components/animate-presence/presence'
88
import { MotionState } from '@/state'
99
import { convertSvgStyleToAttributes, createStyles } from '@/state/style'
1010
import type { DOMKeyframesDefinition } from 'framer-motion'

packages/motion/src/features/animation/animation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export class AnimationFeature extends Feature {
208208
let variantTransition = this.state.options.transition
209209
let variant: Variant = {}
210210
const { variants, custom, transition, animatePresenceContext } = this.state.options
211-
const customValue = isDef(custom) ? custom : animatePresenceContext?.custom
211+
const customValue = custom ?? animatePresenceContext?.custom
212212

213213
this.state.activeStates = { ...this.state.activeStates, ...controlActiveState }
214214
STATE_TYPES.forEach((name) => {

packages/motion/src/features/layout/projection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { getClosestProjectingNode } from '@/features/layout/utils'
44
import { addScaleCorrector } from 'framer-motion/dist/es/projection/styles/scale-correction.mjs'
55
import { defaultScaleCorrector } from '@/features/layout/config'
66
import { isHTMLElement } from '@/features/gestures/drag/utils/is'
7-
import { doneCallbacks } from '@/components/presence'
7+
import { doneCallbacks } from '@/components/animate-presence/presence'
88

99
export class ProjectionFeature extends Feature {
1010
constructor(state) {

packages/motion/src/state/motion-state.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { cancelFrame, frame, noop } from 'framer-motion/dom'
55
import { isAnimateChanged, isSVGElement, resolveVariant } from '@/state/utils'
66
import type { Feature, StateType } from '@/features'
77
import { FeatureManager } from '@/features'
8-
import type { PresenceContext } from '@/components/presence'
9-
import { doneCallbacks } from '@/components/presence'
8+
import type { PresenceContext } from '@/components/animate-presence/presence'
9+
import { doneCallbacks } from '@/components/animate-presence/presence'
1010
import type { AnimateUpdates } from '@/features/animation/types'
1111
import { isVariantLabels } from '@/state/utils/is-variant-labels'
1212
import type { LazyMotionContext } from '@/components/lazy-motion/context'
@@ -103,10 +103,11 @@ export class MotionState {
103103

104104
// Initialize animation target values
105105
private initTarget(initialVariantSource: string[]) {
106+
const custom = this.options.custom ?? this.options.animatePresenceContext?.custom
106107
this.baseTarget = initialVariantSource.reduce((acc, variant) => {
107108
return {
108109
...acc,
109-
...resolveVariant(this.options[variant] || this.context[variant], this.options.variants),
110+
...resolveVariant(this.options[variant] || this.context[variant], this.options.variants, custom),
110111
}
111112
}, {})
112113
this.target = { }

packages/motion/src/state/utils/is-present.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { doneCallbacks } from '@/components/presence'
1+
import { doneCallbacks } from '@/components/animate-presence/presence'
22
import type { VisualElement } from 'framer-motion'
33

44
export function isPresent(visualElement: VisualElement) {

0 commit comments

Comments
 (0)