@canonical/react-components 3.11.1 → 3.12.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.
@@ -51,7 +51,7 @@ const generateHeaders = (currentSortKey, currentSortDirection, expanding, header
51
51
  return /*#__PURE__*/_react.default.createElement(_TableHeader.default, _extends({
52
52
  key: index,
53
53
  sort: sortDirection,
54
- onClick: sortable && sortKey ? updateSort.bind(void 0, setSortKey, setSortDirection, sortKey, sortDirection) : undefined
54
+ onSort: sortable && sortKey ? updateSort.bind(void 0, setSortKey, setSortDirection, sortKey, sortDirection) : undefined
55
55
  }, props), content);
56
56
  });
57
57
  // When there is expanding content then provide an extra hidden header to
@@ -9,6 +9,8 @@ export type Props = PropsWithSpread<{
9
9
  * The direction of sorting, if applicable.
10
10
  */
11
11
  sort?: SortDirection;
12
+ /** Function to call when the sort button is clicked. */
13
+ onSort?: () => void;
12
14
  }, HTMLProps<HTMLTableHeaderCellElement>>;
13
- declare const TableHeader: ({ children, sort, ...props }: Props) => React.JSX.Element;
15
+ declare const TableHeader: ({ children, sort, onSort, ...props }: Props) => React.JSX.Element;
14
16
  export default TableHeader;
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
  var _react = _interopRequireDefault(require("react"));
8
- const _excluded = ["children", "sort"];
8
+ const _excluded = ["children", "sort", "onSort"];
9
9
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
10
  function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
11
11
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
@@ -13,12 +13,17 @@ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) r
13
13
  const TableHeader = _ref => {
14
14
  let {
15
15
  children,
16
- sort
16
+ sort,
17
+ onSort
17
18
  } = _ref,
18
19
  props = _objectWithoutProperties(_ref, _excluded);
20
+ const headerContents = () => sort && onSort ? /*#__PURE__*/_react.default.createElement("button", {
21
+ className: "p-table__sort-button",
22
+ onClick: onSort
23
+ }, children) : children;
19
24
  return /*#__PURE__*/_react.default.createElement("th", _extends({
20
25
  role: "columnheader",
21
26
  "aria-sort": sort
22
- }, props), children);
27
+ }, props), headerContents());
23
28
  };
24
29
  var _default = exports.default = TableHeader;
@@ -62,7 +62,7 @@ export type Props = {
62
62
  */
63
63
  delay?: number;
64
64
  };
