@enact/spotlight 3.4.9-experimental-3 → 3.4.9-experimental-7

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 (68) hide show
  1. package/{build/Accelerator → Accelerator}/Accelerator.d.ts +0 -0
  2. package/Accelerator/Accelerator.js +149 -156
  3. package/CHANGELOG.md +22 -0
  4. package/{build/Pause → Pause}/Pause.d.ts +0 -0
  5. package/Pause/Pause.js +102 -80
  6. package/Pause/tests/Pause-specs.js +65 -97
  7. package/SpotlightContainerDecorator/SpotlightContainer.js +175 -140
  8. package/{build/SpotlightContainerDecorator → SpotlightContainerDecorator}/SpotlightContainerDecorator.d.ts +0 -0
  9. package/SpotlightContainerDecorator/SpotlightContainerDecorator.js +222 -176
  10. package/SpotlightContainerDecorator/tests/Element.prototype.closest.js +44 -40
  11. package/SpotlightContainerDecorator/tests/SpotlightContainerDecorator-specs.js +221 -210
  12. package/SpotlightContainerDecorator/tests/useSpotlightContainer-specs.js +150 -243
  13. package/SpotlightContainerDecorator/useSpotlightContainer.js +47 -28
  14. package/{build/SpotlightRootDecorator → SpotlightRootDecorator}/SpotlightRootDecorator.d.ts +0 -0
  15. package/SpotlightRootDecorator/SpotlightRootDecorator.js +206 -111
  16. package/{build/Spottable → Spottable}/Spottable.d.ts +0 -0
  17. package/Spottable/Spottable.js +448 -410
  18. package/Spottable/tests/Spottable-specs.js +90 -117
  19. package/package.json +3 -2
  20. package/src/container.js +718 -653
  21. package/src/navigate.js +406 -491
  22. package/src/pointer.js +86 -78
  23. package/{build/src → src}/spotlight.d.ts +0 -0
  24. package/src/spotlight.js +846 -885
  25. package/src/target.js +448 -474
  26. package/src/tests/container-specs.js +643 -1251
  27. package/src/tests/navigate-specs.js +77 -134
  28. package/src/tests/pointer-specs.js +126 -182
  29. package/src/tests/spotlight-specs.js +103 -133
  30. package/src/tests/target-specs.js +597 -960
  31. package/src/tests/utils.js +89 -67
  32. package/src/utils.js +155 -158
  33. package/build/Accelerator/Accelerator.js +0 -162
  34. package/build/Accelerator/package.json +0 -3
  35. package/build/CHANGELOG.md +0 -838
  36. package/build/Pause/Pause.js +0 -164
  37. package/build/Pause/package.json +0 -3
  38. package/build/Pause/tests/Pause-specs.js +0 -65
  39. package/build/README.md +0 -61
  40. package/build/SpotlightContainerDecorator/SpotlightContainer.js +0 -176
  41. package/build/SpotlightContainerDecorator/SpotlightContainerDecorator.js +0 -282
  42. package/build/SpotlightContainerDecorator/package.json +0 -3
  43. package/build/SpotlightContainerDecorator/tests/Element.prototype.closest.js +0 -45
  44. package/build/SpotlightContainerDecorator/tests/SpotlightContainerDecorator-specs.js +0 -221
  45. package/build/SpotlightContainerDecorator/tests/useSpotlightContainer-specs.js +0 -160
  46. package/build/SpotlightContainerDecorator/useSpotlightContainer.js +0 -133
  47. package/build/SpotlightRootDecorator/SpotlightRootDecorator.js +0 -189
  48. package/build/SpotlightRootDecorator/package.json +0 -3
  49. package/build/Spottable/Spottable.js +0 -491
  50. package/build/Spottable/package.json +0 -3
  51. package/build/Spottable/tests/Spottable-specs.js +0 -90
  52. package/build/package-lock.json +0 -229
  53. package/build/package.json +0 -28
  54. package/build/src/container.js +0 -1201
  55. package/build/src/navigate.js +0 -419
  56. package/build/src/pointer.js +0 -183
  57. package/build/src/spotlight.js +0 -896
  58. package/build/src/target.js +0 -507
  59. package/build/src/tests/container-specs.js +0 -653
  60. package/build/src/tests/navigate-specs.js +0 -87
  61. package/build/src/tests/pointer-specs.js +0 -130
  62. package/build/src/tests/spotlight-specs.js +0 -112
  63. package/build/src/tests/target-specs.js +0 -614
  64. package/build/src/tests/utils.js +0 -104
  65. package/build/src/utils.js +0 -180
  66. package/build/styles/debug.less +0 -97
  67. package/build/styles/mixins.less +0 -63
  68. package/docs/index.md +0 -364
