({
isVertical = false,
grabThreshold = 5,
releaseThreshold = 100,
strokeWidth = 1,
transition = {
type: "spring",
stiffness: 300,
damping: 5,
},
animateInTransition = {
duration: 0.3,
ease: "easeInOut",
},
className,
})
| 23 | } |
| 24 | |
| 25 | const ElasticLine: React.FC<ElasticLineProps> = ({ |
| 26 | isVertical = false, |
| 27 | grabThreshold = 5, |
| 28 | releaseThreshold = 100, |
| 29 | strokeWidth = 1, |
| 30 | transition = { |
| 31 | type: "spring", |
| 32 | stiffness: 300, |
| 33 | damping: 5, |
| 34 | }, |
| 35 | animateInTransition = { |
| 36 | duration: 0.3, |
| 37 | ease: "easeInOut", |
| 38 | }, |
| 39 | className, |
| 40 | }) => { |
| 41 | const containerRef = useRef<SVGSVGElement>(null) |
| 42 | const dimensions = useDimensions(containerRef) |
| 43 | const pathRef = useRef<SVGPathElement>(null) |
| 44 | const [hasAnimatedIn, setHasAnimatedIn] = useState(false) |
| 45 | |
| 46 | // Clamp releaseThreshold to container dimensions |
| 47 | const clampedReleaseThreshold = Math.min( |
| 48 | releaseThreshold, |
| 49 | isVertical ? dimensions.width / 2 : dimensions.height / 2 |
| 50 | ) |
| 51 | |
| 52 | const { isGrabbed, controlPoint } = useElasticLineEvents( |
| 53 | containerRef, |
| 54 | isVertical, |
| 55 | grabThreshold, |
| 56 | clampedReleaseThreshold |
| 57 | ) |
| 58 | |
| 59 | const x = useMotionValue(dimensions.width / 2) |
| 60 | const y = useMotionValue(dimensions.height / 2) |
| 61 | const pathLength = useMotionValue(0) |
| 62 | |
| 63 | useEffect(() => { |
| 64 | // Initial draw animation |
| 65 | if (!hasAnimatedIn && dimensions.width > 0 && dimensions.height > 0) { |
| 66 | animate(pathLength, 1, { |
| 67 | ...animateInTransition, |
| 68 | onComplete: () => setHasAnimatedIn(true), |
| 69 | }) |
| 70 | } |
| 71 | x.set(dimensions.width / 2) |
| 72 | y.set(dimensions.height / 2) |
| 73 | }, [dimensions, hasAnimatedIn]) |
| 74 | |
| 75 | useEffect(() => { |
| 76 | if (!isGrabbed && hasAnimatedIn) { |
| 77 | animate(x, dimensions.width / 2, transition) |
| 78 | animate(y, dimensions.height / 2, transition) |
| 79 | } |
| 80 | }, [isGrabbed]) |
| 81 | |
| 82 | useAnimationFrame(() => { |
nothing calls this directly
no test coverage detected