TelegramPWA/src/hooks/useFastClick.ts
2025-04-23 18:57:46 +02:00

22 lines
676 B
TypeScript

import type React from '../lib/teact/teact';
import { IS_TOUCH_ENV, MouseButton } from '../util/browser/windowEnvironment';
import useLastCallback from './useLastCallback';
type EventArg<E> = React.MouseEvent<E>;
type EventHandler<E> = (e: EventArg<E>) => void;
export function useFastClick<T extends HTMLDivElement | HTMLButtonElement>(callback?: EventHandler<T>) {
const handler = useLastCallback((e: EventArg<T>) => {
if (e.type === 'mousedown' && e.button !== MouseButton.Main) {
return;
}
callback!(e);
});
return IS_TOUCH_ENV
? { handleClick: callback ? handler : undefined }
: { handleMouseDown: callback ? handler : undefined };
}