@adamjanicki/ui 1.3.1 → 1.3.2
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/components/Layer/Layer.js +3 -3
- package/hooks/useFocusTrap.js +17 -13
- package/package.json +1 -1
|
@@ -29,12 +29,12 @@ var BaseLayer = function (_a) {
|
|
|
29
29
|
document.removeEventListener("keydown", handleEscape);
|
|
30
30
|
};
|
|
31
31
|
}, [onClose, disableEscape]);
|
|
32
|
-
return (_jsx("div", { className: classNames("ajui-layer-backdrop", className), style: style,
|
|
32
|
+
return (_jsx("div", { className: classNames("ajui-layer-backdrop", className), style: style, onMouseDown: onClose, children: React.cloneElement(children, {
|
|
33
33
|
ref: focusRef,
|
|
34
|
-
|
|
34
|
+
onMouseDown: function (e) {
|
|
35
35
|
var _a, _b;
|
|
36
36
|
e.stopPropagation();
|
|
37
|
-
(_b = (_a = children.props).
|
|
37
|
+
(_b = (_a = children.props).onMouseDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
|
|
38
38
|
},
|
|
39
39
|
}) }));
|
|
40
40
|
};
|
package/hooks/useFocusTrap.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useEffect, useRef } from "react";
|
|
1
|
+
import { useEffect, useRef, useState } 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.
|
|
@@ -8,33 +8,37 @@ var focusableElementsString = 'a[href], area[href], input:not([disabled]), selec
|
|
|
8
8
|
*/
|
|
9
9
|
var useFocusTrap = function (isActive) {
|
|
10
10
|
var trapRef = useRef(null);
|
|
11
|
+
var _a = useState(undefined), currentIndex = _a[0], setCurrentIndex = _a[1];
|
|
11
12
|
useEffect(function () {
|
|
12
13
|
if (!isActive || !trapRef.current)
|
|
13
14
|
return;
|
|
14
15
|
var focusableElements = trapRef.current.querySelectorAll(focusableElementsString);
|
|
15
|
-
var firstFocusableElement = focusableElements[0] || trapRef.current;
|
|
16
|
-
var lastFocusableElement = focusableElements[focusableElements.length - 1] || trapRef.current;
|
|
17
16
|
var trapFocus = function (event) {
|
|
17
|
+
var _a;
|
|
18
18
|
if (event.key !== "Tab")
|
|
19
19
|
return;
|
|
20
|
+
event.preventDefault();
|
|
21
|
+
if (focusableElements.length === 0)
|
|
22
|
+
return;
|
|
23
|
+
var newIndex = currentIndex;
|
|
20
24
|
if (event.shiftKey) {
|
|
21
25
|
// Shift + Tab
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
26
|
+
newIndex =
|
|
27
|
+
((currentIndex !== null && currentIndex !== void 0 ? currentIndex : 0) - 1 + focusableElements.length) %
|
|
28
|
+
focusableElements.length;
|
|
25
29
|
}
|
|
26
30
|
else {
|
|
27
31
|
// Tab
|
|
28
|
-
|
|
29
|
-
firstFocusableElement === null || firstFocusableElement === void 0 ? void 0 : firstFocusableElement.focus();
|
|
30
|
-
}
|
|
32
|
+
newIndex = ((currentIndex !== null && currentIndex !== void 0 ? currentIndex : -1) + 1) % focusableElements.length;
|
|
31
33
|
}
|
|
32
|
-
|
|
34
|
+
(_a = focusableElements[newIndex]) === null || _a === void 0 ? void 0 : _a.focus();
|
|
35
|
+
setCurrentIndex(newIndex);
|
|
33
36
|
};
|
|
34
|
-
firstFocusableElement === null || firstFocusableElement === void 0 ? void 0 : firstFocusableElement.focus();
|
|
35
37
|
document.addEventListener("keydown", trapFocus);
|
|
36
|
-
return function () {
|
|
37
|
-
|
|
38
|
+
return function () {
|
|
39
|
+
document.removeEventListener("keydown", trapFocus);
|
|
40
|
+
};
|
|
41
|
+
}, [isActive, currentIndex]);
|
|
38
42
|
return trapRef;
|
|
39
43
|
};
|
|
40
44
|
export default useFocusTrap;
|