carbon-react 151.2.3 → 151.3.0

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 (25) hide show
  1. package/esm/__internal__/dom/globals.d.ts +51 -0
  2. package/esm/__internal__/dom/globals.js +59 -0
  3. package/esm/__spec_helper__/mock-element-scrollto.js +3 -0
  4. package/esm/__spec_helper__/mock-match-media.js +1 -1
  5. package/esm/__spec_helper__/mock-resize-observer.js +1 -1
  6. package/esm/components/flat-table/flat-table-row/flat-table-row.style.js +4 -1
  7. package/esm/components/icon/icon.style.js +6 -4
  8. package/esm/components/menu/menu-full-screen/menu-full-screen.component.js +2 -1
  9. package/esm/components/modal/__internal__/modal-manager.js +18 -7
  10. package/esm/components/modal/modal.component.js +3 -1
  11. package/esm/components/vertical-menu/vertical-menu-full-screen/vertical-menu-full-screen.component.js +3 -1
  12. package/esm/hooks/__internal__/useScrollBlock/scroll-block-manager.js +11 -4
  13. package/lib/__internal__/dom/globals.d.ts +51 -0
  14. package/lib/__internal__/dom/globals.js +67 -0
  15. package/lib/__spec_helper__/mock-element-scrollto.js +3 -0
  16. package/lib/__spec_helper__/mock-match-media.js +1 -1
  17. package/lib/__spec_helper__/mock-resize-observer.js +1 -1
  18. package/lib/components/flat-table/flat-table-row/flat-table-row.style.js +4 -1
  19. package/lib/components/icon/icon.style.js +6 -4
  20. package/lib/components/menu/menu-full-screen/menu-full-screen.component.js +2 -1
  21. package/lib/components/modal/__internal__/modal-manager.js +18 -7
  22. package/lib/components/modal/modal.component.js +3 -1
  23. package/lib/components/vertical-menu/vertical-menu-full-screen/vertical-menu-full-screen.component.js +3 -1
  24. package/lib/hooks/__internal__/useScrollBlock/scroll-block-manager.js +12 -6
  25. package/package.json +2 -1
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Returns the global `window` object in a way that is friendly to SSR.
3
+ * This check can be avoided if you are sure that the code will only run in the browser.
4
+ * Some examples where you can ignore this:
5
+ * - `useEffect` hooks
6
+ * - `useLayoutEffect` hooks
7
+ * - Callbacks for event listeners
8
+ * - Files with the `"use-client"` directive
9
+ *
10
+ * Primarily, this is necessary when attempting to access the `window` object during rendering of components.
11
+ * However, even still, where possible, it is recommended to avoid accessing the `window` object during rendering
12
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
13
+ * client renders due to missing environment-specific data (e.g., `window.innerWidth` being undefined on the server).
14
+ *
15
+ * @returns The global `window` object, if it exists.
16
+ */
17
+ export declare function getWindow(): (Window & typeof globalThis) | undefined;
18
+ /**
19
+ * Returns the global `document` object in a way that is friendly to SSR.
20
+ * This check can be avoided if you are sure that the code will only run in the browser.
21
+ * Some examples where you can ignore this:
22
+ * - `useEffect` hooks
23
+ * - `useLayoutEffect` hooks
24
+ * - Callbacks for event listeners
25
+ * - Files with the `"use-client"` directive
26
+ *
27
+ * Primarily, this is necessary when attempting to access the `document` object during rendering of components.
28
+ * However, even still, where possible, it is recommended to avoid accessing the `document` object during rendering
29
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
30
+ * client renders due to missing environment-specific data (e.g., `document.children` being undefined on the server).
31
+ *
32
+ * @returns The global `document` object, if it exists.
33
+ */
34
+ export declare function getDocument(): Document | undefined;
35
+ /**
36
+ * Returns the global `navigator` object in a way that is friendly to SSR.
37
+ * This check can be avoided if you are sure that the code will only run in the browser.
38
+ * Some examples where you can ignore this:
39
+ * - `useEffect` hooks
40
+ * - `useLayoutEffect` hooks
41
+ * - Callbacks for event listeners
42
+ * - Files with the `"use-client"` directive
43
+ *
44
+ * Primarily, this is necessary when attempting to access the `navigator` object during rendering of components.
45
+ * However, even still, where possible, it is recommended to avoid accessing the `navigator` object during rendering
46
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
47
+ * client renders due to missing environment-specific data (e.g., `navigator.userAgent` being undefined on the server).
48
+ *
49
+ * @returns The global `navigator` object, if it exists.
50
+ */
51
+ export declare function getNavigator(): Navigator | undefined;
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Returns the global `window` object in a way that is friendly to SSR.
3
+ * This check can be avoided if you are sure that the code will only run in the browser.
4
+ * Some examples where you can ignore this:
5
+ * - `useEffect` hooks
6
+ * - `useLayoutEffect` hooks
7
+ * - Callbacks for event listeners
8
+ * - Files with the `"use-client"` directive
9
+ *
10
+ * Primarily, this is necessary when attempting to access the `window` object during rendering of components.
11
+ * However, even still, where possible, it is recommended to avoid accessing the `window` object during rendering
12
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
13
+ * client renders due to missing environment-specific data (e.g., `window.innerWidth` being undefined on the server).
14
+ *
15
+ * @returns The global `window` object, if it exists.
16
+ */
17
+ export function getWindow() {
18
+ return typeof window !== "undefined" ? window : undefined;
19
+ }
20
+
21
+ /**
22
+ * Returns the global `document` object in a way that is friendly to SSR.
23
+ * This check can be avoided if you are sure that the code will only run in the browser.
24
+ * Some examples where you can ignore this:
25
+ * - `useEffect` hooks
26
+ * - `useLayoutEffect` hooks
27
+ * - Callbacks for event listeners
28
+ * - Files with the `"use-client"` directive
29
+ *
30
+ * Primarily, this is necessary when attempting to access the `document` object during rendering of components.
31
+ * However, even still, where possible, it is recommended to avoid accessing the `document` object during rendering
32
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
33
+ * client renders due to missing environment-specific data (e.g., `document.children` being undefined on the server).
34
+ *
35
+ * @returns The global `document` object, if it exists.
36
+ */
37
+ export function getDocument() {
38
+ return typeof document !== "undefined" ? document : undefined;
39
+ }
40
+
41
+ /**
42
+ * Returns the global `navigator` object in a way that is friendly to SSR.
43
+ * This check can be avoided if you are sure that the code will only run in the browser.
44
+ * Some examples where you can ignore this:
45
+ * - `useEffect` hooks
46
+ * - `useLayoutEffect` hooks
47
+ * - Callbacks for event listeners
48
+ * - Files with the `"use-client"` directive
49
+ *
50
+ * Primarily, this is necessary when attempting to access the `navigator` object during rendering of components.
51
+ * However, even still, where possible, it is recommended to avoid accessing the `navigator` object during rendering
52
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
53
+ * client renders due to missing environment-specific data (e.g., `navigator.userAgent` being undefined on the server).
54
+ *
55
+ * @returns The global `navigator` object, if it exists.
56
+ */
57
+ export function getNavigator() {
58
+ return typeof navigator !== "undefined" ? navigator : undefined;
59
+ }
@@ -1,6 +1,9 @@
1
1
  const setupScrollToMock = () => {
2
2
  // need to mock the `scrollTo` method, which is undefined in JSDOM. As we're not actually testing this behaviour, just make it
3
3
  // do nothing.
4
+ if (typeof window === "undefined") {
5
+ return;
6
+ }
4
7
  HTMLElement.prototype.scrollTo = () => {};
5
8
  };
