Skip to content

Commit

Permalink
Simplify useSpring (#3023)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry authored Jan 23, 2025
1 parent 0a96061 commit 260199f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 15 deletions.
9 changes: 5 additions & 4 deletions dev/react/src/examples/useSpring.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { frame, motion, useMotionValue, useSpring } from "framer-motion"
import { useEffect, useRef, useState } from "react"
import { motion, useMotionValue, useSpring } from "framer-motion"
import { useRef, useState } from "react"

const spring = {
stiffness: 300,
Expand All @@ -13,9 +13,10 @@ function DragExample() {
const dragY = useMotionValue(0)
const x = useSpring(dragX, spring)
const y = useSpring(dragY, spring)

return (
<motion.div
drag="x"
drag
dragMomentum={false}
_dragX={dragX}
_dragY={dragY}
Expand Down Expand Up @@ -98,7 +99,7 @@ function MouseEventExample() {
return (
<motion.div
ref={ref}
style={{ width: 100, height: 100, background: "yellow", x }}
style={{ width: 100, height: 100, background: "yellow", x, y }}
onTapStart={startPointer}
onTapCancel={cancelPointer}
onTap={cancelPointer}
Expand Down
13 changes: 2 additions & 11 deletions packages/framer-motion/src/value/use-spring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
animateValue,
} from "../animation/animators/MainThreadAnimation"
import { MotionConfigContext } from "../context/MotionConfigContext"
import { frame, frameData } from "../frameloop"
import { frame } from "../frameloop"
import { useIsomorphicLayoutEffect } from "../utils/use-isomorphic-effect"
import { MotionValue } from "../value"
import { useMotionValue } from "./use-motion-value"
Expand Down Expand Up @@ -50,15 +50,6 @@ export function useSpring(
const latestSetter = useRef<(v: number) => void>(() => {})

const startAnimation = () => {
/**
* If the previous animation hasn't had the chance to even render a frame, render it now.
*/
const animation = activeSpringAnimation.current

if (animation && animation.time === 0) {
animation.sample(frameData.delta)
}

stopAnimation()

activeSpringAnimation.current = animateValue({
Expand Down Expand Up @@ -89,7 +80,7 @@ export function useSpring(
latestValue.current = v
latestSetter.current = set

frame.update(startAnimation)
frame.postRender(startAnimation)

return value.get()
}, stopAnimation)
Expand Down

0 comments on commit 260199f

Please sign in to comment.