grommet 2.40.1 → 2.41.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 (37) hide show
  1. package/components/AccordionPanel/AccordionPanel.js +13 -5
  2. package/components/Button/StyledButtonKind.js +12 -6
  3. package/components/DataChart/Detail.js +19 -4
  4. package/components/DataFilter/DataFilter.js +5 -0
  5. package/components/Drop/DropContainer.js +51 -3
  6. package/components/FocusedContainer.js +134 -17
  7. package/components/Grommet/Grommet.js +11 -5
  8. package/components/MaskedInput/MaskedInput.js +3 -0
  9. package/components/Menu/Menu.js +3 -3
  10. package/components/Select/Select.js +22 -15
  11. package/components/Select/stories/ChildrenRender.js +7 -8
  12. package/components/SelectMultiple/SelectMultiple.js +8 -6
  13. package/components/TextInput/TextInput.js +3 -0
  14. package/contexts/RootsContext.js +23 -5
  15. package/es6/components/AccordionPanel/AccordionPanel.js +15 -6
  16. package/es6/components/Button/StyledButtonKind.js +12 -6
  17. package/es6/components/DataChart/Detail.js +20 -5
  18. package/es6/components/DataFilter/DataFilter.js +5 -0
  19. package/es6/components/Drop/DropContainer.js +51 -3
  20. package/es6/components/FocusedContainer.js +136 -19
  21. package/es6/components/Grommet/Grommet.js +12 -6
  22. package/es6/components/MaskedInput/MaskedInput.js +3 -0
  23. package/es6/components/Menu/Menu.js +3 -3
  24. package/es6/components/Select/Select.js +22 -15
  25. package/es6/components/Select/stories/ChildrenRender.js +7 -8
  26. package/es6/components/SelectMultiple/SelectMultiple.js +8 -6
  27. package/es6/components/TextInput/TextInput.js +3 -0
  28. package/es6/contexts/RootsContext.js +20 -3
  29. package/es6/themes/base.d.ts +1 -0
  30. package/es6/themes/base.js +1 -0
  31. package/es6/utils/DOM.js +2 -1
  32. package/grommet.min.js +1 -1
  33. package/grommet.min.js.map +1 -1
  34. package/package.json +22 -22
  35. package/themes/base.d.ts +1 -0
  36. package/themes/base.js +1 -0
  37. package/utils/DOM.js +2 -1
@@ -11,14 +11,21 @@ var _Heading = require("../Heading");
11
11
  var _AccordionContext = require("../Accordion/AccordionContext");
12
12
  var _propTypes = require("./propTypes");
13
13
  var _useThemeValue2 = require("../../utils/useThemeValue");
14
- var _excluded = ["children", "header", "label", "onClick", "onMouseOut", "onMouseOver", "onFocus", "onBlur"];
14
+ var _excluded = ["children", "header", "id", "label", "onClick", "onMouseOut", "onMouseOver", "onFocus", "onBlur"];
15
15
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
16
16
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
17
17
  function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
18
18
  function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
19
+ // Ideally we should use `useId` from react but this is only available
20
+ // in React 18 and we want to keep compatibility with React 17 and 16.
21
+ function getUID() {
22
+ var timestamp = Date.now() + Math.random();
23
+ return "grommet-accordion-button-" + timestamp;
24
+ }
19
25
  var AccordionPanel = exports.AccordionPanel = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
20
26
  var children = _ref.children,
21
27
  header = _ref.header,
28
+ id = _ref.id,
22
29
  label = _ref.label,
23
30
  onClick = _ref.onClick,
24
31
  _onMouseOut = _ref.onMouseOut,
