All files / src/animations/utils detect-animation-from-options.ts

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                1x               8x         2x 6x       3x           3x 3x 1x 2x             1x     1x    
import { spring } from "../generators/spring"
import { keyframes } from "../generators/keyframes"
import { decay } from "../generators/decay"
 
/**
 * These are the default types of animation included with animate.
 * TODO: Consider removing decay
 */
const types = { keyframes, spring, decay }
 
interface Options {
    to?: any
    type?: "decay" | "keyframes" | "spring"
}
 
export function detectAnimationFromOptions<T extends Options>(config: T) {
    if (Array.isArray(config.to)) {
        /**
         * If to is defined as a keyframes array we want to force this to be a keyframes
         * animation. In the future it might be possible to allow spring keyframes.
         */
        return keyframes
    } else if (types[config.type]) {
        /**
         * Or if the user has explicity defined their own animation type, return that.
         */
        return types[config.type]
    }
 
    /**
     * Attempt to detect which animation to use based on the options provided
     */
    const keys = new Set(Object.keys(config))
    if (keys.has("ease") || keys.has("duration")) {
        return keyframes
    } else if (
        keys.has("stiffness") ||
        keys.has("mass") ||
        keys.has("damping") ||
        keys.has("restSpeed") ||
        keys.has("restDelta")
    ) {
        return spring
    }
 
    return keyframes
}