TelegramPWA/src/hooks/useHorizontalScroll.ts
Alexander Zinchuk 3afcde3217 Initial commit
2021-04-09 14:11:51 +03:00

25 lines
686 B
TypeScript

import { RefObject } from 'react';
import { useEffect } from '../lib/teact/teact';
export default (containerRef: RefObject<HTMLElement>, isDisabled?: boolean) => {
useEffect(() => {
const container = containerRef.current;
if (!container) {
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);
};
}, [containerRef, isDisabled]);
};