@activecollab/components 2.0.554 → 2.0.556

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.
Files changed (38) hide show
  1. package/dist/cjs/components/ModalRoot/ModalRoot.js +248 -25
  2. package/dist/cjs/components/ModalRoot/ModalRoot.js.map +1 -1
  3. package/dist/cjs/components/ModalRoot/Styles.js +2 -1
  4. package/dist/cjs/components/ModalRoot/Styles.js.map +1 -1
  5. package/dist/cjs/components/ModalRoot/modalRootStack.js +92 -23
  6. package/dist/cjs/components/ModalRoot/modalRootStack.js.map +1 -1
  7. package/dist/cjs/components/ModalRoot/useModalStackHost.js +8 -2
  8. package/dist/cjs/components/ModalRoot/useModalStackHost.js.map +1 -1
  9. package/dist/cjs/utils/layers.js +8 -3
  10. package/dist/cjs/utils/layers.js.map +1 -1
  11. package/dist/cjs/utils/layers.test.js +15 -0
  12. package/dist/cjs/utils/layers.test.js.map +1 -1
  13. package/dist/esm/components/ModalRoot/ModalRoot.d.ts +6 -2
  14. package/dist/esm/components/ModalRoot/ModalRoot.d.ts.map +1 -1
  15. package/dist/esm/components/ModalRoot/ModalRoot.js +246 -27
  16. package/dist/esm/components/ModalRoot/ModalRoot.js.map +1 -1
  17. package/dist/esm/components/ModalRoot/Styles.d.ts.map +1 -1
  18. package/dist/esm/components/ModalRoot/Styles.js +2 -1
  19. package/dist/esm/components/ModalRoot/Styles.js.map +1 -1
  20. package/dist/esm/components/ModalRoot/modalRootStack.d.ts +12 -0
  21. package/dist/esm/components/ModalRoot/modalRootStack.d.ts.map +1 -1
  22. package/dist/esm/components/ModalRoot/modalRootStack.js +66 -17
  23. package/dist/esm/components/ModalRoot/modalRootStack.js.map +1 -1
  24. package/dist/esm/components/ModalRoot/useModalStackHost.d.ts +3 -1
  25. package/dist/esm/components/ModalRoot/useModalStackHost.d.ts.map +1 -1
  26. package/dist/esm/components/ModalRoot/useModalStackHost.js +8 -2
  27. package/dist/esm/components/ModalRoot/useModalStackHost.js.map +1 -1
  28. package/dist/esm/utils/layers.d.ts +4 -1
  29. package/dist/esm/utils/layers.d.ts.map +1 -1
  30. package/dist/esm/utils/layers.js +6 -3
  31. package/dist/esm/utils/layers.js.map +1 -1
  32. package/dist/esm/utils/layers.test.js +15 -0
  33. package/dist/esm/utils/layers.test.js.map +1 -1
  34. package/dist/index.js +419 -122
  35. package/dist/index.js.map +1 -1
  36. package/dist/index.min.js +1 -1
  37. package/dist/index.min.js.map +1 -1
  38. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -15688,9 +15688,14 @@
15688
15688
  return (_element$closest = element === null || element === void 0 || (_element$closest2 = element.closest) === null || _element$closest2 === void 0 ? void 0 : _element$closest2.call(element, LAYER_SELECTOR)) !== null && _element$closest !== void 0 ? _element$closest : null;
15689
15689
  };
15690
15690
 
15691
- /** The layer in front: the last open window or dialog, or null when the page itself is in front. */
15691
+ /**
15692
+ * The layer in front: the last open window or dialog that takes input, or null when the page itself is in front. A
15693
+ * window in a dialog that plays its exit transition is inert and does not count.
15694
+ */
15692
15695
  var getTopLayer = function getTopLayer() {
15693
- var windows = document.querySelectorAll(LAYER_SELECTOR);
15696
+ var windows = Array.from(document.querySelectorAll(LAYER_SELECTOR)).filter(function (layer) {
15697
+ return !layer.closest("[inert]");
15698
+ });
15694
15699
  return windows.length ? windows[windows.length - 1] : null;
15695
15700
  };
15696
15701
 
@@ -15699,7 +15704,7 @@
15699
15704
  * goes to the layer the user works in and never to a page or a window under it.
15700
15705
  */
15701
15706
  var isInTopLayer = function isInTopLayer(element) {
15702
- return element ? getLayerOf(element) === getTopLayer() : false;
15707
+ return element && !element.closest("[inert]") ? getLayerOf(element) === getTopLayer() : false;
15703
15708
  };
15704
15709
 
15705
15710
  var colors$1 = {
@@ -19042,6 +19047,14 @@
19042
19047
  return listener();
19043
19048
  });
19044
19049
  };
19050
+ var getOpenEntries = function getOpenEntries() {
19051
+ return stack.filter(function (entry) {
19052
+ return !entry.closing;
19053
+ });
19054
+ };
19055
+ var isInside = function isInside(entry, outside) {
19056
+ return !!outside && !!entry.overlayRoot && outside.contains(entry.overlayRoot);
19057
+ };
19045
19058
  var pushModalRoot = function pushModalRoot(overlayRoot, suspension) {
19046
19059
  var belowOpenSheets = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
19047
19060
  lastId += 1;
@@ -19070,12 +19083,68 @@
19070
19083
  });
19071
19084
  notify();
19072
19085
  };
19073
- var isTopModalRoot = function isTopModalRoot(id) {
19074
- var _stack;
19075
- return ((_stack = stack[stack.length - 1]) === null || _stack === void 0 ? void 0 : _stack.id) === id;
19086
+
19087
+ /**
19088
+ * A modal root that plays its exit transition is no longer open: it leaves every query of the stack, and only keeps
19089
+ * the hosts already in its overlay root until popModalRoot.
19090
+ */
19091
+ var releaseModalRoot = function releaseModalRoot(id) {
19092
+ var entry = stack.find(function (candidate) {
19093
+ return candidate.id === id;
19094
+ });
19095
+ if (!entry || entry.closing) {
19096
+ return;
19097
+ }
19098
+ stack = stack.map(function (candidate) {
19099
+ return candidate === entry ? _objectSpread2(_objectSpread2({}, candidate), {}, {
19100
+ closing: true
19101
+ }) : candidate;
19102
+ });
19103
+ notify();
19076
19104
  };
19077
- var isInside = function isInside(entry, outside) {
19078
- return !!outside && !!entry.overlayRoot && outside.contains(entry.overlayRoot);
19105
+
19106
+ /** Reopened during its exit, a modal root keeps its id unless a newer one is in the stack; false then. */
19107
+ var reopenModalRoot = function reopenModalRoot(id) {
19108
+ var entry = stack[stack.length - 1];
19109
+ if ((entry === null || entry === void 0 ? void 0 : entry.id) !== id) {
19110
+ return false;
19111
+ }
19112
+ stack = stack.map(function (candidate) {
19113
+ return candidate === entry ? _objectSpread2(_objectSpread2({}, candidate), {}, {
19114
+ closing: false
19115
+ }) : candidate;
19116
+ });
19117
+ if (suspensions > 0) {
19118
+ var _entry$suspension;
19119
+ (_entry$suspension = entry.suspension) === null || _entry$suspension === void 0 || _entry$suspension.suspend();
19120
+ }
19121
+ notify();
19122
+ return true;
19123
+ };
19124
+
19125
+ /**
19126
+ * The top open modal root makes a closing one inert unless it contains it, also when it opened during the exit. One
19127
+ * opened from content the closing one holds sits inside it and is skipped.
19128
+ */
19129
+ var isLeftActive = function isLeftActive(entry) {
19130
+ var top = getOpenEntries().filter(function (open) {
19131
+ return !isInside(open, entry.overlayRoot);
19132
+ }).pop();
19133
+ return !top || !!top.overlayRoot && !!entry.overlayRoot && top.overlayRoot.contains(entry.overlayRoot);
19134
+ };
19135
+
19136
+ /**
19137
+ * True when the host sits in the overlay root of a modal root that plays its exit transition and stays active. An
19138
+ * open modal root beside it would leave the host inert, so the host moves into that one at once.
19139
+ */
19140
+ var isHeldByClosingModalRoot = function isHeldByClosingModalRoot(host) {
19141
+ return stack.some(function (entry) {
19142
+ return entry.closing && !!entry.overlayRoot && entry.overlayRoot === host.parentElement && isLeftActive(entry);
19143
+ });
19144
+ };
19145
+ var isTopModalRoot = function isTopModalRoot(id) {
19146
+ var _getOpenEntries$pop;
19147
+ return ((_getOpenEntries$pop = getOpenEntries().pop()) === null || _getOpenEntries$pop === void 0 ? void 0 : _getOpenEntries$pop.id) === id;
19079
19148
  };
19080
19149
 
19081
19150
  /**
@@ -19084,16 +19153,16 @@
19084
19153
  * itself.
19085
19154
  */
19086
19155
  var getTopModalOverlayRoot = function getTopModalOverlayRoot(outside) {
19087
- var _stack$filter$pop$ove, _stack$filter$pop;
19088
- return (_stack$filter$pop$ove = (_stack$filter$pop = stack.filter(function (entry) {
19156
+ var _getOpenEntries$filte, _getOpenEntries$filte2;
19157
+ return (_getOpenEntries$filte = (_getOpenEntries$filte2 = getOpenEntries().filter(function (entry) {
19089
19158
  return !isInside(entry, outside);
19090
- }).pop()) === null || _stack$filter$pop === void 0 ? void 0 : _stack$filter$pop.overlayRoot) !== null && _stack$filter$pop$ove !== void 0 ? _stack$filter$pop$ove : null;
19159
+ }).pop()) === null || _getOpenEntries$filte2 === void 0 ? void 0 : _getOpenEntries$filte2.overlayRoot) !== null && _getOpenEntries$filte !== void 0 ? _getOpenEntries$filte : null;
19091
19160
  };
19092
19161
 
19093
19162
  /** The id of the top open modal root, or undefined when none is open. */
19094
19163
  var getTopModalRootId = function getTopModalRootId() {
19095
- var _stack2;
19096
- return (_stack2 = stack[stack.length - 1]) === null || _stack2 === void 0 ? void 0 : _stack2.id;
19164
+ var _getOpenEntries$pop2;
19165
+ return (_getOpenEntries$pop2 = getOpenEntries().pop()) === null || _getOpenEntries$pop2 === void 0 ? void 0 : _getOpenEntries$pop2.id;
19097
19166
  };
19098
19167
 
19099
19168
  /**
@@ -19102,18 +19171,18 @@
19102
19171
  * when another modal opened in between from outside the sheet.
19103
19172
  */
19104
19173
  var getModalOverlayRootUpTo = function getModalOverlayRootUpTo(id, outside) {
19105
- var _stack$filter$pop$ove2, _stack$filter$pop2;
19106
- return (_stack$filter$pop$ove2 = (_stack$filter$pop2 = stack.filter(function (entry) {
19174
+ var _getOpenEntries$filte3, _getOpenEntries$filte4;
19175
+ return (_getOpenEntries$filte3 = (_getOpenEntries$filte4 = getOpenEntries().filter(function (entry) {
19107
19176
  return (entry.id <= id || entry.belowOpenSheets) && !isInside(entry, outside);
19108
- }).pop()) === null || _stack$filter$pop2 === void 0 ? void 0 : _stack$filter$pop2.overlayRoot) !== null && _stack$filter$pop$ove2 !== void 0 ? _stack$filter$pop$ove2 : null;
19177
+ }).pop()) === null || _getOpenEntries$filte4 === void 0 ? void 0 : _getOpenEntries$filte4.overlayRoot) !== null && _getOpenEntries$filte3 !== void 0 ? _getOpenEntries$filte3 : null;
19109
19178
  };
19110
19179
 
19111
19180
  /** The id of the top modal root that opened below open sheets, or undefined when none is open. */
19112
19181
  var getTopModalRootIdBelowOpenSheets = function getTopModalRootIdBelowOpenSheets() {
19113
- var _stack$filter$pop3;
19114
- return (_stack$filter$pop3 = stack.filter(function (entry) {
19182
+ var _getOpenEntries$filte5;
19183
+ return (_getOpenEntries$filte5 = getOpenEntries().filter(function (entry) {
19115
19184
  return entry.belowOpenSheets;
19116
- }).pop()) === null || _stack$filter$pop3 === void 0 ? void 0 : _stack$filter$pop3.id;
19185
+ }).pop()) === null || _getOpenEntries$filte5 === void 0 ? void 0 : _getOpenEntries$filte5.id;
19117
19186
  };
19118
19187
  var subscribeToModalRoots = function subscribeToModalRoots(listener) {
19119
19188
  listeners.add(listener);
@@ -19137,9 +19206,9 @@
19137
19206
  var suspendModalRoots = function suspendModalRoots() {
19138
19207
  suspensions += 1;
19139
19208
  if (suspensions === 1) {
19140
- [...stack].reverse().forEach(function (entry) {
19141
- var _entry$suspension;
19142
- return (_entry$suspension = entry.suspension) === null || _entry$suspension === void 0 ? void 0 : _entry$suspension.suspend();
19209
+ getOpenEntries().reverse().forEach(function (entry) {
19210
+ var _entry$suspension2;
19211
+ return (_entry$suspension2 = entry.suspension) === null || _entry$suspension2 === void 0 ? void 0 : _entry$suspension2.suspend();
19143
19212
  });
19144
19213
  }
19145
19214
  var released = false;
@@ -19150,9 +19219,9 @@
19150
19219
  released = true;
19151
19220
  suspensions -= 1;
19152
19221
  if (suspensions === 0) {
19153
- stack.forEach(function (entry) {
19154
- var _entry$suspension2;
19155
- return (_entry$suspension2 = entry.suspension) === null || _entry$suspension2 === void 0 ? void 0 : _entry$suspension2.resume();
19222
+ getOpenEntries().forEach(function (entry) {
19223
+ var _entry$suspension3;
19224
+ return (_entry$suspension3 = entry.suspension) === null || _entry$suspension3 === void 0 ? void 0 : _entry$suspension3.resume();
19156
19225
  });
19157
19226
  }
19158
19227
  };
@@ -19163,9 +19232,86 @@
19163
19232
  var StyledModalRoot = styled__default["default"].dialog.withConfig({
19164
19233
  displayName: "Styles__StyledModalRoot",
19165
19234
  componentId: "sc-48b5xq-0"
19166
- })(["", " ", " position:fixed;inset:0;width:100%;height:100%;max-width:none;max-height:none;margin:0;padding:0;border:0;background:var(--modal-background);color:inherit;overflow:visible;animation:", " ", "ms ease;transition:background-color ", "ms ease;&[data-state=\"closing\"]{background-color:transparent;}&:focus-visible{outline:none;}&::backdrop{background:transparent;}"], FontStyle, BoxSizingStyle, backdropIn, MODAL_ROOT_BACKDROP_MS, MODAL_ROOT_BACKDROP_MS);
19235
+ })(["", " ", " position:fixed;inset:0;width:100%;height:100%;max-width:none;max-height:none;margin:0;padding:0;border:0;background:var(--modal-background);color:inherit;overflow:visible;animation:", " ", "ms ease;transition:background-color ", "ms ease;&[data-state=\"closing\"]{background-color:transparent;}&[data-state=\"closing\"]:not([open]){display:block;z-index:", ";pointer-events:none;}&[data-state=\"closing\"]:not([open]) > .c-modal-root__overlays{pointer-events:auto;}&[data-state=\"closing\"]::backdrop{pointer-events:none;}> .c-modal-root__content{display:contents;}&:focus{outline:none;}&::backdrop{background:transparent;}"], FontStyle, BoxSizingStyle, backdropIn, MODAL_ROOT_BACKDROP_MS, MODAL_ROOT_BACKDROP_MS, layers.cloud - 1);
19167
19236
  StyledModalRoot.displayName = "StyledModalRoot";
19168
19237
 
19238
+ var MODAL_STACK_HOST_ATTRIBUTE = "data-modal-stack-host";
19239
+ var getScrollPositions = function getScrollPositions(host) {
19240
+ return Array.from(host.querySelectorAll("*")).filter(function (element) {
19241
+ return element.scrollTop || element.scrollLeft;
19242
+ }).map(function (element) {
19243
+ return {
19244
+ element,
19245
+ top: element.scrollTop,
19246
+ left: element.scrollLeft
19247
+ };
19248
+ });
19249
+ };
19250
+
19251
+ /**
19252
+ * moveBefore keeps the state of the moved content. appendChild resets the scroll positions inside the host and drops
19253
+ * the focus, so without moveBefore they are put back after the move.
19254
+ */
19255
+ var moveHost = function moveHost(host, target) {
19256
+ if (host.isConnected && target.moveBefore) {
19257
+ target.moveBefore(host, null);
19258
+ return;
19259
+ }
19260
+ var scrollPositions = host.isConnected ? getScrollPositions(host) : [];
19261
+ var focused = host.contains(document.activeElement) ? document.activeElement : null;
19262
+ target.appendChild(host);
19263
+ scrollPositions.forEach(function (_ref) {
19264
+ var element = _ref.element,
19265
+ top = _ref.top,
19266
+ left = _ref.left;
19267
+ element.scrollTop = top;
19268
+ element.scrollLeft = left;
19269
+ });
19270
+ focused === null || focused === void 0 || focused.focus({
19271
+ preventScroll: true
19272
+ });
19273
+ };
19274
+
19275
+ /**
19276
+ * Keeps a host node in the overlay root of the top open modal, or of the top one opened no later than
19277
+ * upToModalRootId, and in document.body while there is none. A modal opened from the content of the host is not
19278
+ * counted, since its overlay root is inside the host. A modal that plays its exit transition keeps the node until
19279
+ * it is done. The node moves as modals open and close, so content
19280
+ * portalled into it never remounts and keeps its scroll positions and focus, and it leaves the page on unmount or
19281
+ * when the host changes. Returns whether the
19282
+ * host is in the page, which it is from a layout effect after the first render with it.
19283
+ */
19284
+ var useModalStackHost = function useModalStackHost(host, upToModalRootId) {
19285
+ var _useState = React.useState(null),
19286
+ _useState2 = _slicedToArray(_useState, 2),
19287
+ placedHost = _useState2[0],
19288
+ setPlacedHost = _useState2[1];
19289
+ React.useLayoutEffect(function () {
19290
+ if (!host) {
19291
+ return undefined;
19292
+ }
19293
+ var place = function place() {
19294
+ var _ref2;
19295
+ if (isHeldByClosingModalRoot(host)) {
19296
+ return;
19297
+ }
19298
+ var target = (_ref2 = upToModalRootId === undefined ? getTopModalOverlayRoot(host) : getModalOverlayRootUpTo(upToModalRootId, host)) !== null && _ref2 !== void 0 ? _ref2 : document.body;
19299
+ if (host.parentElement !== target) {
19300
+ moveHost(host, target);
19301
+ }
19302
+ };
19303
+ host.setAttribute(MODAL_STACK_HOST_ATTRIBUTE, "");
19304
+ place();
19305
+ setPlacedHost(host);
19306
+ var unsubscribe = subscribeToModalRoots(place);
19307
+ return function () {
19308
+ unsubscribe();
19309
+ host.remove();
19310
+ };
19311
+ }, [host, upToModalRootId]);
19312
+ return host !== null && placedHost === host;
19313
+ };
19314
+
19169
19315
  var _excluded$1P = ["open", "onRequestClose", "onDismissBlocked", "onClose", "disableCloseOnEsc", "disableBackgroundClick", "disableScrollLock", "disableReturnFocus", "belowOpenSheets", "children", "className", "onPointerDown", "onClick", "onKeyDown"];
19170
19316
  var FOCUSABLE = "button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), summary, iframe, audio[controls], video[controls], [contenteditable]:not([contenteditable='false']), [tabindex]";
19171
19317
 
@@ -19180,6 +19326,82 @@
19180
19326
  return document.activeElement === candidate;
19181
19327
  });
19182
19328
  };
19329
+ var FOCUS_HISTORY_SIZE = 5;
19330
+
19331
+ // Neither removed, disabled nor hidden, so the element could take the focus back.
19332
+ var isFocusable = function isFocusable(element) {
19333
+ return element.isConnected && element.matches(FOCUSABLE) && (
19334
+ // eslint-disable-next-line compat/compat
19335
+ typeof element.checkVisibility !== "function" || element.checkVisibility());
19336
+ };
19337
+
19338
+ // A focused element that a closed dialog hides, or that turned inert, keeps the focus until the browser renders again.
19339
+ var isFocusLost = function isFocusLost() {
19340
+ var focus = document.activeElement;
19341
+ if (!focus || focus === document.body || !focus.isConnected || focus.closest("[inert]")) {
19342
+ return true;
19343
+ }
19344
+ var layer = focus.closest("dialog, .c-window");
19345
+ return layer instanceof HTMLDialogElement && !layer.open;
19346
+ };
19347
+
19348
+ // A sheet is a window, not a dialog, so once the last dialog is closed nothing else takes a lost focus back.
19349
+ var focusTopWindowIfLost = function focusTopWindowIfLost() {
19350
+ var topLayer = getTopLayer();
19351
+ if (getTopModalRootId() === undefined && isFocusLost() && topLayer !== null && topLayer !== void 0 && topLayer.matches(".c-window")) {
19352
+ topLayer.focus({
19353
+ preventScroll: true
19354
+ });
19355
+ }
19356
+ };
19357
+ var keepFocusOn = function keepFocusOn(element) {
19358
+ if (element instanceof HTMLElement && element !== document.body && element.isConnected && document.activeElement !== element) {
19359
+ element.focus({
19360
+ preventScroll: true
19361
+ });
19362
+ }
19363
+ };
19364
+
19365
+ // A closed dialog shown as a manual popover stays in the top layer without blocking the page. showPopover focuses a
19366
+ // dialog, so the focus goes back to where close() left it.
19367
+ var keepInTopLayer = function keepInTopLayer(node) {
19368
+ if (typeof node.showPopover !== "function") {
19369
+ return;
19370
+ }
19371
+ var focus = document.activeElement;
19372
+ node.setAttribute("popover", "manual");
19373
+ // eslint-disable-next-line compat/compat
19374
+ node.showPopover();
19375
+ keepFocusOn(focus);
19376
+ if (document.activeElement !== focus && node.contains(document.activeElement)) {
19377
+ document.activeElement.blur();
19378
+ }
19379
+ };
19380
+ var leaveTopLayer = function leaveTopLayer(node) {
19381
+ if (!node.hasAttribute("popover")) {
19382
+ return;
19383
+ }
19384
+ if (node.matches(":popover-open")) {
19385
+ // eslint-disable-next-line compat/compat
19386
+ node.hidePopover();
19387
+ }
19388
+ node.removeAttribute("popover");
19389
+ };
19390
+ var isHeldStackHost = function isHeldStackHost(element, overlayRoot) {
19391
+ var _element$closest;
19392
+ return !!overlayRoot && (element === null || element === void 0 || (_element$closest = element.closest("[".concat(MODAL_STACK_HOST_ATTRIBUTE, "]"))) === null || _element$closest === void 0 ? void 0 : _element$closest.parentElement) === overlayRoot;
19393
+ };
19394
+
19395
+ // Popups of the content sit in the overlay root next to the stack hosts and leave with the content.
19396
+ var setContentInert = function setContentInert(content, overlayRoot, inert) {
19397
+ var _overlayRoot$children;
19398
+ content === null || content === void 0 || content.toggleAttribute("inert", inert);
19399
+ Array.from((_overlayRoot$children = overlayRoot === null || overlayRoot === void 0 ? void 0 : overlayRoot.children) !== null && _overlayRoot$children !== void 0 ? _overlayRoot$children : []).forEach(function (child) {
19400
+ if (!child.hasAttribute(MODAL_STACK_HOST_ATTRIBUTE)) {
19401
+ child.toggleAttribute("inert", inert);
19402
+ }
19403
+ });
19404
+ };
19183
19405
  var ModalRoot = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
19184
19406
  var open = _ref.open,
19185
19407
  onRequestClose = _ref.onRequestClose,
@@ -19228,6 +19450,8 @@
19228
19450
  var openRef = React.useRef(open);
19229
19451
  var pressStartedOnBackdrop = React.useRef(false);
19230
19452
  var stackIdRef = React.useRef();
19453
+ var exitingRef = React.useRef(false);
19454
+ var contentRef = React.useRef(null);
19231
19455
  var overlayRootRef = React.useRef(null);
19232
19456
  // Menus and pickers opened inside portal here, into the top layer with the dialog, not under it.
19233
19457
  var getOverlayRoot = React.useCallback(function () {
@@ -19237,15 +19461,61 @@
19237
19461
  var disableReturnFocusRef = React.useRef(disableReturnFocus);
19238
19462
  disableReturnFocusRef.current = disableReturnFocus;
19239
19463
 
19240
- // close() gives the focus back to the element that had it before showModal when the focus is in the dialog, so
19241
- // a dialog that leaves it there takes it off again. A focus outside, as in a sheet that left the overlay root,
19242
- // stays where it is.
19464
+ // close() of a modal gives the focus back to the element that had it before showModal, so a dialog that leaves it
19465
+ // there takes it off again. A focus outside, as in a sheet that left the overlay root, stays where it is, and so
19466
+ // does a focus in a sheet the overlay root holds for the exit transition. When that element is gone, also when it
19467
+ // goes with the same update right after, and the last dialog closes over a sheet, the sheet takes it.
19243
19468
  var closeDialog = React.useCallback(function (node) {
19244
- var focusInside = node.contains(document.activeElement);
19469
+ var focus = document.activeElement;
19470
+ var focusHeld = isHeldStackHost(focus, overlayRootRef.current);
19471
+ var focusInside = node.contains(focus) && !focusHeld;
19245
19472
  node.close();
19246
- if (disableReturnFocusRef.current && focusInside) {
19473
+ if (!focusInside) {
19474
+ keepFocusOn(focus);
19475
+ return;
19476
+ }
19477
+ if (disableReturnFocusRef.current) {
19247
19478
  var _document$activeEleme;
19248
19479
  (_document$activeEleme = document.activeElement) === null || _document$activeEleme === void 0 || _document$activeEleme.blur();
19480
+ return;
19481
+ }
19482
+ focusTopWindowIfLost();
19483
+ requestAnimationFrame(focusTopWindowIfLost);
19484
+ }, []);
19485
+ var focusHistoryRef = React.useRef([]);
19486
+ var blurredRef = React.useRef(null);
19487
+
19488
+ // The browser drops the focus to the body when the focused element goes, as a toast that closes or a button that
19489
+ // another replaces, so the top dialog takes it back: on the newest element focused before that is still there, or
19490
+ // on itself. A layer in front of it, such as a sheet in the overlay root, keeps its own focus. A field that blurs
19491
+ // itself, as on Escape or Enter, can still take the focus, so it let it go on purpose and the dialog takes it.
19492
+ var restoreLostFocus = React.useCallback(function () {
19493
+ var node = dialogRef.current;
19494
+ var stackId = stackIdRef.current;
19495
+ var blurred = blurredRef.current;
19496
+ blurredRef.current = null;
19497
+ if (!(node !== null && node !== void 0 && node.open) || stackId === undefined || !isTopModalRoot(stackId) || areModalRootsSuspended() || !isFocusLost() || getTopLayer() !== node) {
19498
+ return;
19499
+ }
19500
+ if (blurred && node.contains(blurred) && isFocusable(blurred)) {
19501
+ node.focus({
19502
+ preventScroll: true
19503
+ });
19504
+ return;
19505
+ }
19506
+ var restored = focusHistoryRef.current.some(function (candidate) {
19507
+ if (!candidate.isConnected || !node.contains(candidate)) {
19508
+ return false;
19509
+ }
19510
+ candidate.focus({
19511
+ preventScroll: true
19512
+ });
19513
+ return document.activeElement === candidate;
19514
+ });
19515
+ if (!restored) {
19516
+ node.focus({
19517
+ preventScroll: true
19518
+ });
19249
19519
  }
19250
19520
  }, []);
19251
19521
  if (open && !mounted) {
@@ -19286,26 +19556,56 @@
19286
19556
  preventScroll: true
19287
19557
  });
19288
19558
  }
19559
+ restoreLostFocus();
19289
19560
  }
19290
19561
  });
19291
- var finishClose = React.useCallback(function () {
19562
+ var leaveStack = React.useCallback(function () {
19563
+ if (stackIdRef.current !== undefined) {
19564
+ popModalRoot(stackIdRef.current);
19565
+ stackIdRef.current = undefined;
19566
+ }
19567
+ }, []);
19568
+
19569
+ // The closed dialog stays shown for its exit transition, and its content takes no input until it unmounts.
19570
+ // Sheets in its overlay root stay there until the transition is done.
19571
+ var releaseForExit = React.useCallback(function () {
19572
+ var node = dialogRef.current;
19573
+ if (!node || exitingRef.current) {
19574
+ return;
19575
+ }
19576
+ exitingRef.current = true;
19292
19577
  // A dialog that closes while suspended leaves the stack before the resume, so it drops its layer here.
19293
- if (dialogRef.current) {
19294
- dialogRef.current.style.zIndex = "";
19578
+ node.style.zIndex = "";
19579
+ if (stackIdRef.current !== undefined) {
19580
+ releaseModalRoot(stackIdRef.current);
19581
+ }
19582
+
19583
+ // Inert before close(), so a focus lock in a popup cannot take back the focus close() gives the opener.
19584
+ setContentInert(contentRef.current, overlayRootRef.current, true);
19585
+ if (node.open) {
19586
+ closeDialog(node);
19587
+ }
19588
+ keepInTopLayer(node);
19589
+ }, [closeDialog]);
19590
+ var finishClose = React.useCallback(function () {
19591
+ var node = dialogRef.current;
19592
+ exitingRef.current = false;
19593
+ if (node) {
19594
+ node.style.zIndex = "";
19295
19595
  }
19296
19596
 
19297
19597
  // Content that sits in the overlay root moves out before the dialog hides it.
19298
- if (stackIdRef.current !== undefined) {
19299
- popModalRoot(stackIdRef.current);
19300
- stackIdRef.current = undefined;
19598
+ leaveStack();
19599
+ if (node !== null && node !== void 0 && node.open) {
19600
+ closeDialog(node);
19301
19601
  }
19302
- if (dialogRef.current) {
19303
- closeDialog(dialogRef.current);
19602
+ if (node) {
19603
+ leaveTopLayer(node);
19304
19604
  }
19305
19605
  setMounted(false);
19306
19606
  setShownDialog(null);
19307
19607
  onClose === null || onClose === void 0 || onClose();
19308
- }, [closeDialog, onClose]);
19608
+ }, [closeDialog, leaveStack, onClose]);
19309
19609
 
19310
19610
  // The entry joins the stack with every showModal, so an owner that opens the dialog again from onClose, on the
19311
19611
  // same node, gets it back. It joins after showModal, so content that moves into the overlay root lands where it
@@ -19314,14 +19614,26 @@
19314
19614
  if (!open || !dialog) {
19315
19615
  return;
19316
19616
  }
19617
+ var reopenedDuringExit = exitingRef.current;
19618
+ if (reopenedDuringExit) {
19619
+ exitingRef.current = false;
19620
+ leaveTopLayer(dialog);
19621
+ setContentInert(contentRef.current, overlayRootRef.current, false);
19622
+ }
19317
19623
  if (!dialog.open) {
19318
19624
  dialog.showModal();
19319
19625
  }
19626
+ if (reopenedDuringExit && stackIdRef.current !== undefined && !reopenModalRoot(stackIdRef.current)) {
19627
+ leaveStack();
19628
+ }
19629
+ if (reopenedDuringExit && !areModalRootsSuspended() && !dialog.contains(document.activeElement)) {
19630
+ focusFirstFocusable(dialog, overlayRootRef.current);
19631
+ }
19320
19632
  if (stackIdRef.current === undefined) {
19321
19633
  stackIdRef.current = pushModalRoot(overlayRootRef.current, suspensionRef.current, belowOpenSheets);
19322
19634
  }
19323
19635
  setShownDialog(dialog);
19324
- }, [open, dialog, belowOpenSheets]);
19636
+ }, [open, dialog, belowOpenSheets, leaveStack]);
19325
19637
 
