mirror of
https://github.com/streamich/react-use.git
synced 2026-01-18 14:06:52 +00:00
51 lines
1014 B
TypeScript
51 lines
1014 B
TypeScript
/* eslint-disable */
|
|
import { RefObject, useEffect } from 'react';
|
|
|
|
import useRafState from './useRafState';
|
|
|
|
export interface State {
|
|
x: number;
|
|
y: number;
|
|
}
|
|
|
|
const useScroll = (ref: RefObject<HTMLElement>): State => {
|
|
if (process.env.NODE_ENV === 'development') {
|
|
if (typeof ref !== 'object' || typeof ref.current === 'undefined') {
|
|
console.error('`useScroll` expects a single ref argument.');
|
|
}
|
|
}
|
|
|
|
const [state, setState] = useRafState<State>({
|
|
x: 0,
|
|
y: 0,
|
|
});
|
|
|
|
useEffect(() => {
|
|
const handler = () => {
|
|
if (ref.current) {
|
|
setState({
|
|
x: ref.current.scrollLeft,
|
|
y: ref.current.scrollTop,
|
|
});
|
|
}
|
|
};
|
|
|
|
if (ref.current) {
|
|
ref.current.addEventListener('scroll', handler, {
|
|
capture: false,
|
|
passive: true,
|
|
});
|
|
}
|
|
|
|
return () => {
|
|
if (ref.current) {
|
|
ref.current.removeEventListener('scroll', handler);
|
|
}
|
|
};
|
|
}, [ref]);
|
|
|
|
return state;
|
|
};
|
|
|
|
export default useScroll;
|