Created
November 19, 2024 18:29
-
-
Save kiritocode1/06e8b38c64728ae77c78449056ca773c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @ts-nocheck | |
| "use client"; | |
| import { useRef, useState } from 'react' | |
| import { motion } from 'framer-motion'; | |
| export default function MagneticFramer({children}) { | |
| const ref = useRef(null); | |
| const [position, setPosition] = useState({x:0,y:0}); | |
| const handleMouse = (e) => { | |
| const { clientX, clientY } = e; | |
| const {height, width, left, top} = ref.current.getBoundingClientRect(); | |
| const middleX = clientX - (left + width/2) | |
| const middleY = clientY - (top + height/2) | |
| setPosition({x: middleX, y: middleY}) | |
| } | |
| const reset = () => { | |
| setPosition({x:0, y:0}) | |
| } | |
| const { x, y } = position; | |
| return ( | |
| <motion.div | |
| style={{position: "relative"}} | |
| ref={ref} | |
| onMouseMove={handleMouse} | |
| onMouseLeave={reset} | |
| animate={{x, y}} | |
| transition={{type: "spring", stiffness: 150, damping: 15, mass: 0.1}} | |
| > | |
| {children} | |
| </motion.div> | |
| ) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment