@adamjanicki/ui 1.3.3 → 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.
@@ -1,5 +1,5 @@
1
- import { useEffect, useRef } from "react";
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]';
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
  *
@@ -10,42 +10,48 @@ var focusableElementsString = 'a[href], area[href], input:not([disabled]), selec
10
10
  var useFocusTrap = function (isActive) {
11
11
  if (isActive === void 0) { isActive = true; }
12
12
  var trapRef = useRef(null);
13
+ var focusableElements = useRef(null);
14
+ var updateFocusableElements = useCallback(function () {
15
+ if (trapRef.current) {
16
+ focusableElements.current = trapRef.current.querySelectorAll(selector);
17
+ }
18
+ }, []);
13
19
  useEffect(function () {
14
20
  if (!isActive || !trapRef.current)
15
21
  return;
16
- var focusableElements = trapRef.current.querySelectorAll(focusableElementsString);
17
- var trapFocus = function (event) {
22
+ updateFocusableElements();
23
+ var handleKeyDown = function (event) {
18
24
  var _a;
19
25
  if (event.key !== "Tab")
20
26
  return;
21
- if (focusableElements.length === 0)
27
+ if (!focusableElements.current ||
28
+ focusableElements.current.length === 0) {
22
29
  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 () {
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) {
31
35
  lastEl.focus();
32
36
  event.preventDefault();
33
- };
34
- if (activeEl === firstEl && event.shiftKey) {
35
- focusLast();
36
37
  }
37
- else if (activeEl === lastEl && !event.shiftKey) {
38
- focusFirst();
38
+ else if (!event.shiftKey && activeElement === lastEl) {
39
+ firstEl.focus();
40
+ event.preventDefault();
39
41
  }
40
- else if (!activeEl || !((_a = trapRef.current) === null || _a === void 0 ? void 0 : _a.contains(activeEl))) {
41
- event.shiftKey ? focusLast() : focusFirst();
42
+ else if (!((_a = trapRef.current) === null || _a === void 0 ? void 0 : _a.contains(activeElement))) {
43
+ (event.shiftKey ? lastEl : firstEl).focus();
44
+ event.preventDefault();
42
45
  }
43
46
  };
44
- document.addEventListener("keydown", trapFocus);
47
+ var observer = new MutationObserver(updateFocusableElements);
48
+ observer.observe(trapRef.current, { childList: true, subtree: true });
49
+ document.addEventListener("keydown", handleKeyDown);
45
50
  return function () {
46
- document.removeEventListener("keydown", trapFocus);
51
+ document.removeEventListener("keydown", handleKeyDown);
52
+ observer.disconnect();
47
53
  };
48
- }, [isActive]);
54
+ }, [isActive, updateFocusableElements]);
49
55
  return trapRef;
50
56
  };
51
57
  export default useFocusTrap;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adamjanicki/ui",
3
- "version": "1.3.3",
3
+ "version": "1.3.4",
4
4
  "description": "Basic UI components and hooks for React in TypeScript",
5
5
  "main": "./index.js",
6
6
  "types": "./index.d.ts",