-
Notifications
You must be signed in to change notification settings - Fork 2
/
useDragInteraction.tsx
137 lines (129 loc) · 4.44 KB
/
useDragInteraction.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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Toolbar
*/
import * as React from "react";
import { Point, Timer } from "@itwin/core-react";
/** @internal */
const getDragDistance = (from: Point, to: Point) => {
return from.getDistanceTo(to);
};
interface UseDragArgs {
readonly initialPosition: React.MutableRefObject<Point | undefined>;
readonly onDrag: () => void;
}
function useDrag(args: UseDragArgs) {
const { initialPosition, onDrag } = args;
const handlePointerMove = React.useCallback(
(e: PointerEvent) => {
// istanbul ignore if
if (!initialPosition.current) return;
const newPosition = new Point(e.clientX, e.clientY);
const dragDistance = getDragDistance(
initialPosition.current,
newPosition
);
if (dragDistance < 20) return;
onDrag();
},
[initialPosition, onDrag]
);
return { handlePointerMove };
}
interface UseLongPressArgs {
readonly initialPosition: React.RefObject<Point | undefined>;
readonly onLongPress: () => void;
}
function useLongPress(args: UseLongPressArgs) {
const longPressTimer = React.useRef(new Timer(500));
const handlePointerDown = React.useCallback(() => {
longPressTimer.current.start();
}, []);
const handlePointerUp = React.useCallback(() => {
longPressTimer.current.stop();
}, []);
const handlePointerMove = React.useCallback(
(e: PointerEvent) => {
if (!args.initialPosition.current) return;
const newPosition = new Point(e.clientX, e.clientY);
const distance = args.initialPosition.current.getDistanceTo(newPosition);
distance >= 10 && longPressTimer.current.stop();
},
[args.initialPosition]
);
React.useEffect(() => {
longPressTimer.current.setOnExecute(args.onLongPress);
return () => {
longPressTimer.current.setOnExecute(undefined); // eslint-disable-line react-hooks/exhaustive-deps
};
}, [args.onLongPress]);
return { handlePointerDown, handlePointerMove, handlePointerUp };
}
/**
* Hook used on expandable item that require drag or long press to open
* @param onClick Function called when item is clicked.
* @param onOpenPanel Function called when item is dragged or long pressed to open panel.
* @public
*/
export function useDragInteraction(
onClick?: () => void,
onOpenPanel?: () => void
) {
const initialPosition = React.useRef<Point | undefined>(undefined);
const skipClick = React.useRef<boolean>(false);
const handleOpenPanel = React.useCallback(() => {
initialPosition.current = undefined;
skipClick.current = true;
onOpenPanel && onOpenPanel();
}, [onOpenPanel]);
const { handlePointerMove: dragPointerMove } = useDrag({
initialPosition,
onDrag: handleOpenPanel,
});
const {
handlePointerDown: longPressPointerDown,
handlePointerMove: longPressPointerMove,
handlePointerUp: longPressPointerUp,
} = useLongPress({
initialPosition,
onLongPress: handleOpenPanel,
});
const handleButtonClick = React.useCallback(() => {
// istanbul ignore if
if (skipClick.current) return;
onClick && onClick();
}, [onClick]);
const handlePointerDown = React.useCallback(
(e: React.PointerEvent) => {
e.target instanceof Element &&
e.target.releasePointerCapture(e.pointerId);
initialPosition.current = new Point(e.clientX, e.clientY);
skipClick.current = false;
longPressPointerDown();
},
[longPressPointerDown]
);
const handlePointerMove = React.useCallback(
(e: PointerEvent) => {
dragPointerMove(e);
longPressPointerMove(e);
},
[dragPointerMove, longPressPointerMove]
);
const handlePointerUp = React.useCallback(() => {
initialPosition.current = undefined;
longPressPointerUp();
}, [longPressPointerUp]);
React.useEffect(() => {
document.addEventListener("pointermove", handlePointerMove);
document.addEventListener("pointerup", handlePointerUp);
return () => {
document.removeEventListener("pointermove", handlePointerMove);
document.removeEventListener("pointerup", handlePointerUp);
};
}, [handlePointerMove, handlePointerUp]);
return { handlePointerDown, handleButtonClick };
}