UNPKG

2.58 kBTypeScriptView Raw
1import { PureComponent, Requireable, Validator } from "react";
2
3/**
4 * Specifies the number of miliseconds during which to disable pointer events while a scroll is in progress.
5 * This improves performance and makes scrolling smoother.
6 */
7export const IS_SCROLLING_TIMEOUT = 150;
8
9export type WindowScrollerChildProps = {
10 height: number;
11 width: number;
12 isScrolling: boolean;
13 scrollTop: number;
14 scrollLeft: number;
15 onChildScroll: (params: { scrollTop: number }) => void;
16 registerChild: (element?: React.ReactNode) => void;
17};
18
19export type WindowScrollerProps = {
20 /**
21 * Function responsible for rendering children.
22 * This function should implement the following signature:
23 * ({ height, isScrolling, scrollLeft, scrollTop, width, onChildScroll }) => PropTypes.element
24 */
25 children: (params: WindowScrollerChildProps) => React.ReactNode;
26
27 /** Callback to be invoked on-resize: ({ height, width }) */
28 onResize?: ((params: { height: number; width: number }) => void) | undefined;
29
30 /** Callback to be invoked on-scroll: ({ scrollLeft, scrollTop }) */
31 onScroll?: ((params: { scrollLeft: number; scrollTop: number }) => void) | undefined;
32
33 /** Element to attach scroll event listeners. Defaults to window. */
34 scrollElement?: typeof window | Element | undefined;
35 /**
36 * Wait this amount of time after the last scroll event before resetting child `pointer-events`.
37 */
38 scrollingResetTimeInterval?: number | undefined;
39
40 /** Height used for server-side rendering */
41 serverHeight?: number | undefined;
42
43 /** Width used for server-side rendering */
44 serverWidth?: number | undefined;
45 /**
46 * PLEASE NOTE
47 * The [key: string]: any; line is here on purpose
48 * This is due to the need of force re-render of PureComponent
49 * Check the following link if you want to know more
50 * https://github.com/bvaughn/react-virtualized#pass-thru-props
51 */
52 [key: string]: any;
53};
54
55export type WindowScrollerState = {
56 height: number;
57 width: number;
58 isScrolling: boolean;
59 scrollLeft: number;
60 scrollTop: number;
61};
62
63export class WindowScroller extends PureComponent<WindowScrollerProps, WindowScrollerState> {
64 static defaultProps: {
65 onResize: () => void;
66 onScroll: () => void;
67 scrollingResetTimeInterval: typeof IS_SCROLLING_TIMEOUT;
68 scrollElement: Window | undefined;
69 serverHeight: 0;
70 serverWidth: 0;
71 };
72
73 updatePosition(scrollElement?: HTMLElement): void;
74}
75
76export default WindowScroller;