react-components/cursor.jsx

113 lines
3.7 KiB
React
Raw Permalink Normal View History

2024-10-26 21:36:28 +02:00
"use client";
2024-10-27 14:53:37 +01:00
import {createContext, useContext, useEffect, useRef, useState} from 'react';
2024-10-26 21:36:28 +02:00
import { gsap } from 'gsap';
2024-10-27 14:53:37 +01:00
const CursorContext = createContext();
export function CursorProvider ({ hidden, enabled, children }) {
const [isCursorHidden, setIsCursorHidden] = useState(hidden);
const [isCursorEnabled, setIsCursorEnabled] = useState(enabled);
const [isCursorInViewport, setIsCursorInViewport] = useState(false);
const toggleCursor = () => setIsCursorHidden(!isCursorHidden);
const toggleCursorEnabled = () => {
setIsCursorEnabled(!isCursorEnabled);
localStorage.setItem('cursorEnabled', JSON.stringify(!isCursorEnabled));
}
2024-10-26 21:36:28 +02:00
const cursorRef = useRef(null);
useEffect(() => {
const moveCursor = (e) => {
const { clientX: x, clientY: y } = e;
2024-10-27 14:53:37 +01:00
setIsCursorInViewport((x > 0 && x < window.innerWidth) && (y > 0 && y < window.innerHeight));
2024-10-26 21:36:28 +02:00
gsap.to(cursorRef.current, {
x: x - 16,
y: y - 16,
duration: 0.1,
ease: "power3.out",
});
2024-10-27 14:53:37 +01:00
const savedCursorState = localStorage.getItem('cursorEnabled');
if (savedCursorState !== null) {
setIsCursorEnabled(JSON.parse(savedCursorState));
}
2024-10-26 21:36:28 +02:00
};
2024-10-31 22:16:00 +01:00
const handlePointer = (event) => {
switch (event.pointerType) {
case "mouse":
setIsCursorEnabled(true);
break;
case "pen":
setIsCursorEnabled(false);
break;
case "touch":
setIsCursorEnabled(false);
break;
default:
setIsCursorEnabled(false);
console.log(`pointerType ${event.pointerType} is not supported`);
}
};
window.addEventListener(
"pointerdown",
handlePointer,
false,
);
const handleMouseEnter = () => {
setIsCursorInViewport(true);
};
const handleMouseLeave = () => {
setIsCursorInViewport(false);
};
// Aggiungi gli eventi di mouse
window.addEventListener('mouseenter', handleMouseEnter);
window.addEventListener('mouseleave', handleMouseLeave);
2024-10-26 21:36:28 +02:00
window.addEventListener('mousemove', moveCursor);
return () => {
2024-10-31 22:16:00 +01:00
window.removeEventListener('mouseenter', handleMouseEnter);
window.removeEventListener('mouseleave', handleMouseLeave);
2024-10-26 21:36:28 +02:00
window.removeEventListener('mousemove', moveCursor);
2024-10-31 22:16:00 +01:00
window.removeEventListener('pointerdown', handlePointer, false)
2024-10-26 21:36:28 +02:00
};
}, []);
2024-10-27 14:53:37 +01:00
useEffect(() => {
const htmlElement = document.documentElement;
2024-10-31 22:16:00 +01:00
if (isCursorEnabled) {
2024-10-27 14:53:37 +01:00
htmlElement.classList.add('custom-cursor-enabled-env');
} else {
htmlElement.classList.remove('custom-cursor-enabled-env');
}
}, [isCursorEnabled]);
2024-10-26 21:36:28 +02:00
return (
<>
2024-10-27 14:53:37 +01:00
<CursorContext.Provider value={{isCustomCursorHidden: isCursorHidden, isCustomCursorEnabled: isCursorEnabled, toggleCursor, toggleCursorEnabled}}>
2024-10-31 22:16:00 +01:00
{isCursorEnabled ?
2024-10-27 14:53:37 +01:00
<div
ref={cursorRef}
className={`pointer-events-none fixed top-0 left-0 ${isCursorHidden || !isCursorEnabled || !isCursorInViewport ? 'invisible' : ''} w-10 h-10 bg-white rounded-full z-50 mix-blend-difference`}
/>
: null
}
{children}
</CursorContext.Provider>
2024-10-26 21:36:28 +02:00
</>
);
2024-10-27 14:53:37 +01:00
}
2024-10-26 21:36:28 +02:00
2024-10-27 14:53:37 +01:00
export function useCursor() {
return useContext(CursorContext);
}