UNPKG

@clayui/shared

Version:

ClayShared component

20 lines (19 loc) 764 B
/** * SPDX-FileCopyrightText: © 2019 Liferay, Inc. <https://liferay.com> * SPDX-License-Identifier: BSD-3-Clause */ import { useEffect, useState } from 'react'; import { throttle } from "./throttle.js"; /** * Hook to get the current mouse position */ export const useMousePosition = function () { let delay = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 200; const [mousePosition, setMousePosition] = useState([0, 0]); useEffect(() => { const handleMousePosition = throttle(event => setMousePosition([event.clientX, event.clientY]), delay); window.addEventListener('mousemove', handleMousePosition); return () => window.removeEventListener('mousemove', handleMousePosition); }, [delay]); return mousePosition; };