|
2 | 2 | import React, { PureComponent, type ComponentType } from 'react'; |
3 | 3 | import { canUseDOM } from 'exenv'; |
4 | 4 |
|
5 | | -import { isTouchDevice, preventTouchMove } from './utils'; |
| 5 | +import { isTouchDevice, listenerOptions, preventTouchMove } from './utils'; |
6 | 6 |
|
7 | 7 | type Props = {}; |
8 | 8 |
|
| 9 | +// Mobile Safari ignores { overflow: hidden } declaration on the body, |
| 10 | +// so we have to prevent touchmove events via JS |
9 | 11 | export default function withTouchListeners(WrappedComponent: ComponentType<*>) { |
10 | 12 | return class TouchProvider extends PureComponent<Props> { |
11 | | - listenerOptions = { |
12 | | - capture: false, |
13 | | - passive: false, |
14 | | - }; |
15 | 13 | componentDidMount() { |
16 | | - if (!canUseDOM) return; |
17 | | - const target = document.body; |
| 14 | + if (!canUseDOM || !isTouchDevice()) return; |
18 | 15 |
|
19 | | - // account for touch devices |
20 | | - if (target && isTouchDevice()) { |
21 | | - // Mobile Safari ignores { overflow: hidden } declaration on the body. |
22 | | - target.addEventListener('touchmove', preventTouchMove, this.listenerOptions); |
23 | | - } |
| 16 | + document.addEventListener('touchmove', preventTouchMove, listenerOptions); |
24 | 17 | } |
25 | 18 | componentWillUnmount() { |
26 | | - if (!canUseDOM) return; |
| 19 | + if (!canUseDOM || !isTouchDevice()) return; |
27 | 20 |
|
28 | | - const target = document.body; |
29 | | - |
30 | | - // remove touch listeners |
31 | | - if (target && isTouchDevice()) { |
32 | | - target.removeEventListener('touchmove', preventTouchMove, this.listenerOptions); |
33 | | - } |
| 21 | + document.removeEventListener('touchmove', preventTouchMove, listenerOptions); |
34 | 22 | } |
35 | 23 | render() { |
36 | 24 | return <WrappedComponent {...this.props} />; |
|
0 commit comments