@enact/spotlight 4.5.1 → 4.6.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 (32) hide show
  1. package/Accelerator/Accelerator.js +11 -31
  2. package/Accelerator/tests/Accelerrator-specs.js +62 -0
  3. package/CHANGELOG.md +28 -0
  4. package/Pause/Pause.js +12 -22
  5. package/Pause/tests/Pause-specs.js +6 -3
  6. package/SpotlightContainerDecorator/SpotlightContainer.js +19 -45
  7. package/SpotlightContainerDecorator/SpotlightContainerDecorator.js +14 -58
  8. package/SpotlightContainerDecorator/tests/Element.prototype.closest.js +5 -10
  9. package/SpotlightContainerDecorator/tests/SpotlightContainerDecorator-specs.js +17 -47
  10. package/SpotlightContainerDecorator/tests/useSpotlightContainer-specs.js +16 -53
  11. package/SpotlightContainerDecorator/useSpotlightContainer.js +4 -15
  12. package/SpotlightRootDecorator/SpotlightRootDecorator.js +4 -58
  13. package/SpotlightRootDecorator/tests/SpotlightRootDecorator-specs.js +38 -0
  14. package/Spottable/Spottable.js +26 -85
  15. package/Spottable/SpottableCore.js +32 -71
  16. package/Spottable/tests/Spottable-specs.js +8 -31
  17. package/Spottable/tests/useSpot-specs.js +27 -105
  18. package/Spottable/useSpottable.js +7 -21
  19. package/package.json +9 -3
  20. package/src/container.js +94 -219
  21. package/src/navigate.js +5 -74
  22. package/src/pointer.js +16 -31
  23. package/src/spotlight.js +40 -170
  24. package/src/target.js +61 -139
  25. package/src/tests/container-specs.js +5 -11
  26. package/src/tests/navigate-specs.js +2 -10
  27. package/src/tests/pointer-specs.js +1 -3
  28. package/src/tests/spotlight-specs.js +42 -24
  29. package/src/tests/target-specs.js +23 -47
  30. package/src/tests/utils-specs.js +54 -0
  31. package/src/tests/utils.js +2 -27
  32. package/src/utils.js +18 -37
@@ -4,93 +4,75 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.spottableClass = exports["default"] = exports.SpottableCore = void 0;
7
-
8
7
  var _handle = require("@enact/core/handle");
9
-
10
8
  var _keymap = require("@enact/core/keymap");
11
-
12
9
  var _container = require("../src/container");
13
-
14
10
  var _spotlight = require("../src/spotlight");
15
-
16
11
  function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
17
-
18
12
  function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
19
-
20
13
  function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
21
-
22
14
  var REMOTE_OK_KEY = 16777221;
15
+
23
16
  /**
24
17
  * The class name for spottable components. In general, you do not need to directly access this class
25
18
  *
26
19
  * @memberof spotlight/Spottable
27
20
  * @public
28
21
  */
29
-
30
22
  var spottableClass = 'spottable';
31
23
  exports.spottableClass = spottableClass;
32
-
33
24
  var isKeyboardAccessible = function isKeyboardAccessible(node) {
34
25
  if (!node) return false;
35
26
  var name = node.nodeName.toUpperCase();
36
27
  var type = node.type ? node.type.toUpperCase() : null;
37
28
  return name === 'BUTTON' || name === 'A' || name === 'INPUT' && (type === 'BUTTON' || type === 'CHECKBOX' || type === 'IMAGE' || type === 'RADIO' || type === 'RESET' || type === 'SUBMIT');
38
29
  };
39
-
40
30
  var isSpottable = function isSpottable(props) {
41
31
  return !props.spotlightDisabled;
42
- }; // Last instance of spottable to be focused
43
-
32
+ };
44
33
 
45
- var lastSelectTarget = null; // Should we prevent select being passed through
34
+ // Last instance of spottable to be focused
35
+ var lastSelectTarget = null;
46
36
 
37
+ // Should we prevent select being passed through
47
38
  var selectCancelled = false;
