InvokeAI/invokeai/frontend/web/src/common/hooks/useSingleAndDoubleClick.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

30 lines
668 B
TypeScript
Raw Normal View History

// https://stackoverflow.com/a/73731908
2023-12-29 05:52:29 +00:00
import { useCallback, useEffect, useState } from 'react';
export function useSingleAndDoubleClick(
handleSingleClick: () => void,
handleDoubleClick: () => void,
delay = 250
) {
const [click, setClick] = useState(0);
useEffect(() => {
const timer = setTimeout(() => {
if (click === 1) {
handleSingleClick();
}
setClick(0);
}, delay);
if (click === 2) {
handleDoubleClick();
}
return () => clearTimeout(timer);
}, [click, handleSingleClick, handleDoubleClick, delay]);
2023-12-29 05:52:29 +00:00
const onClick = useCallback(() => setClick((prev) => prev + 1), []);
return onClick;
}