19326
19638
  // showModal found no content to focus, so the dialog moves the focus in once the content is there, unless an
19327
19639
  // autoFocus field took it. A dialog that opened suspended stays out of the way of the overlay above it.
@@ -19341,20 +19653,23 @@
19341
19653
  // opener: a dialog removed while open would leave it on the body.
19342
19654
  React.useLayoutEffect(function () {
19343
19655
  return function () {
19344
- if (stackIdRef.current !== undefined) {
19345
- popModalRoot(stackIdRef.current);
19346
- stackIdRef.current = undefined;
19347
- }
19656
+ exitingRef.current = false;
19657
+ leaveStack();
19348
19658
  if (dialog !== null && dialog !== void 0 && dialog.open) {
19349
19659
  closeDialog(dialog);
19350
19660
  }
19351
19661
  };
19352
- }, [dialog, closeDialog]);
19662
+ }, [dialog, closeDialog, leaveStack]);
19353
19663
  React.useLayoutEffect(function () {
19354
- if (!open && mounted && !hasTransition) {
19664
+ if (open || !mounted) {
19665
+ return;
19666
+ }
19667
+ if (hasTransition) {
19668
+ releaseForExit();
19669
+ } else {
19355
19670
  finishClose();
19356
19671
  }
19357
- }, [open, mounted, hasTransition, finishClose]);
19672
+ }, [open, mounted, hasTransition, releaseForExit, finishClose]);
19358
19673
  var dismissRef = React.useRef();
