Skip to content

Commit

Permalink
[BUG] the cursor animation is bugged #594 (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravsingh1281 authored Nov 11, 2024
2 parents b0de08d + 1f8845d commit 1cd6ad3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,20 @@ html {

/* Custom Cursor Styling */
.custom-cursor {
position: fixed;
position: absolute;
width: 20px;
height: 20px;
border-radius: 50%;
background-color: green; /* Main cursor color */
pointer-events: none;
transform-origin: center;
transition: transform 0.1s ease;
transition: transform 0.1s ease, opacity 0.2s ease;
z-index: 9999; /* Ensure cursor is on top */
opacity: 1;
}

.custom-cursor.hidden {
opacity: 0;
}

/* Cursor Tail Styling */
Expand All @@ -94,5 +99,9 @@ html {
background-color: green; /* Tail color */
pointer-events: none;
z-index: 9998;
transition: transform 0.1s ease;
transition: transform 0.1s ease, opacity 0.2s ease;
}
.cursor-tail.hidden {
opacity: 0;
}

25 changes: 20 additions & 5 deletions src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import React, { useEffect, useState } from 'react';
import "./App.css";
import { Route, Routes } from "react-router-dom";
import './App.css';

// Pages and Components
import { Home, Login, Registration, Dashboard, ComingSoon } from "./pages";
Expand All @@ -27,6 +26,7 @@ const App = () => {
const [isPreloaderVisible, setIsPreloaderVisible] = useState(true);
const [cursorPos, setCursorPos] = useState({ x: 0, y: 0 });
const [trail, setTrail] = useState(Array(10).fill({ x: 0, y: 0 }));
const [isCursorVisible, setIsCursorVisible] = useState(true); // Controls visibility

// Hide Preloader after 5 seconds
useEffect(() => {
Expand All @@ -43,16 +43,31 @@ const App = () => {
setCursorPos({ x: e.clientX, y: e.clientY });
};

const handleMouseLeave = () => {
setIsCursorVisible(false);
};

const handleMouseEnter = () => {
setIsCursorVisible(true);
};

useEffect(() => {
window.addEventListener("mousemove", updateCursor);
return () => window.removeEventListener("mousemove", updateCursor);
document.addEventListener("mousemove", updateCursor);
document.addEventListener("mouseleave", handleMouseLeave);
document.addEventListener("mouseenter", handleMouseEnter);

return () => {
document.removeEventListener("mousemove", updateCursor);
document.removeEventListener("mouseleave", handleMouseLeave);
document.removeEventListener("mouseenter", handleMouseEnter);
};
}, [trail]);

return (
<>
{/* Custom Main Cursor */}
<div
className="custom-cursor"
className={`custom-cursor ${!isCursorVisible ? "hidden" : ""}`}
style={{
left: `${cursorPos.x}px`,
top: `${cursorPos.y}px`,
Expand All @@ -63,7 +78,7 @@ const App = () => {
{trail.map((pos, index) => (
<div
key={index}
className="cursor-tail"
className={`cursor-tail ${!isCursorVisible ? "hidden" : ""}`}
style={{
left: `${pos.x}px`,
top: `${pos.y}px`,
Expand Down

0 comments on commit 1cd6ad3

Please sign in to comment.