@activecollab/components 2.0.555 → 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 +129 -28
  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 +2 -1
  14. package/dist/esm/components/ModalRoot/ModalRoot.d.ts.map +1 -1
  15. package/dist/esm/components/ModalRoot/ModalRoot.js +130 -29
  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 +300 -125
  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{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
 
@@ -19189,14 +19335,14 @@
19189
19335
  typeof element.checkVisibility !== "function" || element.checkVisibility());
19190
19336
  };
19191
19337
 
19192
- // A focused element that a closed dialog hides keeps the focus until the browser renders again.
19338
+ // A focused element that a closed dialog hides, or that turned inert, keeps the focus until the browser renders again.
19193
19339
  var isFocusLost = function isFocusLost() {
19194
19340
  var focus = document.activeElement;
19195
- if (!focus || focus === document.body || !focus.isConnected) {
19341
+ if (!focus || focus === document.body || !focus.isConnected || focus.closest("[inert]")) {
19196
19342
  return true;
19197
19343
  }
19198
- var dialog = focus.closest("dialog");
19199
- return dialog !== null && !dialog.open;
19344
+ var layer = focus.closest("dialog, .c-window");
19345
+ return layer instanceof HTMLDialogElement && !layer.open;
19200
19346
  };
19201
19347
 
19202
19348
  // A sheet is a window, not a dialog, so once the last dialog is closed nothing else takes a lost focus back.
@@ -19208,6 +19354,54 @@
19208
19354
  });
19209
19355
  }
19210
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
+ };
19211
19405
  var ModalRoot = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
19212
19406
  var open = _ref.open,
19213
19407
  onRequestClose = _ref.onRequestClose,
@@ -19256,6 +19450,8 @@
19256
19450
  var openRef = React.useRef(open);
19257
19451
  var pressStartedOnBackdrop = React.useRef(false);
19258
19452
  var stackIdRef = React.useRef();
19453
+ var exitingRef = React.useRef(false);
19454
+ var contentRef = React.useRef(null);
19259
19455
  var overlayRootRef = React.useRef(null);
19260
19456
  // Menus and pickers opened inside portal here, into the top layer with the dialog, not under it.
19261
19457
  var getOverlayRoot = React.useCallback(function () {
@@ -19265,14 +19461,17 @@
19265
19461
  var disableReturnFocusRef = React.useRef(disableReturnFocus);
19266
19462
  disableReturnFocusRef.current = disableReturnFocus;
19267
19463
 
19268
- // close() gives the focus back to the element that had it before showModal when the focus is in the dialog, so
19269
- // a dialog that leaves it there takes it off again. A focus outside, as in a sheet that left the overlay root,
19270
- // stays where it is. When that element is gone, also when it goes with the same update right after, and the
19271
- // last dialog closes over a sheet, the sheet takes it.
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.
19272
19468
  var closeDialog = React.useCallback(function (node) {
19273
- 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;
19274
19472
  node.close();
19275
19473
  if (!focusInside) {
19474
+ keepFocusOn(focus);
19276
19475
  return;
19277
19476
  }
19278
19477
  if (disableReturnFocusRef.current) {
@@ -19360,24 +19559,53 @@
19360
19559
  restoreLostFocus();
19361
19560
  }
19362
19561
  });
19363
- 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;
19364
19577
  // A dialog that closes while suspended leaves the stack before the resume, so it drops its layer here.
19365
- if (dialogRef.current) {
19366
- 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 = "";
19367
19595
  }
19368
19596
 
19369
19597
  // Content that sits in the overlay root moves out before the dialog hides it.
19370
- if (stackIdRef.current !== undefined) {
19371
- popModalRoot(stackIdRef.current);
19372
- stackIdRef.current = undefined;
19598
+ leaveStack();
19599
+ if (node !== null && node !== void 0 && node.open) {
19600
+ closeDialog(node);
19373
19601
  }
19374
- if (dialogRef.current) {
19375
- closeDialog(dialogRef.current);
19602
+ if (node) {
19603
+ leaveTopLayer(node);
19376
19604
  }
19377
19605
  setMounted(false);
19378
19606
  setShownDialog(null);
19379
19607
  onClose === null || onClose === void 0 || onClose();
19380
- }, [closeDialog, onClose]);
19608
+ }, [closeDialog, leaveStack, onClose]);
19381
19609
 
19382
19610
  // The entry joins the stack with every showModal, so an owner that opens the dialog again from onClose, on the
19383
19611
  // same node, gets it back. It joins after showModal, so content that moves into the overlay root lands where it
@@ -19386,14 +19614,26 @@
19386
19614
  if (!open || !dialog) {
19387
19615
  return;
19388
19616
  }
19617
+ var reopenedDuringExit = exitingRef.current;
19618
+ if (reopenedDuringExit) {
19619
+ exitingRef.current = false;
19620
+ leaveTopLayer(dialog);
19621
+ setContentInert(contentRef.current, overlayRootRef.current, false);
19622
+ }
19389
19623
  if (!dialog.open) {
19390
19624
  dialog.showModal();
19391
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
+ }
19392
19632
  if (stackIdRef.current === undefined) {
19393
19633
  stackIdRef.current = pushModalRoot(overlayRootRef.current, suspensionRef.current, belowOpenSheets);
19394
19634
  }
19395
19635
  setShownDialog(dialog);
19396
- }, [open, dialog, belowOpenSheets]);
19636
+ }, [open, dialog, belowOpenSheets, leaveStack]);
19397
19637
 
