Skip to content

Commit c72ea5c

Browse files
committed
Updating
1 parent 6348c49 commit c72ea5c

File tree

5 files changed

+61
-6
lines changed

5 files changed

+61
-6
lines changed

packages/framer-motion/src/animation/interfaces/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export type VisualElementAnimationOptions = {
55
delay?: number
66
transitionOverride?: Transition
77
custom?: any
8-
type?: AnimationType
8+
type?: AnimationType | "initial"
99
}

packages/framer-motion/src/motion/__tests__/transition-from.test.tsx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,46 @@ describe("transitionFrom", () => {
120120

121121
return expect(promise).resolves.toEqual([0.5, 0, 0.5])
122122
})
123+
124+
test("transitionFrom works with gestures and initial", async () => {
125+
return new Promise<void>((resolve) => {
126+
const Component = () => (
127+
<motion.div
128+
whileHover={{
129+
opacity: 0,
130+
transitionFrom: {
131+
initial: { type: false },
132+
},
133+
}}
134+
initial={{
135+
transitionFrom: {
136+
initial: { type: false },
137+
whileHover: { type: false },
138+
},
139+
}}
140+
style={{ opacity: 0.5 }}
141+
/>
142+
)
143+
144+
const { container, rerender } = render(<Component />)
145+
rerender(<Component />)
146+
147+
expect(container.firstChild).toHaveStyle("opacity: 0.5")
148+
149+
pointerEnter(container.firstChild as Element)
150+
151+
frame.postRender(() => {
152+
expect(container.firstChild).toHaveStyle("opacity: 0")
153+
154+
setTimeout(() => {
155+
pointerLeave(container.firstChild as Element)
156+
157+
frame.postRender(() => {
158+
expect(container.firstChild).toHaveStyle("opacity: 0.5")
159+
resolve()
160+
})
161+
}, 10)
162+
})
163+
})
164+
})
123165
})

packages/framer-motion/src/motion/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export interface AnimationProps {
142142
* <motion.div initial={false} animate={{ opacity: 0 }} />
143143
* ```
144144
*/
145-
initial?: boolean | Target | VariantLabels
145+
initial?: boolean | TargetAndTransition | VariantLabels
146146

147147
/**
148148
* Values to animate to, variant label(s), or `AnimationControls`.

packages/framer-motion/src/render/utils/animation-state.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function createAnimationState(
128128
* lower priority props and flag for animation.
129129
*/
130130
for (let i = 0; i < numAnimationTypes; i++) {
131-
const type = reversePriorityOrder[i]
131+
const type: AnimationType | "initial" = reversePriorityOrder[i]
132132
const typeState = state[type]
133133
const prop = props[type] !== undefined ? props[type] : context[type]
134134
const propIsVariant = isVariantLabel(prop)
@@ -325,16 +325,29 @@ export function createAnimationState(
325325
* defined in the style prop, or the last read value.
326326
*/
327327
if (removedKeys.size) {
328-
const fallbackAnimation = {}
328+
const fallbackAnimation: TargetAndTransition = {}
329329
removedKeys.forEach((key) => {
330330
const fallbackTarget = visualElement.getBaseTarget(key)
331331

332332
if (fallbackTarget !== undefined) {
333333
fallbackAnimation[key] = fallbackTarget
334334
}
335335
})
336+
const { initial } = props
337+
if (initial && typeof initial !== "boolean") {
338+
const { transition, transitionFrom } =
339+
resolveVariant(
340+
visualElement,
341+
Array.isArray(initial) ? initial[0] : initial
342+
) || {}
343+
fallbackAnimation.transition = transition
344+
fallbackAnimation.transitionFrom = transitionFrom
345+
}
336346

337-
animations.push({ animation: fallbackAnimation })
347+
animations.push({
348+
animation: fallbackAnimation,
349+
options: { type: "initial" },
350+
})
338351
}
339352

340353
let shouldAnimate = Boolean(animations.length)

packages/framer-motion/src/value/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class MotionValue<V = any> {
6868
/**
6969
* The type of animation responsible for the MotionValue's current state.
7070
*/
71-
type?: AnimationType
71+
type?: AnimationType | "initial"
7272

7373
/**
7474
* If a MotionValue has an owner, it was created internally within Framer Motion

0 commit comments

Comments
 (0)