65
- export declare const adjustForWindow: (position: Position, fitsWindow: WindowFitment) => Position;
65
+ export declare const adjustForWindow: (position: Position, fitsWindow: WindowFitment, messageHeight?: number) => Position;
66
66
  /**
67
67
  * This is the [React](https://reactjs.org/) component for Vanilla [Tooltips](https://vanillaframework.io/docs/patterns/tooltips).
68
68
  *
@@ -70,14 +70,27 @@ const getPositionStyle = (pos, wrapperNode) => {
70
70
  top
71
71
  };
72
72
  };
73
- const adjustForWindow = (position, fitsWindow) => {
73
+ const adjustForWindow = function (position, fitsWindow) {
74
+ let messageHeight = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
74
75
  let newPosition = position;
75
76
  if (!fitsWindow.fromLeft.fitsLeft && newPosition === "left") {
76
77
  newPosition = "top-right";
77
78
  }
79
+ if (fitsWindow.fromTop.spaceAbove < messageHeight / 2 && newPosition === "left") {
80
+ newPosition = "btm-left";
81
+ }
82
+ if (fitsWindow.fromBottom.spaceBelow < messageHeight / 2 && newPosition === "left") {
83
+ newPosition = "top-left";
84
+ }
78
85
  if (!fitsWindow.fromRight.fitsRight && newPosition === "right") {
79
86
  newPosition = "top-left";
80
87
  }
88
+ if (fitsWindow.fromTop.spaceAbove < messageHeight / 2 && newPosition === "right") {
89
+ newPosition = "btm-right";
90
+ }
91
+ if (fitsWindow.fromBottom.spaceBelow < messageHeight / 2 && newPosition === "right") {
92
+ newPosition = "top-right";
93
+ }
81
94
  if (!fitsWindow.fromRight.fitsLeft && newPosition.endsWith("-right")) {
82
95
  newPosition = newPosition.replace("right", "left");
83
96
  }
@@ -168,7 +181,9 @@ const Tooltip = _ref => {
168
181
  });
169
182
  }, []);
170
183
  const onUpdateWindowFitment = (0, _react.useCallback)(fitsWindow => {
171
- setAdjustedPosition(adjustForWindow(position, fitsWindow));
184
+ var _messageRef$current$g, _messageRef$current;
185
+ const messageHeight = (_messageRef$current$g = (_messageRef$current = messageRef.current) === null || _messageRef$current === void 0 ? void 0 : _messageRef$current.getBoundingClientRect().height) !== null && _messageRef$current$g !== void 0 ? _messageRef$current$g : 0;
186
+ setAdjustedPosition(adjustForWindow(position, fitsWindow, messageHeight));
172
187
  }, [setAdjustedPosition, position]);
173
188
 
174
189
  // Handle mouse events.
@@ -188,19 +203,19 @@ const Tooltip = _ref => {
188
203
  };
189
204
  }, [handleKeyPress]);
190
205
  const handleBlur = e => {
191
- var _wrapperRef$current, _messageRef$current, _wrapperRef$current2;
206
+ var _wrapperRef$current, _messageRef$current2, _wrapperRef$current2;
192
207
  // do not close if the focus is within the tooltip wrapper
193
208
  if (wrapperRef !== null && wrapperRef !== void 0 && (_wrapperRef$current = wrapperRef.current) !== null && _wrapperRef$current !== void 0 && _wrapperRef$current.contains(document.activeElement)) {
194
209
  return;
195
210
  }
196
- if (e.relatedTarget ? !((_messageRef$current = messageRef.current) !== null && _messageRef$current !== void 0 && _messageRef$current.contains(e.relatedTarget)) && !((_wrapperRef$current2 = wrapperRef.current) !== null && _wrapperRef$current2 !== void 0 && _wrapperRef$current2.contains(e.relatedTarget)) : e.target !== messageRef.current) {
211
+ if (e.relatedTarget ? !((_messageRef$current2 = messageRef.current) !== null && _messageRef$current2 !== void 0 && _messageRef$current2.contains(e.relatedTarget)) && !((_wrapperRef$current2 = wrapperRef.current) !== null && _wrapperRef$current2 !== void 0 && _wrapperRef$current2.contains(e.relatedTarget)) : e.target !== messageRef.current) {
197
212
  cancelableClosePortal();
198
213
  }
199
214
  };
200
215
  const handleClick = e => {
201
- var _messageRef$current2;
216
+ var _messageRef$current3;
202
217
  // ignore clicks within the tooltip message
203
- if ((_messageRef$current2 = messageRef.current) !== null && _messageRef$current2 !== void 0 && _messageRef$current2.contains(e.target)) {
218
+ if ((_messageRef$current3 = messageRef.current) !== null && _messageRef$current3 !== void 0 && _messageRef$current3.contains(e.target)) {
204
219
  return;
205
220
  }
206
221
  e.target.focus();
@@ -42,7 +42,7 @@ var generateHeaders = (currentSortKey, currentSortDirection, expanding, headers,
42
42
  return /*#__PURE__*/React.createElement(TableHeader, _extends({
43
43
  key: index,
44
44
  sort: sortDirection,
45
- onClick: sortable && sortKey ? updateSort.bind(this, setSortKey, setSortDirection, sortKey, sortDirection) : undefined
45
+ onSort: sortable && sortKey ? updateSort.bind(this, setSortKey, setSortDirection, sortKey, sortDirection) : undefined
46
46
  }, props), content);
47
47
  });
48
48
  // When there is expanding content then provide an extra hidden header to
@@ -9,6 +9,8 @@ export type Props = PropsWithSpread<{
9
9
  * The direction of sorting, if applicable.
10
10
  */
11
11
  sort?: SortDirection;
12
+ /** Function to call when the sort button is clicked. */
13
+ onSort?: () => void;
12
14
  }, HTMLProps<HTMLTableHeaderCellElement>>;
13
- declare const TableHeader: ({ children, sort, ...props }: Props) => React.JSX.Element;
15
+ declare const TableHeader: ({ children, sort, onSort, ...props }: Props) => React.JSX.Element;
14
16
  export default TableHeader;
@@ -1,4 +1,4 @@
1
- var _excluded = ["children", "sort"];
1
+ var _excluded = ["children", "sort", "onSort"];
2
2
  function _extends() { _extends = Object.assign ? Object.assign.bind() : 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); }
3
3
  function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
4
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
@@ -6,12 +6,17 @@ import React from "react";
6
6
  var TableHeader = _ref => {
7
7
  var {
8
8
  children,
9
- sort
9
+ sort,
10
+ onSort
10
11
  } = _ref,
11
12
  props = _objectWithoutProperties(_ref, _excluded);
13
+ var headerContents = () => sort && onSort ? /*#__PURE__*/React.createElement("button", {
14
+ className: "p-table__sort-button",
15
+ onClick: onSort
16
+ }, children) : children;
12
17
  return /*#__PURE__*/React.createElement("th", _extends({
13
18
  role: "columnheader",
14
19
  "aria-sort": sort
15
- }, props), children);
20
+ }, props), headerContents());
16
21
  };
17
22
  export default TableHeader;
@@ -62,7 +62,7 @@ export type Props = {
62
62
  */
63
63
  delay?: number;
64
64
  };
