react-magma-dom 7.1.0-next.6 → 7.1.0-next.7

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.
@@ -28,3 +28,4 @@ export declare const WithPortalContainer: {
28
28
  render: (args: React.JSX.IntrinsicAttributes & ModalProps & React.RefAttributes<HTMLDivElement>) => React.JSX.Element;
29
29
  };
30
30
  export declare const EscWithForeignAriaModal: () => React.JSX.Element;
31
+ export declare const SubmitButtonBecomesDisabled: () => React.JSX.Element;
package/dist/esm/index.js CHANGED
@@ -13379,6 +13379,15 @@ function isElementVisible(element) {
13379
13379
  }
13380
13380
  return true;
13381
13381
  }
13382
+ /**
13383
+ * Checks that the element can still hold focus (not disabled, inert, hidden or removed).
13384
+ */
13385
+ function canElementHoldFocus(element) {
13386
+ return element.isConnected && !element.matches(':disabled') && !element.closest('[inert]') && isElementVisible(element);
13387
+ }
13388
+ // Browsers move focus to `<body>` asynchronously, so check after a short delay
13389
+ var LOST_FOCUS_CHECK_DELAY = 50;
13390
+ var LOST_FOCUS_ATTRIBUTES = ['disabled', 'hidden', 'style', 'class', 'inert'];
13382
13391
  /**
13383
13392
  * Module-level registry of currently active focus-lock root elements.
13384
13393
  * Used in Safari to prevent an outer lock from handling Tab for elements
@@ -13391,6 +13400,7 @@ function useFocusLock(active, header, body, autoFocusOnActivate) {
13391
13400
  }
13392
13401
  var rootNode = useRef(null);
13393
13402
  var focusableItems = useRef([]);
13403
+ var lastFocusedInside = useRef(null);
13394
13404
  var _useDeviceDetect = useDeviceDetect(),
13395
13405
  isSafari = _useDeviceDetect.isSafari;
13396
13406
  // The filter is necessary for the proper functioning of focus in drawer-navigation or similar cases
@@ -13401,6 +13411,34 @@ function useFocusLock(active, header, body, autoFocusOnActivate) {
13401
13411
  });
13402
13412
  focusableItems.current = deduplicateTabStops(allFocusable);
13403
13413
  };
13414
+ var isInsideNestedLock = function isInsideNestedLock(element) {
13415
+ return !!element && !!rootNode.current && Array.from(activeFocusLockRoots).some(function (lockRoot) {
13416
+ return lockRoot !== rootNode.current && rootNode.current.contains(lockRoot) && lockRoot.contains(element);
13417
+ });
13418
+ };
13419
+ // Checks that focus fell to `<body>` because the last focused element became unfocusable
13420
+ var isFocusLostFromLock = function isFocusLostFromLock() {
13421
+ var lastFocused = lastFocusedInside.current;
13422
+ return !!rootNode.current && !!lastFocused && document.activeElement === document.body && document.hasFocus() && !canElementHoldFocus(lastFocused) && !isInsideNestedLock(lastFocused);
13423
+ };
13424
+ // Moves focus to the header, the first focusable item or the root.
13425
+ var restoreFocusInsideLock = function restoreFocusInsideLock() {
13426
+ var root = rootNode.current;
13427
+ if (!root) {
13428
+ return;
13429
+ }
13430
+ updateFocusableItems();
13431
+ if (header != null && header.current && root.contains(header.current)) {
13432
+ header.current.focus();
13433
+ } else if (focusableItems.current.length > 0) {
13434
+ focusableItems.current[0].focus();
13435
+ } else {
13436
+ if (!root.hasAttribute('tabindex')) {
13437
+ root.setAttribute('tabindex', '-1');
13438
+ }
13439
+ root.focus();
13440
+ }
13441
+ };
13404
13442
  useEffect(function () {
13405
13443
  if (active) {
13406
13444
  var root = rootNode.current;
@@ -13408,15 +13446,50 @@ function useFocusLock(active, header, body, autoFocusOnActivate) {
13408
13446
  activeFocusLockRoots.add(root);
13409
13447
  }
13410
13448
  updateFocusableItems();
13411
- var observer = new MutationObserver(function () {
13412
- updateFocusableItems();
13449
+ // Focus loss fires no events, so check for it after DOM mutations inside the lock
13450
+ var lostFocusTimeout;
13451
+ var scheduleLostFocusCheck = function scheduleLostFocusCheck() {
13452
+ if (lostFocusTimeout !== undefined || !lastFocusedInside.current) {
13453
+ return;
13454
+ }
13455
+ lostFocusTimeout = setTimeout(function () {
13456
+ lostFocusTimeout = undefined;
13457
+ if (isFocusLostFromLock()) {
13458
+ restoreFocusInsideLock();
13459
+ }
13460
+ }, LOST_FOCUS_CHECK_DELAY);
13461
+ };
13462
+ var observer = new MutationObserver(function (mutations) {
13463
+ if (mutations.some(function (mutation) {
13464
+ return mutation.type === 'childList';
13465
+ })) {
13466
+ updateFocusableItems();
13467
+ }
13468
+ scheduleLostFocusCheck();
13413
13469
  });
13414
13470
  if (root) {
13415
13471
  observer.observe(root, {
13472
+ attributeFilter: LOST_FOCUS_ATTRIBUTES,
13473
+ attributes: true,
13416
13474
  childList: true,
13417
13475
  subtree: true
13418
13476
  });
13419
13477
  }
13478
+ var handleFocusIn = function handleFocusIn(event) {
13479
+ if (event.target instanceof HTMLElement) {
13480
+ lastFocusedInside.current = event.target;
13481
+ }
13482
+ };
13483
+ var handleFocusOut = function handleFocusOut(event) {
13484
+ if (!event.relatedTarget) {
13485
+ scheduleLostFocusCheck();
13486
+ }
13487
+ };
13488
+ root == null || root.addEventListener('focusin', handleFocusIn);
13489
+ root == null || root.addEventListener('focusout', handleFocusOut);
13490
+ if (root != null && root.contains(document.activeElement)) {
13491
+ lastFocusedInside.current = document.activeElement;
13492
+ }
13420
13493
  // Defer the initial focus to the next frame so the browser can update
13421
13494
  // the accessibility tree first (e.g. after a `display: none` toggle).
13422
13495
  var focusFrame;
@@ -13443,6 +13516,10 @@ function useFocusLock(active, header, body, autoFocusOnActivate) {
13443
13516
  activeFocusLockRoots["delete"](root);
13444
13517
  }
13445
13518
  cancelAnimationFrame(focusFrame);
13519
+ clearTimeout(lostFocusTimeout);
13520
+ root == null || root.removeEventListener('focusin', handleFocusIn);
13521
+ root == null || root.removeEventListener('focusout', handleFocusOut);
13522
+ lastFocusedInside.current = null;
13446
13523
  observer.disconnect();
13447
13524
  };
13448
13525
  }
@@ -13458,7 +13535,17 @@ function useFocusLock(active, header, body, autoFocusOnActivate) {
13458
13535
  var length = focusableItems.current.length;
13459
13536
  var firstItem = focusableItems.current[0];
13460
13537
  var lastItem = focusableItems.current[length - 1];
13538
+ // Keep Tab inside the lock when focus was lost to `<body>`
13539
+ if (length > 0 && isFocusLostFromLock()) {
13540
+ event.preventDefault();
13541
+ (shiftKey ? lastItem : firstItem).focus();
13542
+ return;
13543
+ }
13461
13544
  var activeElement = document.activeElement;
13545
+ // Elements inside a nested lock are handled by that lock
13546
+ if (isInsideNestedLock(activeElement)) {
13547
+ return;
13548
+ }
13462
13549
  var eventTarget = event.target;
13463
13550
  var isEventInsideCurrentLock = !!rootNode.current && !!eventTarget && rootNode.current.contains(eventTarget);
13464
13551
  var isActiveElementTracked = !!activeElement && (activeElement === (header == null ? void 0 : header.current) || focusableItems.current.includes(activeElement));
@@ -13476,14 +13563,6 @@ function useFocusLock(active, header, body, autoFocusOnActivate) {
13476
13563
  *
13477
13564
  * This keeps the default logic intact and avoids breaking nested focus locks.
13478
13565
  */
13479
- // Check whether the active element sits inside a nested active lock.
13480
- // If so, that lock should handle Tab — not this (outer) one.
13481
- var isInsideNestedLock = !!activeElement && Array.from(activeFocusLockRoots).some(function (lockRoot) {
13482
- return lockRoot !== rootNode.current && rootNode.current.contains(lockRoot) && lockRoot.contains(activeElement);
13483
- });
13484
- if (isInsideNestedLock) {
13485
- return;
13486
- }
13487
13566
  if (length > 0 && (isEventInsideCurrentLock || isSafari) && !isActiveElementTracked && (isActiveElementInsideCurrentLock || activeElement === document.body)) {
13488
13567
  event.preventDefault();
13489
13568
  if (shiftKey) {