@adamjanicki/ui 1.3.2 → 1.3.4
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 +35 -22
- 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,44 +1,57 @@
|
|
|
1
|
-
import { useEffect, useRef,
|
|
2
|
-
var
|
|
1
|
+
import { useEffect, useRef, useCallback } from "react";
|
|
2
|
+
var selector = '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
|
|
13
|
+
var focusableElements = useRef(null);
|
|
14
|
+
var updateFocusableElements = useCallback(function () {
|
|
15
|
+
if (trapRef.current) {
|
|
16
|
+
focusableElements.current = trapRef.current.querySelectorAll(selector);
|
|
17
|
+
}
|
|
18
|
+
}, []);
|
|
12
19
|
useEffect(function () {
|
|
13
20
|
if (!isActive || !trapRef.current)
|
|
14
21
|
return;
|
|
15
|
-
|
|
16
|
-
var
|
|
22
|
+
updateFocusableElements();
|
|
23
|
+
var handleKeyDown = function (event) {
|
|
17
24
|
var _a;
|
|
18
25
|
if (event.key !== "Tab")
|
|
19
26
|
return;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
if (!focusableElements.current ||
|
|
28
|
+
focusableElements.current.length === 0) {
|
|
29
|
+
return event.preventDefault();
|
|
30
|
+
}
|
|
31
|
+
var activeElement = document.activeElement;
|
|
32
|
+
var firstEl = focusableElements.current[0];
|
|
33
|
+
var lastEl = focusableElements.current[focusableElements.current.length - 1];
|
|
34
|
+
if (event.shiftKey && activeElement === firstEl) {
|
|
35
|
+
lastEl.focus();
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
}
|
|
38
|
+
else if (!event.shiftKey && activeElement === lastEl) {
|
|
39
|
+
firstEl.focus();
|
|
40
|
+
event.preventDefault();
|
|
29
41
|
}
|
|
30
|
-
else {
|
|
31
|
-
|
|
32
|
-
|
|
42
|
+
else if (!((_a = trapRef.current) === null || _a === void 0 ? void 0 : _a.contains(activeElement))) {
|
|
43
|
+
(event.shiftKey ? lastEl : firstEl).focus();
|
|
44
|
+
event.preventDefault();
|
|
33
45
|
}
|
|
34
|
-
(_a = focusableElements[newIndex]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
35
|
-
setCurrentIndex(newIndex);
|
|
36
46
|
};
|
|
37
|
-
|
|
47
|
+
var observer = new MutationObserver(updateFocusableElements);
|
|
48
|
+
observer.observe(trapRef.current, { childList: true, subtree: true });
|
|
49
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
38
50
|
return function () {
|
|
39
|
-
document.removeEventListener("keydown",
|
|
51
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
52
|
+
observer.disconnect();
|
|
40
53
|
};
|
|
41
|
-
}, [isActive,
|
|
54
|
+
}, [isActive, updateFocusableElements]);
|
|
42
55
|
return trapRef;
|
|
43
56
|
};
|
|
44
57
|
export default useFocusTrap;
|