Please use Desktop to view and interact with components
Microinteractions200msspring
Magnetic Cursor Link
Link that slightly follows mouse cursor within boundary and snaps back
magneticcursorlinkinteractive
Useto navigate between components
Preview
Magnetic Link
Code
TypeScript + React
'use client'
import { useState, useRef, MouseEvent } from 'react'
import { motion, useMotionValue, useSpring, useTransform } from 'framer-motion'
export function MagneticCursorLink() {
const ref = useRef<HTMLDivElement>(null)
const [isHovered, setIsHovered] = useState(false)
const x = useMotionValue(0)
const y = useMotionValue(0)
const springConfig = { stiffness: 300, damping: 30 }
const springX = useSpring(x, springConfig)
const springY = useSpring(y, springConfig)
const translateX = useTransform(springX, (latest) => latest * 0.3)
const translateY = useTransform(springY, (latest) => latest * 0.3)
const handleMouseMove = (e: MouseEvent<HTMLDivElement>) => {
if (!ref.current) return
const rect = ref.current.getBoundingClientRect()
const centerX = rect.left + rect.width / 2
const centerY = rect.top + rect.height / 2
const deltaX = e.clientX - centerX
const deltaY = e.clientY - centerY
x.set(deltaX)
y.set(deltaY)
}
const handleMouseLeave = () => {
x.set(0)
y.set(0)
setIsHovered(false)
}
return (
<motion.div
ref={ref}
onMouseMove={handleMouseMove}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={handleMouseLeave}
style={{
x: translateX,
y: translateY,
}}
whileHover={{ scale: 1.05 }}
whileTap={{ scale: 0.95 }}
transition={{ type: 'spring', stiffness: 400, damping: 25 }}
className="inline-block cursor-pointer rounded-lg bg-primary px-6 py-3 text-primary-foreground"
>
Magnetic Link
</motion.div>
)
}
How to Use
- 1Install Framer Motion:
npm install framer-motion - 2Copy the code from above
- 3Paste it into your project and customize as needed
- 4Colors are customizable via Tailwind CSS classes. The default theme uses dark mode colors defined in your globals.css file