All files / src/render/utils motion-values.ts

94.74% Statements 18/19
87.5% Branches 14/16
100% Functions 1/1
100% Lines 18/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53  36x 36x     36x         751x 383x 383x   383x         356x 27x         3x 24x           16x 4x   4x   12x                 751x 205x     751x    
import { MotionStyle } from "../../motion/types"
import { motionValue } from "../../value"
import { isMotionValue } from "../../value/utils/is-motion-value"
import { VisualElement } from "../types"
 
export function updateMotionValuesFromProps(
    element: VisualElement,
    next: MotionStyle,
    prev: MotionStyle
) {
    for (const key in next) {
        const nextValue = next[key]
        const prevValue = prev[key]
 
        if (isMotionValue(nextValue)) {
            /**
             * If this is a motion value found in props or style, we want to add it
             * to our visual element's motion value map.
             */
            element.addValue(key, nextValue)
        } else if (isMotionValue(prevValue)) {
            /**
             * If we're swapping to a new motion value, create a new motion value
             * from that
             */
            element.addValue(key, motionValue(nextValue))
        } else if (prevValue !== nextValue) {
            /**
             * If this is a flat value that has changed, update the motion value
             * or create one if it doesn't exist. We only want to do this if we're
             * not handling the value with our animation state.
             */
            if (element.hasValue(key)) {
                const existingValue = element.getValue(key)!
                // TODO: Only update values that aren't being animated or even looked at
                !existingValue.hasAnimated && existingValue.set(nextValue)
            } else {
                element.addValue(
                    key,
                    motionValue(element.getStaticValue(key) ?? nextValue)
                )
            }
        }
    }
 
    // Handle removed values
    for (const key in prev) {
        Iif (next[key] === undefined) element.removeValue(key)
    }
 
    return next
}