Skip to content

Instantly share code, notes, and snippets.

@jordanhudgens
Created October 26, 2021 01:44
Show Gist options
  • Save jordanhudgens/649856f8fb88aca3d7d3e03fc3eeeeb5 to your computer and use it in GitHub Desktop.
Save jordanhudgens/649856f8fb88aca3d7d3e03fc3eeeeb5 to your computer and use it in GitHub Desktop.
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