Skip to content

Instantly share code, notes, and snippets.

@kiritocode1
Created November 19, 2024 18:29
Show Gist options
  • Select an option

  • Save kiritocode1/06e8b38c64728ae77c78449056ca773c to your computer and use it in GitHub Desktop.

Select an option

Save kiritocode1/06e8b38c64728ae77c78449056ca773c to your computer and use it in GitHub Desktop.
// @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