UNPKG

framer-motion

Version:

A simple and powerful JavaScript animation library

1 lines 1.37 kB
{"version":3,"file":"use-velocity.mjs","sources":["../../../src/value/use-velocity.ts"],"sourcesContent":["\"use client\"\n\nimport { frame, MotionValue } from \"motion-dom\"\nimport { useMotionValueEvent } from \"../utils/use-motion-value-event\"\nimport { useMotionValue } from \"./use-motion-value\"\n/**\n * Creates a `MotionValue` that updates when the velocity of the provided `MotionValue` changes.\n *\n * ```javascript\n * const x = useMotionValue(0)\n * const xVelocity = useVelocity(x)\n * const xAcceleration = useVelocity(xVelocity)\n * ```\n *\n * @public\n */\nexport function useVelocity(value: MotionValue<number>): MotionValue<number> {\n const velocity = useMotionValue(value.getVelocity())\n\n const updateVelocity = () => {\n const latest = value.getVelocity()\n velocity.set(latest)\n\n /**\n * If we still have velocity, schedule an update for the next frame\n * to keep checking until it is zero.\n */\n if (latest) frame.update(updateVelocity)\n }\n\n useMotionValueEvent(value, \"change\", () => {\n // Schedule an update to this value at the end of the current frame.\n frame.update(updateVelocity, false, true)\n })\n\n return velocity\n}\n"],"names":[],"mappings":";;;;;AAKA;;;;;;;;;;AAUG;AACG;;;AAIE;AACA;AAEA;;;AAGG;AACH;AAAY;AAChB;AAEA;;;AAGA;AAEA;AACJ;;"}