48
-
49
39
  var SpottableCore = /*#__PURE__*/function () {
50
40
  function SpottableCore(_ref) {
51
41
  var _this = this;
52
-
53
42
  var emulateMouse = _ref.emulateMouse;
54
-
55
43
  _classCallCheck(this, SpottableCore);
56
-
57
44
  this.didUpdate = function () {
58
- _this.isFocused = _this.node && _this.node === _spotlight.Spotlight.getCurrent(); // if the component is focused and became disabled
45
+ _this.isFocused = _this.node && _this.node === _spotlight.Spotlight.getCurrent();
59
46
 
47
+ // if the component is focused and became disabled
60
48
  if (_this.isFocused && _this.props.disabled && lastSelectTarget === _this && !selectCancelled) {
61
49
  selectCancelled = true;
62
50
  (0, _handle.forwardCustom)('onSelectionCancel')(null, _this.props);
63
- } // if the component became enabled, notify spotlight to enable restoring "lost" focus
64
-
51
+ }
65
52
 
53
+ // if the component became enabled, notify spotlight to enable restoring "lost" focus
66
54
  if (isSpottable(_this.props) && _this.context.prevSpotlightDisabled && !_spotlight.Spotlight.isPaused()) {
67
55
  if (_spotlight.Spotlight.getPointerMode()) {
68
56
  if (_this.isHovered) {
69
57
  _spotlight.Spotlight.setPointerMode(false);
70
-
71
58
  _spotlight.Spotlight.focus(_this.node);
72
-
73
59
  _spotlight.Spotlight.setPointerMode(true);
74
60
  }
75
61
  } else if (!_spotlight.Spotlight.getCurrent()) {
76
62
  var containers = (0, _container.getContainersForNode)(_this.node);
77
-
78
63
  var containerId = _spotlight.Spotlight.getActiveContainer();
79
-
80
64
  if (containers.indexOf(containerId) >= 0) {
81
65
  _spotlight.Spotlight.focus(containerId);
82
66
  }
83
67
  }
84
68
  }
85
69
  };
86
-
87
70
  this.forwardSpotlightEvents = function (ev, _ref2) {
88
71
  var onSpotlightDown = _ref2.onSpotlightDown,
89
- onSpotlightLeft = _ref2.onSpotlightLeft,
90
- onSpotlightRight = _ref2.onSpotlightRight,
91
- onSpotlightUp = _ref2.onSpotlightUp;
72
+ onSpotlightLeft = _ref2.onSpotlightLeft,
73
+ onSpotlightRight = _ref2.onSpotlightRight,
74
+ onSpotlightUp = _ref2.onSpotlightUp;
92
75
  var keyCode = ev.keyCode;
93
-
94
76
  if (onSpotlightDown && (0, _keymap.is)('down', keyCode)) {
95
77
  onSpotlightDown(ev);
96
78
  } else if (onSpotlightLeft && (0, _keymap.is)('left', keyCode)) {
@@ -100,86 +82,80 @@ var SpottableCore = /*#__PURE__*/function () {
100
82
  } else if (onSpotlightUp && (0, _keymap.is)('up', keyCode)) {
101
83
  onSpotlightUp(ev);
102
84
  }
103
-
104
85
  return true;
105
86
  };
106
-
107
87
  this.forwardAndResetLastSelectTarget = function (ev, props) {
108
88
  var keyCode = ev.keyCode;
109
89
  var selectionKeys = props.selectionKeys;
110
90
  var key = selectionKeys.find(function (value) {
111
91
  return keyCode === value;
112
92
  });
113
- var notPrevented = !ev.defaultPrevented; // bail early for non-selection keyup to avoid clearing lastSelectTarget prematurely
93
+ var notPrevented = !ev.defaultPrevented;
114
94
 
95
+ // bail early for non-selection keyup to avoid clearing lastSelectTarget prematurely
115
96
  if (!key && (!(0, _keymap.is)('enter', keyCode) || !(0, _spotlight.getDirection)(keyCode))) {
116
97
  return notPrevented;
117
98
  }
118
-
119
99
  var allow = lastSelectTarget === _this;
120
100
  selectCancelled = false;
121
101
  lastSelectTarget = null;
122
102
  return notPrevented && allow;
123
103
  };
124
-
125
104
  this.shouldEmulateMouse = function (ev, props) {
126
105
  if (!_this.config.emulateMouse) {
127
106
  return;
128
107
  }
129
-
130
108
  var currentTarget = ev.currentTarget,
131
- repeat = ev.repeat,
132
- type = ev.type,
133
- which = ev.which;
109
+ repeat = ev.repeat,
110
+ type = ev.type,
111
+ which = ev.which;
134
112
  var selectionKeys = props.selectionKeys;
135
113
  var keyboardAccessible = isKeyboardAccessible(currentTarget);
136
114
  var keyCode = selectionKeys.find(function (value) {
137
- return (// emulate mouse events for any remote okay button event
138
- which === REMOTE_OK_KEY || // or a non-keypress selection event or any selection event on a non-keyboard accessible
115
+ return (
116
+ // emulate mouse events for any remote okay button event
117
+ which === REMOTE_OK_KEY ||
118
+ // or a non-keypress selection event or any selection event on a non-keyboard accessible
139
119
  // control
120
+
140
121
  which === value && (type !== 'keypress' || !keyboardAccessible)
141
122
  );
142
123
  });
143
-
144
124
  if ((0, _spotlight.getDirection)(keyCode)) {
145
125
  (0, _handle.preventDefault)(ev);
146
126
  (0, _handle.stop)(ev);
147
127
  } else if (keyCode && keyboardAccessible) {
148
128
  (0, _handle.preventDefault)(ev);
149
129
  }
150
-
151
130
  return keyCode && !repeat;
152
131
  };
153
-
154
132
  this.isActionable = function (ev, props) {
155
133
  return isSpottable(props);
156
134
  };
157
-
158
135
  this.handleSelect = function (_ref3, props) {
159
136
  var which = _ref3.which;
160
- var selectionKeys = props.selectionKeys; // Only apply accelerator if handling a selection key
137
+ var selectionKeys = props.selectionKeys;
161
138
 
139
+ // Only apply accelerator if handling a selection key
162
140
  if (selectionKeys.find(function (value) {
163
141
  return which === value;
164
142
  })) {
165
143
  if (selectCancelled || lastSelectTarget && lastSelectTarget !== _this) {
166
144
  return false;
167
145
  }
168
-
169
146
  lastSelectTarget = _this;
170
147
  }
171
-
172
148
  return true;
173
149
  };
174
-
175
150
  this.handle = _handle.handle.bind(this);
176
151
  this.handleKeyDown = this.handle(function (ev) {
177
152
  return !ev.defaultPrevented;
178
- }, this.forwardSpotlightEvents, this.isActionable, this.handleSelect, this.shouldEmulateMouse // `forwardMouseDown` is usually called out of the useSpottable if the `this.shouldEmulateMouse` returns true.
153
+ }, this.forwardSpotlightEvents, this.isActionable, this.handleSelect, this.shouldEmulateMouse
154
+ // `forwardMouseDown` is usually called out of the useSpottable if the `this.shouldEmulateMouse` returns true.
179
155
  );
180
- this.handleKeyUp = this.handle(this.forwardAndResetLastSelectTarget, this.isActionable, this.shouldEmulateMouse // `forwardMouseUp` and `forwardClick` are usually called out of the useSpottable if the `this.shouldEmulateMouse` returns true.
156
+ this.handleKeyUp = this.handle(this.forwardAndResetLastSelectTarget, this.isActionable, this.shouldEmulateMouse
157
+ // `forwardMouseUp` and `forwardClick` are usually called out of the useSpottable if the `this.shouldEmulateMouse` returns true.
181
158
  );
182
-
183
159
  this.handleFocus = function (ev) {
184
160
  if (_this.props.spotlightDisabled) {
185
161
  _this.shouldPreventBlur = true;
@@ -187,50 +163,39 @@ var SpottableCore = /*#__PURE__*/function () {
187
163
  _this.shouldPreventBlur = false;
188
164
  return;
189
165
  }
190
-
191
166
  if (ev.currentTarget === ev.target) {
192
167
  _this.isFocused = true;
193
168
  }
194
-
195
169
  if (_spotlight.Spotlight.isMuted(ev.target)) {
196
170
  ev.stopPropagation();
197
171
  return false;
198
172
  }
199
-
200
173
  return true;
201
174
  };
202
-
203
175
  this.handleBlur = function (ev) {
204
176
  if (_this.shouldPreventBlur) return false;
205
-
206
177
  if (ev.currentTarget === ev.target) {
207
178
  _this.isFocused = false;
208
-
209
179
  if (_this.isFocusedWhenDisabled) {
210
- _this.isFocusedWhenDisabled = false; // We only need to trigger a rerender if a focused item becomes disabled and still needs its focus.
180
+ _this.isFocusedWhenDisabled = false;
181
+ // We only need to trigger a rerender if a focused item becomes disabled and still needs its focus.
211
182
  // Once it blurs we need to rerender to remove the spottable class so it will not spot again.
212
183
  // The reason we don't use state is for performance reasons to avoid updates.
213
-
214
184
  _this.props.handleForceUpdate();
215
185
  }
216
186
  }
217
-
218
187
  if (_spotlight.Spotlight.isMuted(ev.target)) {
219
188
  ev.stopPropagation();
220
189
  return false;
221
190
  }
222
-
223
191
  return true;
224
192
  };
225
-
226
193
  this.handleEnter = function () {
227
194
  _this.isHovered = true;
228
195
  };
229
-
230
196
  this.handleLeave = function () {
231
197
  _this.isHovered = false;
232
198
  };
233
-
234
199
  this.config = {
235
200
  emulateMouse: emulateMouse
236
201
  };
@@ -239,12 +204,11 @@ var SpottableCore = /*#__PURE__*/function () {
239
204
  this.isFocused = false;
240
205
  this.isFocusedWhenDisabled = false;
241
206
  this.isHovered = false;
242
- this.spottableClass = null; // Used to indicate that we want to stop propagation on blur events that occur as a
207
+ this.spottableClass = null;
208
+ // Used to indicate that we want to stop propagation on blur events that occur as a
243
209
  // result of this component imperatively blurring itself on focus when spotlightDisabled
244
-
245
210
  this.shouldPreventBlur = false;
246
211
  }
247
-
248
212
  _createClass(SpottableCore, [{
249
213
  key: "setPropsAndContext",
250
214
  value: function setPropsAndContext(props, context) {
@@ -265,16 +229,13 @@ var SpottableCore = /*#__PURE__*/function () {
265
229
  if (this.isFocused) {
266
230
  (0, _handle.forwardCustom)('onSpotlightDisappear')(null, this.props);
267
231
  }
268
-
269
232
  if (lastSelectTarget === this) {
270
233
  lastSelectTarget = null;
271
234
  }
272
235
  }
273
236
  }]);
274
-
275
237
  return SpottableCore;
276
238
  }();
277
-
278
239
  exports.SpottableCore = SpottableCore;
279
240
  var _default = SpottableCore;
280
241
  exports["default"] = _default;
@@ -1,21 +1,13 @@
1
1
  "use strict";
2
2
 
3
3
  require("@testing-library/jest-dom");
4
-
5
4
  var _react = require("@testing-library/react");
6
-
7
5
  var _Spottable = _interopRequireDefault(require("../Spottable"));
8
-
9
6
  var _jsxRuntime = require("react/jsx-runtime");
10
-
11
7
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
12
-
13
8
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
14
-
15
9
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
16
-
17
10
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
18
-
19
11
  var id = 'test-spottable';
20
12
  describe('Spottable', function () {
21
13
  test('should add the spottable class', function () {
@@ -23,9 +15,7 @@ describe('Spottable', function () {
23
15
  (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
24
16
  "data-testid": id
25
17
  }));
26
-
27
18
  var div = _react.screen.getByTestId(id);
28
-
29
19
  var expectedClass = 'spottable';
30
20
  expect(div).toHaveClass(expectedClass);
31
21
  });
@@ -35,9 +25,7 @@ describe('Spottable', function () {
35
25
  "data-testid": id,
36
26
  disabled: true
37
27
  }));
38
-
39
28
  var div = _react.screen.getByTestId(id);
40
-
41
29
  var expectedClass = 'spottable';
42
30
  expect(div).toHaveClass(expectedClass);
43
31
  });
@@ -47,24 +35,19 @@ describe('Spottable', function () {
47
35
  "data-testid": id,
48
36
  spotlightDisabled: true
49
37
  }));
50
-
51
38
  var div = _react.screen.getByTestId(id);
52
-
53
39
  var expectedClass = 'spottable';
54
40
  expect(div).not.toHaveClass(expectedClass);
55
41
  });