package/src/target.js CHANGED
@@ -1,415 +1,382 @@
1
- import clamp from 'ramda/src/clamp';
2
- import last from 'ramda/src/last';
3
-
4
- import {
5
- getAllContainerIds,
6
- getContainerConfig,
7
- getContainerFocusTarget,
8
- getContainerId,
9
- getContainerNode,
10
- getContainerPreviousTarget,
11
- getContainersForNode,
12
- getDeepSpottableDescendants,
13
- getDefaultContainer,
14
- getLastContainer,
15
- getNavigableContainersForNode,
16
- isContainer,
17
- isNavigable
18
- } from './container';
19
- import navigate from './navigate';
20
- import {
21
- contains,
22
- getContainerRect,
23
- getPointRect,
24
- getRect,
25
- getRects,
26
- intersects,
27
- parseSelector
28
- } from './utils';
29
-
30
- function isFocusable (elem) {
31
- const containers = getContainersForNode(elem);
32
- let verifySelector = true;
33
-
34
- for (let i = containers.length - 1; i >= 0; i--) {
35
- const containerId = containers[i];
36
- if (!isNavigable(elem, containerId, verifySelector)) {
37
- return false;
38
- }
39
-
40
- // only verify selector for the first (immediate ancestor) container
41
- verifySelector = false;
42
- }
43
-
44
- return true;
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.getNavigableTarget = getNavigableTarget;
7
+ exports.getTargetByContainer = getTargetByContainer;
8
+ exports.getTargetByDirectionFromElement = getTargetByDirectionFromElement;
9
+ exports.getTargetByDirectionFromPosition = getTargetByDirectionFromPosition;
10
+ exports.getTargetBySelector = getTargetBySelector;
11
+ exports.isFocusable = isFocusable;
12
+ exports.getNearestTargetFromPosition = void 0;
13
+
14
+ var _clamp = _interopRequireDefault(require("ramda/src/clamp"));
15
+
16
+ var _last = _interopRequireDefault(require("ramda/src/last"));
17
+
18
+ var _container = require("./container");
19
+
20
+ var _navigate = _interopRequireDefault(require("./navigate"));
21
+
22
+ var _utils = require("./utils");
23
+
24
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
25
+
26
+ function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
27
+
28
+ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
29
+
30
+ 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; }
31
+
32
+ function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
33
+
34
+ function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
35
+
36
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
37
+
38
+ function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
39
+
40
+ function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
41
+
42
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
43
+
44
+ function isFocusable(elem) {
45
+ var containers = (0, _container.getContainersForNode)(elem);
46
+ var verifySelector = true;
47
+
48
+ for (var i = containers.length - 1; i >= 0; i--) {
49
+ var containerId = containers[i];
50
+
51
+ if (!(0, _container.isNavigable)(elem, containerId, verifySelector)) {
52
+ return false;
53
+ } // only verify selector for the first (immediate ancestor) container
54
+
55
+
56
+ verifySelector = false;
57
+ }
58
+
59
+ return true;
45
60
  }
46
61
 
