All files / src/render/utils setters.ts

94.2% Statements 65/69
82.26% Branches 51/62
100% Functions 10/10
96.83% Lines 61/63

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 15838x               38x 38x 38x 38x 38x 38x     38x                     25x 25x           38x       99x   99x 99x 99x 99x   99x   99x 25x 25x         4x   4x 4x 4x   4x 2x         38x       10x   10x 2x   8x       38x         265x 287x     265x 265x   110x 122x 122x 122x           122x 6x               122x 116x             122x   122x         8x 114x 1x     122x 122x 122x       38x 305x   305x 305x     38x         364x   364x 305x         364x    
import { complex } from "style-value-types"
import {
    Target,
    TargetAndTransition,
    TargetResolver,
    TargetWithKeyframes,
    Transition,
} from "../../types"
import { isNumericalString } from "../../utils/is-numerical-string"
import { isZeroValueString } from "../../utils/is-zero-value-string"
import { resolveFinalValueInKeyframes } from "../../utils/resolve-value"
import { motionValue } from "../../value"
import { getAnimatableNone } from "../dom/value-types/animatable-none"
import { findValueType } from "../dom/value-types/find"
import { ResolvedValues, VisualElement } from "../types"
import { AnimationDefinition } from "./animation"
import { resolveVariant } from "./variants"
 
/**
 * Set VisualElement's MotionValue, creating a new MotionValue for it if
 * it doesn't exist.
 */
function setMotionValue(
    visualElement: VisualElement,
    key: string,
    value: string | number
) {
    Eif (visualElement.hasValue(key)) {
        visualElement.getValue(key)!.set(value)
    } else {
        visualElement.addValue(key, motionValue(value))
    }
}
 
export function setTarget(
    visualElement: VisualElement,
    definition: string | TargetAndTransition | TargetResolver
) {
    const resolved = resolveVariant(visualElement, definition)
    let {
        transitionEnd = {},
        transition = {},
        ...target
    } = resolved ? visualElement.makeTargetAnimatable(resolved, false) : {}
 
    target = { ...target, ...transitionEnd }
 
    for (const key in target) {
        const value = resolveFinalValueInKeyframes(target[key])
        setMotionValue(visualElement, key, value as string | number)
    }
}
 
function setVariants(visualElement: VisualElement, variantLabels: string[]) {
    const reversedLabels = [...variantLabels].reverse()
 
    reversedLabels.forEach((key) => {
        const variant = visualElement.getVariant(key)
        variant && setTarget(visualElement, variant)
 
        visualElement.variantChildren?.forEach((child) => {
            setVariants(child, variantLabels)
        })
    })
}
 
export function setValues(
    visualElement: VisualElement,
    definition: AnimationDefinition
) {
    Iif (Array.isArray(definition)) {
        return setVariants(visualElement, definition)
    } else if (typeof definition === "string") {
        return setVariants(visualElement, [definition])
    } else {
        setTarget(visualElement, definition as any)
    }
}
 
export function checkTargetForNewValues(
    visualElement: VisualElement,
    target: TargetWithKeyframes,
    origin: ResolvedValues
) {
    const newValueKeys = Object.keys(target).filter(
        (key) => !visualElement.hasValue(key)
    )
 
    const numNewValues = newValueKeys.length
    if (!numNewValues) return
 
    for (let i = 0; i < numNewValues; i++) {
        const key = newValueKeys[i]
        const targetValue = target[key]
        let value: string | number | null = null
 
        /**
         * If the target is a series of keyframes, we can use the first value
         * in the array. If this first value is null, we'll still need to read from the DOM.
         */
        if (Array.isArray(targetValue)) {
            value = targetValue[0]
        }
 
        /**
         * If the target isn't keyframes, or the first keyframe was null, we need to
         * first check if an origin value was explicitly defined in the transition as "from",
         * if not read the value from the DOM. As an absolute fallback, take the defined target value.
         */
        if (value === null) {
            value = origin[key] ?? visualElement.readValue(key) ?? target[key]
        }
 
        /**
         * If value is still undefined or null, ignore it. Preferably this would throw,
         * but this was causing issues in Framer.
         */
        Iif (value === undefined || value === null) continue
 
        if (
            typeof value === "string" &&
            (isNumericalString(value) || isZeroValueString(value))
        ) {
            // If this is a number read as a string, ie "0" or "200", convert it to a number
            value = parseFloat(value)
        } else if (!findValueType(value) && complex.test(targetValue)) {
            value = getAnimatableNone(key, targetValue)
        }
 
        visualElement.addValue(key, motionValue(value))
        ;(origin as any)[key] ??= value
        visualElement.setBaseTarget(key, value)
    }
}
 
export function getOriginFromTransition(key: string, transition: Transition) {
    Iif (!transition) return
    const valueTransition =
        transition[key] || transition["default"] || transition
    return valueTransition.from
}
 
export function getOrigin(
    target: Target,
    transition: Transition,
    visualElement: VisualElement
) {
    const origin: Target = {}
 
    for (const key in target) {
        origin[key] =
            getOriginFromTransition(key, transition) ??
            visualElement.getValue(key)?.get()
    }
 
    return origin
}