-
Notifications
You must be signed in to change notification settings - Fork 209
Expand file tree
/
Copy pathhooks.tsx
More file actions
109 lines (94 loc) · 3.12 KB
/
hooks.tsx
File metadata and controls
109 lines (94 loc) · 3.12 KB
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
import { useEffect, RefObject, useLayoutEffect } from 'react';
export const useOnEscape = (
handler: (event: KeyboardEvent) => void,
active = true
) => {
useEffect(() => {
if (!active) return;
const listener = (event: KeyboardEvent) => {
// check if key is an Escape
if (event.key === 'Escape') handler(event);
};
document.addEventListener('keyup', listener);
return () => {
document.removeEventListener('keyup', listener);
};
}, [handler, active]);
};
export const useRepositionOnResize = (handler: () => void, active = true) => {
useEffect(() => {
if (!active) return;
const listener = () => {
handler();
};
window.addEventListener('resize', listener);
return () => {
window.removeEventListener('resize', listener);
};
}, [handler, active]);
};
export const useOnClickOutside = (
ref: RefObject<HTMLElement> | RefObject<HTMLElement>[],
handler: (event: TouchEvent | MouseEvent) => void,
active = true
) => {
useEffect(() => {
if (!active) return;
const listener = (event: TouchEvent | MouseEvent) => {
// Do nothing if clicking ref's element or descendent elements
const refs = Array.isArray(ref) ? ref : [ref];
let contains = false;
refs.forEach(r => {
if (!r.current || r.current.contains(event.target as Node)) {
contains = true;
return;
}
});
event.stopPropagation();
if (!contains) handler(event);
};
document.addEventListener('mousedown', listener);
document.addEventListener('touchstart', listener);
return () => {
document.removeEventListener('mousedown', listener);
document.removeEventListener('touchstart', listener);
};
}, [ref, handler, active]);
};
// Make sure that user is not able TAB out of the Modal content on Open
export const useTabbing = (
contentRef: RefObject<HTMLElement>,
active = true
) => {
useEffect(() => {
if (!active) return;
const listener = (event: KeyboardEvent) => {
// check if key is an Tab
if (event.keyCode === 9) {
const els = contentRef?.current?.querySelectorAll(
'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), [tabindex="0"]'
);
const focusableEls = Array.prototype.slice.call(els);
if (focusableEls.length === 1) {
event.preventDefault();
return;
}
const firstFocusableEl = focusableEls[0];
const lastFocusableEl = focusableEls[focusableEls.length - 1];
if (event.shiftKey && document.activeElement === firstFocusableEl) {
event.preventDefault();
lastFocusableEl.focus();
} else if (document.activeElement === lastFocusableEl) {
event.preventDefault();
firstFocusableEl.focus();
}
}
};
document.addEventListener('keydown', listener);
return () => {
document.removeEventListener('keydown', listener);
};
}, [contentRef, active]);
};
export const useIsomorphicLayoutEffect =
typeof window !== 'undefined' ? useLayoutEffect : useEffect;