47
- function getContainersToSearch (containerId) {
48
- let range = [];
49
- let addRange = function (id) {
50
- const config = getContainerConfig(id);
51
- if (id && range.indexOf(id) < 0 &&
52
- config && !config.selectorDisabled) {
53
- range.push(id);
54
- }
55
- };
56
-
57
- if (containerId) {
58
- addRange(containerId);
59
- } else {
60
- addRange(getDefaultContainer());
61
- addRange(getLastContainer());
62
- [...getAllContainerIds()].map(addRange);
63
- }
64
-
65
- return range;
62
+ function getContainersToSearch(containerId) {
63
+ var range = [];
64
+
65
+ var addRange = function addRange(id) {
66
+ var config = (0, _container.getContainerConfig)(id);
67
+
68
+ if (id && range.indexOf(id) < 0 && config && !config.selectorDisabled) {
69
+ range.push(id);
70
+ }
71
+ };
72
+
73
+ if (containerId) {
74
+ addRange(containerId);
75
+ } else {
76
+ addRange((0, _container.getDefaultContainer)());
77
+ addRange((0, _container.getLastContainer)());
78
+
79
+ _toConsumableArray((0, _container.getAllContainerIds)()).map(addRange);
80
+ }
81
+
82
+ return range;
66
83
  }
67
84
 
68
- function getTargetByContainer (containerId, enterTo) {
69
- return getContainersToSearch(containerId)
70
- .reduce((next, id) => {
71
- return next || getContainerFocusTarget(id, enterTo);
72
- }, null);
85
+ function getTargetByContainer(containerId, enterTo) {
86
+ return getContainersToSearch(containerId).reduce(function (next, id) {
87
+ return next || (0, _container.getContainerFocusTarget)(id, enterTo);
88
+ }, null);
73
89
  }
74
90
 
75
- function getTargetBySelector (selector) {
76
- if (!selector) return null;
91
+ function getTargetBySelector(selector) {
92
+ if (!selector) return null;
77
93
 
78
- if (selector.charAt(0) === '@') {
79
- const containerId = selector.length === 1 ? null : selector.substr(1);
94
+ if (selector.charAt(0) === '@') {
95
+ var containerId = selector.length === 1 ? null : selector.substr(1);
96
+ return getTargetByContainer(containerId);
97
+ }
80
98
 
81
- return getTargetByContainer(containerId);
82
- }
99
+ var next = (0, _utils.parseSelector)(selector)[0];
83
100
 
84
- const next = parseSelector(selector)[0];
85
- if (next) {
101
+ if (next) {
102
+ if ((0, _container.isContainer)(next)) {
103
+ return getTargetByContainer((0, _container.getContainerId)(next));
104
+ }
86
105
 
87
- if (isContainer(next)) {
88
- return getTargetByContainer(getContainerId(next));
89
- }
106
+ var nextContainerIds = (0, _container.getContainersForNode)(next);
90
107
 
91
- const nextContainerIds = getContainersForNode(next);
92
- if (isNavigable(next, last(nextContainerIds), true)) {
93
- return next;
94
- }
95
- }
108
+ if ((0, _container.isNavigable)(next, (0, _last["default"])(nextContainerIds), true)) {
109
+ return next;
110
+ }
111
+ }
96
112
 
97
- return null;
113
+ return null;
98
114
  }
99
115
 
100
- function isRestrictedContainer (containerId) {
101
- const config = getContainerConfig(containerId);
102
- return config && (config.enterTo === 'last-focused' || config.enterTo === 'default-element');
116
+ function isRestrictedContainer(containerId) {
117
+ var config = (0, _container.getContainerConfig)(containerId);
118
+ return config && (config.enterTo === 'last-focused' || config.enterTo === 'default-element');
103
119
  }
104
120
 
105
- function filterRects (elementRects, boundingRect) {
106
- if (!boundingRect) {
107
- return elementRects;
108
- }
109
-
110
- // remove elements that are outside of boundingRect, if specified
111
- return elementRects.filter(rect => {
112
- if (isContainer(rect.element)) {
113
- // For containers, test intersection since they may be larger than the bounding rect
114
- return intersects(boundingRect, rect);
115
- } else {
116
- // For elements, use contains with the center to include mostly visible elements
117
- return contains(boundingRect, rect.center);
118
- }
119
- }).map(rect => {
120
- let topUpdate = rect.top < boundingRect.top;
121
- let bottomUpdate = rect.bottom > boundingRect.bottom;
122
- let leftUpdate = rect.left < boundingRect.left;
123
- let rightUpdate = rect.right > boundingRect.right;
124
-
125
- // if the element's rect is larger than the bounding rect, clamp it to the bounding rect and
126
- // recalculate the center based on the new bounds.
127
- if (topUpdate || bottomUpdate || leftUpdate || rightUpdate) {
128
- const updated = {...rect, center: {...rect.center}};
129
-
130
- if (topUpdate) updated.top = boundingRect.top;
131
- if (bottomUpdate) updated.bottom = boundingRect.bottom;
132
- if (leftUpdate) updated.left = boundingRect.left;
133
- if (rightUpdate) updated.right = boundingRect.right;
134
-
135
- if (leftUpdate || rightUpdate) {
136
- const centerX = updated.left + (updated.right - updated.left) / 2;
137
- updated.center.x = updated.center.left = updated.center.right = centerX;
138
- }
139
-
140
- if (topUpdate || bottomUpdate) {
141
- const centerY = updated.top + (updated.bottom - updated.top) / 2;
142
- updated.center.y = updated.center.top = updated.center.bottom = centerY;
143
- }
144
-
145
- return updated;
146
- }
147
-
148
- return rect;
149
- });
121
+ function filterRects(elementRects, boundingRect) {
122
+ if (!boundingRect) {
123
+ return elementRects;
124
+ } // remove elements that are outside of boundingRect, if specified
125
+
126
+
127
+ return elementRects.filter(function (rect) {
128
+ if ((0, _container.isContainer)(rect.element)) {
129
+ // For containers, test intersection since they may be larger than the bounding rect
130
+ return (0, _utils.intersects)(boundingRect, rect);
131
+ } else {
132
+ // For elements, use contains with the center to include mostly visible elements
133
+ return (0, _utils.contains)(boundingRect, rect.center);
134
+ }
135
+ }).map(function (rect) {
136
+ var topUpdate = rect.top < boundingRect.top;
137
+ var bottomUpdate = rect.bottom > boundingRect.bottom;
138
+ var leftUpdate = rect.left < boundingRect.left;
139
+ var rightUpdate = rect.right > boundingRect.right; // if the element's rect is larger than the bounding rect, clamp it to the bounding rect and
140
+ // recalculate the center based on the new bounds.
141
+
142
+ if (topUpdate || bottomUpdate || leftUpdate || rightUpdate) {
143
+ var updated = _objectSpread(_objectSpread({}, rect), {}, {
144
+ center: _objectSpread({}, rect.center)
145
+ });
146
+
147
+ if (topUpdate) updated.top = boundingRect.top;
148
+ if (bottomUpdate) updated.bottom = boundingRect.bottom;
149
+ if (leftUpdate) updated.left = boundingRect.left;
150
+ if (rightUpdate) updated.right = boundingRect.right;
151
+
152
+ if (leftUpdate || rightUpdate) {
153
+ var centerX = updated.left + (updated.right - updated.left) / 2;
154
+ updated.center.x = updated.center.left = updated.center.right = centerX;
155
+ }
156
+
157
+ if (topUpdate || bottomUpdate) {
158
+ var centerY = updated.top + (updated.bottom - updated.top) / 2;
159
+ updated.center.y = updated.center.top = updated.center.bottom = centerY;
160
+ }
161
+
162
+ return updated;
163
+ }
164
+
165
+ return rect;
166
+ });
150
167
  }
151
168
 
152
- function getContainerContainingRect (elementRects, elementRect) {
153
- // find candidates that are containers and *visually* contain element
154
- const overlapping = elementRects.filter(rect => {
155
- return isContainer(rect.element) && contains(rect, elementRect);
156
- });
169
+ function getContainerContainingRect(elementRects, elementRect) {
170
+ // find candidates that are containers and *visually* contain element
171
+ var overlapping = elementRects.filter(function (rect) {
172
+ return (0, _container.isContainer)(rect.element) && (0, _utils.contains)(rect, elementRect);
173
+ }); // if the next element is a container AND the current element is *visually* contained within
174
+ // one of the candidate element, we need to ignore container `enterTo` preferences and
175
+ // retrieve its spottable descendants and try to navigate to them.
157
176
 
158
- // if the next element is a container AND the current element is *visually* contained within
159
- // one of the candidate element, we need to ignore container `enterTo` preferences and
160
- // retrieve its spottable descendants and try to navigate to them.
161
- if (overlapping.length) {
162
- return overlapping[0].element.dataset.spotlightId;
163
- }
177
+ if (overlapping.length) {
178
+ return overlapping[0].element.dataset.spotlightId;
179
+ }
164
180
 
165
- return false;
181
+ return false;
166
182
  }
167
183
 
168
- function getOverflowContainerRect (containerId) {
169
- // if the target container has overflowing content, update the boundingRect to match its
170
- // bounds to prevent finding elements within the container's hierarchy but not visible.
171
- // This filter only applies when waterfalling to prevent filtering out elements that share
172
- // a container tree with `element`
173
- const nextConfig = getContainerConfig(containerId);
174
- if (nextConfig && nextConfig.overflow) {
175
- return getContainerRect(containerId);
176
- }
177
- }
184
+ function getOverflowContainerRect(containerId) {
185
+ // if the target container has overflowing content, update the boundingRect to match its
186
+ // bounds to prevent finding elements within the container's hierarchy but not visible.
187
+ // This filter only applies when waterfalling to prevent filtering out elements that share
188
+ // a container tree with `element`
189
+ var nextConfig = (0, _container.getContainerConfig)(containerId);
178
190
 
179
- function getTargetInContainerByDirectionFromPosition (direction, containerId, positionRect, elementContainerIds, boundingRect) {
180
- const elements = getDeepSpottableDescendants(containerId);
181
- let elementRects = filterRects(getRects(elements), boundingRect);
182
-
183
- let next = null;
184
-
185
- while (elementRects.length > 0) {
186
- const overlappingContainerId = getContainerContainingRect(elementRects, positionRect);
187
-
188
- // if the pointer is within a container that is a candidate element, we need to ignore container
189
- // `enterTo` preferences and retrieve its spottable descendants and try to navigate to them.
190
- if (overlappingContainerId) {
191
- next = getTargetInContainerByDirectionFromPosition(
192
- direction,
193
- overlappingContainerId,
194
- positionRect,
195
- elementContainerIds,
196
- boundingRect
197
- );
198
-
199
- if (!next) {
200
- // filter out the container and try again
201
- elementRects = elementRects.filter(rect => {
202
- return rect.element.dataset.spotlightId !== overlappingContainerId;
203
- });
204
- continue;
205
- }
206
-
207
- // found a target so break out and return
208
- break;
209
- }
210
-
211
- // try to navigate from position to one of the candidates in containerId
212
- next = navigate(
213
- positionRect,
214
- direction,
215
- elementRects,
216
- getContainerConfig(containerId)
217
- );
218
-
219
- // if we match a container, recurse into it
220
- if (next && isContainer(next)) {
221
- const nextContainerId = next.dataset.spotlightId;
222
-
223
- // need to cache this reference so we can filter it out later if necessary
224
- const lastNavigated = next;
225
-
226
- next = getTargetInContainerByDirectionFromPosition(
227
- direction,
228
- nextContainerId,
229
- positionRect,
230
- elementContainerIds,
231
- getOverflowContainerRect(nextContainerId) || boundingRect
232
- );
233
-
234
- if (!next) {
235
- // filter out the container and try again
236
- elementRects = elementRects.filter(rect => rect.element !== lastNavigated);
237
- continue;
238
- }
239
- }
240
-
241
- // If we've met every condition and haven't explicitly retried the search via `continue`,
242
- // break out and return
243
- break;
244
- }
245
-
246
- return next;
191
+ if (nextConfig && nextConfig.overflow) {
192
+ return (0, _utils.getContainerRect)(containerId);
193
+ }
247
194
  }
248
195
 
196
+ function getTargetInContainerByDirectionFromPosition(direction, containerId, positionRect, elementContainerIds, boundingRect) {
197
+ var elements = (0, _container.getDeepSpottableDescendants)(containerId);
198
+ var elementRects = filterRects((0, _utils.getRects)(elements), boundingRect);
199
+ var next = null;
200
+
201
+ var _loop = function _loop() {
202
+ var overlappingContainerId = getContainerContainingRect(elementRects, positionRect); // if the pointer is within a container that is a candidate element, we need to ignore container
203
+ // `enterTo` preferences and retrieve its spottable descendants and try to navigate to them.
204
+
205
+ if (overlappingContainerId) {
206
+ next = getTargetInContainerByDirectionFromPosition(direction, overlappingContainerId, positionRect, elementContainerIds, boundingRect);
207
+
208
+ if (!next) {
209
+ // filter out the container and try again
210
+ elementRects = elementRects.filter(function (rect) {
211
+ return rect.element.dataset.spotlightId !== overlappingContainerId;
212
+ });
213
+ return "continue";
214
+ } // found a target so break out and return
215
+
216
+
217
+ return "break";
218
+ } // try to navigate from position to one of the candidates in containerId
249
219
 
250
- function getTargetInContainerByDirectionFromElement (direction, containerId, element, elementRect, elementContainerIds, boundingRect) {
251
- const elements = getDeepSpottableDescendants(containerId);
252
-
253
- // shortcut for previous target from element if it were saved
254
- const previous = getContainerPreviousTarget(containerId, direction, element);
255
- if (previous && elements.indexOf(previous) !== -1) {
256
- return previous;
257
- }
258
-
259
- // `spotlightOverflow` is a private, and likely temporary, API to allow a component within an
260
- // spotlight container with `overflow: true` to be treated as if it were outside of the
261
- // container. The result is that the candidates, `elements` are filtered by the bounds of the
262
- // overflow container effectively hiding those that have overflowed and are visually hidden.
263
- //
264
- // Currently only used by moonstone/Scroller.Scrollbar as a means to allow 5-way navigation to
265
- // escape the Scrollable from paging controls rather than focusing contents that are out of view
266
- if (element.dataset.spotlightOverflow === 'ignore') {
267
- boundingRect = getOverflowContainerRect(containerId) || boundingRect;
268
- }
269
-
270
- let elementRects = filterRects(getRects(elements), boundingRect);
271
-
272
- let next = null;
273
-
274
- while (elementRects.length > 0) {
275
- const overlappingContainerId = getContainerContainingRect(elementRects, elementRect);
276
-
277
- // if the next element is a container AND the current element is *visually* contained within
278
- // one of the candidate elements, we need to ignore container `enterTo` preferences and
279
- // retrieve its spottable descendants and try to navigate to them.
280
- if (overlappingContainerId) {
281
- next = getTargetInContainerByDirectionFromElement(
282
- direction,
283
- overlappingContainerId,
284
- element,
285
- elementRect,
286
- elementContainerIds,
287
- boundingRect
288
- );
289
-
290
- if (!next) {
291
- // filter out the container and try again
292
- elementRects = elementRects.filter(rect => {
293
- return rect.element.dataset.spotlightId !== overlappingContainerId;
294
- });
295
- continue;
296
- }
297
-
298
- // found a target so break out and return
299
- break;
300
- }
301
-
302
- // If one of the downstream containers is configured for partition, we use that
303
- // container's bounds as the partition rect for navigation.
304
- const partitionContainer = elementContainerIds
305
- .slice(elementContainerIds.indexOf(containerId) + 1)
306
- .find(id => {
307
- const cfg = getContainerConfig(id);
308
- return cfg && cfg.partition;
309
- });
310
-
311
- let partitionRect = elementRect;
312
- if (partitionContainer) {
313
- partitionRect = getContainerRect(partitionContainer);
314
- }
315
-
316
- // try to navigate from element to one of the candidates in containerId
317
- next = navigate(
318
- elementRect,
319
- direction,
320
- elementRects,
321
- getContainerConfig(containerId),
322
- partitionRect
323
- );
324
-
325
- // if we match a container,
326
- if (next && isContainer(next)) {
327
- const nextContainerId = next.dataset.spotlightId;
328
-
329
- // need to cache this reference so we can filter it out later if necessary
330
- const lastNavigated = next;
331
-
332
- // and it is restricted, return its target
333
- if (isRestrictedContainer(nextContainerId)) {
334
- next = getTargetByContainer(nextContainerId);
335
- } else {
336
- // otherwise, recurse into it
337
- next = getTargetInContainerByDirectionFromElement(
338
- direction,
339
- nextContainerId,
340
- element,
341
- elementRect,
342
- elementContainerIds,
343
- getOverflowContainerRect(nextContainerId) || boundingRect
344
- );
345
- }
346
-
347
- if (!next) {
348
- elementRects = elementRects.filter(rect => rect.element !== lastNavigated);
349
- continue;
350
- }
351
- }
352
-
353
- // If we've met every condition and haven't explicitly retried the search via `continue`,
354
- // break out and return
355
- break;
356
- }
357
-
358
- return next;
220
+
221
+ next = (0, _navigate["default"])(positionRect, direction, elementRects, (0, _container.getContainerConfig)(containerId)); // if we match a container, recurse into it
222
+
223
+ if (next && (0, _container.isContainer)(next)) {
224
+ var nextContainerId = next.dataset.spotlightId; // need to cache this reference so we can filter it out later if necessary
225
+
226
+ var lastNavigated = next;
227
+ next = getTargetInContainerByDirectionFromPosition(direction, nextContainerId, positionRect, elementContainerIds, getOverflowContainerRect(nextContainerId) || boundingRect);
228
+
229
+ if (!next) {
230
+ // filter out the container and try again
231
+ elementRects = elementRects.filter(function (rect) {
232
+ return rect.element !== lastNavigated;
233
+ });
234
+ return "continue";
235
+ }
236
+ } // If we've met every condition and haven't explicitly retried the search via `continue`,
237
+ // break out and return
238
+
239
+
240
+ return "break";
241
+ };
242
+
243
+ while (elementRects.length > 0) {
244
+ var _ret = _loop();
245
+
246
+ if (_ret === "continue") continue;
247
+ if (_ret === "break") break;
248
+ }
249
+
250
+ return next;
359
251
  }
360
252
 
361
- function getTargetByDirectionFromElement (direction, element) {
362
- const extSelector = element.getAttribute('data-spot-' + direction);
363
- if (typeof extSelector === 'string') {
364
- return getTargetBySelector(extSelector);
365
- }
366
-
367
- const elementRect = getRect(element);
368
-
369
- const next = getNavigableContainersForNode(element)
370
- .reduceRight((result, containerId, index, elementContainerIds) => {
371
- result = result || getTargetInContainerByDirectionFromElement(
372
- direction,
373
- containerId,
374
- element,
375
- elementRect,
376
- elementContainerIds
377
- );
378
-
379
- if (!result) {
380
- result = getLeaveForTarget(containerId, direction);
381
-
382
- // To support a `leaveFor` configuration with navigation disallowed in the current
383
- // `direction`, we return the current element to prevent further searches for a
384
- // target in this reduction.
385
- if (result === false) {
386
- result = element;
387
- }
388
- }
389
-
390
- return result;
391
- }, null);
392
-
393
- // if the reduce above returns the original element, it means it hit a `leaveFor` config that
394
- // prevents navigation so we enforce that here by returning null.
395
- return next !== element ? next : null;
253
+ function getTargetInContainerByDirectionFromElement(direction, containerId, element, elementRect, elementContainerIds, boundingRect) {
254
+ var elements = (0, _container.getDeepSpottableDescendants)(containerId); // shortcut for previous target from element if it were saved
255
+
256
+ var previous = (0, _container.getContainerPreviousTarget)(containerId, direction, element);
257
+
258
+ if (previous && elements.indexOf(previous) !== -1) {
259
+ return previous;
260
+ } // `spotlightOverflow` is a private, and likely temporary, API to allow a component within an
261
+ // spotlight container with `overflow: true` to be treated as if it were outside of the
262
+ // container. The result is that the candidates, `elements` are filtered by the bounds of the
263
+ // overflow container effectively hiding those that have overflowed and are visually hidden.
264
+ //
265
+ // Currently only used by moonstone/Scroller.Scrollbar as a means to allow 5-way navigation to
266
+ // escape the Scrollable from paging controls rather than focusing contents that are out of view
267
+
268
+
269
+ if (element.dataset.spotlightOverflow === 'ignore') {
270
+ boundingRect = getOverflowContainerRect(containerId) || boundingRect;
271
+ }
272
+
273
+ var elementRects = filterRects((0, _utils.getRects)(elements), boundingRect);
274
+ var next = null;
275
+
276
+ var _loop2 = function _loop2() {
277
+ var overlappingContainerId = getContainerContainingRect(elementRects, elementRect); // if the next element is a container AND the current element is *visually* contained within
278
+ // one of the candidate elements, we need to ignore container `enterTo` preferences and
279
+ // retrieve its spottable descendants and try to navigate to them.
280
+
281
+ if (overlappingContainerId) {
282
+ next = getTargetInContainerByDirectionFromElement(direction, overlappingContainerId, element, elementRect, elementContainerIds, boundingRect);
283
+
284
+ if (!next) {
285
+ // filter out the container and try again
286
+ elementRects = elementRects.filter(function (rect) {
287
+ return rect.element.dataset.spotlightId !== overlappingContainerId;
288
+ });
289
+ return "continue";
290
+ } // found a target so break out and return
291
+
292
+
293
+ return "break";
294
+ } // If one of the downstream containers is configured for partition, we use that
295
+ // container's bounds as the partition rect for navigation.
296
+
297
+
298
+ var partitionContainer = elementContainerIds.slice(elementContainerIds.indexOf(containerId) + 1).find(function (id) {
299
+ var cfg = (0, _container.getContainerConfig)(id);
300
+ return cfg && cfg.partition;
301
+ });
302
+ var partitionRect = elementRect;
303
+
304
+ if (partitionContainer) {
305
+ partitionRect = (0, _utils.getContainerRect)(partitionContainer);
306
+ } // try to navigate from element to one of the candidates in containerId
307
+
308
+
309
+ next = (0, _navigate["default"])(elementRect, direction, elementRects, (0, _container.getContainerConfig)(containerId), partitionRect); // if we match a container,
310
+
311
+ if (next && (0, _container.isContainer)(next)) {
312
+ var nextContainerId = next.dataset.spotlightId; // need to cache this reference so we can filter it out later if necessary
313
+
314
+ var lastNavigated = next; // and it is restricted, return its target
315
+
316
+ if (isRestrictedContainer(nextContainerId)) {
317
+ next = getTargetByContainer(nextContainerId);
318
+ } else {
319
+ // otherwise, recurse into it
320
+ next = getTargetInContainerByDirectionFromElement(direction, nextContainerId, element, elementRect, elementContainerIds, getOverflowContainerRect(nextContainerId) || boundingRect);
321
+ }
322
+
323
+ if (!next) {
324
+ elementRects = elementRects.filter(function (rect) {
325
+ return rect.element !== lastNavigated;
326
+ });
327
+ return "continue";
328
+ }
329
+ } // If we've met every condition and haven't explicitly retried the search via `continue`,
330
+ // break out and return
331
+
332
+
333
+ return "break";
334
+ };
335
+
336
+ while (elementRects.length > 0) {
337
+ var _ret2 = _loop2();
338
+
339
+ if (_ret2 === "continue") continue;
340
+ if (_ret2 === "break") break;
341
+ }
342
+
343
+ return next;
396
344
  }
397
345
 
398
- function getTargetByDirectionFromPosition (direction, position, containerId) {
399
- const pointerRect = getPointRect(position);
400
-
401
- return getNavigableContainersForNode(getContainerNode(containerId))
402
- .reduceRight((result, id, index, elementContainerIds) => {
403
- return result ||
404
- getTargetInContainerByDirectionFromPosition(
405
- direction,
406
- id,
407
- pointerRect,
408
- elementContainerIds
409
- );
410
- }, null);
346
+ function getTargetByDirectionFromElement(direction, element) {
347
+ var extSelector = element.getAttribute('data-spot-' + direction);
348
+
349
+ if (typeof extSelector === 'string') {
350
+ return getTargetBySelector(extSelector);
351
+ }
352
+
353
+ var elementRect = (0, _utils.getRect)(element);
354
+ var next = (0, _container.getNavigableContainersForNode)(element).reduceRight(function (result, containerId, index, elementContainerIds) {
355
+ result = result || getTargetInContainerByDirectionFromElement(direction, containerId, element, elementRect, elementContainerIds);
356
+
357
+ if (!result) {
358
+ result = getLeaveForTarget(containerId, direction); // To support a `leaveFor` configuration with navigation disallowed in the current
359
+ // `direction`, we return the current element to prevent further searches for a
360
+ // target in this reduction.
361
+
362
+ if (result === false) {
363
+ result = element;
364
+ }
365
+ }
366
+
367
+ return result;
368
+ }, null); // if the reduce above returns the original element, it means it hit a `leaveFor` config that
369
+ // prevents navigation so we enforce that here by returning null.
370
+
371
+ return next !== element ? next : null;
411
372
  }
412
373
 
374
+ function getTargetByDirectionFromPosition(direction, position, containerId) {
375
+ var pointerRect = (0, _utils.getPointRect)(position);
376
+ return (0, _container.getNavigableContainersForNode)((0, _container.getContainerNode)(containerId)).reduceRight(function (result, id, index, elementContainerIds) {
377
+ return result || getTargetInContainerByDirectionFromPosition(direction, id, pointerRect, elementContainerIds);
378
+ }, null);
379
+ }
413
380
  /**
414
381
  * Returns the target identified by the selector configured for the container identified by
415
382
  * `containerId` for the given `direction`. If the selector is an empty string, the method returns
@@ -421,113 +388,120 @@ function getTargetByDirectionFromPosition (direction, position, containerId) {
421
388
  * @returns {Node|Boolean} Target, if found, or `false` if navigation is disallowed
422
389
  * @private
423
390
  */
424
- function getLeaveForTarget (containerId, direction) {
425
- const config = getContainerConfig(containerId);
426
-
427
- if (config) {
428
- const target = config.restrict !== 'self-only' && config.leaveFor && config.leaveFor[direction];
429
- if (typeof target === 'string') {
430
- if (target === '') {
431
- return false;
432
- }
433
- return getTargetBySelector(target);
434
- }
435
-
436
- const nextContainerIds = getContainersForNode(target);
437
- if (isNavigable(target, last(nextContainerIds))) {
438
- return target;
439
- }
440
- }
441
-
442
- return null;
443
- }
444
391
 
445
- function getNavigableTarget (target) {
446
- if (target === document) return null;
447
392
 
448
- let parent;
449
- while (target && (isContainer(target) || !isFocusable(target))) {
450
- parent = target.parentNode;
451
- target = parent === document ? null : parent; // calling isNavigable on document is problematic
452
- }
453
- return target;
393
+ function getLeaveForTarget(containerId, direction) {
394
+ var config = (0, _container.getContainerConfig)(containerId);
395
+
396
+ if (config) {
397
+ var target = config.restrict !== 'self-only' && config.leaveFor && config.leaveFor[direction];
398
+
399
+ if (typeof target === 'string') {
400
+ if (target === '') {
401
+ return false;
402
+ }
403
+
404
+ return getTargetBySelector(target);
405
+ }
406
+
407
+ var nextContainerIds = (0, _container.getContainersForNode)(target);
408
+
409
+ if ((0, _container.isNavigable)(target, (0, _last["default"])(nextContainerIds))) {
410
+ return target;
411
+ }
412
+ }
413
+
414
+ return null;
454
415
  }
455
416
 
456
- const getOffsetDistanceToTargetFromPosition = (distance, direction, {x, y}, {left, right, top, bottom}) => {
457
- if (direction === 'left' || direction === 'right') {
458
- if (y > bottom) {
459
- distance += y - bottom;
460
- } else if (y < top) {
461
- distance += top - y;
462
- }
463
- } else if (x > right) {
464
- distance += x - right;
465
- } else if (x < left) {
466
- distance += left - x;
467
- }
468
-
469
- return distance;
470
- };
417
+ function getNavigableTarget(target) {
418
+ if (target === document) return null;
419
+ var parent;
471
420
 
472
- const getDistanceToTargetFromPosition = (direction, position, elementRect) => {
473
- const {x, y} = position;
474
- let distance;
475
-
476
- if (direction === 'left') {
477
- distance = x - elementRect.right;
478
- } else if (direction === 'right') {
479
- distance = elementRect.left - x;
480
- } else if (direction === 'up') {
481
- distance = y - elementRect.bottom;
482
- } else if (direction === 'down') {
483
- distance = elementRect.top - y;
484
- }
485
-
486
- return getOffsetDistanceToTargetFromPosition(clamp(0, Math.abs(distance), distance), direction, position, elementRect);
487
- };
421
+ while (target && ((0, _container.isContainer)(target) || !isFocusable(target))) {
422
+ parent = target.parentNode;
423
+ target = parent === document ? null : parent; // calling isNavigable on document is problematic
424
+ }
488
425
 
489
- const getNearestTargetsInContainerFromPosition = (position, containerId) => {
490
- return ['up', 'left', 'right', 'down'].reduce((result, direction) => {
491
- const target = getTargetByDirectionFromPosition(direction, position, containerId);
426
+ return target;
427
+ }
492
428
 
493
- if (target) {
494
- result.push({
495
- direction,
496
- target
497
- });
498
- }
429
+ var getOffsetDistanceToTargetFromPosition = function getOffsetDistanceToTargetFromPosition(distance, direction, _ref, _ref2) {
430
+ var x = _ref.x,
431
+ y = _ref.y;
432
+ var left = _ref2.left,
433
+ right = _ref2.right,
434
+ top = _ref2.top,
435
+ bottom = _ref2.bottom;
436
+
437
+ if (direction === 'left' || direction === 'right') {
438
+ if (y > bottom) {
439
+ distance += y - bottom;
440
+ } else if (y < top) {
441
+ distance += top - y;
442
+ }
443
+ } else if (x > right) {
444
+ distance += x - right;
445
+ } else if (x < left) {
446
+ distance += left - x;
447
+ }
448
+
449
+ return distance;
450
+ };
499
451
 
500
- return result;
501
- }, []);
452
+ var getDistanceToTargetFromPosition = function getDistanceToTargetFromPosition(direction, position, elementRect) {
453
+ var x = position.x,
454
+ y = position.y;
455
+ var distance;
456
+
457
+ if (direction === 'left') {
458
+ distance = x - elementRect.right;
459
+ } else if (direction === 'right') {
460
+ distance = elementRect.left - x;
461
+ } else if (direction === 'up') {
462
+ distance = y - elementRect.bottom;
463
+ } else if (direction === 'down') {
464
+ distance = elementRect.top - y;
465
+ }
466
+
467
+ return getOffsetDistanceToTargetFromPosition((0, _clamp["default"])(0, Math.abs(distance), distance), direction, position, elementRect);
502
468
  };
503
469
 
504
- const getNearestTargetInContainerFromPosition = (position, containerId) => {
505
- const targets = getNearestTargetsInContainerFromPosition(position, containerId);
470
+ var getNearestTargetsInContainerFromPosition = function getNearestTargetsInContainerFromPosition(position, containerId) {
471
+ return ['up', 'left', 'right', 'down'].reduce(function (result, direction) {
472
+ var target = getTargetByDirectionFromPosition(direction, position, containerId);
506
473
 
507
- if (!targets.length) {
508
- return;
509
- }
474
+ if (target) {
475
+ result.push({
476
+ direction: direction,
477
+ target: target
478
+ });
479
+ }
510
480
 
511
- targets.forEach((item) => {
512
- const {direction, target} = item;
513
- item.distance = getDistanceToTargetFromPosition(direction, position, getRect(target));
514
- });
481
+ return result;
482
+ }, []);
483
+ };
515
484
 
516
- targets.sort((a, b) => a.distance - b.distance);
517
- return targets[0].target;
485
+ var getNearestTargetInContainerFromPosition = function getNearestTargetInContainerFromPosition(position, containerId) {
486
+ var targets = getNearestTargetsInContainerFromPosition(position, containerId);
487
+
488
+ if (!targets.length) {
489
+ return;
490
+ }
491
+
492
+ targets.forEach(function (item) {
493
+ var direction = item.direction,
494
+ target = item.target;
495
+ item.distance = getDistanceToTargetFromPosition(direction, position, (0, _utils.getRect)(target));
496
+ });
497
+ targets.sort(function (a, b) {
498
+ return a.distance - b.distance;
499
+ });
500
+ return targets[0].target;
518
501
  };
519
502
 
520
- const getNearestTargetFromPosition = (position, containerId) => (
521
- getNavigableTarget(document.elementFromPoint(position.x, position.y)) ||
522
- getNearestTargetInContainerFromPosition(position, containerId)
523
- );
524
-
525
- export {
526
- getNavigableTarget,
527
- getNearestTargetFromPosition,
528
- getTargetByContainer,
529
- getTargetByDirectionFromElement,
530
- getTargetByDirectionFromPosition,
531
- getTargetBySelector,
532
- isFocusable
503
+ var getNearestTargetFromPosition = function getNearestTargetFromPosition(position, containerId) {
504
+ return getNavigableTarget(document.elementFromPoint(position.x, position.y)) || getNearestTargetInContainerFromPosition(position, containerId);
533
505
  };
506
+
507
+ exports.getNearestTargetFromPosition = getNearestTargetFromPosition;