Skip to content

Commit 12f9da9

Browse files
Merge pull request #75 from codeXsit/remove-cursor-on-mobile
Remove cursor on mobile view
2 parents 52c6467 + 07af92f commit 12f9da9

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/components/Cursor/index.jsx

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ function Cursor() {
1111
y: 0,
1212
});
1313

14+
const [isMobile, setIsMobile] = useState(false); // state to track if it's mobile
1415
const cursorRef = useRef(null);
1516

1617
const debouncedMouseMove = debounce((e) => {
@@ -24,10 +25,21 @@ function Cursor() {
2425
debouncedMouseMove(e);
2526
}
2627

28+
// Check if the device is mobile
2729
useEffect(() => {
28-
if (cursorRef.current) {
29-
// cursorRef.current.style.left = `${mousePosition.x}px`;
30-
// cursorRef.current.style.top = `${mousePosition.y}px`;
30+
const checkIfMobile = () => {
31+
setIsMobile(window.innerWidth <= 768); // Change 768px to your desired mobile breakpoint
32+
};
33+
checkIfMobile();
34+
35+
window.addEventListener("resize", checkIfMobile);
36+
return () => {
37+
window.removeEventListener("resize", checkIfMobile);
38+
};
39+
}, []);
40+
41+
useEffect(() => {
42+
if (cursorRef.current && !isMobile) {
3143
cursorRef.current.animate(
3244
{
3345
left: `${mousePosition.x}px`,
@@ -36,12 +48,16 @@ function Cursor() {
3648
{ duration: 400, fill: "forwards" },
3749
);
3850
}
39-
window.addEventListener("mousemove", mouseMove);
51+
if (!isMobile) {
52+
window.addEventListener("mousemove", mouseMove);
53+
}
4054

4155
return () => {
42-
window.removeEventListener("mousemove", mouseMove);
56+
if (!isMobile) {
57+
window.removeEventListener("mousemove", mouseMove);
58+
}
4359
};
44-
}, [mousePosition]);
60+
}, [mousePosition, isMobile]);
4561

4662
const variants = {
4763
default: {
@@ -60,6 +76,11 @@ function Cursor() {
6076
},
6177
};
6278

79+
// If on mobile, render nothing (invisible cursor)
80+
if (isMobile) {
81+
return null;
82+
}
83+
6384
return (
6485
<motion.div
6586
ref={cursorRef}

0 commit comments

Comments
 (0)