-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTouchScrollable.js
More file actions
55 lines (48 loc) · 1.39 KB
/
TouchScrollable.js
File metadata and controls
55 lines (48 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// @flow
import { cloneElement, PureComponent, type Element, type ElementRef } from 'react';
import { canUseEventListeners } from 'exenv';
import { allowTouchMove, preventInertiaScroll, listenerOptions } from './utils';
export type ChildrenType = Element<*> | ElementRef<*> => Element<*>;
type Props = {
// allow touch-scroll on this element
children: ChildrenType,
};
export class TouchScrollable extends PureComponent<Props> {
scrollableArea: HTMLElement;
getScrollableArea = (ref: HTMLElement) => {
this.scrollableArea = ref;
};
componentDidMount() {
if (!canUseEventListeners) return;
this.scrollableArea.addEventListener(
'touchstart',
preventInertiaScroll,
listenerOptions,
);
this.scrollableArea.addEventListener(
'touchmove',
allowTouchMove,
listenerOptions,
);
}
componentWillUnmount() {
if (!canUseEventListeners) return;
if (!this.scrollableArea) return;
this.scrollableArea.removeEventListener(
'touchstart',
preventInertiaScroll,
listenerOptions,
);
this.scrollableArea.removeEventListener(
'touchmove',
allowTouchMove,
listenerOptions,
);
}
render() {
const { children, ...rest } = this.props;
return typeof children === 'function'
? children(this.getScrollableArea)
: cloneElement(children, { ref: this.getScrollableArea, ...rest });
}
}