6
9
  export default setupScrollToMock;
@@ -2,7 +2,7 @@ let mocked = false;
2
2
  let _matches = false;
3
3
  const removeEventListener = jest.fn();
4
4
  export const setupMatchMediaMock = () => {
5
- if (!global.window) {
5
+ if (typeof window === "undefined") {
6
6
  return;
7
7
  }
8
8
  const noop = () => {};
@@ -1,5 +1,5 @@
1
1
  const setupResizeObserverMock = () => {
2
- if (!window) {
2
+ if (typeof window === "undefined") {
3
3
  return;
4
4
  }
5
5
  window.ResizeObserver = window.ResizeObserver || jest.fn().mockImplementation(callback => {
@@ -7,6 +7,7 @@ import StyledFlatTableHeader from "../flat-table-header/flat-table-header.style"
7
7
  import StyledIcon from "../../icon/icon.style";
8
8
  import { toColor } from "../../../style/utils/color";
9
9
  import addFocusStyling from "../../../style/utils/add-focus-styling";
10
+ import { getNavigator } from "../../../__internal__/dom/globals";
10
11
  import { isSafari } from "../../../__internal__/utils/helpers/browser-type-check";
11
12
  const horizontalBorderSizes = {
12
13
  medium: "2px",
@@ -101,6 +102,7 @@ const StyledFlatTableRow = styled.tr`
101
102
  draggable,
102
103
  rowHeight
103
104
  }) => {
105
+ const nav = getNavigator();
104
106
  const backgroundColor = bgColor ? toColor(theme, bgColor) : undefined;
105
107
  const customBorderColor = horizontalBorderColor ? toColor(theme, horizontalBorderColor) : undefined;
106
108
  const colorOfSelected = isInSidebar ? "var(--colorsUtilityMajor150)" : "var(--colorsUtilityMajor075)";
@@ -209,7 +211,8 @@ const StyledFlatTableRow = styled.tr`
209
211
  }
210
212
 
211
213
  /* Styling for safari. Position relative does not work on tr elements on Safari */
212
- ${isSafari(navigator) && css`
214
+ // FIXME: this can cause hydration mismatches during SSR.
215
+ ${nav && isSafari(nav) && css`
213
216
  position: -webkit-sticky;
214
217
  :after {
215
218
  border: none;
@@ -5,6 +5,7 @@ import baseTheme from "../../style/themes/base";
5
5
  import addFocusStyling from "../../style/utils/add-focus-styling";
6
6
  import styledColor from "../../style/utils/color";
7
7
  import getColorValue from "../../style/utils/get-color-value";
8
+ import { getNavigator, getWindow } from "../../__internal__/dom/globals";
8
9
  import browserTypeCheck, { isSafari } from "../../__internal__/utils/helpers/browser-type-check";
9
10
  import iconConfig from "./icon-config";
10
11
  import iconUnicodes from "./icon-unicodes";
@@ -48,6 +49,8 @@ const StyledIcon = styled.span`
48
49
  let finalHoverColor;
49
50
  let bgColor;
50
51
  let bgHoverColor;
52
+ const win = getWindow();
53
+ const nav = getNavigator();
51
54
  const adjustedBgSize = adjustIconBgSize(fontSize, bgSize);
52
55
  try {
53
56
  if (disabled) {
@@ -116,12 +119,11 @@ const StyledIcon = styled.span`
116
119
  font-size: ${iconConfig.iconSize[fontSize]};
117
120
  line-height: ${iconConfig.iconSize[fontSize]};
118
121
  `}
119
-
120
- ${type === "services" && browserTypeCheck(window) && css`
122
+ // FIXME: this can cause hydration mismatches during SSR.
123
+ ${win && type === "services" && browserTypeCheck(win) && css`
121
124
  margin-top: ${fontSize === "small" ? "-7px" : "-8px"};
122
125
  `}
123
-
124
- ${type === "services" && isSafari(navigator) && !browserTypeCheck(window) && css`
126
+ ${nav && win && type === "services" && isSafari(nav) && !browserTypeCheck(win) && css`
125
127
  margin-top: -6px;
126
128
  `}
127
129
 
@@ -10,6 +10,7 @@ import Icon from "../../icon";
10
10
  import Portal from "../../portal";
11
11
  import FocusTrap from "../../../__internal__/focus-trap";
12
12
  import MenuDivider from "../menu-divider/menu-divider.component";
13
+ import { getDocument } from "../../../__internal__/dom/globals";
13
14
  import useLocale from "../../../hooks/__internal__/useLocale";
14
15
  import useModalAria from "../../../hooks/__internal__/useModalAria";
15
16
  import useModalManager from "../../../hooks/__internal__/useModalManager";
@@ -54,7 +55,7 @@ export const MenuFullscreen = ({
54
55
  closeModal,
55
56
  modalRef: menuRef,
56
57
  topModalOverride,
57
- focusCallToActionElement: document.activeElement
58
+ focusCallToActionElement: getDocument()?.activeElement
58
59
  });
59
60
  return /*#__PURE__*/React.createElement("li", null, /*#__PURE__*/React.createElement(Portal, null, /*#__PURE__*/React.createElement(CSSTransition, {
60
61
  nodeRef: menuRef,
@@ -4,6 +4,7 @@ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r),
4
4
  function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
5
5
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
6
6
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
+ import { getWindow } from "../../../__internal__/dom/globals";
7
8
  let ModalManagerInstance = /*#__PURE__*/function () {
8
9
  function ModalManagerInstance() {
9
10
  _classCallCheck(this, ModalManagerInstance);
@@ -27,12 +28,17 @@ let ModalManagerInstance = /*#__PURE__*/function () {
27
28
  });
28
29
  this.callTopModalSetters();
29
30
  });
31
+ const safeWindow = getWindow();
32
+ if (!safeWindow) {
33
+ this.modalList = [];
34
+ return;
35
+ }
30
36
  // Due to possibility of multiple carbon versions using it
31
37
  // it is necessary to maintain same structure in this global variable
32
- if (!window.__CARBON_INTERNALS_MODAL_LIST) {
33
- window.__CARBON_INTERNALS_MODAL_LIST = [];
38
+ if (!safeWindow.__CARBON_INTERNALS_MODAL_LIST) {
39
+ safeWindow.__CARBON_INTERNALS_MODAL_LIST = [];
34
40
  }
35
- this.modalList = window.__CARBON_INTERNALS_MODAL_LIST;
41
+ this.modalList = safeWindow.__CARBON_INTERNALS_MODAL_LIST;
36
42
  }
37
43
  return _createClass(ModalManagerInstance, [{
38
44
  key: "getTopModal",
@@ -78,16 +84,21 @@ let ModalManagerInstance = /*#__PURE__*/function () {
78
84
  }, {
79
85
  key: "clearList",
80
86
  value: function clearList() {
81
- window.__CARBON_INTERNALS_MODAL_LIST = [];
82
- this.modalList = window.__CARBON_INTERNALS_MODAL_LIST;
87
+ const safeWindow = getWindow();
88
+ const cleared = [];
89
+ if (safeWindow) {
90
+ safeWindow.__CARBON_INTERNALS_MODAL_LIST = cleared;
91
+ }
92
+ this.modalList = cleared;
83
93
  this.callTopModalSetters();
84
94
  }
85
95
  }, {
86
96
  key: "callTopModalSetters",
87
97
  value: function callTopModalSetters() {
88
- if (window.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
98
+ const safeWindow = getWindow();
99
+ if (safeWindow?.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
89
100
  const topModal = this.getTopModal()?.modal || null;
90
- for (const setTopModal of window.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
101
+ for (const setTopModal of safeWindow.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
91
102
  setTopModal(topModal);
92
103
  }
93
104
  }
@@ -3,6 +3,7 @@ import React, { useEffect, useRef, useCallback, useState } from "react";
3
3
  import { TransitionGroup, CSSTransition } from "react-transition-group";
4
4
  import useScrollBlock from "../../hooks/__internal__/useScrollBlock";
5
5
  import Portal from "../portal";
6
+ import { getDocument } from "../../__internal__/dom/globals";
6
7
  import Events from "../../__internal__/utils/helpers/events";
7
8
  import useModalManager from "../../hooks/__internal__/useModalManager";
8
9
  import { StyledModal, StyledModalBackground } from "./modal.style";
@@ -53,13 +54,14 @@ const Modal = ({
53
54
  onCancel(ev);
54
55
  }
55
56
  }, [disableClose, disableEscKey, onCancel]);
57
+ const safeDocument = getDocument();
56
58
  useModalManager({
57
59
  open,
58
60
  closeModal,
59
61
  modalRef: ref,
60
62
  setTriggerRefocusFlag,
61
63
  topModalOverride,
62
- focusCallToActionElement: restoreFocusOnClose ? document.activeElement : undefined
64
+ focusCallToActionElement: restoreFocusOnClose && safeDocument ? safeDocument.activeElement : undefined
63
65
  });
64
66
  let background;
65
67
  let content;
@@ -9,6 +9,7 @@ import Icon from "../../icon";
9
9
  import Box from "../../box";
10
10
  import { StyledList, StyledVerticalMenuFullScreen } from "../vertical-menu.style";
11
11
  import VerticalMenuFullScreenContext from "./__internal__/vertical-menu-full-screen.context";
12
+ import { getDocument } from "../../../__internal__/dom/globals";
12
13
  import Events from "../../../__internal__/utils/helpers/events/events";
13
14
  import useModalManager from "../../../hooks/__internal__/useModalManager";
14
15
  export const VerticalMenuFullScreen = ({
@@ -27,12 +28,13 @@ export const VerticalMenuFullScreen = ({
27
28
  onClose(ev);
28
29
  }
29
30
  }, [onClose]);
31
+ const safeDocument = getDocument();
30
32
  useModalManager({
31
33
  open: isOpen,
32
34
  closeModal: handleKeyDown,
33
35
  modalRef: menuWrapperRef,
34
36
  topModalOverride: true,
35
- focusCallToActionElement: document.activeElement
37
+ focusCallToActionElement: safeDocument?.activeElement
36
38
  });
37
39
 
38
40
  // TODO remove this as part of FE-5650
@@ -6,24 +6,31 @@ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol"
6
6
  function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
7
7
  // TODO: This component can be refactored to remove redundant code
8
8
  // once we can confirm that all Sage products use version 105.0.0^
9
+ import { getWindow } from "../../../__internal__/dom/globals";
9
10
  let ScrollBlockManager = /*#__PURE__*/function () {
10
11
  function ScrollBlockManager() {
11
12
  _classCallCheck(this, ScrollBlockManager);
12
13
  _defineProperty(this, "components", void 0);
13
14
  _defineProperty(this, "originalValues", void 0);
15
+ const safeWindow = getWindow();
16
+ if (!safeWindow) {
17
+ this.components = {};
18
+ this.originalValues = [];
19
+ return;
20
+ }
14
21
  // Due to possibility of multiple carbon versions using it
15
22
  // it is necessary to maintain same structure in this global variable
16
- if (!window.__CARBON_INTERNALS_SCROLL_BLOCKERS) {
17
- window.__CARBON_INTERNALS_SCROLL_BLOCKERS = {
23
+ if (!safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS) {
24
+ safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS = {
18
25
  components: {},
19
26
  // originalValues can be removed
20
27
  originalValues: [],
21
28
  restoreValues: null
22
29
  };
23
30
  }
24
- this.components = window.__CARBON_INTERNALS_SCROLL_BLOCKERS.components;
31
+ this.components = safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS.components;
25
32
  // TODO: originalValues can be removed
26
- this.originalValues = window.__CARBON_INTERNALS_SCROLL_BLOCKERS.originalValues;
33
+ this.originalValues = safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS.originalValues;
27
34
  }
28
35
  return _createClass(ScrollBlockManager, [{
29
36
  key: "registerComponent",
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Returns the global `window` object in a way that is friendly to SSR.
3
+ * This check can be avoided if you are sure that the code will only run in the browser.
4
+ * Some examples where you can ignore this:
5
+ * - `useEffect` hooks
6
+ * - `useLayoutEffect` hooks
7
+ * - Callbacks for event listeners
8
+ * - Files with the `"use-client"` directive
9
+ *
10
+ * Primarily, this is necessary when attempting to access the `window` object during rendering of components.
11
+ * However, even still, where possible, it is recommended to avoid accessing the `window` object during rendering
12
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
13
+ * client renders due to missing environment-specific data (e.g., `window.innerWidth` being undefined on the server).
14
+ *
15
+ * @returns The global `window` object, if it exists.
16
+ */
17
+ export declare function getWindow(): (Window & typeof globalThis) | undefined;
18
+ /**
19
+ * Returns the global `document` object in a way that is friendly to SSR.
20
+ * This check can be avoided if you are sure that the code will only run in the browser.
21
+ * Some examples where you can ignore this:
22
+ * - `useEffect` hooks
23
+ * - `useLayoutEffect` hooks
24
+ * - Callbacks for event listeners
25
+ * - Files with the `"use-client"` directive
26
+ *
27
+ * Primarily, this is necessary when attempting to access the `document` object during rendering of components.
28
+ * However, even still, where possible, it is recommended to avoid accessing the `document` object during rendering
29
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
30
+ * client renders due to missing environment-specific data (e.g., `document.children` being undefined on the server).
31
+ *
32
+ * @returns The global `document` object, if it exists.
33
+ */
34
+ export declare function getDocument(): Document | undefined;
35
+ /**
36
+ * Returns the global `navigator` object in a way that is friendly to SSR.
37
+ * This check can be avoided if you are sure that the code will only run in the browser.
38
+ * Some examples where you can ignore this:
39
+ * - `useEffect` hooks
40
+ * - `useLayoutEffect` hooks
41
+ * - Callbacks for event listeners
42
+ * - Files with the `"use-client"` directive
43
+ *
44
+ * Primarily, this is necessary when attempting to access the `navigator` object during rendering of components.
45
+ * However, even still, where possible, it is recommended to avoid accessing the `navigator` object during rendering
46
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
47
+ * client renders due to missing environment-specific data (e.g., `navigator.userAgent` being undefined on the server).
48
+ *
49
+ * @returns The global `navigator` object, if it exists.
50
+ */
51
+ export declare function getNavigator(): Navigator | undefined;
@@ -0,0 +1,67 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getDocument = getDocument;
7
+ exports.getNavigator = getNavigator;
8
+ exports.getWindow = getWindow;
9
+ /**
10
+ * Returns the global `window` object in a way that is friendly to SSR.
11
+ * This check can be avoided if you are sure that the code will only run in the browser.
12
+ * Some examples where you can ignore this:
13
+ * - `useEffect` hooks
14
+ * - `useLayoutEffect` hooks
15
+ * - Callbacks for event listeners
16
+ * - Files with the `"use-client"` directive
17
+ *
18
+ * Primarily, this is necessary when attempting to access the `window` object during rendering of components.
19
+ * However, even still, where possible, it is recommended to avoid accessing the `window` object during rendering
20
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
21
+ * client renders due to missing environment-specific data (e.g., `window.innerWidth` being undefined on the server).
22
+ *
23
+ * @returns The global `window` object, if it exists.
24
+ */
25
+ function getWindow() {
26
+ return typeof window !== "undefined" ? window : undefined;
27
+ }
28
+
29
+ /**
30
+ * Returns the global `document` object in a way that is friendly to SSR.
31
+ * This check can be avoided if you are sure that the code will only run in the browser.
32
+ * Some examples where you can ignore this:
33
+ * - `useEffect` hooks
34
+ * - `useLayoutEffect` hooks
35
+ * - Callbacks for event listeners
36
+ * - Files with the `"use-client"` directive
37
+ *
38
+ * Primarily, this is necessary when attempting to access the `document` object during rendering of components.
39
+ * However, even still, where possible, it is recommended to avoid accessing the `document` object during rendering
40
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
41
+ * client renders due to missing environment-specific data (e.g., `document.children` being undefined on the server).
42
+ *
43
+ * @returns The global `document` object, if it exists.
44
+ */
45
+ function getDocument() {
46
+ return typeof document !== "undefined" ? document : undefined;
47
+ }
48
+
49
+ /**
50
+ * Returns the global `navigator` object in a way that is friendly to SSR.
51
+ * This check can be avoided if you are sure that the code will only run in the browser.
52
+ * Some examples where you can ignore this:
53
+ * - `useEffect` hooks
54
+ * - `useLayoutEffect` hooks
55
+ * - Callbacks for event listeners
56
+ * - Files with the `"use-client"` directive
57
+ *
58
+ * Primarily, this is necessary when attempting to access the `navigator` object during rendering of components.
59
+ * However, even still, where possible, it is recommended to avoid accessing the `navigator` object during rendering
60
+ * because it can lead to hydration mismatches. This happens when the server-rendered HTML differs from what the
61
+ * client renders due to missing environment-specific data (e.g., `navigator.userAgent` being undefined on the server).
62
+ *
63
+ * @returns The global `navigator` object, if it exists.
64
+ */
65
+ function getNavigator() {
66
+ return typeof navigator !== "undefined" ? navigator : undefined;
67
+ }
@@ -7,6 +7,9 @@ exports.default = void 0;
7
7
  const setupScrollToMock = () => {
8
8
  // need to mock the `scrollTo` method, which is undefined in JSDOM. As we're not actually testing this behaviour, just make it
9
9
  // do nothing.
10
+ if (typeof window === "undefined") {
11
+ return;
12
+ }
10
13
  HTMLElement.prototype.scrollTo = () => {};
11
14
  };
12
15
  var _default = exports.default = setupScrollToMock;
@@ -8,7 +8,7 @@ let mocked = false;
8
8
  let _matches = false;
9
9
  const removeEventListener = jest.fn();
10
10
  const setupMatchMediaMock = () => {
11
- if (!global.window) {
11
+ if (typeof window === "undefined") {
12
12
  return;
13
13
  }
14
14
  const noop = () => {};
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  const setupResizeObserverMock = () => {
8
- if (!window) {
8
+ if (typeof window === "undefined") {
9
9
  return;
10
10
  }
11
11
  window.ResizeObserver = window.ResizeObserver || jest.fn().mockImplementation(callback => {
@@ -13,6 +13,7 @@ var _flatTableHeader = _interopRequireDefault(require("../flat-table-header/flat
13
13
  var _icon = _interopRequireDefault(require("../../icon/icon.style"));
14
14
  var _color = require("../../../style/utils/color");
15
15
  var _addFocusStyling = _interopRequireDefault(require("../../../style/utils/add-focus-styling"));
16
+ var _globals = require("../../../__internal__/dom/globals");
16
17
  var _browserTypeCheck = require("../../../__internal__/utils/helpers/browser-type-check");
17
18
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
18
19
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
@@ -110,6 +111,7 @@ const StyledFlatTableRow = _styledComponents.default.tr`
110
111
  draggable,
111
112
  rowHeight
112
113
  }) => {
114
+ const nav = (0, _globals.getNavigator)();
113
115
  const backgroundColor = bgColor ? (0, _color.toColor)(theme, bgColor) : undefined;
114
116
  const customBorderColor = horizontalBorderColor ? (0, _color.toColor)(theme, horizontalBorderColor) : undefined;
115
117
  const colorOfSelected = isInSidebar ? "var(--colorsUtilityMajor150)" : "var(--colorsUtilityMajor075)";
@@ -218,7 +220,8 @@ const StyledFlatTableRow = _styledComponents.default.tr`
218
220
  }
219
221
 
220
222
  /* Styling for safari. Position relative does not work on tr elements on Safari */
221
- ${(0, _browserTypeCheck.isSafari)(navigator) && (0, _styledComponents.css)`
223
+ // FIXME: this can cause hydration mismatches during SSR.
224
+ ${nav && (0, _browserTypeCheck.isSafari)(nav) && (0, _styledComponents.css)`
222
225
  position: -webkit-sticky;
223
226
  :after {
224
227
  border: none;
@@ -11,6 +11,7 @@ var _base = _interopRequireDefault(require("../../style/themes/base"));
11
11
  var _addFocusStyling = _interopRequireDefault(require("../../style/utils/add-focus-styling"));
12
12
  var _color = _interopRequireDefault(require("../../style/utils/color"));
13
13
  var _getColorValue = _interopRequireDefault(require("../../style/utils/get-color-value"));
14
+ var _globals = require("../../__internal__/dom/globals");
14
15
  var _browserTypeCheck = _interopRequireWildcard(require("../../__internal__/utils/helpers/browser-type-check"));
15
16
  var _iconConfig = _interopRequireDefault(require("./icon-config"));
16
17
  var _iconUnicodes = _interopRequireDefault(require("./icon-unicodes"));
@@ -57,6 +58,8 @@ const StyledIcon = _styledComponents.default.span`
57
58
  let finalHoverColor;
58
59
  let bgColor;
59
60
  let bgHoverColor;
61
+ const win = (0, _globals.getWindow)();
62
+ const nav = (0, _globals.getNavigator)();
60
63
  const adjustedBgSize = adjustIconBgSize(fontSize, bgSize);
61
64
  try {
62
65
  if (disabled) {
@@ -125,12 +128,11 @@ const StyledIcon = _styledComponents.default.span`
125
128
  font-size: ${_iconConfig.default.iconSize[fontSize]};
126
129
  line-height: ${_iconConfig.default.iconSize[fontSize]};
127
130
  `}
128
-
129
- ${type === "services" && (0, _browserTypeCheck.default)(window) && (0, _styledComponents.css)`
131
+ // FIXME: this can cause hydration mismatches during SSR.
132
+ ${win && type === "services" && (0, _browserTypeCheck.default)(win) && (0, _styledComponents.css)`
130
133
  margin-top: ${fontSize === "small" ? "-7px" : "-8px"};
131
134
  `}
132
-
133
- ${type === "services" && (0, _browserTypeCheck.isSafari)(navigator) && !(0, _browserTypeCheck.default)(window) && (0, _styledComponents.css)`
135
+ ${nav && win && type === "services" && (0, _browserTypeCheck.isSafari)(nav) && !(0, _browserTypeCheck.default)(win) && (0, _styledComponents.css)`
134
136
  margin-top: -6px;
135
137
  `}
136
138
 
@@ -16,6 +16,7 @@ var _icon = _interopRequireDefault(require("../../icon"));
16
16
  var _portal = _interopRequireDefault(require("../../portal"));
17
17
  var _focusTrap = _interopRequireDefault(require("../../../__internal__/focus-trap"));
18
18
  var _menuDivider = _interopRequireDefault(require("../menu-divider/menu-divider.component"));
19
+ var _globals = require("../../../__internal__/dom/globals");
19
20
  var _useLocale = _interopRequireDefault(require("../../../hooks/__internal__/useLocale"));
20
21
  var _useModalAria = _interopRequireDefault(require("../../../hooks/__internal__/useModalAria"));
21
22
  var _useModalManager = _interopRequireDefault(require("../../../hooks/__internal__/useModalManager"));
@@ -63,7 +64,7 @@ const MenuFullscreen = ({
63
64
  closeModal,
64
65
  modalRef: menuRef,
65
66
  topModalOverride,
66
- focusCallToActionElement: document.activeElement
67
+ focusCallToActionElement: (0, _globals.getDocument)()?.activeElement
67
68
  });
68
69
  return /*#__PURE__*/_react.default.createElement("li", null, /*#__PURE__*/_react.default.createElement(_portal.default, null, /*#__PURE__*/_react.default.createElement(_reactTransitionGroup.CSSTransition, {
69
70
  nodeRef: menuRef,
@@ -4,6 +4,7 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = exports.ModalManagerInstance = void 0;
7
+ var _globals = require("../../../__internal__/dom/globals");
7
8
  function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
8
9
  function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
9
10
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
@@ -33,12 +34,17 @@ let ModalManagerInstance = exports.ModalManagerInstance = /*#__PURE__*/function
33
34
  });
34
35
  this.callTopModalSetters();
35
36
  });
37
+ const safeWindow = (0, _globals.getWindow)();
38
+ if (!safeWindow) {
39
+ this.modalList = [];
40
+ return;
41
+ }
36
42
  // Due to possibility of multiple carbon versions using it
37
43
  // it is necessary to maintain same structure in this global variable
38
- if (!window.__CARBON_INTERNALS_MODAL_LIST) {
39
- window.__CARBON_INTERNALS_MODAL_LIST = [];
44
+ if (!safeWindow.__CARBON_INTERNALS_MODAL_LIST) {
45
+ safeWindow.__CARBON_INTERNALS_MODAL_LIST = [];
40
46
  }
41
- this.modalList = window.__CARBON_INTERNALS_MODAL_LIST;
47
+ this.modalList = safeWindow.__CARBON_INTERNALS_MODAL_LIST;
42
48
  }
43
49
  return _createClass(ModalManagerInstance, [{
44
50
  key: "getTopModal",
@@ -84,16 +90,21 @@ let ModalManagerInstance = exports.ModalManagerInstance = /*#__PURE__*/function
84
90
  }, {
85
91
  key: "clearList",
86
92
  value: function clearList() {
87
- window.__CARBON_INTERNALS_MODAL_LIST = [];
88
- this.modalList = window.__CARBON_INTERNALS_MODAL_LIST;
93
+ const safeWindow = (0, _globals.getWindow)();
94
+ const cleared = [];
95
+ if (safeWindow) {
96
+ safeWindow.__CARBON_INTERNALS_MODAL_LIST = cleared;
97
+ }
98
+ this.modalList = cleared;
89
99
  this.callTopModalSetters();
90
100
  }
91
101
  }, {
92
102
  key: "callTopModalSetters",
93
103
  value: function callTopModalSetters() {
94
- if (window.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
104
+ const safeWindow = (0, _globals.getWindow)();
105
+ if (safeWindow?.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
95
106
  const topModal = this.getTopModal()?.modal || null;
96
- for (const setTopModal of window.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
107
+ for (const setTopModal of safeWindow.__CARBON_INTERNALS_MODAL_SETTER_LIST) {
97
108
  setTopModal(topModal);
98
109
  }
99
110
  }
@@ -8,6 +8,7 @@ var _react = _interopRequireWildcard(require("react"));
8
8
  var _reactTransitionGroup = require("react-transition-group");
9
9
  var _useScrollBlock = _interopRequireDefault(require("../../hooks/__internal__/useScrollBlock"));
10
10
  var _portal = _interopRequireDefault(require("../portal"));
11
+ var _globals = require("../../__internal__/dom/globals");
11
12
  var _events = _interopRequireDefault(require("../../__internal__/utils/helpers/events"));
12
13
  var _useModalManager = _interopRequireDefault(require("../../hooks/__internal__/useModalManager"));
13
14
  var _modal = require("./modal.style");
@@ -62,13 +63,14 @@ const Modal = ({
62
63
  onCancel(ev);
63
64
  }
64
65
  }, [disableClose, disableEscKey, onCancel]);
66
+ const safeDocument = (0, _globals.getDocument)();
65
67
  (0, _useModalManager.default)({
66
68
  open,
67
69
  closeModal,
68
70
  modalRef: ref,
69
71
  setTriggerRefocusFlag,
70
72
  topModalOverride,
71
- focusCallToActionElement: restoreFocusOnClose ? document.activeElement : undefined
73
+ focusCallToActionElement: restoreFocusOnClose && safeDocument ? safeDocument.activeElement : undefined
72
74
  });
73
75
  let background;
74
76
  let content;
@@ -14,6 +14,7 @@ var _icon = _interopRequireDefault(require("../../icon"));
14
14
  var _box = _interopRequireDefault(require("../../box"));
15
15
  var _verticalMenu = require("../vertical-menu.style");
16
16
  var _verticalMenuFullScreen = _interopRequireDefault(require("./__internal__/vertical-menu-full-screen.context"));
17
+ var _globals = require("../../../__internal__/dom/globals");
17
18
  var _events = _interopRequireDefault(require("../../../__internal__/utils/helpers/events/events"));
18
19
  var _useModalManager = _interopRequireDefault(require("../../../hooks/__internal__/useModalManager"));
19
20
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
@@ -36,12 +37,13 @@ const VerticalMenuFullScreen = ({
36
37
  onClose(ev);
37
38
  }
38
39
  }, [onClose]);
40
+ const safeDocument = (0, _globals.getDocument)();
39
41
  (0, _useModalManager.default)({
40
42
  open: isOpen,
41
43
  closeModal: handleKeyDown,
42
44
  modalRef: menuWrapperRef,
43
45
  topModalOverride: true,
44
- focusCallToActionElement: document.activeElement
46
+ focusCallToActionElement: safeDocument?.activeElement
45
47
  });
46
48
 
47
49
  // TODO remove this as part of FE-5650
@@ -4,32 +4,38 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
+ var _globals = require("../../../__internal__/dom/globals");
7
8
  function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
8
9
  function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
9
10
  function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
10
11
  function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
11
12
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
12
- function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
13
- // TODO: This component can be refactored to remove redundant code
13
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // TODO: This component can be refactored to remove redundant code
14
14
  // once we can confirm that all Sage products use version 105.0.0^
15
15
  let ScrollBlockManager = /*#__PURE__*/function () {
16
16
  function ScrollBlockManager() {
17
17
  _classCallCheck(this, ScrollBlockManager);
18
18
  _defineProperty(this, "components", void 0);
19
19
  _defineProperty(this, "originalValues", void 0);
20
+ const safeWindow = (0, _globals.getWindow)();
21
+ if (!safeWindow) {
22
+ this.components = {};
23
+ this.originalValues = [];
24
+ return;
25
+ }
20
26
  // Due to possibility of multiple carbon versions using it
21
27
  // it is necessary to maintain same structure in this global variable
22
- if (!window.__CARBON_INTERNALS_SCROLL_BLOCKERS) {
23
- window.__CARBON_INTERNALS_SCROLL_BLOCKERS = {
28
+ if (!safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS) {
29
+ safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS = {
24
30
  components: {},
25
31
  // originalValues can be removed
26
32
  originalValues: [],
27
33
  restoreValues: null
28
34
  };
29
35
  }
30
- this.components = window.__CARBON_INTERNALS_SCROLL_BLOCKERS.components;
36
+ this.components = safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS.components;
31
37
  // TODO: originalValues can be removed
32
- this.originalValues = window.__CARBON_INTERNALS_SCROLL_BLOCKERS.originalValues;
38
+ this.originalValues = safeWindow.__CARBON_INTERNALS_SCROLL_BLOCKERS.originalValues;
33
39
  }
34
40
  return _createClass(ScrollBlockManager, [{
35
41
  key: "registerComponent",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-react",
3
- "version": "151.2.3",
3
+ "version": "151.3.0",
4
4
  "description": "A library of reusable React components for easily building user interfaces.",
5
5
  "files": [
6
6
  "lib",
@@ -131,6 +131,7 @@
131
131
  "eslint-plugin-playwright": "^1.6.0",
132
132
  "eslint-plugin-react": "^7.33.2",
133
133
  "eslint-plugin-react-hooks": "^4.6.0",
134
+ "eslint-plugin-ssr-friendly": "^1.3.0",
134
135
  "eslint-plugin-testing-library": "^6.2.2",
135
136
  "events": "~1.1.1",
136
137
  "fast-glob": "^3.3.2",