carbon-react 110.0.3 → 110.1.1

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__/focus-trap/focus-trap-utils.d.ts +4 -3
  2. package/esm/__internal__/focus-trap/focus-trap-utils.js +3 -12
  3. package/esm/__internal__/focus-trap/focus-trap.component.d.ts +19 -0
  4. package/esm/__internal__/focus-trap/focus-trap.component.js +61 -45
  5. package/esm/__internal__/focus-trap/index.d.ts +2 -0
  6. package/esm/components/definition-list/definition-list.style.js +0 -9
  7. package/esm/components/dialog/dialog.component.js +3 -3
  8. package/esm/components/dialog-full-screen/dialog-full-screen.component.js +2 -2
  9. package/esm/components/dialog-full-screen/dialog-full-screen.style.js +4 -0
  10. package/esm/components/sidebar/sidebar.component.js +21 -19
  11. package/esm/components/sidebar/sidebar.d.ts +2 -1
  12. package/esm/components/sidebar/sidebar.style.js +8 -3
  13. package/lib/__internal__/focus-trap/focus-trap-utils.d.ts +4 -3
  14. package/lib/__internal__/focus-trap/focus-trap-utils.js +5 -13
  15. package/lib/__internal__/focus-trap/focus-trap.component.d.ts +19 -0
  16. package/lib/__internal__/focus-trap/focus-trap.component.js +59 -43
  17. package/lib/__internal__/focus-trap/index.d.ts +2 -0
  18. package/lib/components/definition-list/definition-list.style.js +0 -10
  19. package/lib/components/dialog/dialog.component.js +3 -3
  20. package/lib/components/dialog-full-screen/dialog-full-screen.component.js +2 -2
  21. package/lib/components/dialog-full-screen/dialog-full-screen.style.js +4 -0
  22. package/lib/components/sidebar/sidebar.component.js +22 -18
  23. package/lib/components/sidebar/sidebar.d.ts +2 -1
  24. package/lib/components/sidebar/sidebar.style.js +8 -3
  25. package/package.json +1 -1
@@ -1,3 +1,4 @@
1
- export const defaultFocusableSelectors: "button:not([disabled]), [href], input:not([type=\"hidden\"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]";
2
- export function getNextElement(element: any, focusableElements: any, shiftKey: any): any;
3
- export function setElementFocus(element: any): void;
1
+ declare const defaultFocusableSelectors = "button:not([disabled]), [href], input:not([type=\"hidden\"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]";
2
+ declare const setElementFocus: (element: HTMLElement) => void;
3
+ declare const getNextElement: (element: HTMLElement, focusableElements: HTMLElement[], shiftKey: boolean) => HTMLElement;
4
+ export { defaultFocusableSelectors, getNextElement, setElementFocus };
@@ -1,8 +1,8 @@
1
1
  const defaultFocusableSelectors = 'button:not([disabled]), [href], input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]';
2
+ const INTERVAL = 10;
3
+ const MAX_TIME = 100;
2
4
 