@@ -26,7 +33,7 @@ var AccordionPanel = exports.AccordionPanel = /*#__PURE__*/(0, _react.forwardRef
26
33
  _onFocus = _ref.onFocus,
27
34
  _onBlur = _ref.onBlur,
28
35
  rest = _objectWithoutPropertiesLoose(_ref, _excluded);
29
- var uniqueId = (0, _react.useId)();
36
+ var panelButtonId = id ? "grommet_accordion_button_" + id : getUID();
30
37
  var _useThemeValue = (0, _useThemeValue2.useThemeValue)(),
31
38
  theme = _useThemeValue.theme;
32
39
  var _useContext = (0, _react.useContext)(_AccordionContext.AccordionContext),
@@ -78,7 +85,7 @@ var AccordionPanel = exports.AccordionPanel = /*#__PURE__*/(0, _react.forwardRef
78
85
  border: panelBorder,
79
86
  margin: abutMargin
80
87
  }, /*#__PURE__*/_react["default"].createElement(_Button.Button, {
81
- id: uniqueId,
88
+ id: panelButtonId,
82
89
  "aria-expanded": active,
83
90
  plain: theme.button["default"] ? true : undefined,
84
91
  onClick: onPanelChange,
@@ -107,7 +114,8 @@ var AccordionPanel = exports.AccordionPanel = /*#__PURE__*/(0, _react.forwardRef
107
114
  }, header || /*#__PURE__*/_react["default"].createElement(_Box.Box, _extends({
108
115
  align: "center",
109
116
  direction: "row",
110
- justify: "between"
117
+ justify: "between",
118
+ id: id
111
119
  }, rest), typeof label === 'string' ? /*#__PURE__*/_react["default"].createElement(_Box.Box, {
112
120
  pad: {
113
121
  horizontal: 'xsmall'
@@ -128,7 +136,7 @@ var AccordionPanel = exports.AccordionPanel = /*#__PURE__*/(0, _react.forwardRef
128
136
  })))), /*#__PURE__*/_react["default"].createElement(_Box.Box, {
129
137
  role: "region",
130
138
  border: contentBorder,
131
- "aria-labelledby": uniqueId
139
+ "aria-labelledby": panelButtonId
132
140
  }, animate ? /*#__PURE__*/_react["default"].createElement(_Collapsible.Collapsible, {
133
141
  open: active
134
142
  }, children) : active && children));
@@ -77,9 +77,15 @@ var getPath = function getPath(theme, path) {
77
77
  }
78
78
  return obj;
79
79
  };
80
- var adjustPadStyle = function adjustPadStyle(pad, width) {
81
- var offset = (0, _utils.parseMetricToNum)(width);
82
- return (0, _styledComponents.css)(["padding:", "px ", "px;"], Math.max((0, _utils.parseMetricToNum)(pad.vertical) - offset, 0), Math.max((0, _utils.parseMetricToNum)(pad.horizontal) - offset, 0));
80
+ var adjustPadStyle = function adjustPadStyle(pad, width, theme) {
81
+ var _theme$button;
82
+ var intelligentPad = theme == null || (_theme$button = theme.button) == null ? void 0 : _theme$button.intelligentPad;
83
+ if (intelligentPad === true) {
84
+ var offset = (0, _utils.parseMetricToNum)(width);
85
+ return (0, _styledComponents.css)(["padding:", "px ", "px;"], Math.max((0, _utils.parseMetricToNum)(pad.vertical) - offset, 0), Math.max((0, _utils.parseMetricToNum)(pad.horizontal) - offset, 0));
86
+ }
87
+ // If intelligentPad is false return padding without adjustment
88
+ return (0, _styledComponents.css)(["padding:", " ", ";"], pad.vertical, pad.horizontal);
83
89
  };
84
90
 
85
91
  // build up CSS from basic to specific based on the supplied sub-object paths
@@ -108,7 +114,7 @@ var kindStyle = function kindStyle(_ref2) {
108
114
  // Adjust padding from the button.size or just top button.padding
109
115
  // to deal with the kind's border width. But don't override any
110
116
  // padding in the kind itself for backward compatibility
111
- styles.push(adjustPadStyle(pad, obj.border.width));
117
+ styles.push(adjustPadStyle(pad, obj.border.width, theme));
112
118
  }
113
119
  }
114
120
  });
@@ -122,7 +128,7 @@ var kindStyle = function kindStyle(_ref2) {
122
128
  // Adjust padding from the button.size or just top button.padding
123
129
  // to deal with the kind's border width. But don't override any
124
130
  // padding in the kind itself for backward compatibility
125
- styles.push(adjustPadStyle(pad, obj.border.width));
131
+ styles.push(adjustPadStyle(pad, obj.border.width, theme));
126
132
  }
127
133
  }
128
134
  }
@@ -135,7 +141,7 @@ var kindStyle = function kindStyle(_ref2) {
135
141
  // Adjust padding from the button.size or just top button.padding
136
142
  // to deal with the hover's border width. But don't override any
137
143
  // padding in the hover or hover.kind itself for backward compatibility
138
- adjPadStyles = adjustPadStyle(pad, obj.border.width);
144
+ adjPadStyles = adjustPadStyle(pad, obj.border.width, theme);
139
145
  }