65
- export declare const adjustForWindow: (position: Position, fitsWindow: WindowFitment) => Position;
65
+ export declare const adjustForWindow: (position: Position, fitsWindow: WindowFitment, messageHeight?: number) => Position;
66
66
  /**
67
67
  * This is the [React](https://reactjs.org/) component for Vanilla [Tooltips](https://vanillaframework.io/docs/patterns/tooltips).
68
68
  *
@@ -61,14 +61,27 @@ var getPositionStyle = (pos, wrapperNode) => {
61
61
  top
62
62
  };
63
63
  };
64
- export var adjustForWindow = (position, fitsWindow) => {
64
+ export var adjustForWindow = function adjustForWindow(position, fitsWindow) {
65
+ var messageHeight = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
65
66
  var newPosition = position;
66
67
  if (!fitsWindow.fromLeft.fitsLeft && newPosition === "left") {
67
68
  newPosition = "top-right";
68
69
  }
70
+ if (fitsWindow.fromTop.spaceAbove < messageHeight / 2 && newPosition === "left") {
71
+ newPosition = "btm-left";
72
+ }
73
+ if (fitsWindow.fromBottom.spaceBelow < messageHeight / 2 && newPosition === "left") {
74
+ newPosition = "top-left";
75
+ }
69
76
  if (!fitsWindow.fromRight.fitsRight && newPosition === "right") {
70
77
  newPosition = "top-left";
71
78
  }
79
+ if (fitsWindow.fromTop.spaceAbove < messageHeight / 2 && newPosition === "right") {
80
+ newPosition = "btm-right";
81
+ }
82
+ if (fitsWindow.fromBottom.spaceBelow < messageHeight / 2 && newPosition === "right") {
83
+ newPosition = "top-right";
84
+ }
72
85
  if (!fitsWindow.fromRight.fitsLeft && newPosition.endsWith("-right")) {
73
86
  newPosition = newPosition.replace("right", "left");
74
87
  }
@@ -158,7 +171,9 @@ var Tooltip = _ref => {
158
171
  });
159
172
  }, []);
160
173
  var onUpdateWindowFitment = useCallback(fitsWindow => {
161
- setAdjustedPosition(adjustForWindow(position, fitsWindow));
174
+ var _messageRef$current$g, _messageRef$current;
175
+ var messageHeight = (_messageRef$current$g = (_messageRef$current = messageRef.current) === null || _messageRef$current === void 0 ? void 0 : _messageRef$current.getBoundingClientRect().height) !== null && _messageRef$current$g !== void 0 ? _messageRef$current$g : 0;
176
+ setAdjustedPosition(adjustForWindow(position, fitsWindow, messageHeight));
162
177
  }, [setAdjustedPosition, position]);
163
178
 
164
179
  // Handle mouse events.
@@ -178,19 +193,19 @@ var Tooltip = _ref => {
178
193
  };
179
194
  }, [handleKeyPress]);
180
195
  var handleBlur = e => {
181
- var _wrapperRef$current, _messageRef$current, _wrapperRef$current2;
196
+ var _wrapperRef$current, _messageRef$current2, _wrapperRef$current2;
182
197
  // do not close if the focus is within the tooltip wrapper
183
198
  if (wrapperRef !== null && wrapperRef !== void 0 && (_wrapperRef$current = wrapperRef.current) !== null && _wrapperRef$current !== void 0 && _wrapperRef$current.contains(document.activeElement)) {
184
199
  return;
185
200
  }
186
- if (e.relatedTarget ? !((_messageRef$current = messageRef.current) !== null && _messageRef$current !== void 0 && _messageRef$current.contains(e.relatedTarget)) && !((_wrapperRef$current2 = wrapperRef.current) !== null && _wrapperRef$current2 !== void 0 && _wrapperRef$current2.contains(e.relatedTarget)) : e.target !== messageRef.current) {
201
+ if (e.relatedTarget ? !((_messageRef$current2 = messageRef.current) !== null && _messageRef$current2 !== void 0 && _messageRef$current2.contains(e.relatedTarget)) && !((_wrapperRef$current2 = wrapperRef.current) !== null && _wrapperRef$current2 !== void 0 && _wrapperRef$current2.contains(e.relatedTarget)) : e.target !== messageRef.current) {
187
202
  cancelableClosePortal();
188
203
  }
189
204
  };
190
205
  var handleClick = e => {
191
- var _messageRef$current2;
206
+ var _messageRef$current3;
192
207
  // ignore clicks within the tooltip message
193
- if ((_messageRef$current2 = messageRef.current) !== null && _messageRef$current2 !== void 0 && _messageRef$current2.contains(e.target)) {
208
+ if ((_messageRef$current3 = messageRef.current) !== null && _messageRef$current3 !== void 0 && _messageRef$current3.contains(e.target)) {
194
209
  return;
195
210
  }
196
211
  e.target.focus();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@canonical/react-components",
3
- "version": "3.11.1",
3
+ "version": "3.12.1",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "author": {