19359
19674
  dismissRef.current = function (reason, event) {
19360
19675
  if (areModalRootsSuspended()) {
@@ -19432,6 +19747,54 @@
19432
19747
  dialog.removeEventListener("close", handleNativeClose);
19433
19748
  };
19434
19749
  }, [dialog]);
19750
+
19751
+ // The check waits a frame, so a close() that gives the focus back to an opener runs first. A modal root above
19752
+ // leaves the stack before its close(), and may sit outside this dialog, so the stack starts the check too.
19753
+ React.useEffect(function () {
19754
+ if (!dialog) {
19755
+ return undefined;
19756
+ }
19757
+ var frame;
19758
+ var scheduleRestore = function scheduleRestore() {
19759
+ if (frame === undefined) {
19760
+ frame = requestAnimationFrame(function () {
19761
+ frame = undefined;
19762
+ restoreLostFocus();
19763
+ });
19764
+ }
19765
+ };
19766
+ var handleFocusIn = function handleFocusIn(event) {
19767
+ var target = event.target;
19768
+ if (target instanceof HTMLElement && target !== dialog) {
19769
+ focusHistoryRef.current = [target, ...focusHistoryRef.current.filter(function (element) {
19770
+ return element !== target;
19771
+ })].slice(0, FOCUS_HISTORY_SIZE);
19772
+ }
19773
+ };
19774
+ var handleFocusOut = function handleFocusOut(event) {
19775
+ if (!event.relatedTarget) {
19776
+ blurredRef.current = event.target instanceof HTMLElement ? event.target : null;
19777
+ scheduleRestore();
19778
+ }
19779
+ };
19780
+ var observer = new MutationObserver(scheduleRestore);
19781
+ observer.observe(dialog, {
19782
+ childList: true,
19783
+ subtree: true
19784
+ });
19785
+ dialog.addEventListener("focusin", handleFocusIn);
19786
+ dialog.addEventListener("focusout", handleFocusOut);
19787
+ var unsubscribe = subscribeToModalRoots(scheduleRestore);
19788
+ return function () {
19789
+ unsubscribe();
19790
+ observer.disconnect();
19791
+ dialog.removeEventListener("focusin", handleFocusIn);
19792
+ dialog.removeEventListener("focusout", handleFocusOut);
19793
+ if (frame !== undefined) {
19794
+ cancelAnimationFrame(frame);
19795
+ }
19796
+ };
19797
+ }, [dialog, restoreLostFocus]);
19435
19798
  var handlePointerDown = React.useCallback(function (event) {
19436
19799
  pressStartedOnBackdrop.current = event.target === event.currentTarget;
19437
19800
  onPointerDown === null || onPointerDown === void 0 || onPointerDown(event);
@@ -19493,15 +19856,20 @@
19493
19856
  ref: handleRef,
19494
19857
  enabled: !disableScrollLock,
19495
19858
  noIsolation: true
19496
- }, /*#__PURE__*/React__default["default"].createElement(StyledModalRoot, _extends({}, rest, {
19859
+ }, /*#__PURE__*/React__default["default"].createElement(StyledModalRoot, _extends({
19860
+ tabIndex: -1
19861
+ }, rest, {
19497
19862
  className: classnames__default["default"]("c-modal-root", className),
19498
19863
  "data-state": open ? "open" : "closing",
19499
19864
  onPointerDown: handlePointerDown,
19500
19865
  onClick: handleClick,
19501
19866
  onKeyDown: handleKeyDown
19502
- }), /*#__PURE__*/React__default["default"].createElement(PortalContainerProvider, {
19867
+ }), /*#__PURE__*/React__default["default"].createElement("div", {
19868
+ ref: contentRef,
19869
+ className: "c-modal-root__content"
19870
+ }, /*#__PURE__*/React__default["default"].createElement(PortalContainerProvider, {
19503
19871
  value: getOverlayRoot
19504
- }, content), /*#__PURE__*/React__default["default"].createElement("div", {
19872
+ }, content)), /*#__PURE__*/React__default["default"].createElement("div", {
19505
19873
  ref: overlayRootRef,
19506
19874
  className: "c-modal-root__overlays"
19507
19875
  }))));
@@ -28838,77 +29206,6 @@
28838
29206
  })(["@media (min-width:640px){display:none;}"]);
