@adamjanicki/ui 1.3.2 → 1.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/hooks/useFocusTrap.d.ts +2 -1
- package/hooks/useFocusTrap.js +23 -16
- package/package.json +1 -1
package/hooks/useFocusTrap.d.ts
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
* A hook for trapping focus within an element.
|
|
3
3
|
*
|
|
4
4
|
* @param isActive true if the element is active, false otherwise
|
|
5
|
+
* `true` by default
|
|
5
6
|
* @returns ref object that must be passed to the element that should be trapped
|
|
6
7
|
*/
|
|
7
|
-
declare const useFocusTrap: <T extends HTMLElement>(isActive
|
|
8
|
+
declare const useFocusTrap: <T extends HTMLElement>(isActive?: boolean) => import("react").MutableRefObject<T | null>;
|
|
8
9
|
export default useFocusTrap;
|
package/hooks/useFocusTrap.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { useEffect, useRef
|
|
1
|
+
import { useEffect, useRef } from "react";
|
|
2
2
|
var focusableElementsString = 'a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, [tabindex="0"], [contenteditable]';
|
|
3
3
|
/**
|
|
4
4
|
* A hook for trapping focus within an element.
|
|
5
5
|
*
|
|
6
6
|
* @param isActive true if the element is active, false otherwise
|
|
7
|
+
* `true` by default
|
|
7
8
|
* @returns ref object that must be passed to the element that should be trapped
|
|
8
9
|
*/
|
|
9
10
|
var useFocusTrap = function (isActive) {
|
|
11
|
+
if (isActive === void 0) { isActive = true; }
|
|
10
12
|
var trapRef = useRef(null);
|
|
11
|
-
var _a = useState(undefined), currentIndex = _a[0], setCurrentIndex = _a[1];
|
|
12
13
|
useEffect(function () {
|
|
13
14
|
if (!isActive || !trapRef.current)
|
|
14
15
|
return;
|
|
@@ -17,28 +18,34 @@ var useFocusTrap = function (isActive) {
|
|
|
17
18
|
var _a;
|
|
18
19
|
if (event.key !== "Tab")
|
|
19
20
|
return;
|
|
20
|
-
event.preventDefault();
|
|
21
21
|
if (focusableElements.length === 0)
|
|
22
|
-
return;
|
|
23
|
-
var
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
return event.preventDefault();
|
|
23
|
+
var activeEl = document.activeElement;
|
|
24
|
+
var firstEl = focusableElements[0];
|
|
25
|
+
var lastEl = focusableElements[focusableElements.length - 1];
|
|
26
|
+
var focusFirst = function () {
|
|
27
|
+
firstEl.focus();
|
|
28
|
+
event.preventDefault();
|
|
29
|
+
};
|
|
30
|
+
var focusLast = function () {
|
|
31
|
+
lastEl.focus();
|
|
32
|
+
event.preventDefault();
|
|
33
|
+
};
|
|
34
|
+
if (activeEl === firstEl && event.shiftKey) {
|
|
35
|
+
focusLast();
|
|
36
|
+
}
|
|
37
|
+
else if (activeEl === lastEl && !event.shiftKey) {
|
|
38
|
+
focusFirst();
|
|
29
39
|
}
|
|
30
|
-
else {
|
|
31
|
-
|
|
32
|
-
newIndex = ((currentIndex !== null && currentIndex !== void 0 ? currentIndex : -1) + 1) % focusableElements.length;
|
|
40
|
+
else if (!activeEl || !((_a = trapRef.current) === null || _a === void 0 ? void 0 : _a.contains(activeEl))) {
|
|
41
|
+
event.shiftKey ? focusLast() : focusFirst();
|
|
33
42
|
}
|
|
34
|
-
(_a = focusableElements[newIndex]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
35
|
-
setCurrentIndex(newIndex);
|
|
36
43
|
};
|
|
37
44
|
document.addEventListener("keydown", trapFocus);
|
|
38
45
|
return function () {
|
|
39
46
|
document.removeEventListener("keydown", trapFocus);
|
|
40
47
|
};
|
|
41
|
-
}, [isActive
|
|
48
|
+
}, [isActive]);
|
|
42
49
|
return trapRef;
|
|
43
50
|
};
|
|
44
51
|
export default useFocusTrap;
|