19398
19638
  // showModal found no content to focus, so the dialog moves the focus in once the content is there, unless an
19399
19639
  // autoFocus field took it. A dialog that opened suspended stays out of the way of the overlay above it.
@@ -19413,20 +19653,23 @@
19413
19653
  // opener: a dialog removed while open would leave it on the body.
19414
19654
  React.useLayoutEffect(function () {
19415
19655
  return function () {
19416
- if (stackIdRef.current !== undefined) {
19417
- popModalRoot(stackIdRef.current);
19418
- stackIdRef.current = undefined;
19419
- }
19656
+ exitingRef.current = false;
19657
+ leaveStack();
19420
19658
  if (dialog !== null && dialog !== void 0 && dialog.open) {
19421
19659
  closeDialog(dialog);
19422
19660
  }
19423
19661
  };
19424
- }, [dialog, closeDialog]);
19662
+ }, [dialog, closeDialog, leaveStack]);
19425
19663
  React.useLayoutEffect(function () {
19426
- if (!open && mounted && !hasTransition) {
19664
+ if (open || !mounted) {
19665
+ return;
19666
+ }
19667
+ if (hasTransition) {
19668
+ releaseForExit();
19669
+ } else {
19427
19670
  finishClose();
19428
19671
  }
19429
- }, [open, mounted, hasTransition, finishClose]);
19672
+ }, [open, mounted, hasTransition, releaseForExit, finishClose]);
19430
19673
  var dismissRef = React.useRef();
19431
19674
  dismissRef.current = function (reason, event) {
19432
19675
  if (areModalRootsSuspended()) {
@@ -19621,9 +19864,12 @@
19621
19864
  onPointerDown: handlePointerDown,
19622
19865
  onClick: handleClick,
19623
19866
  onKeyDown: handleKeyDown
19624
- }), /*#__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, {
19625
19871
  value: getOverlayRoot
19626
- }, content), /*#__PURE__*/React__default["default"].createElement("div", {
19872
+ }, content)), /*#__PURE__*/React__default["default"].createElement("div", {
19627
19873
  ref: overlayRootRef,
19628
19874
  className: "c-modal-root__overlays"
19629
19875
  }))));
@@ -28960,77 +29206,6 @@
28960
29206
  })(["@media (min-width:640px){display:none;}"]);
28961
29207
  StyledHeader$2.displayName = "StyledHeader";
28962
29208
 
28963
- var getScrollPositions = function getScrollPositions(host) {
28964
- return Array.from(host.querySelectorAll("*")).filter(function (element) {
28965
- return element.scrollTop || element.scrollLeft;
28966
- }).map(function (element) {
28967
- return {
28968
- element,
28969
- top: element.scrollTop,
28970
- left: element.scrollLeft
28971
- };
28972
- });
28973
- };
28974
-
28975
- /**
28976
- * moveBefore keeps the state of the moved content. appendChild resets the scroll positions inside the host and drops
28977
- * the focus, so without moveBefore they are put back after the move.
28978
- */
28979
- var moveHost = function moveHost(host, target) {
28980
- if (host.isConnected && target.moveBefore) {
28981
- target.moveBefore(host, null);
28982
- return;
28983
- }
28984
- var scrollPositions = host.isConnected ? getScrollPositions(host) : [];
28985
- var focused = host.contains(document.activeElement) ? document.activeElement : null;
28986
- target.appendChild(host);
28987
- scrollPositions.forEach(function (_ref) {
28988
- var element = _ref.element,
28989
- top = _ref.top,
28990
- left = _ref.left;
28991
- element.scrollTop = top;
28992
- element.scrollLeft = left;
28993
- });
28994
- focused === null || focused === void 0 || focused.focus({
28995
- preventScroll: true
28996
- });
28997
- };
28998
-
28999
- /**
29000
- * Keeps a host node in the overlay root of the top open modal, or of the top one opened no later than
29001
- * upToModalRootId, and in document.body while there is none. A modal opened from the content of the host is not
29002
- * counted, since its overlay root is inside the host. The node moves as modals open and close, so content
29003
- * portalled into it never remounts and keeps its scroll positions and focus, and it leaves the page on unmount or
29004
- * when the host changes. Returns whether the
29005
- * host is in the page, which it is from a layout effect after the first render with it.
29006
- */
29007
- var useModalStackHost = function useModalStackHost(host, upToModalRootId) {
29008
- var _useState = React.useState(null),
29009
- _useState2 = _slicedToArray(_useState, 2),
29010
- placedHost = _useState2[0],
29011
- setPlacedHost = _useState2[1];
29012
- React.useLayoutEffect(function () {
29013
- if (!host) {
29014
- return undefined;
29015
- }
29016
- var place = function place() {
29017
- var _ref2;
29018
- var target = (_ref2 = upToModalRootId === undefined ? getTopModalOverlayRoot(host) : getModalOverlayRootUpTo(upToModalRootId, host)) !== null && _ref2 !== void 0 ? _ref2 : document.body;
29019
- if (host.parentElement !== target) {
29020
- moveHost(host, target);
29021
- }
29022
- };
29023
- place();
29024
- setPlacedHost(host);
29025
- var unsubscribe = subscribeToModalRoots(place);
29026
- return function () {
29027
- unsubscribe();
29028
- host.remove();
29029
- };
29030
- }, [host, upToModalRootId]);
29031
- return host !== null && placedHost === host;
29032
- };
29033
-
29034
29209
  var createHost$1 = function createHost() {
29035
29210
  var host = document.createElement("div");
29036
29211
  host.className = "c-sheet-modal-host";