UNPKG

framer-motion

Version:

A simple and powerful JavaScript animation library

1 lines 2.29 kB
{"version":3,"file":"use-spring.mjs","sources":["../../../src/value/use-spring.ts"],"sourcesContent":["\"use client\"\n\nimport {\n AnyResolvedKeyframe,\n attachSpring,\n isMotionValue,\n MotionValue,\n SpringOptions,\n} from \"motion-dom\"\nimport { useContext, useInsertionEffect } from \"react\"\nimport { MotionConfigContext } from \"../context/MotionConfigContext\"\nimport { useMotionValue } from \"./use-motion-value\"\nimport { useTransform } from \"./use-transform\"\n\n/**\n * Creates a `MotionValue` that, when `set`, will use a spring animation to animate to its new state.\n *\n * It can either work as a stand-alone `MotionValue` by initialising it with a value, or as a subscriber\n * to another `MotionValue`.\n *\n * @remarks\n *\n * ```jsx\n * const x = useSpring(0, { stiffness: 300 })\n * const y = useSpring(x, { damping: 10 })\n * ```\n *\n * @param inputValue - `MotionValue` or number. If provided a `MotionValue`, when the input `MotionValue` changes, the created `MotionValue` will spring towards that value.\n * @param springConfig - Configuration options for the spring.\n * @returns `MotionValue`\n *\n * @public\n */\nexport function useSpring(\n source: MotionValue<string>,\n options?: SpringOptions\n): MotionValue<string>\nexport function useSpring(\n source: string,\n options?: SpringOptions\n): MotionValue<string>\nexport function useSpring(\n source: MotionValue<number>,\n options?: SpringOptions\n): MotionValue<number>\nexport function useSpring(\n source: number,\n options?: SpringOptions\n): MotionValue<number>\nexport function useSpring(\n source: MotionValue<string> | MotionValue<number> | AnyResolvedKeyframe,\n options: SpringOptions = {}\n) {\n const { isStatic } = useContext(MotionConfigContext)\n const getFromSource = () => (isMotionValue(source) ? source.get() : source)\n\n // isStatic will never change, allowing early hooks return\n if (isStatic) {\n return useTransform(getFromSource)\n }\n\n const value = useMotionValue(getFromSource())\n\n useInsertionEffect(() => {\n return attachSpring(value, source, options)\n }, [value, JSON.stringify(options)])\n\n return value\n}\n"],"names":[],"mappings":";;;;;;;;;;;;AA0DQ;;AAGJ;;;AAIA;AAEA;AACJ;;"}