140
146
  if (partStyles.length > 0 && !busy && !success) {
141
147
  styles.push((0, _styledComponents.css)(["&:hover{", " ", "}"], partStyles, adjPadStyles));
@@ -4,6 +4,7 @@ exports.__esModule = true;
4
4
  exports.Detail = void 0;
5
5
  var _react = _interopRequireWildcard(require("react"));
6
6
  var _styledComponents = _interopRequireDefault(require("styled-components"));
7
+ var _contexts = require("../../contexts");
7
8
  var _Box = require("../Box");
8
9
  var _Drop = require("../Drop");
9
10
  var _Grid = require("../Grid");
@@ -30,6 +31,7 @@ var Detail = exports.Detail = function Detail(_ref) {
30
31
  seriesStyles = _ref.seriesStyles,
31
32
  renderValue = _ref.renderValue,
32
33
  thickness = _ref.thickness;
34
+ var announce = (0, _react.useContext)(_contexts.AnnounceContext);
33
35
  var _useThemeValue = (0, _useThemeValue2.useThemeValue)(),
34
36
  theme = _useThemeValue.theme;
35
37
  var _useState = (0, _react.useState)(),
@@ -78,6 +80,19 @@ var Detail = exports.Detail = function Detail(_ref) {
78
80
  };
79
81
  return res;
80
82
  }, [data.length, detailIndex, horizontalProp]);