28839
29207
  StyledHeader$2.displayName = "StyledHeader";
28840
29208
 
28841
- var getScrollPositions = function getScrollPositions(host) {
28842
- return Array.from(host.querySelectorAll("*")).filter(function (element) {
28843
- return element.scrollTop || element.scrollLeft;
28844
- }).map(function (element) {
28845
- return {
28846
- element,
28847
- top: element.scrollTop,
28848
- left: element.scrollLeft
28849
- };
28850
- });
28851
- };
28852
-
28853
- /**
28854
- * moveBefore keeps the state of the moved content. appendChild resets the scroll positions inside the host and drops
28855
- * the focus, so without moveBefore they are put back after the move.
28856
- */
28857
- var moveHost = function moveHost(host, target) {
28858
- if (host.isConnected && target.moveBefore) {
28859
- target.moveBefore(host, null);
28860
- return;
28861
- }
28862
- var scrollPositions = host.isConnected ? getScrollPositions(host) : [];
28863
- var focused = host.contains(document.activeElement) ? document.activeElement : null;
28864
- target.appendChild(host);
28865
- scrollPositions.forEach(function (_ref) {
28866
- var element = _ref.element,
28867
- top = _ref.top,
28868
- left = _ref.left;
28869
- element.scrollTop = top;
28870
- element.scrollLeft = left;
28871
- });
28872
- focused === null || focused === void 0 || focused.focus({
28873
- preventScroll: true
28874
- });
28875
- };
28876
-
28877
- /**
28878
- * Keeps a host node in the overlay root of the top open modal, or of the top one opened no later than
28879
- * upToModalRootId, and in document.body while there is none. A modal opened from the content of the host is not
28880
- * counted, since its overlay root is inside the host. The node moves as modals open and close, so content
28881
- * portalled into it never remounts and keeps its scroll positions and focus, and it leaves the page on unmount or
28882
- * when the host changes. Returns whether the
28883
- * host is in the page, which it is from a layout effect after the first render with it.
28884
- */
28885
- var useModalStackHost = function useModalStackHost(host, upToModalRootId) {
28886
- var _useState = React.useState(null),
28887
- _useState2 = _slicedToArray(_useState, 2),
28888
- placedHost = _useState2[0],
28889
- setPlacedHost = _useState2[1];
28890
- React.useLayoutEffect(function () {
28891
- if (!host) {
28892
- return undefined;
28893
- }
28894
- var place = function place() {
28895
- var _ref2;
28896
- var target = (_ref2 = upToModalRootId === undefined ? getTopModalOverlayRoot(host) : getModalOverlayRootUpTo(upToModalRootId, host)) !== null && _ref2 !== void 0 ? _ref2 : document.body;
28897
- if (host.parentElement !== target) {
28898
- moveHost(host, target);
28899
- }
28900
- };
28901
- place();
28902
- setPlacedHost(host);
28903
- var unsubscribe = subscribeToModalRoots(place);
28904
- return function () {
28905
- unsubscribe();
28906
- host.remove();
28907
- };
28908
- }, [host, upToModalRootId]);
28909
- return host !== null && placedHost === host;
28910
- };
28911
-
28912
29209
  var createHost$1 = function createHost() {
28913
29210
  var host = document.createElement("div");
28914
29211
  host.className = "c-sheet-modal-host";