Use Elastic Line Events
fancy/use-elastic-line-eventsFreeHook
A hook component.
Install it
npx shadcn@latest add https://www.fancycomponents.dev/r/use-elastic-line-events.jsonSource
1import { useEffect, useState } from "react"23import { useDimensions } from "@/hooks/use-dimensions"4import { useMousePosition } from "@/hooks/use-mouse-position"56interface ElasticLineEvents {7 isGrabbed: boolean8 controlPoint: { x: number; y: number }9}1011export function useElasticLineEvents(12 containerRef: React.RefObject<SVGSVGElement | null>,13 isVertical: boolean,14 grabThreshold: number,15 releaseThreshold: number16): ElasticLineEvents {17 const mousePosition = useMousePosition(containerRef)18 const dimensions = useDimensions(containerRef)19 const [isGrabbed, setIsGrabbed] = useState(false)20 const [controlPoint, setControlPoint] = useState({21 x: dimensions.width / 2,22 y: dimensions.height / 2,23 })2425 useEffect(() => {26 if (containerRef.current) {27 const { width, height } = dimensions28 const x = mousePosition.x29 const y = mousePosition.y3031 // Check if mouse is outside container bounds32 const isOutsideBounds = x < 0 || x > width || y < 0 || y > height3334 if (isOutsideBounds) {35 setIsGrabbed(false)36 return37 }3839 let distance: number40 let newControlPoint: { x: number; y: number }4142 if (isVertical) {43 const midX = width / 244 distance = Math.abs(x - midX)45 newControlPoint = {46 x: midX + 2.2 * (x - midX),47 y: y,48 }49 } else {50 const midY = height / 251 distance = Math.abs(y - midY)52 newControlPoint = {53 x: x,54 y: midY + 2.2 * (y - midY),55 }56 }5758 setControlPoint(newControlPoint)5960 if (!isGrabbed && distance < grabThreshold) {61 setIsGrabbed(true)62 } else if (isGrabbed && distance > releaseThreshold) {63 setIsGrabbed(false)64 }65 }66 }, [mousePosition, isVertical, isGrabbed, grabThreshold, releaseThreshold])6768 return { isGrabbed, controlPoint }69}
Files it writes
More from fancy
All 158 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Use Debounced Dimensionsuse-debounced-dimensions | fancy | Hooks | Free | no deps | |
| Use Detect Browseruse-detect-browser | fancy | Hooks | Free | no deps | |
| Use Dimensionsuse-dimensions | fancy | Hooks | Free | no deps | |
| Use Line Breakdownuse-line-breakdown | fancy | Hooks | Free | no deps | |
| Use Line Countuse-line-count | fancy | Hooks | Free | no deps | |
| Use Mounteduse-mounted | fancy | Hooks | Free | no deps | |
| Use Mouse Positionuse-mouse-position | fancy | Hooks | Free | no deps | |
| Use Mouse Position Refuse-mouse-position-ref | fancy | Hooks | Free | no deps |
