TelegramPWA/src/hooks/useHorizontalScroll.ts
2022-02-02 22:52:33 +01:00

25 lines
661 B
TypeScript

import { useEffect } from '../lib/teact/teact';
const useHorizontalScroll = (container: HTMLElement | null, isDisabled?: boolean) => {
useEffect(() => {
if (!container || isDisabled) {
return undefined;
}
function handleScroll(e: WheelEvent) {
// Ignore horizontal scroll and let it work natively (e.g. on touchpad)
if (!e.deltaX) {
container!.scrollLeft += e.deltaY / 4;
}
}
container.addEventListener('wheel', handleScroll, { passive: true });
return () => {
container.removeEventListener('wheel', handleScroll);
};
}, [container, isDisabled]);
};
export default useHorizontalScroll;