@canonical/react-components 0.51.0 → 0.51.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.
|
@@ -27,7 +27,7 @@ export type Props = PropsWithSpread<{
|
|
|
27
27
|
/**
|
|
28
28
|
* Function to perform the action prompted by the modal.
|
|
29
29
|
*/
|
|
30
|
-
onConfirm: (
|
|
30
|
+
onConfirm: (event: MouseEvent<HTMLElement>) => void;
|
|
31
31
|
}, Omit<ModalProps, "buttonRow">>;
|
|
32
32
|
export declare const ConfirmationModal: ({ cancelButtonLabel, children, confirmButtonAppearance, confirmButtonLabel, confirmExtra, onConfirm, ...props }: Props) => ReactElement;
|
|
33
33
|
export default ConfirmationModal;
|
|
@@ -19,14 +19,22 @@ const ConfirmationModal = _ref => {
|
|
|
19
19
|
onConfirm,
|
|
20
20
|
...props
|
|
21
21
|
} = _ref;
|
|
22
|
+
const handleClick = action => event => {
|
|
23
|
+
if (!props.shouldPropagateClickEvent) {
|
|
24
|
+
event.stopPropagation();
|
|
25
|
+
}
|
|
26
|
+
if (action) {
|
|
27
|
+
action(event);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
22
30
|
return /*#__PURE__*/_react.default.createElement(_Modal.default, _extends({
|
|
23
31
|
buttonRow: /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, confirmExtra, /*#__PURE__*/_react.default.createElement(_Button.default, {
|
|
24
32
|
className: "u-no-margin--bottom",
|
|
25
|
-
onClick: props.close
|
|
33
|
+
onClick: handleClick(props.close)
|
|
26
34
|
}, cancelButtonLabel), /*#__PURE__*/_react.default.createElement(_Button.default, {
|
|
27
35
|
appearance: confirmButtonAppearance,
|
|
28
36
|
className: "u-no-margin--bottom",
|
|
29
|
-
onClick: onConfirm
|
|
37
|
+
onClick: handleClick(onConfirm)
|
|
30
38
|
}, confirmButtonLabel))
|
|
31
39
|
}, props), children);
|
|
32
40
|
};
|
|
@@ -22,6 +22,10 @@ export type Props = PropsWithSpread<{
|
|
|
22
22
|
* The title of the modal.
|
|
23
23
|
*/
|
|
24
24
|
title?: ReactNode | null;
|
|
25
|
+
/**
|
|
26
|
+
* Whether the button click event should propagate.
|
|
27
|
+
*/
|
|
28
|
+
shouldPropagateClickEvent?: boolean;
|
|
25
29
|
}, HTMLProps<HTMLDivElement>>;
|
|
26
|
-
export declare const Modal: ({ buttonRow, children, className, close, title, ...wrapperProps }: Props) => ReactElement;
|
|
30
|
+
export declare const Modal: ({ buttonRow, children, className, close, title, shouldPropagateClickEvent, ...wrapperProps }: Props) => ReactElement;
|
|
27
31
|
export default Modal;
|
|
@@ -17,6 +17,7 @@ const Modal = _ref => {
|
|
|
17
17
|
className,
|
|
18
18
|
close,
|
|
19
19
|
title,
|
|
20
|
+
shouldPropagateClickEvent = false,
|
|
20
21
|
...wrapperProps
|
|
21
22
|
} = _ref;
|
|
22
23
|
// list of focusable selectors is based on this Stack Overflow answer:
|
|
@@ -27,29 +28,31 @@ const Modal = _ref => {
|
|
|
27
28
|
const shouldClose = (0, _react.useRef)(false);
|
|
28
29
|
const modalRef = (0, _react.useRef)(null);
|
|
29
30
|
const focusableModalElements = (0, _react.useRef)(null);
|
|
30
|
-
const handleTabKey =
|
|
31
|
+
const handleTabKey = event => {
|
|
31
32
|
if (focusableModalElements.current.length > 0) {
|
|
32
33
|
const firstElement = focusableModalElements.current[0];
|
|
33
34
|
const lastElement = focusableModalElements.current[focusableModalElements.current.length - 1];
|
|
34
|
-
if (!
|
|
35
|
+
if (!event.shiftKey && document.activeElement === lastElement) {
|
|
35
36
|
firstElement.focus();
|
|
36
|
-
|
|
37
|
+
event.preventDefault();
|
|
37
38
|
}
|
|
38
|
-
if (
|
|
39
|
+
if (event.shiftKey && document.activeElement === firstElement) {
|
|
39
40
|
lastElement.focus();
|
|
40
|
-
return
|
|
41
|
+
return event.preventDefault();
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
};
|
|
44
|
-
const handleEscKey =
|
|
45
|
-
if ("nativeEvent" in
|
|
46
|
-
|
|
47
|
-
} else if ("stopImmediatePropagation" in
|
|
48
|
-
|
|
49
|
-
} else if (
|
|
50
|
-
|
|
45
|
+
const handleEscKey = event => {
|
|
46
|
+
if ("nativeEvent" in event && event.nativeEvent.stopImmediatePropagation) {
|
|
47
|
+
event.nativeEvent.stopImmediatePropagation();
|
|
48
|
+
} else if ("stopImmediatePropagation" in event) {
|
|
49
|
+
event.stopImmediatePropagation();
|
|
50
|
+
} else if (event.stopPropagation) {
|
|
51
|
+
event.stopPropagation();
|
|
52
|
+
}
|
|
53
|
+
if (close) {
|
|
54
|
+
close();
|
|
51
55
|
}
|
|
52
|
-
close();
|
|
53
56
|
};
|
|
54
57
|
const keyListenersMap = new Map([["Escape", handleEscKey], ["Tab", handleTabKey]]);
|
|
55
58
|
(0, _react.useEffect)(() => {
|
|
@@ -68,9 +71,9 @@ const Modal = _ref => {
|
|
|
68
71
|
});
|
|
69
72
|
}, [close]);
|
|
70
73
|
(0, _react.useEffect)(() => {
|
|
71
|
-
const keyDown =
|
|
72
|
-
const listener = keyListenersMap.get(
|
|
73
|
-
return listener && listener(
|
|
74
|
+
const keyDown = event => {
|
|
75
|
+
const listener = keyListenersMap.get(event.code);
|
|
76
|
+
return listener && listener(event);
|
|
74
77
|
};
|
|
75
78
|
document.addEventListener("keydown", keyDown);
|
|
76
79
|
return () => {
|
|
@@ -88,11 +91,19 @@ const Modal = _ref => {
|
|
|
88
91
|
shouldClose.current = true;
|
|
89
92
|
}
|
|
90
93
|
};
|
|
91
|
-
const
|
|
92
|
-
if (
|
|
94
|
+
const handleClose = event => {
|
|
95
|
+
if (!shouldPropagateClickEvent) {
|
|
96
|
+
event.stopPropagation();
|
|
97
|
+
}
|
|
98
|
+
if (close) {
|
|
93
99
|
close();
|
|
94
100
|
}
|
|
95
101
|
};
|
|
102
|
+
const handleOverlayOnClick = event => {
|
|
103
|
+
if (shouldClose.current) {
|
|
104
|
+
handleClose(event);
|
|
105
|
+
}
|
|
106
|
+
};
|
|
96
107
|
return /*#__PURE__*/_react.default.createElement("div", _extends({
|
|
97
108
|
className: (0, _classnames.default)("p-modal", className),
|
|
98
109
|
onClick: handleOverlayOnClick,
|
|
@@ -115,7 +126,7 @@ const Modal = _ref => {
|
|
|
115
126
|
}, title), !!close && /*#__PURE__*/_react.default.createElement("button", {
|
|
116
127
|
className: "p-modal__close",
|
|
117
128
|
"aria-label": "Close active modal",
|
|
118
|
-
onClick:
|
|
129
|
+
onClick: handleClose
|
|
119
130
|
}, "Close")), /*#__PURE__*/_react.default.createElement("div", {
|
|
120
131
|
id: descriptionId
|
|
121
132
|
}, children), !!buttonRow && /*#__PURE__*/_react.default.createElement("footer", {
|