Created
October 26, 2021 01:44
-
-
Save jordanhudgens/649856f8fb88aca3d7d3e03fc3eeeeb5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const useMousePosition = () => { | |
const [mousePosition, setMousePosition] = React.useState({ | |
x: null, | |
y: null, | |
}); | |
React.useEffect(() => { | |
const mouseMoveHandler = (event: { clientX: number; clientY: number }) => { | |
const { clientX, clientY } = event; | |
setMousePosition({ x: clientX, y: clientY }); | |
}; | |
document.addEventListener("mousemove", mouseMoveHandler); | |
return () => { | |
document.removeEventListener("mousemove", mouseMoveHandler); | |
}; | |
}, []); | |
return mousePosition; | |
}; | |
export const DTSliderMouse = () => { | |
const { x, y } = useMousePosition(); | |
const { windowWidth } = React.useContext(DTScreenContext); | |
const middle = windowWidth / 2; | |
return ( | |
<div style={{ left: `${x}px`, top: `${y}px` }} className="slider-cursor"> | |
{x < middle ? <CaretLeftIcon /> : <CaretRightIcon />} | |
</div> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment