@lightningtv/solid 3.0.0-14 → 3.0.0-16

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.
@@ -1,113 +1,324 @@
1
- import { ElementNode, assertTruthy, Config } from '@lightningtv/core';
2
- import { type KeyHandler } from '@lightningtv/core/focusManager';
3
- import type { NavigableElement, OnSelectedChanged } from '../types.js';
4
-
5
- export function onGridFocus(onSelectedChanged: OnSelectedChanged | undefined) {
6
- return function (this: ElementNode) {
7
- if (!this || this.children.length === 0) return false;
8
-
9
- // only check onFocus, and not when grid.setFocus() to retrigger focus
10
- if (!this.states.has(Config.focusStateKey)) {
11
- // if a child already has focus, assume that should be selected
12
- this.children.find((child, index) => {
13
- if (child.states.has(Config.focusStateKey)) {
14
- this.selected = index;
15
- return true;
16
- }
17
- return false;
18
- });
19
- }
1
+ import * as s from 'solid-js';
2
+ import * as lng from '@lightningtv/solid';
3
+ import * as lngp from '@lightningtv/solid/primitives';
20
4
 
21
- this.selected = this.selected || 0;
22
- let child = this.selected
23
- ? this.children[this.selected]
24
- : this.selectedNode;
5
+ declare module '@lightningtv/core' {
6
+ interface ElementNode {
7
+ /** For children of {@link lngp.NavigableElement}, set to `true` to prevent being selected */
8
+ skipFocus?: boolean;
9
+ }
10
+ }
25
11
 
26
- while (child?.skipFocus) {
27
- this.selected++;
28
- child = this.children[this.selected];
29
- }
30
- if (!(child instanceof ElementNode)) return false;
31
- child.setFocus();
12
+ function idxInArray(idx: number, arr: readonly any[]): boolean {
13
+ return idx >= 0 && idx < arr.length;
14
+ }
32
15
 
33
- if (onSelectedChanged) {
34
- const grid = this as NavigableElement;
35
- onSelectedChanged.call(grid, grid.selected, grid, child);
16
+ function findFirstFocusableChildIdx(
17
+ el: lngp.NavigableElement,
18
+ from = 0,
19
+ delta = 1,
20
+ ): number {
21
+ for (let i = from; ; i += delta) {
22
+ if (!idxInArray(i, el.children)) {
23
+ if (el.wrap) {
24
+ i = (i + el.children.length) % el.children.length;
25
+ } else break;
36
26
  }
37
- return true;
27
+ if (!el.children[i]!.skipFocus) {
28
+ return i;
29
+ }
30
+ }
31
+ return -1;
32
+ }
33
+
34
+ function selectChild(el: lngp.NavigableElement, index: number): boolean {
35
+ const child = el.children[index];
36
+
37
+ if (child == null || child.skipFocus) {
38
+ el.selected = -1;
39
+ return false;
40
+ }
41
+
42
+ const lastSelected = el.selected;
43
+ el.selected = index;
44
+ child.setFocus();
45
+
46
+ if (lastSelected !== index) {
47
+ el.onSelectedChanged?.(index, el, child as lng.ElementNode, lastSelected);
48
+ }
49
+
50
+ return true;
51
+ }
52
+
53
+ /** @deprecated Use {@link navigableForwardFocus} instead */
54
+ export function onGridFocus(
55
+ _?: lngp.OnSelectedChanged,
56
+ ): lng.ForwardFocusHandler {
57
+ return function () {
58
+ return navigableForwardFocus.call(this, this);
38
59
  };
39
60
  }
40
61
 
62
+ /**
63
+ * Forwards focus to the first focusable child of a {@link lngp.NavigableElement} and
64
+ * selects it.
65
+ *
66
+ * @example
67
+ * ```tsx
68
+ * <view
69
+ * selected={0}
70
+ * forwardFocus={navigableForwardFocus}
71
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
72
+ * >
73
+ * ```
74
+ */
75
+ export const navigableForwardFocus: lng.ForwardFocusHandler = function () {
76
+ const navigable = this as lngp.NavigableElement;
77
+
78
+ if (!lng.isFocused(this)) {
79
+ // if a child already has focus, assume that should be selected
80
+ for (let [i, child] of this.children.entries()) {
81
+ if (lng.isFocused(child)) {
82
+ this.selected = i;
83
+ break;
84
+ }
85
+ }
86
+ }
87
+
88
+ let selected = navigable.selected;
89
+ selected = idxInArray(selected, this.children) ? selected : 0;
90
+ selected = findFirstFocusableChildIdx(navigable, selected);
91
+ return selectChild(navigable, selected);
92
+ };
93
+
94
+ /** @deprecated Use {@link navigableHandleNavigation} instead */
41
95
  export function handleNavigation(
42
96
  direction: 'up' | 'right' | 'down' | 'left',
43
- ): KeyHandler {
97
+ ): lng.KeyHandler {
44
98
  return function () {
45
- const numChildren = this.children.length;
46
- const wrap = this.wrap;
47
- const lastSelected = this.selected || 0;
99
+ return moveSelection(
100
+ this as lngp.NavigableElement,
101
+ direction === 'up' || direction === 'left' ? -1 : 1,
102
+ );
103
+ };
104
+ }
48
105
 
49
- if (numChildren === 0) {
106
+ /**
107
+ * Handles navigation key events for navigable elements, \
108
+ * such as {@link lngp.Row} and {@link lngp.Column}.
109
+ *
110
+ * Uses {@link moveSelection} to select the next or previous child based on the key pressed.
111
+ *
112
+ * @example
113
+ * ```tsx
114
+ * <view
115
+ * selected={0}
116
+ * onUp={navigableHandleNavigation}
117
+ * onDown={navigableHandleNavigation}
118
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
119
+ * >
120
+ * ```
121
+ */
122
+ export const navigableHandleNavigation: lng.KeyHandler = function (e) {
123
+ return moveSelection(
124
+ this as lngp.NavigableElement,
125
+ e.key === 'ArrowUp' || e.key === 'ArrowLeft' ? -1 : 1,
126
+ );
127
+ };
128
+
129
+ /**
130
+ * Moves the selection within a {@link lngp.NavigableElement}.
131
+ */
132
+ export function moveSelection(
133
+ el: lngp.NavigableElement,
134
+ delta: number,
135
+ ): boolean {
136
+ let selected = findFirstFocusableChildIdx(el, el.selected + delta, delta);
137
+
138
+ if (selected === -1) {
139
+ if (
140
+ !idxInArray(el.selected, el.children) ||
141
+ el.children[el.selected]!.skipFocus ||
142
+ lng.isFocused(el.children[el.selected]!)
143
+ ) {
50
144
  return false;
51
145
  }
146
+ selected = el.selected;
147
+ }
52
148
 
53
- if (direction === 'right' || direction === 'down') {
54
- do {
55
- this.selected = ((this.selected || 0) % numChildren) + 1;
56
- if (this.selected >= numChildren) {
57
- if (!wrap) {
58
- this.selected = -1;
59
- break;
60
- }
61
- this.selected = 0;
62
- }
63
- } while (this.children[this.selected]?.skipFocus);
64
- } else if (direction === 'left' || direction === 'up') {
65
- do {
66
- this.selected = ((this.selected || 0) % numChildren) - 1;
67
- if (this.selected < 0) {
68
- if (!wrap) {
69
- this.selected = -1;
70
- break;
71
- }
72
- this.selected = numChildren - 1;
73
- }
74
- } while (this.children[this.selected]?.skipFocus);
75
- }
149
+ const active = el.children[selected]!;
76
150
 
77
- if (this.selected === -1) {
78
- this.selected = lastSelected;
79
- if (
80
- this.children[this.selected]?.states!.has(
81
- Config.focusStateKey || '$focus',
82
- )
83
- ) {
84
- // This child is already focused, so bubble up to next handler
85
- return false;
151
+ if (el.plinko) {
152
+ // Set the next item to have the same selected index
153
+ // so we move up / down directly
154
+ const lastSelectedChild = el.children[el.selected];
155
+ lng.assertTruthy(lastSelectedChild instanceof lng.ElementNode);
156
+
157
+ const num = lastSelectedChild.selected || 0;
158
+ active.selected =
159
+ num < active.children.length ? num : active.children.length - 1;
160
+ }
161
+
162
+ return selectChild(el, selected);
163
+ }
164
+
165
+ function distanceBetweenRectCenters(a: lng.Rect, b: lng.Rect): number {
166
+ const dx = Math.abs(a.x + a.width / 2 - (b.x + b.width / 2)) / 2;
167
+ const dy = Math.abs(a.y + a.height / 2 - (b.y + b.height / 2)) / 2;
168
+ return Math.sqrt(dx * dx + dy * dy);
169
+ }
170
+
171
+ function findClosestFocusableChildIdx(
172
+ el: lng.ElementNode,
173
+ prevEl: lng.ElementNode,
174
+ ): number {
175
+ // select child closest to the previous active element
176
+ const prevRect = lng.getElementScreenRect(prevEl);
177
+ const elRect = lng.getElementScreenRect(el);
178
+ const childRect: lng.Rect = { x: 0, y: 0, width: 0, height: 0 };
179
+
180
+ let closestIdx = -1;
181
+ let closestDist = Infinity;
182
+
183
+ for (const [idx, child] of el.children.entries()) {
184
+ if (!child.skipFocus) {
185
+ lng.getElementScreenRect(child, el, childRect);
186
+ childRect.x += elRect.x;
187
+ childRect.y += elRect.y;
188
+ const distance = distanceBetweenRectCenters(prevRect, childRect);
189
+ if (distance < closestDist) {
190
+ closestDist = distance;
191
+ closestIdx = idx;
86
192
  }
87
193
  }
88
- const active = this.children[this.selected || 0] || this.children[0];
89
- if (!(active instanceof ElementNode)) return false;
90
- const navigableThis = this as NavigableElement;
91
-
92
- navigableThis.onSelectedChanged &&
93
- navigableThis.onSelectedChanged.call(
94
- navigableThis,
95
- navigableThis.selected,
96
- navigableThis,
97
- active,
98
- lastSelected,
99
- );
100
-
101
- if (this.plinko) {
102
- // Set the next item to have the same selected index
103
- // so we move up / down directly
104
- const lastSelectedChild = this.children[lastSelected];
105
- assertTruthy(lastSelectedChild instanceof ElementNode);
106
- const num = lastSelectedChild.selected || 0;
107
- active.selected =
108
- num < active.children.length ? num : active.children.length - 1;
109
- }
110
- active.setFocus();
111
- return true;
112
- };
194
+ }
195
+
196
+ return closestIdx;
113
197
  }
198
+
199
+ /**
200
+ * Forwards focus to the closest or first focusable child of a {@link lngp.NavigableElement} and
201
+ * selects it.
202
+ *
203
+ * To determine the closest child, it uses the distance between the center of the previous focused element
204
+ * and the center of each child element.
205
+ *
206
+ * @example
207
+ * ```tsx
208
+ * <view
209
+ * selected={0}
210
+ * forwardFocus={spatialForwardFocus}
211
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
212
+ * >
213
+ * ```
214
+ */
215
+ export const spatialForwardFocus: lng.ForwardFocusHandler = function () {
216
+ const prevEl = s.untrack(lng.activeElement);
217
+ if (prevEl) {
218
+ const idx = findClosestFocusableChildIdx(this, prevEl);
219
+ const selected = selectChild(this as lngp.NavigableElement, idx);
220
+ if (selected) return true;
221
+ }
222
+ const idx = findFirstFocusableChildIdx(this as lngp.NavigableElement);
223
+ return selectChild(this as lngp.NavigableElement, idx);
224
+ };
225
+
226
+ /**
227
+ * Handles spatial navigation within a {@link lngp.NavigableElement} by moving focus
228
+ * based on the arrow keys pressed.
229
+ *
230
+ * This function allows for navigation in a grid-like manner for flex-wrap containers, \
231
+ * where pressing the arrow keys will either:
232
+ * - move focus to the next/prev child in the same row/column
233
+ * - or find the closest child in the next/prev row/column.
234
+ *
235
+ * @example
236
+ * ```tsx
237
+ * <view
238
+ * selected={0}
239
+ * display="flex"
240
+ * flexWrap="wrap"
241
+ * onUp={spatialHandleNavigation}
242
+ * onDown={spatialHandleNavigation}
243
+ * onSelectedChanged={(idx, el, child, lastIdx) => {...}}
244
+ * >
245
+ * ```
246
+ */
247
+ export const spatialHandleNavigation: lng.KeyHandler = function (e) {
248
+ let selected = this.selected;
249
+
250
+ if (typeof selected !== 'number' || !idxInArray(selected, this.children)) {
251
+ selected = findFirstFocusableChildIdx(this as lngp.NavigableElement);
252
+ return selectChild(this as lngp.NavigableElement, selected);
253
+ }
254
+
255
+ const prevChild = this.children[selected]!;
256
+
257
+ const move = { x: 0, y: 0 };
258
+ switch (e.key) {
259
+ case 'ArrowLeft':
260
+ move.x = -1;
261
+ break;
262
+ case 'ArrowRight':
263
+ move.x = 1;
264
+ break;
265
+ case 'ArrowUp':
266
+ move.y = -1;
267
+ break;
268
+ case 'ArrowDown':
269
+ move.y = 1;
270
+ break;
271
+ default:
272
+ return false;
273
+ }
274
+
275
+ const flexDir = this.flexDirection === 'column' ? 'y' : 'x';
276
+ const crossDir = flexDir === 'x' ? 'y' : 'x';
277
+ const flexDelta = move[flexDir];
278
+ const crossDelta = move[crossDir];
279
+
280
+ // Select next/prev child in the current column/row
281
+ if (flexDelta !== 0) {
282
+ for (
283
+ let i = selected + flexDelta;
284
+ idxInArray(i, this.children);
285
+ i += flexDelta
286
+ ) {
287
+ const child = this.children[i]!;
288
+ if (child.skipFocus) continue;
289
+
290
+ // Different column/row
291
+ if (child[crossDir] !== prevChild[crossDir]) break;
292
+
293
+ return selectChild(this as lngp.NavigableElement, i);
294
+ }
295
+ }
296
+ // Find child in next/prev column/row
297
+ else {
298
+ let closestIdx = -1;
299
+ let closestDist = Infinity;
300
+
301
+ for (
302
+ let i = selected + crossDelta;
303
+ idxInArray(i, this.children);
304
+ i += crossDelta
305
+ ) {
306
+ const child = this.children[i]!;
307
+ if (child.skipFocus) continue;
308
+
309
+ // Same column/row, skip
310
+ if (child[crossDir] === prevChild[crossDir]) continue;
311
+
312
+ // Different column/row, check distance
313
+ const distance = Math.abs(child[flexDir] - prevChild[flexDir]);
314
+ if (distance >= closestDist) break; // getting further away
315
+
316
+ closestDist = distance;
317
+ closestIdx = i;
318
+ }
319
+
320
+ return selectChild(this as lngp.NavigableElement, closestIdx);
321
+ }
322
+
323
+ return false;
324
+ };