-
-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathutils.ts
57 lines (49 loc) · 1.24 KB
/
utils.ts
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
import { MouseEvent } from "react";
export const simpleDebounce = (func: () => void, delay: number) => {
let timer: ReturnType<typeof setTimeout>;
function cancel() {
clearTimeout(timer);
}
function debounced() {
cancel();
timer = setTimeout(() => {
func();
}, delay);
}
debounced.cancel = cancel;
return debounced;
};
export const ensureWithinLimits = (value: number, min: number, max: number) => {
min = !min && min !== 0 ? value : min;
max = !max && max !== 0 ? value : max;
if (min > max) {
console.error("min limit is greater than max limit");
return value;
}
if (value < min) {
return min;
}
if (value > max) {
return max;
}
return value;
};
export interface ElementLayout {
top: number;
right: number;
height: number;
left: number;
width?: number;
}
export const isEventPosOnLayout = (event: MouseEvent, layout: ElementLayout) =>
event.clientX > layout.left &&
event.clientX < layout.right &&
event.clientY > layout.top &&
event.clientY < layout.top + layout.height;
export const isEventPosOnDomNode = (
event: MouseEvent,
domNode: HTMLElement,
) => {
const nodeClientRect = domNode.getBoundingClientRect();
return isEventPosOnLayout(event, nodeClientRect);
};