From c42dc1071a079c01313034582968f27745672968 Mon Sep 17 00:00:00 2001 From: dragoni7 Date: Sun, 22 Sep 2024 11:38:55 -0700 Subject: [PATCH] cursor tweaks --- src/components/CustomCursor.tsx | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/components/CustomCursor.tsx b/src/components/CustomCursor.tsx index a559a62..4c60db6 100644 --- a/src/components/CustomCursor.tsx +++ b/src/components/CustomCursor.tsx @@ -1,8 +1,9 @@ -import cursorImage from '../../public/assets/cursor.png'; -import React, { useEffect, useRef } from 'react'; +import cursorImage from '/assets/cursor.png'; +import React, { useEffect, useRef, useState } from 'react'; const CustomCursor: React.FC = () => { const cursorRef = useRef(null); + const [visible, setVisible] = useState(true); useEffect(() => { const cursor = cursorRef.current; @@ -17,18 +18,34 @@ const CustomCursor: React.FC = () => { document.head.appendChild(style); const updateCursor = (e: MouseEvent) => { + if (!visible) return; requestAnimationFrame(() => { cursor.style.transform = `translate(${e.clientX}px, ${e.clientY}px)`; }); }; window.addEventListener('mousemove', updateCursor); + document.addEventListener('mouseleave', () => setVisible(false)); + document.addEventListener('mouseenter', () => setVisible(true)); + + const handleVisibilityChange = () => { + if (document.hidden) { + setVisible(false); + } else { + setVisible(true); + } + }; + + document.addEventListener('visibilitychange', handleVisibilityChange); return () => { window.removeEventListener('mousemove', updateCursor); + document.removeEventListener('mouseleave', () => setVisible(false)); + document.removeEventListener('mouseenter', () => setVisible(true)); + document.removeEventListener('visibilitychange', handleVisibilityChange); document.head.removeChild(style); }; - }, []); + }, [visible]); return (
{ height: '32px', position: 'fixed', pointerEvents: 'none', - zIndex: 9999, + zIndex: 99999, backgroundImage: `url(${cursorImage})`, backgroundSize: 'contain', backgroundRepeat: 'no-repeat', top: 0, left: 0, - transform: 'translate(-50%, -50%)', - transition: 'transform 0.1s ease-out', + transition: 'transform 0.05s ease-out', willChange: 'transform', + display: visible ? 'block' : 'none', }} /> );