mirror of
https://github.com/invoke-ai/InvokeAI
synced 2024-08-30 20:32:17 +00:00
28 lines
610 B
TypeScript
28 lines
610 B
TypeScript
|
// https://stackoverflow.com/a/73731908
|
||
|
import { 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]);
|
||
|
|
||
|
return () => setClick((prev) => prev + 1);
|
||
|
}
|