56
42
  test('should emit {onSpotlightDisappear} when unmounted while focused', function () {
57
43
  var spy = jest.fn();
58
44
  var Component = (0, _Spottable["default"])('div');
59
-
60
45
  var _render = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
61
- "data-testid": id,
62
- onSpotlightDisappear: spy
63
- })),
64
- unmount = _render.unmount;
65
-
46
+ "data-testid": id,
47
+ onSpotlightDisappear: spy
48
+ })),
49
+ unmount = _render.unmount;
66
50
  var div = _react.screen.getByTestId(id);
67
-
68
51
  div.focus();
69
52
  unmount();
70
53
  var expected = 1;
@@ -79,10 +62,8 @@ describe('Spottable', function () {
79
62
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread({}, props));
80
63
  });
81
64
  var Component = (0, _Spottable["default"])(spy);
82
-
83
65
  var _render2 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {})),
84
- rerender = _render2.rerender;
85
-
66
+ rerender = _render2.rerender;
86
67
  rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
87
68
  "data-id": "123"
88
69
  }));
@@ -94,12 +75,10 @@ describe('Spottable', function () {
94
75
  return /*#__PURE__*/(0, _jsxRuntime.jsx)("div", _objectSpread({}, props));
95
76
  });
96
77
  var Component = (0, _Spottable["default"])(spy);
97
-
98
78
  var _render3 = (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
99
- selectionKeys: [1, 2, 3]
100
- })),
101
- rerender = _render3.rerender;
102
-
79
+ selectionKeys: [1, 2, 3]
80
+ })),
81
+ rerender = _render3.rerender;
103
82
  rerender( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
104
83
  selectionKeys: [2, 1, 3]
105
84
  }));
@@ -114,9 +93,7 @@ describe('Spottable', function () {
114
93
  (0, _react.render)( /*#__PURE__*/(0, _jsxRuntime.jsx)(Component, {
115
94
  "data-testid": id
116
95
  }));
117
-
118
96
  var div = _react.screen.getByTestId(id);
119
-
120
97
  div.focus();
121
98
  var expected = 2;
122
99
  expect(spy).toHaveBeenCalledTimes(expected);