shellphone.app/app/features/keypad/hooks/use-on-backspace-press.ts
2022-05-22 12:59:53 +02:00

35 lines
877 B
TypeScript

import { useRef } from "react";
import { usePress } from "@react-aria/interactions";
import { useRemoveDigit } from "./atoms";
export default function useOnBackspacePress() {
const removeDigit = useRemoveDigit();
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const intervalRef = useRef<ReturnType<typeof setInterval> | null>(null);
const { pressProps: onBackspacePressProps } = usePress({
onPressStart() {
timeoutRef.current = setTimeout(() => {
removeDigit();
intervalRef.current = setInterval(removeDigit, 75);
}, 325);
},
onPressEnd() {
if (timeoutRef.current) {
clearTimeout(timeoutRef.current);
timeoutRef.current = null;
}
if (intervalRef.current) {
clearInterval(intervalRef.current);
intervalRef.current = null;
return;
}
removeDigit();
},
});
return onBackspacePressProps;
}