mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-12 02:43:50 +00:00
6bbf8ce795
* refactor: event editor color picker * refactor: throttle and debounce function signature
32 lines
815 B
TypeScript
32 lines
815 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
import { throttle } from '../../common/utils/throttle';
|
|
|
|
export const useFadeOutOnInactivity = () => {
|
|
const [isMouseMoved, setIsMouseMoved] = useState(false);
|
|
|
|
// show on mouse move
|
|
useEffect(() => {
|
|
let fadeOut: NodeJS.Timeout | null = null;
|
|
const setShowMenuTrue = () => {
|
|
setIsMouseMoved(true);
|
|
if (fadeOut) {
|
|
clearTimeout(fadeOut);
|
|
}
|
|
fadeOut = setTimeout(() => setIsMouseMoved(false), 3000);
|
|
};
|
|
|
|
const throttledShowMenu = throttle(setShowMenuTrue, 1000);
|
|
|
|
document.addEventListener('mousemove', throttledShowMenu);
|
|
return () => {
|
|
document.removeEventListener('mousemove', throttledShowMenu);
|
|
if (fadeOut) {
|
|
clearTimeout(fadeOut);
|
|
}
|
|
};
|
|
}, []);
|
|
|
|
return isMouseMoved;
|
|
};
|