3
- const waitForVisibleAndFocus = element => {
4
- const INTERVAL = 10;
5
- const MAX_TIME = 100;
5
+ const setElementFocus = element => {
6
6
  let timeSoFar = 0;
7
7
 
8
8
  const stylesMatch = () => {
@@ -24,15 +24,6 @@ const waitForVisibleAndFocus = element => {
24
24
  check();
25
25
  };
26
26
 
27
- function setElementFocus(element) {
28
- if (typeof element === "function") {
29
- element();
30
- } else {
31
- const el = element.current || element;
32
- waitForVisibleAndFocus(el);
33
- }
34
- }
35
-
36
27
  const isRadio = element => {
37
28
  return element.hasAttribute("type") && element.getAttribute("type") === "radio";
38
29
  };
@@ -0,0 +1,19 @@
1
+ import React from "react";
2
+ declare type CustomRefObject<T> = {
3
+ current?: T | null;
4
+ };
5
+ export interface FocusTrapProps {
6
+ children: React.ReactNode;
7
+ autoFocus?: boolean;
8
+ /** provide a custom first element to focus */
9
+ focusFirstElement?: CustomRefObject<HTMLElement> | HTMLElement;
10
+ /** a custom callback that will override the default focus trap behaviour */
11
+ bespokeTrap?: (ev: KeyboardEvent, firstElement?: HTMLElement, lastElement?: HTMLElement) => void;
12
+ /** a ref to the container wrapping the focusable elements */
13
+ wrapperRef?: CustomRefObject<HTMLElement>;
14
+ isOpen?: boolean;
15
+ /** an optional array of refs to containers whose content should also be reachable from the FocusTrap */
16
+ additionalWrapperRefs?: CustomRefObject<HTMLElement>[];
17
+ }
18
+ declare const FocusTrap: ({ children, autoFocus, focusFirstElement, bespokeTrap, wrapperRef, isOpen, additionalWrapperRefs, }: FocusTrapProps) => JSX.Element;
19
+ export default FocusTrap;
@@ -1,8 +1,8 @@
1
- import React, { useCallback, useContext, useEffect, useLayoutEffect, useRef, useState } from "react";
1
+ import React, { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
2
2
  import PropTypes from "prop-types";
3
3
  import { defaultFocusableSelectors, getNextElement, setElementFocus } from "./focus-trap-utils";
4
4
  import { ModalContext } from "../../components/modal/modal.component";
5
- import usePrevious from "../../hooks/__internal__/usePrevious";
5
+ import usePrevious from "../../hooks/__internal__/usePrevious"; // TODO investigate why React.RefObject<T> produces a failed prop type when current = null
6
6
 
7
7
  const FocusTrap = ({
8
8
  children,
@@ -29,7 +29,8 @@ const FocusTrap = ({
29
29
 
30
30
  return Array.from(candidate).some((el, i) => el !== focusableElements[i]);
31
31
  }, [focusableElements]);
32
- const allRefs = [wrapperRef, ...additionalWrapperRefs].map(ref => ref === null || ref === void 0 ? void 0 : ref.current);
32
+ const trapWrappers = useMemo(() => additionalWrapperRefs !== null && additionalWrapperRefs !== void 0 && additionalWrapperRefs.length ? [wrapperRef, ...additionalWrapperRefs] : [wrapperRef], [additionalWrapperRefs, wrapperRef]);
33
+ const allRefs = trapWrappers.map(ref => ref === null || ref === void 0 ? void 0 : ref.current);
33
34
  const updateFocusableElements = useCallback(() => {
34
35
  const elements = [];
35
36
  allRefs.forEach(ref => {
@@ -46,14 +47,18 @@ const FocusTrap = ({
46
47
  }, [hasNewInputs, allRefs]);
47
48
  useEffect(() => {
48
49
  const observer = new MutationObserver(updateFocusableElements);
49
- observer.observe(trapRef.current, {
50
- subtree: true,
51
- childList: true,
52
- attributes: true,
53
- characterData: true
50
+ trapWrappers.forEach(wrapper => {
51
+ if (wrapper !== null && wrapper !== void 0 && wrapper.current) {
52
+ observer.observe(wrapper === null || wrapper === void 0 ? void 0 : wrapper.current, {
53
+ subtree: true,
54
+ childList: true,
55
+ attributes: true,
56
+ characterData: true
57
+ });
58
+ }
54
59
  });
55
60
  return () => observer.disconnect();
56
- }, [updateFocusableElements]);
61
+ }, [updateFocusableElements, trapWrappers]);
57
62
  useLayoutEffect(() => {
58
63
  updateFocusableElements();
59
64
  }, [children, updateFocusableElements]);
@@ -61,7 +66,8 @@ const FocusTrap = ({
61
66
  const prevShouldSetFocus = usePrevious(shouldSetFocus);
62
67
  useEffect(() => {
63
68
  if (shouldSetFocus && !prevShouldSetFocus) {
64
- setElementFocus(focusFirstElement || (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current));
69
+ const candidateFirstElement = focusFirstElement && "current" in focusFirstElement ? focusFirstElement.current : focusFirstElement;
70
+ setElementFocus(candidateFirstElement || (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current));
65
71
  }
66
72
  }, [shouldSetFocus, prevShouldSetFocus, focusFirstElement, wrapperRef]);
67
73
  useEffect(() => {
@@ -71,19 +77,17 @@ const FocusTrap = ({
71
77
  return;
72
78
  }
73
79
 
74
- const {
75
- activeElement
76
- } = document;
80
+ const activeElement = document.activeElement;
77
81
 
78
82
  if (ev.key === "Tab") {
79
- if (!focusableElements.length) {
83
+ if (!(focusableElements !== null && focusableElements !== void 0 && focusableElements.length)) {
80
84
  /* Block the trap */
81
85
  ev.preventDefault();
82
86
  } else if (ev.shiftKey) {
83
87
  /* shift + tab */
84
88
  let elementToFocus;
85
89
 
86
- if (activeElement === wrapperRef.current) {
90
+ if (activeElement === (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current)) {
87
91
  elementToFocus = getNextElement(firstElement, focusableElements, ev.shiftKey);
88
92
  } else {
89
93
  elementToFocus = getNextElement(activeElement, focusableElements, ev.shiftKey);
@@ -99,9 +103,9 @@ const FocusTrap = ({
99
103
  }
100
104
  };
101
105
 
102
- document.addEventListener("keydown", trapFn);
106
+ document.addEventListener("keydown", trapFn, true);
103
107
  return function cleanup() {
104
- document.removeEventListener("keydown", trapFn);
108
+ document.removeEventListener("keydown", trapFn, true);
105
109
  };
106
110
  }, [firstElement, lastElement, focusableElements, bespokeTrap, wrapperRef]);
107
111
  const updateCurrentFocusedElement = useCallback(() => {
@@ -153,7 +157,10 @@ const FocusTrap = ({
153
157
 
154
158
 
155
159
  const clonedChildren = React.Children.map(children, child => {
156
- return /*#__PURE__*/React.cloneElement(child, focusProps(child.props.tabIndex === undefined));
160
+ var _focusableChild$props;
161
+
162
+ const focusableChild = child;
163
+ return /*#__PURE__*/React.cloneElement(focusableChild, focusProps((focusableChild === null || focusableChild === void 0 ? void 0 : (_focusableChild$props = focusableChild.props) === null || _focusableChild$props === void 0 ? void 0 : _focusableChild$props.tabIndex) === undefined));
157
164
  });
158
165
  return /*#__PURE__*/React.createElement("div", {
159
166
  ref: trapRef
@@ -161,33 +168,42 @@ const FocusTrap = ({
161
168
  };
162
169
 
163
170
  FocusTrap.propTypes = {
164
- children: PropTypes.node.isRequired,
165
-
166
- /** flag to set focus automatically on first render */
167
- autoFocus: PropTypes.bool,
168
-
169
- /** provide a custom first element to focus */
170
- focusFirstElement: PropTypes.oneOfType([PropTypes.func, PropTypes.shape({
171
- current: PropTypes.any
171
+ "additionalWrapperRefs": PropTypes.arrayOf(PropTypes.shape({
172
+ "current": function (props, propName) {
173
+ if (props[propName] == null) {
174
+ return null;
175
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
176
+ return new Error("Expected prop '" + propName + "' to be of type Element");
177
+ }
178
+ }
179
+ })),
180
+ "autoFocus": PropTypes.bool,
181
+ "bespokeTrap": PropTypes.func,
182
+ "children": PropTypes.node,
183
+ "focusFirstElement": PropTypes.oneOfType([function (props, propName) {
184
+ if (props[propName] == null) {
185
+ return new Error("Prop '" + propName + "' is required but wasn't specified");
186
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
187
+ return new Error("Expected prop '" + propName + "' to be of type Element");
188
+ }
189
+ }, PropTypes.shape({
190
+ "current": function (props, propName) {
191
+ if (props[propName] == null) {
192
+ return null;
193
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
194
+ return new Error("Expected prop '" + propName + "' to be of type Element");
195
+ }
196
+ }
172
197
  })]),
173
-
174
- /** a custom callback that will override the default focus trap behaviour */
175
- bespokeTrap: PropTypes.func,
176
-
177
- /** a ref to the container wrapping the focusable elements */
178
- wrapperRef: PropTypes.shape({
179
- current: PropTypes.any
180
- }),
181
-
182
- /* whether the modal (etc.) component that the focus trap is inside is open or not */
183
- isOpen: PropTypes.bool,
184
-
185
- /** an optional array of refs to containers whose content should also be reachable from the FocusTrap */
186
- additionalWrapperRefs: PropTypes.arrayOf(PropTypes.shape({
187
- current: PropTypes.any
188
- }))
189
- };
190
- FocusTrap.defaultProps = {
191
- additionalWrapperRefs: []
198
+ "isOpen": PropTypes.bool,
199
+ "wrapperRef": PropTypes.shape({
200
+ "current": function (props, propName) {
201
+ if (props[propName] == null) {
202
+ return null;
203
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
204
+ return new Error("Expected prop '" + propName + "' to be of type Element");
205
+ }
206
+ }
207
+ })
192
208
  };
193
209
  export default FocusTrap;
@@ -0,0 +1,2 @@
1
+ export { default } from "./focus-trap.component";
2
+ export type { FocusTrapProps } from "./focus-trap.component";
@@ -1,7 +1,6 @@
1
1
  import styled, { css } from "styled-components";
2
2
  import { space } from "styled-system";
3
3
  import StyledButton from "../button/button.style";
4
- import { StyledLink } from "../link/link.style";
5
4
  import { baseTheme } from "../../style/themes";
6
5
  export const StyledDl = styled.dl`
7
6
  ${space}
@@ -64,13 +63,5 @@ export const StyledDd = styled.dd`
64
63
  padding: 0;
65
64
  border: none;
66
65
  }
67
-
68
- ${StyledLink} {
69
- a,
70
- button {
71
- font-weight: 700px;
72
- text-decoration: none;
73
- }
74
- }
75
66
  ${space}
76
67
  `;
@@ -35,9 +35,9 @@ const Dialog = ({
35
35
  ...rest
36
36
  }) => {
37
37
  const locale = useLocale();
38
- const dialogRef = useRef();
39
- const innerContentRef = useRef();
40
- const titleRef = useRef();
38
+ const dialogRef = useRef(null);
39
+ const innerContentRef = useRef(null);
40
+ const titleRef = useRef(null);
41
41
  const listenersAdded = useRef(false);
42
42
  const {
43
43
  current: titleId
@@ -36,8 +36,8 @@ const DialogFullScreen = ({
36
36
  ...rest
37
37
  }) => {
38
38
  const locale = useLocale();
39
- const dialogRef = useRef();
40
- const headingRef = useRef();
39
+ const dialogRef = useRef(null);
40
+ const headingRef = useRef(null);
41
41
  const {
42
42
  current: titleId
43
43
  } = useRef(createGuid());
@@ -6,6 +6,10 @@ import StyledFullScreenHeading from "../../__internal__/full-screen-heading/full
6
6
  import { StyledHeader, StyledHeaderContent, StyledHeading } from "../heading/heading.style";
7
7
  import { StyledForm } from "../form/form.style";
8
8
  const StyledDialogFullScreen = styled.div`
9
+ :focus {
10
+ outline: none;
11
+ }
12
+
9
13
  background-color: var(--colorsUtilityMajor025);
10
14
  height: 100%;
11
15
  left: 0;
@@ -2,27 +2,29 @@ function _extends() { _extends = Object.assign || function (target) { for (var i
2
2
 
3
3
  import React, { useRef } from "react";
4
4
  import PropTypes from "prop-types";
5
+ import styledSystemPropTypes from "@styled-system/prop-types";
5
6
  import Modal from "../modal";
6
- import SidebarStyle from "./sidebar.style";
7
+ import StyledSidebar from "./sidebar.style";
7
8
  import IconButton from "../icon-button";
8
9
  import Icon from "../icon";
9
10
  import FocusTrap from "../../__internal__/focus-trap";
10
11
  import SidebarHeader from "./__internal__/sidebar-header";
11
12
  import Box from "../box";
12
- import { SIDEBAR_SIZES, SIDEBAR_ALIGNMENTS } from "./sidebar.config";
13
13
  import createGuid from "../../__internal__/utils/helpers/guid";
14
14
  import useLocale from "../../hooks/__internal__/useLocale";
15
+ import { filterStyledSystemPaddingProps } from "../../style/utils";
16
+ const paddingPropTypes = filterStyledSystemPaddingProps(styledSystemPropTypes.space);
15
17
  export const SidebarContext = /*#__PURE__*/React.createContext({});
16
18
  const Sidebar = /*#__PURE__*/React.forwardRef(({
17
19
  "aria-describedby": ariaDescribedBy,
18
20
  "aria-label": ariaLabel,
19
21
  "aria-labelledby": ariaLabelledBy,
20
22
  open,
21
- disableEscKey,
22
- enableBackgroundUI,
23
+ disableEscKey = false,
24
+ enableBackgroundUI = false,
23
25
  header,
24
- position,
25
- size,
26
+ position = "right",
27
+ size = "medium",
26
28
  children,
27
29
  onCancel,
28
30
  role = "dialog",
@@ -33,7 +35,7 @@ const Sidebar = /*#__PURE__*/React.forwardRef(({
33
35
  const {
34
36
  current: titleId
35
37
  } = useRef(createGuid());
36
- let sidebarRef = useRef();
38
+ let sidebarRef = useRef(null);
37
39
  if (ref) sidebarRef = ref;
38
40
 
39
41
  const closeIcon = () => {
@@ -52,7 +54,7 @@ const Sidebar = /*#__PURE__*/React.forwardRef(({
52
54
  "data-element": rest["data-element"],
53
55
  "data-role": rest["data-role"]
54
56
  };
55
- const sidebar = /*#__PURE__*/React.createElement(SidebarStyle, {
57
+ const sidebar = /*#__PURE__*/React.createElement(StyledSidebar, {
56
58
  "aria-modal": !enableBackgroundUI,
57
59
  "aria-describedby": ariaDescribedBy,
58
60
  "aria-label": ariaLabel,
@@ -65,14 +67,16 @@ const Sidebar = /*#__PURE__*/React.forwardRef(({
65
67
  role: role
66
68
  }, header && /*#__PURE__*/React.createElement(SidebarHeader, {
67
69
  id: titleId
68
- }, header), closeIcon(), /*#__PURE__*/React.createElement(Box, {
70
+ }, header), closeIcon(), /*#__PURE__*/React.createElement(Box, _extends({
69
71
  "data-element": "sidebar-content",
70
- p: 4,
71
- pt: "27px",
72
+ pt: "var(--spacing300)",
73
+ pb: "var(--spacing400)",
74
+ px: "var(--spacing400)"
75
+ }, filterStyledSystemPaddingProps(rest), {
72
76
  scrollVariant: "light",
73
77
  overflow: "auto",
74
78
  flex: "1"
75
- }, /*#__PURE__*/React.createElement(SidebarContext.Provider, {
79
+ }), /*#__PURE__*/React.createElement(SidebarContext.Provider, {
76
80
  value: {
77
81
  isInSidebar: true
78
82
  }
@@ -90,6 +94,9 @@ const Sidebar = /*#__PURE__*/React.forwardRef(({
90
94
  }, sidebar));
91
95
  });
92
96
  Sidebar.propTypes = {
97
+ /** Styled system padding props to apply to Sidebar content */
98
+ ...paddingPropTypes,
99
+
93
100
  /** Prop to specify the aria-describedby property of the component */
94
101
  "aria-describedby": PropTypes.string,
95
102
 
@@ -115,10 +122,10 @@ Sidebar.propTypes = {
115
122
  enableBackgroundUI: PropTypes.bool,
116
123
 
117
124
  /** Sets the position of sidebar, either left or right. */
118
- position: PropTypes.oneOf(SIDEBAR_ALIGNMENTS),
125
+ position: PropTypes.oneOf(["left", "right"]),
119
126
 
120
127
  /** Sets the size of the sidebar when open. */
121
- size: PropTypes.oneOf(SIDEBAR_SIZES),
128
+ size: PropTypes.oneOf(["extra-small", "small", "medium-small", "medium", "medium-large", "large", "extra-large"]),
122
129
 
123
130
  /** Node that will be used as sidebar header. */
124
131
  header: PropTypes.node,
@@ -131,9 +138,4 @@ Sidebar.propTypes = {
131
138
  current: PropTypes.any
132
139
  }))
133
140
  };
134
- Sidebar.defaultProps = {
135
- position: "right",
136
- size: "medium",
137
- enableBackgroundUI: false
138
- };
139
141
  export default Sidebar;
@@ -1,9 +1,10 @@
1
1
  import * as React from "react";
2
+ import { PaddingProps } from "styled-system";
2
3
 
3
4
  export interface SidebarContextProps {
4
5
  isInSidebar: boolean;
5
6
  }
6
- export interface SidebarProps {
7
+ export interface SidebarProps extends PaddingProps {
7
8
  /** Prop to specify the aria-describedby property of the component */
8
9
  "aria-describedby"?: string;
9
10
  /**
@@ -2,7 +2,12 @@ import styled, { css } from "styled-components";
2
2
  import baseTheme from "../../style/themes/base";
3
3
  import StyledIconButton from "../icon-button/icon-button.style";
4
4
  import { SIDEBAR_SIZES_CSS } from "./sidebar.config";
5
- const SidebarStyle = styled.div`
5
+ const StyledSidebar = styled.div`
6
+ // prevents outline being added in safari
7
+ :focus {
8
+ outline: none;
9
+ }
10
+
6
11
  ${({
7
12
  onCancel,
8
13
  position,
@@ -37,7 +42,7 @@ const SidebarStyle = styled.div`
37
42
  `}
38
43
  `}
39
44
  `;
40
- SidebarStyle.defaultProps = {
45
+ StyledSidebar.defaultProps = {
41
46
  theme: baseTheme
42
47
  };
43
- export default SidebarStyle;
48
+ export default StyledSidebar;
@@ -1,3 +1,4 @@
1
- export const defaultFocusableSelectors: "button:not([disabled]), [href], input:not([type=\"hidden\"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]";
2
- export function getNextElement(element: any, focusableElements: any, shiftKey: any): any;
3
- export function setElementFocus(element: any): void;
1
+ declare const defaultFocusableSelectors = "button:not([disabled]), [href], input:not([type=\"hidden\"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]";
2
+ declare const setElementFocus: (element: HTMLElement) => void;
3
+ declare const getNextElement: (element: HTMLElement, focusableElements: HTMLElement[], shiftKey: boolean) => HTMLElement;
4
+ export { defaultFocusableSelectors, getNextElement, setElementFocus };
@@ -3,14 +3,13 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.setElementFocus = setElementFocus;
7
- exports.getNextElement = exports.defaultFocusableSelectors = void 0;
6
+ exports.setElementFocus = exports.getNextElement = exports.defaultFocusableSelectors = void 0;
8
7
  const defaultFocusableSelectors = 'button:not([disabled]), [href], input:not([type="hidden"]):not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]';
9
8
  exports.defaultFocusableSelectors = defaultFocusableSelectors;
9
+ const INTERVAL = 10;
10
+ const MAX_TIME = 100;
10
11
 
11
- const waitForVisibleAndFocus = element => {
12
- const INTERVAL = 10;
13
- const MAX_TIME = 100;
12
+ const setElementFocus = element => {
14
13
  let timeSoFar = 0;
15
14
 
16
15
  const stylesMatch = () => {
@@ -32,14 +31,7 @@ const waitForVisibleAndFocus = element => {
32
31
  check();
33
32
  };
34
33
 
35
- function setElementFocus(element) {
36
- if (typeof element === "function") {
37
- element();
38
- } else {
39
- const el = element.current || element;
40
- waitForVisibleAndFocus(el);
41
- }
42
- }
34
+ exports.setElementFocus = setElementFocus;
43
35
 
44
36
  const isRadio = element => {
45
37
  return element.hasAttribute("type") && element.getAttribute("type") === "radio";
@@ -0,0 +1,19 @@
1
+ import React from "react";
2
+ declare type CustomRefObject<T> = {
3
+ current?: T | null;
4
+ };
5
+ export interface FocusTrapProps {
6
+ children: React.ReactNode;
7
+ autoFocus?: boolean;
8
+ /** provide a custom first element to focus */
9
+ focusFirstElement?: CustomRefObject<HTMLElement> | HTMLElement;
10
+ /** a custom callback that will override the default focus trap behaviour */
11
+ bespokeTrap?: (ev: KeyboardEvent, firstElement?: HTMLElement, lastElement?: HTMLElement) => void;
12
+ /** a ref to the container wrapping the focusable elements */
13
+ wrapperRef?: CustomRefObject<HTMLElement>;
14
+ isOpen?: boolean;
15
+ /** an optional array of refs to containers whose content should also be reachable from the FocusTrap */
16
+ additionalWrapperRefs?: CustomRefObject<HTMLElement>[];
17
+ }
18
+ declare const FocusTrap: ({ children, autoFocus, focusFirstElement, bespokeTrap, wrapperRef, isOpen, additionalWrapperRefs, }: FocusTrapProps) => JSX.Element;
19
+ export default FocusTrap;
@@ -46,7 +46,8 @@ const FocusTrap = ({
46
46
 
47
47
  return Array.from(candidate).some((el, i) => el !== focusableElements[i]);
48
48
  }, [focusableElements]);
49
- const allRefs = [wrapperRef, ...additionalWrapperRefs].map(ref => ref === null || ref === void 0 ? void 0 : ref.current);
49
+ const trapWrappers = (0, _react.useMemo)(() => additionalWrapperRefs !== null && additionalWrapperRefs !== void 0 && additionalWrapperRefs.length ? [wrapperRef, ...additionalWrapperRefs] : [wrapperRef], [additionalWrapperRefs, wrapperRef]);
50
+ const allRefs = trapWrappers.map(ref => ref === null || ref === void 0 ? void 0 : ref.current);
50
51
  const updateFocusableElements = (0, _react.useCallback)(() => {
51
52
  const elements = [];
52
53
  allRefs.forEach(ref => {
@@ -63,14 +64,18 @@ const FocusTrap = ({
63
64
  }, [hasNewInputs, allRefs]);
64
65
  (0, _react.useEffect)(() => {
65
66
  const observer = new MutationObserver(updateFocusableElements);
66
- observer.observe(trapRef.current, {
67
- subtree: true,
68
- childList: true,
69
- attributes: true,
70
- characterData: true
67
+ trapWrappers.forEach(wrapper => {
68
+ if (wrapper !== null && wrapper !== void 0 && wrapper.current) {
69
+ observer.observe(wrapper === null || wrapper === void 0 ? void 0 : wrapper.current, {
70
+ subtree: true,
71
+ childList: true,
72
+ attributes: true,
73
+ characterData: true
74
+ });
75
+ }
71
76
  });
72
77
  return () => observer.disconnect();
73
- }, [updateFocusableElements]);
78
+ }, [updateFocusableElements, trapWrappers]);
74
79
  (0, _react.useLayoutEffect)(() => {
75
80
  updateFocusableElements();
76
81
  }, [children, updateFocusableElements]);
@@ -78,7 +83,8 @@ const FocusTrap = ({
78
83
  const prevShouldSetFocus = (0, _usePrevious.default)(shouldSetFocus);
79
84
  (0, _react.useEffect)(() => {
80
85
  if (shouldSetFocus && !prevShouldSetFocus) {
81
- (0, _focusTrapUtils.setElementFocus)(focusFirstElement || (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current));
86
+ const candidateFirstElement = focusFirstElement && "current" in focusFirstElement ? focusFirstElement.current : focusFirstElement;
87
+ (0, _focusTrapUtils.setElementFocus)(candidateFirstElement || (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current));
82
88
  }
83
89
  }, [shouldSetFocus, prevShouldSetFocus, focusFirstElement, wrapperRef]);
84
90
  (0, _react.useEffect)(() => {
@@ -88,19 +94,17 @@ const FocusTrap = ({
88
94
  return;
89
95
  }
90
96
 
91
- const {
92
- activeElement
93
- } = document;
97
+ const activeElement = document.activeElement;
94
98
 
95
99
  if (ev.key === "Tab") {
96
- if (!focusableElements.length) {
100
+ if (!(focusableElements !== null && focusableElements !== void 0 && focusableElements.length)) {
97
101
  /* Block the trap */
98
102
  ev.preventDefault();
99
103
  } else if (ev.shiftKey) {
100
104
  /* shift + tab */
101
105
  let elementToFocus;
102
106
 
103
- if (activeElement === wrapperRef.current) {
107
+ if (activeElement === (wrapperRef === null || wrapperRef === void 0 ? void 0 : wrapperRef.current)) {
104
108
  elementToFocus = (0, _focusTrapUtils.getNextElement)(firstElement, focusableElements, ev.shiftKey);
105
109
  } else {
106
110
  elementToFocus = (0, _focusTrapUtils.getNextElement)(activeElement, focusableElements, ev.shiftKey);
@@ -116,9 +120,9 @@ const FocusTrap = ({
116
120
  }
117
121
  };
118
122
 
119
- document.addEventListener("keydown", trapFn);
123
+ document.addEventListener("keydown", trapFn, true);
120
124
  return function cleanup() {
121
- document.removeEventListener("keydown", trapFn);
125
+ document.removeEventListener("keydown", trapFn, true);
122
126
  };
123
127
  }, [firstElement, lastElement, focusableElements, bespokeTrap, wrapperRef]);
124
128
  const updateCurrentFocusedElement = (0, _react.useCallback)(() => {
@@ -170,7 +174,10 @@ const FocusTrap = ({
170
174
 
171
175
 
172
176
  const clonedChildren = _react.default.Children.map(children, child => {
173
- return /*#__PURE__*/_react.default.cloneElement(child, focusProps(child.props.tabIndex === undefined));
177
+ var _focusableChild$props;
178
+
179
+ const focusableChild = child;
180
+ return /*#__PURE__*/_react.default.cloneElement(focusableChild, focusProps((focusableChild === null || focusableChild === void 0 ? void 0 : (_focusableChild$props = focusableChild.props) === null || _focusableChild$props === void 0 ? void 0 : _focusableChild$props.tabIndex) === undefined));
174
181
  });
175
182
 
176
183
  return /*#__PURE__*/_react.default.createElement("div", {
@@ -179,34 +186,43 @@ const FocusTrap = ({
179
186
  };
180
187
 
181
188
  FocusTrap.propTypes = {
182
- children: _propTypes.default.node.isRequired,
183
-
184
- /** flag to set focus automatically on first render */
185
- autoFocus: _propTypes.default.bool,
186
-
187
- /** provide a custom first element to focus */
188
- focusFirstElement: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.shape({
189
- current: _propTypes.default.any
189
+ "additionalWrapperRefs": _propTypes.default.arrayOf(_propTypes.default.shape({
190
+ "current": function (props, propName) {
191
+ if (props[propName] == null) {
192
+ return null;
193
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
194
+ return new Error("Expected prop '" + propName + "' to be of type Element");
195
+ }
196
+ }
197
+ })),
198
+ "autoFocus": _propTypes.default.bool,
199
+ "bespokeTrap": _propTypes.default.func,
200
+ "children": _propTypes.default.node,
201
+ "focusFirstElement": _propTypes.default.oneOfType([function (props, propName) {
202
+ if (props[propName] == null) {
203
+ return new Error("Prop '" + propName + "' is required but wasn't specified");
204
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
205
+ return new Error("Expected prop '" + propName + "' to be of type Element");
206
+ }
207
+ }, _propTypes.default.shape({
208
+ "current": function (props, propName) {
209
+ if (props[propName] == null) {
210
+ return null;
211
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
212
+ return new Error("Expected prop '" + propName + "' to be of type Element");
213
+ }
214
+ }
190
215
  })]),
191
-
192
- /** a custom callback that will override the default focus trap behaviour */
193
- bespokeTrap: _propTypes.default.func,
194
-
195
- /** a ref to the container wrapping the focusable elements */
196
- wrapperRef: _propTypes.default.shape({
197
- current: _propTypes.default.any
198
- }),
199
-
200
- /* whether the modal (etc.) component that the focus trap is inside is open or not */
201
- isOpen: _propTypes.default.bool,
202
-
203
- /** an optional array of refs to containers whose content should also be reachable from the FocusTrap */
204
- additionalWrapperRefs: _propTypes.default.arrayOf(_propTypes.default.shape({
205
- current: _propTypes.default.any
206
- }))
207
- };
208
- FocusTrap.defaultProps = {
209
- additionalWrapperRefs: []
216
+ "isOpen": _propTypes.default.bool,
217
+ "wrapperRef": _propTypes.default.shape({
218
+ "current": function (props, propName) {
219
+ if (props[propName] == null) {
220
+ return null;
221
+ } else if (typeof props[propName] !== 'object' || props[propName].nodeType !== 1) {
222
+ return new Error("Expected prop '" + propName + "' to be of type Element");
223
+ }
224
+ }
225
+ })
210
226
  };
211
227
  var _default = FocusTrap;
212
228
  exports.default = _default;
@@ -0,0 +1,2 @@
1
+ export { default } from "./focus-trap.component";
2
+ export type { FocusTrapProps } from "./focus-trap.component";
@@ -11,8 +11,6 @@ var _styledSystem = require("styled-system");
11
11
 
12
12
  var _button = _interopRequireDefault(require("../button/button.style"));
13
13
 
14
- var _link = require("../link/link.style");
15
-
16
14
  var _themes = require("../../style/themes");
17
15
 
18
16
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
@@ -86,14 +84,6 @@ const StyledDd = _styledComponents.default.dd`
86
84
  padding: 0;
87
85
  border: none;
88
86
  }
89
-
90
- ${_link.StyledLink} {
91
- a,
92
- button {
93
- font-weight: 700px;
94
- text-decoration: none;
95
- }
96
- }
97
87
  ${_styledSystem.space}
98
88
  `;
99
89
  exports.StyledDd = StyledDd;
@@ -59,9 +59,9 @@ const Dialog = ({
59
59
  ...rest
60
60
  }) => {
61
61
  const locale = (0, _useLocale.default)();
62
- const dialogRef = (0, _react.useRef)();
63
- const innerContentRef = (0, _react.useRef)();
64
- const titleRef = (0, _react.useRef)();
62
+ const dialogRef = (0, _react.useRef)(null);
63
+ const innerContentRef = (0, _react.useRef)(null);
64
+ const titleRef = (0, _react.useRef)(null);
65
65
  const listenersAdded = (0, _react.useRef)(false);
66
66
  const {
67
67
  current: titleId
@@ -60,8 +60,8 @@ const DialogFullScreen = ({
60
60
  ...rest
61
61
  }) => {
62
62
  const locale = (0, _useLocale.default)();
63
- const dialogRef = (0, _react.useRef)();
64
- const headingRef = (0, _react.useRef)();
63
+ const dialogRef = (0, _react.useRef)(null);
64
+ const headingRef = (0, _react.useRef)(null);
65
65
  const {
66
66
  current: titleId
67
67
  } = (0, _react.useRef)((0, _guid.default)());
@@ -26,6 +26,10 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
26
26
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
27
27
 
28
28
  const StyledDialogFullScreen = _styledComponents.default.div`
29
+ :focus {
30
+ outline: none;
31
+ }
32
+
29
33
  background-color: var(--colorsUtilityMajor025);
30
34
  height: 100%;
31
35
  left: 0;
@@ -9,6 +9,8 @@ var _react = _interopRequireWildcard(require("react"));
9
9
 
10
10
  var _propTypes = _interopRequireDefault(require("prop-types"));
11
11
 
12
+ var _propTypes2 = _interopRequireDefault(require("@styled-system/prop-types"));
13
+
12
14
  var _modal = _interopRequireDefault(require("../modal"));
13
15
 
14
16
  var _sidebar = _interopRequireDefault(require("./sidebar.style"));
@@ -23,12 +25,12 @@ var _sidebarHeader = _interopRequireDefault(require("./__internal__/sidebar-head
23
25
 
24
26
  var _box = _interopRequireDefault(require("../box"));
25
27
 
26
- var _sidebar2 = require("./sidebar.config");
27
-
28
28
  var _guid = _interopRequireDefault(require("../../__internal__/utils/helpers/guid"));
29
29
 
30
30
  var _useLocale = _interopRequireDefault(require("../../hooks/__internal__/useLocale"));
31
31
 
32
+ var _utils = require("../../style/utils");
33
+
32
34
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
35
 
34
36
  function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
@@ -37,6 +39,8 @@ function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj;
37
39
 
38
40
  function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
39
41
 
42
+ const paddingPropTypes = (0, _utils.filterStyledSystemPaddingProps)(_propTypes2.default.space);
43
+
40
44
  const SidebarContext = /*#__PURE__*/_react.default.createContext({});
41
45
 
42
46
  exports.SidebarContext = SidebarContext;
@@ -46,11 +50,11 @@ const Sidebar = /*#__PURE__*/_react.default.forwardRef(({
46
50
  "aria-label": ariaLabel,
47
51
  "aria-labelledby": ariaLabelledBy,
48
52
  open,
49
- disableEscKey,
50
- enableBackgroundUI,
53
+ disableEscKey = false,
54
+ enableBackgroundUI = false,
51
55
  header,
52
- position,
53
- size,
56
+ position = "right",
57
+ size = "medium",
54
58
  children,
55
59
  onCancel,
56
60
  role = "dialog",
@@ -61,7 +65,7 @@ const Sidebar = /*#__PURE__*/_react.default.forwardRef(({
61
65
  const {
62
66
  current: titleId
63
67
  } = (0, _react.useRef)((0, _guid.default)());
64
- let sidebarRef = (0, _react.useRef)();
68
+ let sidebarRef = (0, _react.useRef)(null);
65
69
  if (ref) sidebarRef = ref;
66
70
 
67
71
  const closeIcon = () => {
@@ -94,14 +98,16 @@ const Sidebar = /*#__PURE__*/_react.default.forwardRef(({
94
98
  role: role
95
99
  }, header && /*#__PURE__*/_react.default.createElement(_sidebarHeader.default, {
96
100
  id: titleId
97
- }, header), closeIcon(), /*#__PURE__*/_react.default.createElement(_box.default, {
101
+ }, header), closeIcon(), /*#__PURE__*/_react.default.createElement(_box.default, _extends({
98
102
  "data-element": "sidebar-content",
99
- p: 4,
100
- pt: "27px",
103
+ pt: "var(--spacing300)",
104
+ pb: "var(--spacing400)",
105
+ px: "var(--spacing400)"
106
+ }, (0, _utils.filterStyledSystemPaddingProps)(rest), {
101
107
  scrollVariant: "light",
102
108
  overflow: "auto",
103
109
  flex: "1"
104
- }, /*#__PURE__*/_react.default.createElement(SidebarContext.Provider, {
110
+ }), /*#__PURE__*/_react.default.createElement(SidebarContext.Provider, {
105
111
  value: {
106
112
  isInSidebar: true
107
113
  }
@@ -121,6 +127,9 @@ const Sidebar = /*#__PURE__*/_react.default.forwardRef(({
121
127
  });
122
128
 
123
129
  Sidebar.propTypes = {
130
+ /** Styled system padding props to apply to Sidebar content */
131
+ ...paddingPropTypes,
132
+
124
133
  /** Prop to specify the aria-describedby property of the component */
125
134
  "aria-describedby": _propTypes.default.string,
126
135
 
@@ -146,10 +155,10 @@ Sidebar.propTypes = {
146
155
  enableBackgroundUI: _propTypes.default.bool,
147
156
 
148
157
  /** Sets the position of sidebar, either left or right. */
149
- position: _propTypes.default.oneOf(_sidebar2.SIDEBAR_ALIGNMENTS),
158
+ position: _propTypes.default.oneOf(["left", "right"]),
150
159
 
151
160
  /** Sets the size of the sidebar when open. */
152
- size: _propTypes.default.oneOf(_sidebar2.SIDEBAR_SIZES),
161
+ size: _propTypes.default.oneOf(["extra-small", "small", "medium-small", "medium", "medium-large", "large", "extra-large"]),
153
162
 
154
163
  /** Node that will be used as sidebar header. */
155
164
  header: _propTypes.default.node,
@@ -162,10 +171,5 @@ Sidebar.propTypes = {
162
171
  current: _propTypes.default.any
163
172
  }))
164
173
  };
165
- Sidebar.defaultProps = {
166
- position: "right",
167
- size: "medium",
168
- enableBackgroundUI: false
169
- };
170
174
  var _default = Sidebar;
171
175
  exports.default = _default;
@@ -1,9 +1,10 @@
1
1
  import * as React from "react";
2
+ import { PaddingProps } from "styled-system";
2
3
 
3
4
  export interface SidebarContextProps {
4
5
  isInSidebar: boolean;
5
6
  }
6
- export interface SidebarProps {
7
+ export interface SidebarProps extends PaddingProps {
7
8
  /** Prop to specify the aria-describedby property of the component */
8
9
  "aria-describedby"?: string;
9
10
  /**
@@ -19,7 +19,12 @@ function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return
19
19
 
20
20
  function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
21
21
 
22
- const SidebarStyle = _styledComponents.default.div`
22
+ const StyledSidebar = _styledComponents.default.div`
23
+ // prevents outline being added in safari
24
+ :focus {
25
+ outline: none;
26
+ }
27
+
23
28
  ${({
24
29
  onCancel,
25
30
  position,
@@ -54,8 +59,8 @@ const SidebarStyle = _styledComponents.default.div`
54
59
  `}
55
60
  `}
56
61
  `;
57
- SidebarStyle.defaultProps = {
62
+ StyledSidebar.defaultProps = {
58
63
  theme: _base.default
59
64
  };
60
- var _default = SidebarStyle;
65
+ var _default = StyledSidebar;
61
66
  exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "carbon-react",
3
- "version": "110.0.3",
3
+ "version": "110.1.1",
4
4
  "description": "A library of reusable React components for easily building user interfaces.",
5
5
  "engineStrict": true,
6
6
  "engines": {