@adamjanicki/ui 1.3.1 → 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.
@@ -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, onClick: onClose, children: React.cloneElement(children, {
32
+ return (_jsx("div", { className: classNames("ajui-layer-backdrop", className), style: style, onMouseDown: onClose, children: React.cloneElement(children, {
33
33
  ref: focusRef,
34
- onClick: function (e) {
34
+ onMouseDown: function (e) {
35
35
  var _a, _b;
36
36
  e.stopPropagation();
37
- (_b = (_a = children.props).onClick) === null || _b === void 0 ? void 0 : _b.call(_a, e);
37
+ (_b = (_a = children.props).onMouseDown) === null || _b === void 0 ? void 0 : _b.call(_a, e);
38
38
  },
39
39
  }) }));
40
40
  };
@@ -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: boolean) => import("react").MutableRefObject<T | null>;
8
+ declare const useFocusTrap: <T extends HTMLElement>(isActive?: boolean) => import("react").MutableRefObject<T | null>;
8
9
  export default useFocusTrap;
@@ -4,36 +4,47 @@ var focusableElementsString = 'a[href], area[href], input:not([disabled]), selec
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
13
  useEffect(function () {
12
14
  if (!isActive || !trapRef.current)
13
15
  return;
14
16
  var focusableElements = trapRef.current.querySelectorAll(focusableElementsString);
15
- var firstFocusableElement = focusableElements[0] || trapRef.current;
16
- var lastFocusableElement = focusableElements[focusableElements.length - 1] || trapRef.current;
17
17
  var trapFocus = function (event) {
18
+ var _a;
18
19
  if (event.key !== "Tab")
19
20
  return;
20
- if (event.shiftKey) {
21
- // Shift + Tab
22
- if (document.activeElement === firstFocusableElement) {
23
- lastFocusableElement === null || lastFocusableElement === void 0 ? void 0 : lastFocusableElement.focus();
24
- }
21
+ if (focusableElements.length === 0)
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();
25
36
  }
26
- else {
27
- // Tab
28
- if (document.activeElement === lastFocusableElement) {
29
- firstFocusableElement === null || firstFocusableElement === void 0 ? void 0 : firstFocusableElement.focus();
30
- }
37
+ else if (activeEl === lastEl && !event.shiftKey) {
38
+ focusFirst();
39
+ }
40
+ else if (!activeEl || !((_a = trapRef.current) === null || _a === void 0 ? void 0 : _a.contains(activeEl))) {
41
+ event.shiftKey ? focusLast() : focusFirst();
31
42
  }
32
- event.preventDefault();
33
43
  };
34
- firstFocusableElement === null || firstFocusableElement === void 0 ? void 0 : firstFocusableElement.focus();
35
44
  document.addEventListener("keydown", trapFocus);
36
- return function () { return document.removeEventListener("keydown", trapFocus); };
45
+ return function () {
46
+ document.removeEventListener("keydown", trapFocus);
47
+ };
37
48
  }, [isActive]);
38
49
  return trapRef;
39
50
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamjanicki/ui",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Basic UI components and hooks for React in TypeScript",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",