/
ElementSeparator.tsx
280 lines (241 loc) · 8.24 KB
/
ElementSeparator.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module ElementSeparator
*/
import "./ElementSeparator.scss";
import * as React from "react";
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from "react";
import classnames from "classnames";
import { Orientation } from "../enums/Orientation";
import type { CommonProps } from "../utils/Props";
import { useThrottledFn } from "../utils/hooks/useThrottledFn";
import { useTranslation } from "../l10n/useTranslation";
/**
* Results returned by onRatioChanged callback for determining new ratio and whether the ratio was updated.
* @public
*/
export interface RatioChangeResult {
ratio: number;
}
/** Properties of [[ElementSeparator]] React component
* @public
*/
export interface ElementSeparatorProps extends CommonProps {
/** Separator orientation */
orientation: Orientation;
/** Ratio between left cell and right cell */
ratio: number;
/** Area width or height (depending on orientation) in pixels */
movableArea?: number;
/** Separator width or height in pixels. 30 by default */
separatorSize?: number;
/** Callback to ratio changed event */
onRatioChanged?: (ratio: number) => void | RatioChangeResult;
/** Is resize handle hovered */
isResizeHandleHovered?: boolean;
/** Callback to hover event change */
onResizeHandleHoverChanged?: (isHovered: boolean) => void;
/** Is resize handle being dragged */
isResizeHandleBeingDragged?: boolean;
/** Callback to drag event change */
onResizeHandleDragChanged?: (isDragStarted: boolean) => void;
}
function getCurrentGlobalPosition(
orientation: Orientation,
e: PointerEvent | React.PointerEvent
) {
return orientation === Orientation.Horizontal ? e.clientX : e.clientY;
}
const useConditionalCleanup = (condition: boolean, cleanup: () => void) => {
const conditionRef = useRef(condition);
const cleanupRef = useRef(cleanup);
conditionRef.current = condition;
cleanupRef.current = cleanup;
useEffect(() => {
return () => {
if (conditionRef.current) cleanupRef.current();
};
}, []);
};
const useElementSeparatorPointerHandler = ({
onResizeHandleDragChanged,
onResizeHandleHoverChanged,
isResizeHandleBeingDragged,
isResizeHandleHovered,
movableArea,
ratio,
orientation,
onRatioChanged,
}: ElementSeparatorProps) => {
const globalPosition = useRef(0);
const pointerOutOfBounds = useRef(false);
const [isElementDragged, setIsDragged] = useState(false);
const [isElementHovered, setIsHovered] = useState(false);
const isGroupDragged = isResizeHandleBeingDragged ?? isElementDragged;
const isGroupHovered = isResizeHandleHovered ?? isElementHovered;
useConditionalCleanup(isElementDragged && !!onResizeHandleDragChanged, () =>
onResizeHandleDragChanged!(false)
);
useConditionalCleanup(isElementHovered && !!onResizeHandleHoverChanged, () =>
onResizeHandleHoverChanged!(false)
);
if (isGroupHovered && pointerOutOfBounds.current)
pointerOutOfBounds.current = false;
const stopDrag = useCallback(() => {
// istanbul ignore else
if (isGroupDragged) {
setIsDragged(false);
if (onResizeHandleDragChanged) onResizeHandleDragChanged(false);
}
}, [isGroupDragged, onResizeHandleDragChanged]);
const startDrag = useCallback(
(e: PointerEvent | React.PointerEvent) => {
globalPosition.current = getCurrentGlobalPosition(orientation, e);
// istanbul ignore else
if (!isGroupDragged) {
setIsDragged(true);
if (onResizeHandleDragChanged) onResizeHandleDragChanged(true);
}
},
[isGroupDragged, orientation, onResizeHandleDragChanged]
);
const onPointerUp = useCallback(() => {
stopDrag();
}, [stopDrag]);
const onThrottledPointerMove = useThrottledFn(
(e: PointerEvent | React.PointerEvent) => {
if (!movableArea) {
stopDrag();
return;
}
const currentPosition = getCurrentGlobalPosition(orientation, e);
const positionChange = currentPosition - globalPosition.current;
// Should not need to recalculate if position on our movement axis does not change
if (Math.abs(positionChange) < 1) return;
const currentLocalPosition = movableArea * ratio + positionChange;
const newRatio = currentLocalPosition / movableArea;
globalPosition.current = currentPosition;
if (pointerOutOfBounds.current || !onRatioChanged) return;
const result = onRatioChanged(newRatio);
if (
result &&
result.ratio === ratio &&
!isGroupHovered &&
!pointerOutOfBounds.current
)
pointerOutOfBounds.current = true;
},
16,
[stopDrag, isGroupHovered, ratio, movableArea, onRatioChanged, orientation]
);
useEffect(() => {
return () => onThrottledPointerMove.cancel();
}, [onThrottledPointerMove]);
useLayoutEffect(() => {
if (isElementDragged) {
document.addEventListener("pointerup", onPointerUp);
document.addEventListener("pointermove", onThrottledPointerMove);
}
return () => {
document.removeEventListener("pointerup", onPointerUp);
document.removeEventListener("pointermove", onThrottledPointerMove);
};
}, [isElementDragged, onPointerUp, onThrottledPointerMove]);
const onPointerDown = useCallback(
(e: PointerEvent | React.PointerEvent) => {
if (!isGroupDragged) {
startDrag(e);
} else {
stopDrag();
}
},
[isGroupDragged, startDrag, stopDrag]
);
const onPointerOver = useCallback(() => {
// istanbul ignore next
if (isGroupHovered) return;
setIsHovered(true);
if (onResizeHandleHoverChanged) onResizeHandleHoverChanged(true);
}, [isGroupHovered, onResizeHandleHoverChanged]);
const onPointerOut = useCallback(() => {
// istanbul ignore next
if (!isGroupHovered) return;
setIsHovered(false);
if (onResizeHandleHoverChanged) onResizeHandleHoverChanged(false);
}, [isGroupHovered, onResizeHandleHoverChanged]);
return {
isHovered: isGroupHovered,
isDragged: isGroupDragged,
onPointerDown,
onPointerOver,
onPointerOut,
};
};
function getStyle(
orientation: Orientation,
separatorSize?: number
): React.CSSProperties {
separatorSize = separatorSize || 30;
if (orientation === Orientation.Horizontal)
return {
width: separatorSize,
margin: `0px ${-Math.floor(separatorSize / 2)}px`,
};
return {
height: separatorSize,
margin: `${-Math.floor(separatorSize / 2)}px 1px`,
};
}
/** A movable button, which allows to change the ratio between left element and right element
* @public
*/
export const ElementSeparator = (props: ElementSeparatorProps) => {
const { translate } = useTranslation();
const [hasHoverHappened, setHasHoverHappened] = useState(false);
const { isDragged, isHovered, onPointerDown, onPointerOver, onPointerOut } =
useElementSeparatorPointerHandler(props);
const isHoverNeeded = isHovered || isDragged;
if (!hasHoverHappened && isHoverNeeded) setHasHoverHappened(isHoverNeeded);
// This is done to avoid fade-out animation when first rendering.
const unhoverClass = hasHoverHappened
? "core-element-separator-group-unhovered"
: "";
const classNames = classnames(
"core-element-separator",
props.orientation === Orientation.Horizontal
? "core-element-separator--horizontal"
: "core-element-separator--vertical",
props.className,
isHoverNeeded ? "core-element-separator-group-hovered" : unhoverClass
);
const orientation = props.orientation;
const separatorSize = props.separatorSize;
const style = useMemo(
() => getStyle(orientation, separatorSize),
[orientation, separatorSize]
);
const styles: React.CSSProperties = {
...style,
...props.style,
};
return (
<button
style={styles}
className={classNames}
onPointerDown={onPointerDown}
onPointerOver={onPointerOver}
onPointerOut={onPointerOut}
aria-label={translate("elementSeparator.label")}
/>
);
};