123456789101112131415161718192021222324252627 |
- // https://stackoverflow.com/a/54749871/2760968
- import { useEffect, useState } from "react";
- export default function useLongPress(callback: () => void, ms = 300) {
- const [startLongPress, setStartLongPress] = useState(false);
- useEffect(() => {
- let timerId: NodeJS.Timeout;
- if (startLongPress) {
- timerId = setTimeout(callback, ms);
- } else {
- clearTimeout(timerId);
- }
- return () => {
- clearTimeout(timerId);
- };
- }, [callback, ms, startLongPress]);
- return {
- onMouseDown: () => setStartLongPress(true),
- onMouseUp: () => setStartLongPress(false),
- onMouseLeave: () => setStartLongPress(false),
- onTouchStart: () => setStartLongPress(true),
- onTouchEnd: () => setStartLongPress(false),
- };
- }
|