-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathSideEffect.tsx
203 lines (166 loc) · 6.32 KB
/
SideEffect.tsx
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import * as React from 'react';
import {TouchEvent} from "react";
import {RemoveScrollBar} from 'react-remove-scroll-bar';
import {styleSingleton} from 'react-style-singleton';
import {handleScroll, locationCouldBeScrolled} from "./handleScroll";
import {nonPassive} from './aggresiveCapture';
import {Axis, IRemoveScrollEffectProps} from "./types";
export const getTouchXY = (event: TouchEvent) => (
event.changedTouches
? [event.changedTouches[0].clientX, event.changedTouches[0].clientY]
: [0, 0]
);
export const getDeltaXY = (event: WheelEvent) => (
[event.deltaX, event.deltaY]
);
const extractRef = (ref: React.RefObject<any> | HTMLElement): HTMLElement => (
(ref && 'current' in ref) ? ref.current : ref
);
const deltaCompare = (x: number[], y: number[]) => (
x[0] === y[0] && x[1] === y[1]
);
const generateStyle = (id: number) => `
.block-interactivity-${id} {pointer-events: none;}
.allow-interactivity-${id} {pointer-events: all;}
`;
let idCounter = 0;
let lockStack: any[] = [];
export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {
const shouldPreventQueue = React.useRef<Array<{ name: string, delta: number[], target: any, should: boolean }>>([]);
const touchStartRef = React.useRef([0, 0]);
const activeAxis = React.useRef<Axis | undefined>();
const [id] = React.useState(idCounter++);
const [Style] = React.useState(() => styleSingleton());
const lastProps = React.useRef<IRemoveScrollEffectProps>(props);
React.useEffect(() => {
lastProps.current = props;
}, [props]);
React.useEffect(() => {
if (props.inert) {
document.body.classList.add(`block-interactivity-${id}`);
const allow = [
props.lockRef.current,
...(props.shards || []).map(extractRef)
].filter(Boolean);
allow.forEach(el => el!.classList.add(`allow-interactivity-${id}`));
return () => {
document.body.classList.remove(`block-interactivity-${id}`);
allow.forEach(el => el!.classList.remove(`allow-interactivity-${id}`));
}
}
return;
}, [props.inert, props.lockRef.current, props.shards]);
const shouldCancelEvent = React.useCallback((event: any, parent: HTMLElement) => {
const touch = getTouchXY(event);
const touchStart = touchStartRef.current;
const deltaX = 'deltaX' in event ? event.deltaX : (touchStart[0] - touch[0]);
const deltaY = 'deltaY' in event ? event.deltaY : (touchStart[1] - touch[1]);
let currentAxis: Axis | undefined;
let target: HTMLElement = event.target as any;
const moveDirection: Axis = Math.abs(deltaX) > Math.abs(deltaY) ? 'h' : 'v';
let canBeScrolledInMainDirection = locationCouldBeScrolled(moveDirection, target);
if (!canBeScrolledInMainDirection) {
return true;
}
if (canBeScrolledInMainDirection) {
currentAxis = moveDirection;
} else {
currentAxis = moveDirection === 'v' ? 'h' : 'v';
canBeScrolledInMainDirection = locationCouldBeScrolled(moveDirection, target);
// other axis might be not scrollable
}
if (!canBeScrolledInMainDirection) {
return false;
}
if (!activeAxis.current && event.changedTouches && (deltaX || deltaY)) {
activeAxis.current = currentAxis;
}
if (!currentAxis) {
return true
}
const cancelingAxis = activeAxis.current || currentAxis;
return handleScroll(
cancelingAxis,
parent,
event,
cancelingAxis == 'h' ? deltaX : deltaY
);
}, []);
const shouldPrevent = React.useCallback((event: any) => {
if (!lockStack.length || lockStack[lockStack.length - 1] !== Style) {
// not the last active
return;
}
const delta = 'deltaY' in event ? getDeltaXY(event) : getTouchXY(event);
const sourceEvent = shouldPreventQueue.current.filter(
(e) => e.name === event.type && e.target === event.target && deltaCompare(e.delta, delta)
)[0];
// self event, and should be canceled
if (sourceEvent && sourceEvent.should) {
event.preventDefault();
return;
}
// outside or shard event
if (!sourceEvent) {
const shardNodes = (lastProps.current.shards || [])
.map(extractRef)
.filter(Boolean)
.filter(node => node.contains(event.target));
const shouldStop = shardNodes.length > 0
? shouldCancelEvent(event, shardNodes[0])
: !lastProps.current.noIsolation;
if (shouldStop) {
event.preventDefault();
}
}
}, []);
const shouldCancel = React.useCallback((name: string, delta: number[], target: any, should: boolean) => {
const event = {name, delta, target, should};
shouldPreventQueue.current.push(event);
setTimeout(() => {
shouldPreventQueue.current = shouldPreventQueue.current.filter(e => e !== event);
}, 1);
}, []);
const scrollTouchStart = React.useCallback((event: any) => {
touchStartRef.current = getTouchXY(event);
activeAxis.current = undefined;
}, []);
const scrollWheel = React.useCallback((event: WheelEvent) => {
shouldCancel(event.type, getDeltaXY(event), event.target, shouldCancelEvent(event, props.lockRef.current as any));
}, []);
const scrollTouchMove = React.useCallback((event: TouchEvent<HTMLDivElement>) => {
shouldCancel(event.type, getTouchXY(event), event.target, shouldCancelEvent(event, props.lockRef.current as any));
}, []);
React.useEffect(() => {
lockStack.push(Style);
props.setCallbacks({
onScrollCapture: scrollWheel,
onWheelCapture: scrollWheel,
onTouchMoveCapture: scrollTouchMove,
});
document.addEventListener('wheel', shouldPrevent, nonPassive);
document.addEventListener('touchmove', shouldPrevent, nonPassive);
document.addEventListener('touchstart', scrollTouchStart, nonPassive);
return () => {
lockStack = lockStack.filter(inst => inst !== Style);
document.removeEventListener('wheel', shouldPrevent, nonPassive as any);
document.removeEventListener('touchmove', shouldPrevent, nonPassive as any);
document.removeEventListener('touchstart', scrollTouchStart, nonPassive as any);
}
}, []);
const {removeScrollBar, inert} = props;
return (
<React.Fragment>
{
inert
? <Style styles={generateStyle(id)}/>
: null
}
{
removeScrollBar
? <RemoveScrollBar gapMode="margin"/>
: null
}
</React.Fragment>
)
}