Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry committed Oct 25, 2024
1 parent f4c72c8 commit 5507ae3
Showing 1 changed file with 27 additions and 41 deletions.
68 changes: 27 additions & 41 deletions packages/framer-motion/src/motion/utils/use-visual-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
isControllingVariants as checkIsControllingVariants,
isVariantNode as checkIsVariantNode,
} from "../../render/utils/is-controlling-variants"
import { TargetAndTransition } from "../../types"

export interface VisualState<Instance, RenderState> {
renderState: RenderState
Expand Down Expand Up @@ -74,24 +73,6 @@ export const makeUseVisualState =
return isStatic ? make() : useConstant(make)
}

function forEachDefinition(
props: MotionProps,
definition: MotionProps["animate"] | MotionProps["initial"],
callback: (
target: TargetAndTransition,
transitionEnd: ResolvedValues
) => void
) {
const list = Array.isArray(definition) ? definition : [definition]
for (let i = 0; i < list.length; i++) {
const resolved = resolveVariantFromProps(props, list[i] as any)
if (resolved) {
const { transitionEnd, transition, ...target } = resolved
callback(target, transitionEnd as ResolvedValues)
}
}
}

function makeLatestValues(
props: MotionProps,
context: MotionContextProps,
Expand Down Expand Up @@ -131,31 +112,36 @@ function makeLatestValues(
typeof variantToSet !== "boolean" &&
!isAnimationControls(variantToSet)
) {
forEachDefinition(props, variantToSet, (target, transitionEnd) => {
for (const key in target) {
let valueTarget = target[key as keyof typeof target]

if (Array.isArray(valueTarget)) {
/**
* Take final keyframe if the initial animation is blocked because
* we want to initialise at the end of that blocked animation.
*/
const index = isInitialAnimationBlocked
? valueTarget.length - 1
: 0
valueTarget = valueTarget[index]
const list = Array.isArray(variantToSet) ? variantToSet : [variantToSet]
for (let i = 0; i < list.length; i++) {
const resolved = resolveVariantFromProps(props, list[i] as any)
if (resolved) {
const { transitionEnd, transition, ...target } = resolved
for (const key in target) {
let valueTarget = target[key as keyof typeof target]

if (Array.isArray(valueTarget)) {
/**
* Take final keyframe if the initial animation is blocked because
* we want to initialise at the end of that blocked animation.
*/
const index = isInitialAnimationBlocked
? valueTarget.length - 1
: 0
valueTarget = valueTarget[index]
}

if (valueTarget !== null) {
values[key] = valueTarget as string | number
}
}

if (valueTarget !== null) {
values[key] = valueTarget as string | number
for (const key in transitionEnd) {
values[key] = transitionEnd[
key as keyof typeof transitionEnd
] as string | number
}
}
for (const key in transitionEnd) {
values[key] = transitionEnd[
key as keyof typeof transitionEnd
] as string | number
}
})
}
}

return values
Expand Down

0 comments on commit 5507ae3

Please sign in to comment.