83
+ (0, _react.useEffect)(function () {
84
+ if (detailIndex !== undefined) {
85
+ var content = series.filter(function (_ref2) {
86
+ var _data$detailIndex;
87
+ var property = _ref2.property;
88
+ return (!activeProperty || activeProperty === property) && (data == null || (_data$detailIndex = data[detailIndex]) == null ? void 0 : _data$detailIndex[property]) !== undefined || axis && axis.x && axis.x.property === property;
89
+ }).map(function (serie) {
90
+ var axisValue = horizontalProp ? data[detailIndex][serie.property] : detailIndex;
91
+ return (serie.label || serie.property) + " " + renderValue(serie, axisValue);
92
+ }).join(' ');
93
+ announce(content);
94
+ }
95
+ }, [activeProperty, announce, axis, data, detailIndex, horizontalProp, renderValue, series]);
81
96
  return /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement(_Keyboard.Keyboard, {
82
97
  onLeft: function onLeft() {
83
98
  if (detailIndex === undefined) setDetailIndex(data.length - 1);else if (detailIndex > 0) setDetailIndex(detailIndex - 1);
@@ -154,10 +169,10 @@ var Detail = exports.Detail = function Detail(_ref) {
154
169
  columns: ['auto', 'auto', 'auto'],
155
170
  gap: "xsmall",
156
171
  align: "center"
157
- }, series.filter(function (_ref2) {
158
- var _data$detailIndex;
159
- var property = _ref2.property;
160
- return (!activeProperty || activeProperty === property) && (data == null || (_data$detailIndex = data[detailIndex]) == null ? void 0 : _data$detailIndex[property]) !== undefined || axis && axis.x && axis.x.property === property;
172
+ }, series.filter(function (_ref3) {
173
+ var _data$detailIndex2;
174
+ var property = _ref3.property;
175
+ return (!activeProperty || activeProperty === property) && (data == null || (_data$detailIndex2 = data[detailIndex]) == null ? void 0 : _data$detailIndex2[property]) !== undefined || axis && axis.x && axis.x.property === property;
161
176
  }).map(function (serie) {
162
177
  var propertyStyle = seriesStyles[serie.property];
163
178
  var axisValue = horizontalProp ? data[detailIndex][serie.property] : detailIndex;
@@ -198,6 +198,11 @@ var DataFilter = exports.DataFilter = function DataFilter(_ref) {
198
198
  onSearch: options.length >= minSelectSearchOptions ? setSearchText : undefined,
199
199
  onClose: function onClose() {
200
200
  return setSearchText('');
201
+ },
202
+ labelKey: "label",
203
+ valueKey: {
204
+ key: 'value',
205
+ reduce: true
201
206
  }
202
207
  });
203
208
  }
@@ -182,12 +182,50 @@ var DropContainer = exports.DropContainer = /*#__PURE__*/(0, _react.forwardRef)(
182
182
  direction, change the Drop to display above/below. If there is
183
183
  less room in the other direction leave the Drop in its current
184
184
  position. */
185
- if (responsive && (align.top === 'top' && targetRect.top < 0 || align.bottom === 'top' && targetRect.top - containerRect.height <= 0 && targetRect.bottom + containerRect.height < windowHeight)) {
185
+ if (responsive &&
186
+ // drop is above target
187
+ align.bottom === 'top' &&
188
+ // drop is overflowing above window
189
+ targetRect.top - containerRect.height <= 0 &&
190
+ // there is room to display the drop below the target
191
+ targetRect.bottom + containerRect.height < windowHeight) {
192
+ // top of drop is aligned to bottom of target
186
193
  top = targetRect.bottom;
187
194
  maxHeight = top;
188
- } else if (responsive && (align.bottom === 'bottom' && targetRect.bottom > windowHeight || align.top === 'bottom' && targetRect.bottom + containerRect.height >= windowHeight && targetRect.top - containerRect.height > 0)) {
195
+ } else if (responsive &&
196
+ // top of drop is aligned to top of target
197
+ align.top === 'top' &&
198
+ // drop is overflowing below window
199
+ targetRect.top + containerRect.height >= windowHeight &&
200
+ // height of the drop is larger than the target.
201
+ targetRect.top + containerRect.height > targetRect.bottom &&
202
+ // there is room to display the drop above the target
203
+ targetRect.bottom - containerRect.height > 0) {
204
+ // bottom of drop is aligned to bottom of target
205
+ bottom = targetRect.bottom;
206
+ maxHeight = top;
207
+ } else if (responsive &&
208
+ // top of drop is aligned to bottom of target
209
+ align.top === 'bottom' &&
210
+ // drop is overflowing below window
211
+ targetRect.bottom + containerRect.height >= windowHeight &&
212
+ // there is room to display the drop above the target
213
+ targetRect.top - containerRect.height > 0) {
214
+ // bottom of drop is aligned to top of target
189
215
  bottom = targetRect.top;
190
216
  maxHeight = bottom;
217
+ } else if (responsive &&
218
+ // bottom of drop is aligned to bottom of target
219
+ align.bottom === 'bottom' &&
220
+ // drop is overflowing above window
221
+ targetRect.bottom - containerRect.height <= 0 &&
222
+ // height of the drop is larger than the target.
223
+ targetRect.bottom - containerRect.height > targetRect.top &&
224
+ // there is room to display the drop below the target
225
+ targetRect.top + containerRect.height > 0) {
226
+ // top of drop is aligned to top of target
227
+ top = targetRect.top;
228
+ maxHeight = bottom;
191
229
  } else if (align.top === 'top') {
192
230
  top = targetRect.top;
193
231
  maxHeight = windowHeight - top;
@@ -270,9 +308,19 @@ var DropContainer = exports.DropContainer = /*#__PURE__*/(0, _react.forwardRef)(
270
308
  window.removeEventListener('resize', onResize);
271
309
  };
272
310
  }, [align, containerTarget, onAlign, dropTarget, portalContext, portalId, responsive, restrictFocus, stretch, theme.drop, dropRef, dropOptions]);
311
+
312
+ // Once drop is open the focus will be put on the drop container
313
+ // if restrictFocus is true. If the caller put focus
314
+ // on an element already, we honor that. Otherwise, we put
315
+ // the focus on the drop container.
273
316
  (0, _react.useEffect)(function () {
274
317
  if (restrictFocus) {
275
- dropRef.current.focus();
318
+ var dropContainer = dropRef.current;
319
+ if (dropContainer) {
320
+ if (!dropContainer.contains(document.activeElement)) {
321
+ dropContainer.focus();
322
+ }
323
+ }
276
324
  }
277
325
  }, [dropRef, restrictFocus]);
278
326
  var content = /*#__PURE__*/_react["default"].createElement(_StyledDrop.StyledDrop, _extends({
@@ -10,6 +10,25 @@ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return
10
10
  function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
11
11
  function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
12
12
  function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (e.includes(n)) continue; t[n] = r[n]; } return t; }
13
+ var isFocusable = function isFocusable(element) {
14
+ if ((element == null ? void 0 : element.tabIndex) < 0 || element != null && element.disabled) {
15
+ return false;
16
+ }
17
+ switch (element == null ? void 0 : element.nodeName) {
18
+ case 'A':
19
+ return !!element.href && element.rel !== 'ignore';
20
+ case 'INPUT':
21
+ return element.type !== 'hidden';
22
+ case 'BUTTON':
23
+ case 'SELECT':
24
+ case 'TEXTAREA':
25
+ return true;
26
+ case 'DIV':
27
+ return element.tabIndex >= 0;
28
+ default:
29
+ return false;
30
+ }
31
+ };
13
32
  var FocusedContainer = exports.FocusedContainer = function FocusedContainer(_ref) {
14
33
  var _ref$hidden = _ref.hidden,
15
34
  hidden = _ref$hidden === void 0 ? false : _ref$hidden,
@@ -22,14 +41,103 @@ var FocusedContainer = exports.FocusedContainer = function FocusedContainer(_ref
22
41
  bodyOverflowStyle = _useState[0],
23
42
  setBodyOverflowStyle = _useState[1];
24
43
  var ref = (0, _react.useRef)(null);
25
- var roots = (0, _react.useContext)(_RootsContext.RootsContext);
26
- var _useState2 = (0, _react.useState)(roots),
27
- nextRoots = _useState2[0],
28
- setNextRoots = _useState2[1];
44
+ var preNodeRef = (0, _react.useRef)(null);
45
+ var postNodeRef = (0, _react.useRef)(null);
46
+ var _useRoots = (0, _RootsContext.useRoots)(),
47
+ contextValue = _useRoots.contextValue,
48
+ hasRoots = _useRoots.hasRoots;
49
+ var contextRoots = contextValue.roots;
50
+ // When attemptFocus moves focus around to find the focusable element,
51
+ // set this to true so handleTrapFocus doesn't focus the element.
52
+ var ignoreUtilFocusRef = (0, _react.useRef)(false);
53
+ var lastFocusRef = (0, _react.useRef)(null);
29
54
  (0, _react.useEffect)(function () {
30
- // make sure value of null is not added to array
31
- if (ref.current) setNextRoots([].concat(roots, [ref.current]));
32
- }, [roots]);
55
+ var container = ref.current;
56
+ var roots = contextRoots.current;
57
+ var attemptFocus = function attemptFocus(element) {
58
+ // Check if the element is focusable; if not, return false
59
+ if (!isFocusable(element)) {
60
+ return false;
61
+ }
62
+ ignoreUtilFocusRef.current = true;
63
+ try {
64
+ element.focus();
65
+ } catch (e) {
66
+ // continue regardless of error
67
+ }
68
+ ignoreUtilFocusRef.current = false;
69
+ // Return true if the element is currently the active element or has focus
70
+ return document.activeElement === element;
71
+ };
72
+ var _focusFirstDescendant = function focusFirstDescendant(element) {
73
+ // Iterate through all child nodes of the provided element
74
+ for (var i = 0; i < element.childNodes.length; i += 1) {
75
+ var child = element.childNodes[i];
76
+ if (attemptFocus(child) || _focusFirstDescendant(child)) return true;
77
+ }
78
+ // If no focusable child or descendant was found, return false
79
+ return false;
80
+ };
81
+ var _focusLastDescendant = function focusLastDescendant(element) {
82
+ for (var i = element.childNodes.length - 1; i >= 0; i -= 1) {
83
+ var child = element.childNodes[i];
84
+ if (attemptFocus(child) || _focusLastDescendant(child)) return true;
85
+ }
86
+ return false;
87
+ };
88
+ var handleTrapFocus = function handleTrapFocus(e) {
89
+ if (!hidden && trapFocus && ignoreUtilFocusRef.current === false && container &&
90
+ // only perform focus if this is the most recently opened drop
91
+ roots[roots.length - 1] === container) {
92
+ if (container.contains(e.target)) {
93
+ lastFocusRef.current = e.target;
94
+ } else {
95
+ _focusFirstDescendant(container);
96
+ if (lastFocusRef.current === document.activeElement) {
97
+ _focusLastDescendant(container);
98
+ }
99
+ lastFocusRef.current = document.activeElement;
100
+ }
101
+ }
102
+ };
103
+
104
+ // add container to the global roots
105
+ if (container) roots.push(container);
106
+
107
+ // Create and insert focusable nodes to help track when focus
108
+ // has left this container but without letting focus be noticeably
109
+ // placed on anything outside the container
110
+ if (!hidden && trapFocus) {
111
+ var preDiv = document.createElement('div');
112
+ var postDiv = document.createElement('div');
113
+ var commonStyles = {
114
+ position: 'absolute',
115
+ height: '1px',
116
+ left: '0',
117
+ right: '0'
118
+ };
119
+ Object.assign(preDiv.style, _extends({}, commonStyles, {
120
+ top: '0'
121
+ }));
122
+ Object.assign(postDiv.style, _extends({}, commonStyles, {
123
+ bottom: '0'
124
+ }));
125
+ preNodeRef.current = container.parentNode.insertBefore(preDiv, container);
126
+ postNodeRef.current = container.parentNode.insertBefore(postDiv, container.nextSibling);
127
+ preNodeRef.current.tabIndex = 0;
128
+ postNodeRef.current.tabIndex = 0;
129
+ }
130
+ document.addEventListener('focus', handleTrapFocus, true);
131
+ return function () {
132
+ var _preNodeRef$current, _postNodeRef$current;
133
+ // remove from global roots array
134
+ if (roots.includes(container)) roots.splice(roots.indexOf(container), 1);
135
+ document.removeEventListener('focus', handleTrapFocus, true);
136
+ if (roots != null && roots[roots.length - 1]) (0, _utils.makeNodeFocusable)(roots[roots.length - 1]);
137
+ preNodeRef == null || (_preNodeRef$current = preNodeRef.current) == null || _preNodeRef$current.remove();
138
+ postNodeRef == null || (_postNodeRef$current = postNodeRef.current) == null || _postNodeRef$current.remove();
139
+ };
140
+ }, [hidden, contextRoots, trapFocus]);
33
141
  (0, _react.useEffect)(function () {
34
142
  if (bodyOverflowStyle !== 'hidden' && !hidden && restrictScroll && trapFocus) {
35
143
  setBodyOverflowStyle(document.body.style.overflow);
@@ -42,21 +150,30 @@ var FocusedContainer = exports.FocusedContainer = function FocusedContainer(_ref
42
150
  };
43
151
  }, [bodyOverflowStyle, hidden, trapFocus, restrictScroll]);
44
152
  (0, _react.useEffect)(function () {
153
+ var roots = contextRoots.current;
45
154
  var timer = setTimeout(function () {
46
- if (!hidden && trapFocus && roots && roots[0]) {
47
- roots.forEach(_utils.makeNodeUnfocusable);
155
+ if (!hidden && trapFocus) {
156
+ // make every root before this one unfocusable
157
+ roots == null || roots.forEach(function (root, index) {
158
+ if (index < roots.length - 1) (0, _utils.makeNodeUnfocusable)(root);
159
+ });
48
160
  }
49
161
  }, 0);
50
162
  return function () {
51
- // remove trap and restore ability to focus on the last root only
52
- if (roots && roots[0]) (0, _utils.makeNodeFocusable)(roots[roots.length - 1]);
53
- clearTimeout(timer);
163
+ return clearTimeout(timer);
54
164
  };
55
- }, [hidden, roots, trapFocus]);
56
- return /*#__PURE__*/_react["default"].createElement(_RootsContext.RootsContext.Provider, {
57
- value: nextRoots
58
- }, /*#__PURE__*/_react["default"].createElement("div", _extends({
165
+ }, [hidden, contextRoots, trapFocus]);
166
+ var focusedContainer = /*#__PURE__*/_react["default"].createElement("div", _extends({
59
167
  ref: ref,
60
168
  "aria-hidden": hidden
61
- }, rest), children));
169
+ }, rest), children);
170
+ if (hasRoots) return focusedContainer;
171
+ return (
172
+ /*#__PURE__*/
173
+ // for cases outside of Grommet React tree, manage trapFocus when
174
+ // Drop/Layer opens another Drop/Layer
175
+ _react["default"].createElement(_RootsContext.RootsContext.Provider, {
176
+ value: contextValue
177
+ }, focusedContainer)
178
+ );
62
179
  };
@@ -60,9 +60,6 @@ var Grommet = exports.Grommet = /*#__PURE__*/(0, _react.forwardRef)(function (pr
60
60
  var _useState = (0, _react.useState)(),
61
61
  stateResponsive = _useState[0],
62
62
  setResponsive = _useState[1];
63
- var _useState2 = (0, _react.useState)([]),
64
- roots = _useState2[0],
65
- setRoots = _useState2[1];
66
63
  var theme = (0, _react.useMemo)(function () {
67
64
  var nextTheme = (0, _utils.deepMerge)(_themes.base, themeProp || {});
68
65
 
@@ -113,15 +110,24 @@ var Grommet = exports.Grommet = /*#__PURE__*/(0, _react.forwardRef)(function (pr
113
110
  }, [theme]);
114
111
  var responsive = stateResponsive || deviceResponsive(userAgent, theme) || theme.global.deviceBreakpoints.tablet;
115
112
  var grommetRef = (0, _utils.useForwardedRef)(ref);
113
+
114
+ // track open FocusedContainers in a global array to manage
115
+ // focus event listeners for trapFocus
116
+ var roots = (0, _react.useRef)([]);
116
117
  (0, _react.useEffect)(function () {
117
- if (grommetRef.current) setRoots([grommetRef.current]);
118
+ if (grommetRef.current) roots.current.push(grommetRef.current);
118
119
  }, [grommetRef]);
120
+ var rootsContextValue = (0, _react.useMemo)(function () {
121
+ return {
122
+ roots: roots
123
+ };
124
+ }, []);
119
125
  return /*#__PURE__*/_react["default"].createElement(_contexts.ThemeContext.Provider, {
120
126
  value: theme
121
127
  }, /*#__PURE__*/_react["default"].createElement(_contexts.ResponsiveContext.Provider, {
122
128
  value: responsive
123
129
  }, /*#__PURE__*/_react["default"].createElement(_RootsContext.RootsContext.Provider, {
124
- value: roots
130
+ value: rootsContextValue
125
131
  }, /*#__PURE__*/_react["default"].createElement(_contexts.ContainerTargetContext.Provider, {
126
132
  value: containerTarget
127
133
  }, /*#__PURE__*/_react["default"].createElement(_OptionsContext.OptionsContext.Provider, {
@@ -387,6 +387,9 @@ var MaskedInput = exports.MaskedInput = /*#__PURE__*/(0, _react.forwardRef)(func
387
387
  target: inputRef.current,
388
388
  onClickOutside: onHideDrop,
389
389
  onEsc: onHideDrop
390
+ // MaskedInput manages its own keyboard behavior via Keyboard
391
+ ,
392
+ trapFocus: false
390
393
  }, dropProps), /*#__PURE__*/_react["default"].createElement(ContainerBox, _extends({
391
394
  ref: dropRef,
392
395
  overflow: "auto",
@@ -397,14 +397,14 @@ var Menu = exports.Menu = /*#__PURE__*/(0, _react.forwardRef)(function (props, r
397
397
  ref: dropContainerRef,
398
398
  tabIndex: -1,
399
399
  background: dropBackground || theme.menu.background
400
- }, passThemeFlag), alignControlMirror === 'top' && align.top === 'top' ? controlMirror : undefined, /*#__PURE__*/_react["default"].createElement(_Box.Box, {
400
+ }, passThemeFlag), alignControlMirror === 'top' && align.bottom !== 'top' && align.top !== 'bottom' ? controlMirror : undefined, /*#__PURE__*/_react["default"].createElement(_Box.Box, {
401
401
  overflow: "auto",
402
402
  role: "menu",
403
403
  a11yTitle: a11y
404
- }, menuContent), !initialAlignTop && (
404
+ }, menuContent), !initialAlignTop &&
405
405
  // don't show controlMirror if caller is using
406
406
  // align.bottom === 'top'
407
- alignControlMirror === 'bottom' && !align.bottom === 'top' || align.bottom === 'bottom') ? controlMirror : undefined))
407
+ alignControlMirror === 'bottom' && align.bottom !== 'top' && align.top !== 'bottom' ? controlMirror : undefined))
408
408
  }), content));
409
409
  });
410
410
  Menu.displayName = 'Menu';
@@ -132,23 +132,30 @@ var Select = exports.Select = /*#__PURE__*/(0, _react.forwardRef)(function (_ref
132
132
  // the option indexes present in the value
133
133
  var optionIndexesInValue = (0, _react.useMemo)(function () {
134
134
  var result = [];
135
- allOptions.forEach(function (option, index) {
136
- if (selected !== undefined) {
137
- if (Array.isArray(selected)) {
138
- if (selected.indexOf(index) !== -1) result.push(index);
139
- } else if (index === selected) {
140
- result.push(index);
141
- }
142
- } else if (Array.isArray(normalizedValue)) {
143
- if (normalizedValue.some(function (v) {
144
- return v === (0, _utils2.applyKey)(option, valueKey);
145
- })) {
146
- result.push(index);
147
- }
148
- } else if (normalizedValue === (0, _utils2.applyKey)(option, valueKey)) {
135
+ if (selected !== undefined) {
136
+ if (Array.isArray(selected)) {
137
+ var validSelections = selected.filter(function (i) {
138
+ return i in allOptions;
139
+ });
140
+ result.push.apply(result, validSelections);
141
+ } else if (selected in allOptions) {
142
+ result.push(selected);
143
+ }
144
+ } else if (Array.isArray(normalizedValue)) {
145
+ normalizedValue.forEach(function (v) {
146
+ var index = allOptions.map(function (option) {
147
+ return (0, _utils2.applyKey)(option, valueKey);
148
+ }).indexOf(v);
149
+ if (index !== -1) result.push(index);
150
+ });
151
+ } else {
152
+ var index = allOptions.map(function (option) {
153
+ return (0, _utils2.applyKey)(option, valueKey);
154
+ }).indexOf(normalizedValue);
155
+ if (index !== -1) {
149
156
  result.push(index);
150
157
  }
151
- });
158
+ }
152
159
  return result;
153
160
  }, [allOptions, selected, valueKey, normalizedValue]);
154
161
  var _useState3 = (0, _react.useState)(propOpen),
@@ -13,9 +13,8 @@ var Children = exports.Children = function Children() {
13
13
  selected = _useState[0],
14
14
  setSelected = _useState[1];
15
15
  var onRemoveSeason = function onRemoveSeason(season) {
16
- var seasonIndex = allSeasons.indexOf(season);
17
16
  setSelected(selected.filter(function (selectedSeason) {
18
- return selectedSeason !== seasonIndex;
17
+ return selectedSeason !== season;
19
18
  }));
20
19
  };
21
20
  var renderSeason = function renderSeason(season) {
@@ -73,12 +72,12 @@ var Children = exports.Children = function Children() {
73
72
  }, /*#__PURE__*/_react["default"].createElement(_grommet.Select, {
74
73
  closeOnChange: false,
75
74
  multiple: true,
76
- value: /*#__PURE__*/_react["default"].createElement(_grommet.Box, {
75
+ valueLabel: /*#__PURE__*/_react["default"].createElement(_grommet.Box, {
77
76
  wrap: true,
78
77
  direction: "row",
79
78
  width: "small"
80
- }, selected && selected.length ? selected.map(function (index) {
81
- return renderSeason(allSeasons[index]);
79
+ }, selected && selected.length ? selected.map(function (value) {
80
+ return renderSeason(value);
82
81
  }) : /*#__PURE__*/_react["default"].createElement(_grommet.Box, {
83
82
  pad: {
84
83
  vertical: 'xsmall',
@@ -87,11 +86,11 @@ var Children = exports.Children = function Children() {
87
86
  margin: "xsmall"
88
87
  }, "Select Season")),
89
88
  options: allSeasons,
90
- selected: selected,
89
+ value: selected,
91
90
  disabled: [2, 6],
92
91
  onChange: function onChange(_ref) {
93
- var nextSelected = _ref.selected;
94
- setSelected([].concat(nextSelected).sort());
92
+ var nextSelected = _ref.value;
93
+ setSelected(nextSelected);
95
94
  }
96
95
  }, renderOption))
97
96
  // </Grommet>
@@ -166,12 +166,14 @@ var SelectMultiple = exports.SelectMultiple = /*#__PURE__*/(0, _react.forwardRef
166
166
  // the option indexes present in the value
167
167
  var optionIndexesInValue = (0, _react.useMemo)(function () {
168
168
  var result = [];
169
- allOptions.forEach(function (option, index) {
170
- if (normalizedValue != null && normalizedValue.some != null && normalizedValue.some(function (v) {
171
- return v === (0, _utils2.applyKey)(option, valueKey);
172
- })) {
173
- result.push(index);
174
- }
169
+ if (!Array.isArray(normalizedValue)) {
170
+ return result;
171
+ }
172
+ normalizedValue.forEach(function (v) {
173
+ var index = allOptions.map(function (option) {
174
+ return (0, _utils2.applyKey)(option, valueKey);
175
+ }).indexOf(v);
176
+ if (index !== -1) result.push(index);
175
177
  });
176
178
  return result;
177
179
  }, [allOptions, valueKey, normalizedValue]);
@@ -283,6 +283,9 @@ var TextInput = exports.TextInput = /*#__PURE__*/(0, _react.forwardRef)(function
283
283
  target: dropTarget || inputRef.current,
284
284
  onClickOutside: clickOutside,
285
285
  onEsc: closeDrop
286
+ // TextInput manages its own keyboard behavior via keyboardProps
287
+ ,
288
+ trapFocus: false
286
289
  }, dropProps), /*#__PURE__*/_react["default"].createElement(ContainerBox, _extends({
287
290
  id: id ? "listbox__" + id : undefined,
288
291
  role: "listbox",
@@ -1,10 +1,28 @@
1
1
  "use strict";
2
2
 
3
3
  exports.__esModule = true;
4
- exports.RootsContext = void 0;
5
- var _react = _interopRequireDefault(require("react"));
6
- function _interopRequireDefault(e) { return e && e.__esModule ? e : { "default": e }; }
4
+ exports.useRoots = exports.RootsContext = void 0;
5
+ var _react = _interopRequireWildcard(require("react"));
6
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
7
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
7
8
  // When toggling aria-hidden values, we only want to affect elements
8
9
  // in the DOM that come from Grommet, so we track those elements in this
9
- // context value. See FocusedContainer.js
10
- var RootsContext = exports.RootsContext = /*#__PURE__*/_react["default"].createContext([]);
10
+ // context value. We also only want `trapFocus` to apply to the most recently
11
+ // opened Drop/Layer. See FocusedContainer.js
12
+ var RootsContext = exports.RootsContext = /*#__PURE__*/_react["default"].createContext(undefined);
13
+ var useRoots = exports.useRoots = function useRoots() {
14
+ // If FocusedContainer is within Grommet React tree, there will be a value
15
+ // from RootsContext. If not, set up a default value that can be passed
16
+ // to drops that might open from within a drop.
17
+ var defaultRoots = (0, _react.useRef)([]);
18
+ var defaultValue = (0, _react.useMemo)(function () {
19
+ return {
20
+ roots: defaultRoots
21
+ };
22
+ }, []);
23
+ var existingRoots = (0, _react.useContext)(RootsContext);
24
+ return {
25
+ contextValue: existingRoots || defaultValue,
26
+ hasRoots: (existingRoots == null ? void 0 : existingRoots.roots) !== undefined
27
+ };
28
+ };