Skip to content

Commit

Permalink
add custom cursor
Browse files Browse the repository at this point in the history
  • Loading branch information
yinkakun committed Oct 23, 2022
1 parent 6acaa2b commit 78e9220
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions storefront/components/layout/custom-cursor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { useState, useCallback, useEffect } from 'react';
import { motion, useAnimation } from 'framer-motion';

export const CustomCursor = () => {
const controls = useAnimation();
const [cursorHasMoved, setCursorHasMoved] = useState(false);

const CUSTOM_CURSOR_RADIUS = 40;

const trailMouse = ({ clientX, clientY }: MouseEvent) => {
controls.start({
x: clientX - CUSTOM_CURSOR_RADIUS,
y: clientY - CUSTOM_CURSOR_RADIUS,
transition: {
ease: 'backOut',
duration: cursorHasMoved ? 0.7 : 0,
},
});

if (!cursorHasMoved) {
setCursorHasMoved(true);
}
};

const handleMouseMove = useCallback(trailMouse, [cursorHasMoved]);

useEffect(() => {
window.addEventListener('mousemove', handleMouseMove, false);

return () => {
window.removeEventListener('mousemove', handleMouseMove, false);
};
}, [handleMouseMove]);

return (
<div
className={`pointer-events-none fixed inset-0 z-[1000] h-full w-full overflow-hidden ${
cursorHasMoved ? 'visible' : 'invisible'
}`}
>
<motion.div
animate={controls}
className="absolute z-[200] h-20 w-20 -translate-x-1/2 -translate-y-1/2 rounded-full border border-monster-green-300 border-opacity-100 bg-monster-green-300 bg-opacity-30"
></motion.div>
</div>
);
};

0 comments on commit 78e9220

Please sign in to comment.