@solidtv/solid 1.6.8 → 1.6.10

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solidtv/solid",
3
- "version": "1.6.8",
3
+ "version": "1.6.10",
4
4
  "description": "SolidTV",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,5 +1,5 @@
1
1
  import { createSignal, createMemo, createEffect, JSX, untrack, Index } from "solid-js";
2
- import { type NodeProps, ElementNode, NewOmit, hasFocus } from "@solidtv/solid";
2
+ import { type NodeProps, type INode, ElementNode, NewOmit, hasFocus } from "@solidtv/solid";
3
3
  import { chainFunctions, chainRefs } from "./utils/chainFunctions.js";
4
4
 
5
5
  export interface GridItemProps<T> {
@@ -19,7 +19,13 @@ export interface GridProps<T> extends NewOmit<NodeProps, 'children'> {
19
19
  itemOffset?: number;
20
20
  columns?: number;
21
21
  looping?: boolean;
22
- scroll?: "auto" | "none";
22
+ /** How the grid follows focus vertically:
23
+ * auto - the focused row moves to the top (default)
24
+ * edge - the grid moves only when focus goes past the last row that fits
25
+ * (above the screen's bottom, or a clipping parent's) or above the first
26
+ * row shown; it then moves just enough to show that row
27
+ * none - the grid does not move */
28
+ scroll?: "auto" | "edge" | "none";
23
29
  selected?: number;
24
30
  onSelectedChanged?: (index: number, grid: ElementNode, elm?: ElementNode) => void;
25
31
  }
@@ -114,9 +120,29 @@ export function Grid<T>(props: GridProps<T>): JSX.Element {
114
120
  });
115
121
  }
116
122
 
117
- const scrollY = createMemo(() =>
118
- props.scroll === "none" ? props.y ?? 0 : -Math.floor(focusedIndex() / columns()) * totalHeight() + (props.y || 0)
119
- );
123
+ // edge: the rows that fit between the grid's unscrolled top and the bottom
124
+ // of its clipping parent, or of the screen. At least one.
125
+ function visibleRows() {
126
+ const parent = gridRef.parent;
127
+ const parentTop = parent?.absY ?? 0;
128
+ const top = parentTop + (props.y ?? 0);
129
+ const bottom = parent?.clipping
130
+ ? parentTop + (parent.height ?? 0)
131
+ : (gridRef.lng as INode).stage.root.h;
132
+ return Math.max(1, Math.floor((bottom - top + (props.itemOffset ?? 0)) / totalHeight()));
133
+ }
134
+
135
+ // edge: the first row shown.
136
+ let topRow = 0;
137
+ const scrollY = createMemo(() => {
138
+ const y = props.y ?? 0;
139
+ if (props.scroll === "none") return y;
140
+ const row = Math.floor(focusedIndex() / columns());
141
+ if (props.scroll !== "edge") return -row * totalHeight() + y;
142
+ if (row < topRow) topRow = row;
143
+ else if (row > topRow) topRow = Math.max(topRow, row - visibleRows() + 1);
144
+ return -topRow * totalHeight() + y;
145
+ });
120
146
 
121
147
  let gridRef!: ElementNode;
122
148
  return (
@@ -52,15 +52,25 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
52
52
  return Math.min(items().length, rawEnd);
53
53
  });
54
54
 
55
- const [slice, setSlice] = s.createSignal(items().slice(start(), end()));
55
+ const [slice, setSliceSignal] = s.createSignal(items().slice(start(), end()));
56
+ // Set when the window's items change, cleared by the grid's next flex pass
57
+ // (onLayout): a window shift lays the grid out only if nothing else has.
58
+ let layoutPending = false;
59
+ function setSlice(window: T[]) {
60
+ layoutPending = true;
61
+ setSliceSignal(window);
62
+ }
56
63
 
57
64
  let viewRef!: lngp.NavigableElement;
58
65
 
59
66
  function onVerticalNav(dir: -1 | 1): lngp.KeyHandler {
60
67
  return function () {
61
68
  const perRow = itemsPerRow();
69
+ const count = items().length;
62
70
  const currentRowIndex = Math.floor(cursor() / perRow);
63
- const maxRows = Math.floor(items().length / perRow);
71
+ // B15: the last row's index. floor(length / perRow) was one row late
72
+ // when the length is a multiple of perRow.
73
+ const maxRows = Math.floor((count - 1) / perRow);
64
74
 
65
75
  if (
66
76
  currentRowIndex === 0 && dir === -1
@@ -69,19 +79,21 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
69
79
 
70
80
  const selected = this.selected || 0;
71
81
  const offset = dir * perRow;
72
- const newIndex = utils.clamp(selected + offset, 0, items().length - 1);
73
- const lastIdx = selected;
74
- this.selected = newIndex;
75
- const active = this.children[this.selected];
82
+ const newIndex = utils.clamp(selected + offset, 0, count - 1);
83
+ const active = this.children[newIndex];
76
84
 
85
+ // B15: `selected` changes only when there is a child to move to. It was
86
+ // written first, so a Down with nothing below left it past the mounted
87
+ // children and the next Up was lost.
77
88
  if (active instanceof lng.ElementNode) {
89
+ this.selected = newIndex;
78
90
  active.setFocus();
79
91
  chainedOnSelectedChanged.call(
80
92
  this as lngp.NavigableElement,
81
- this.selected,
93
+ newIndex,
82
94
  this as lngp.NavigableElement,
83
95
  active,
84
- lastIdx
96
+ selected
85
97
  );
86
98
  return true;
87
99
  }
@@ -91,9 +103,16 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
91
103
  const onUp = onVerticalNav(-1);
92
104
  const onDown = onVerticalNav(1);
93
105
 
106
+ // B15: set by applySelected for the call that applies an initial
107
+ // `selected`. The app gets lastIdx === idx there (as Row and Column report
108
+ // a mount selection); internally there is no previous selection, so the
109
+ // scroll to that row runs.
110
+ let forceRowScroll = false;
111
+
94
112
  const onSelectedChanged: lngp.OnSelectedChanged = function (_idx, elm, active, _lastIdx,) {
95
113
  let idx = _idx;
96
- let lastIdx = _lastIdx;
114
+ let lastIdx = forceRowScroll ? undefined : _lastIdx;
115
+ forceRowScroll = false;
97
116
  const perRow = itemsPerRow();
98
117
  const newRowIndex = Math.floor(idx / perRow);
99
118
  const prevRowIndex = Math.floor((lastIdx || 0) / perRow);
@@ -103,9 +122,9 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
103
122
  if (newRowIndex === prevRowIndex) return;
104
123
 
105
124
  // Read before the window changes: the post-mutation flex pass can run
106
- // before the microtask below, and active.y read there is already the new
107
- // layout, so the rows jumped a row instead of animating once the window slid.
108
- const prevRowY = this.y + active.y;
125
+ // before applyShift, and active.y read there is already the new layout,
126
+ // so the rows jumped a row instead of animating once the window slid.
127
+ shiftPrevRowY = this.y + active.y;
109
128
  setSlice(items().slice(start(), end()));
110
129
 
111
130
  // this.selected is relative to the slice
@@ -119,15 +138,51 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
119
138
  props.onEndReached?.();
120
139
  }
121
140
 
122
- queueMicrotask(() => {
123
- this.updateLayout();
124
- this.lng.y = prevRowY - active.y;
125
- columnScroll(idx, elm, active, lastIdx);
126
- });
141
+ // `elm` is the grid (`this`): navigation calls it on itself.
142
+ shiftView = elm;
143
+ shiftActive = active;
144
+ shiftIdx = idx;
145
+ shiftLastIdx = lastIdx;
146
+ queueMicrotask(applyShift);
127
147
  };
128
148
 
149
+ // The scroll after a row change, run in a microtask once the new window is
150
+ // mounted. One function for every press (no closure per press): the press
151
+ // stores what it needs here.
152
+ let shiftView!: lngp.NavigableElement;
153
+ let shiftActive!: lng.ElementNode;
154
+ let shiftIdx = 0;
155
+ let shiftLastIdx: number | undefined;
156
+ let shiftPrevRowY = 0;
157
+ function applyShift() {
158
+ const view = shiftView;
159
+ const active = shiftActive;
160
+ // One flex pass per window shift: lay out here only if the post-mutation
161
+ // layout has not already run since the window changed.
162
+ if (layoutPending) view.updateLayout();
163
+ view.lng.y = shiftPrevRowY - active.y;
164
+ columnScroll(shiftIdx, view, active, shiftLastIdx);
165
+ }
166
+
129
167
  const chainedOnSelectedChanged = lngp.chainFunctions(props.onSelectedChanged, onSelectedChanged)!;
130
168
 
169
+ // B15: an initial `selected` has not been applied (scrolled to) yet. It
170
+ // stays pending through runs that find no child (items still loading, or
171
+ // waiting for onEndReached) and is cleared by the run that applies it.
172
+ let initialSelectedPending = s.untrack(() => props.selected != null);
173
+
174
+ function applySelected(active: lng.ElementNode, lastSelected: number) {
175
+ const idx = viewRef.selected;
176
+ if (initialSelectedPending) {
177
+ initialSelectedPending = false;
178
+ forceRowScroll = true;
179
+ lastSelected = idx;
180
+ }
181
+ chainedOnSelectedChanged.call(viewRef, idx, viewRef, active, lastSelected);
182
+ // Not consumed when the app's handler returned true and stopped the chain.
183
+ forceRowScroll = false;
184
+ }
185
+
131
186
  let cachedSelected: number | undefined;
132
187
  const updateSelected = ([selected, _items]: [number?, any?]) => {
133
188
  if (!viewRef || selected == null) return;
@@ -153,20 +208,20 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
153
208
  // force focus as scrollToIndex is manually called
154
209
  active.setFocus();
155
210
  }
156
- chainedOnSelectedChanged.call(viewRef, viewRef.selected, viewRef, active, lastSelected);
211
+ applySelected(active, lastSelected);
157
212
  } else {
158
213
  setCursor(selected);
159
214
  setSlice(items().slice(start(), end()));
160
215
 
161
216
  queueMicrotask(() => {
162
- viewRef.updateLayout();
217
+ if (layoutPending) viewRef.updateLayout();
163
218
  active = viewRef.children.find(x => x.item === item);
164
219
  if (active instanceof lng.ElementNode) {
165
220
  viewRef.selected = viewRef.children.indexOf(active);
166
221
  if (lng.hasFocus(viewRef)) {
167
222
  active.setFocus();
168
223
  }
169
- chainedOnSelectedChanged.call(viewRef, viewRef.selected, viewRef, active, lastSelected);
224
+ applySelected(active, lastSelected);
170
225
  }
171
226
  });
172
227
  }
@@ -205,13 +260,28 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
205
260
  );
206
261
 
207
262
 
208
- return (
263
+ // Clears layoutPending: the grid's flex pass has run since the window
264
+ // changed. The app's onLayout still runs, read when called.
265
+ function onLayout(this: lng.ElementNode, target: lng.ElementNode) {
266
+ layoutPending = false;
267
+ return props.onLayout?.call(this, target);
268
+ }
269
+
270
+ // B15: `selected` on the node is a child index into the window, set once
271
+ // here and then by navigation and the selected effect. Passing the data
272
+ // index (`props.selected`) made the first forwardFocus pick the wrong child,
273
+ // and its reactive rewrite hid the previous child index from updateSelected.
274
+ const initialSelected = s.untrack(() =>
275
+ Math.max(0, (props.selected || 0) - start()),
276
+ );
277
+
278
+ const view = (
209
279
  <view
210
280
  {...props}
211
281
  scroll={props.scroll || 'always'}
212
282
  ref={lngp.chainRefs(el => { viewRef = el as lngp.NavigableElement; }, props.ref)}
213
- selected={props.selected || 0}
214
- cursor={cursor()}
283
+ selected={/* @once */ initialSelected}
284
+ onLayout={/* @once */ onLayout}
215
285
  onLeft={/* @once */ lngp.chainFunctions(props.onLeft, lngp.navigableHandleNavigation)}
216
286
  onRight={/* @once */ lngp.chainFunctions(props.onRight, lngp.navigableHandleNavigation)}
217
287
  onUp={/* @once */ lngp.chainFunctions(props.onUp, onUp)}
@@ -225,4 +295,12 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
225
295
  <List each={slice()}>{props.children}</List>
226
296
  </view>
227
297
  );
298
+
299
+ // `cursor` has an effect of its own: in the spread above, a cursor change
300
+ // (every press) re-ran every prop of the spread.
301
+ s.createRenderEffect(() => {
302
+ viewRef.cursor = cursor();
303
+ });
304
+
305
+ return view;
228
306
  }
@@ -21,9 +21,16 @@ interface MouseStateOptions {
21
21
  pressedState: CustomState;
22
22
  }
23
23
 
24
- type UseMouseOptions =
25
- | { customStates: MouseStateOptions }
26
- | { customStates: undefined };
24
+ interface UseMouseOptions {
25
+ customStates?: MouseStateOptions;
26
+ /**
27
+ * Hit-test children that draw outside their parent's bounds. A subtree is
28
+ * cut off only where a node has `clipping` set; otherwise the search keeps
29
+ * descending even when the pointer misses the node itself. Off by default:
30
+ * the default search stops at any node the pointer misses.
31
+ */
32
+ respectClipping?: boolean;
33
+ }
27
34
 
28
35
  export function addCustomStateToElement(
29
36
  element: RenderableNode,
@@ -133,8 +140,9 @@ function findElementWithCustomState<TApp extends ElementNode>(
133
140
  x: number,
134
141
  y: number,
135
142
  customState: CustomState,
143
+ respectClipping: boolean,
136
144
  ): ElementNode | undefined {
137
- const path = getChildrenByPosition(myApp, x, y);
145
+ const path = getChildrenByPosition(myApp, x, y, respectClipping);
138
146
  let element: ElementNode | undefined;
139
147
  for (let i = path.length - 1; i >= 0; i--) {
140
148
  if (hasCustomState(path[i]!, customState)) {
@@ -235,6 +243,7 @@ function handleElementClick(
235
243
 
236
244
  function createHandleClick<TApp extends ElementNode>(
237
245
  myApp: TApp,
246
+ respectClipping: boolean,
238
247
  customStates?: MouseStateOptions,
239
248
  pressedElementRef?: { current: ElementNode | null },
240
249
  ) {
@@ -245,6 +254,7 @@ function createHandleClick<TApp extends ElementNode>(
245
254
  e.clientX,
246
255
  e.clientY,
247
256
  customStates.hoverState,
257
+ respectClipping,
248
258
  )
249
259
  : findElementByActiveElement(e);
250
260
 
@@ -258,6 +268,7 @@ function createHandleClick<TApp extends ElementNode>(
258
268
 
259
269
  function createHandleMouseDown<TApp extends ElementNode>(
260
270
  myApp: TApp,
271
+ respectClipping: boolean,
261
272
  customStates?: MouseStateOptions,
262
273
  pressedElementRef?: { current: ElementNode | null },
263
274
  ) {
@@ -271,6 +282,7 @@ function createHandleMouseDown<TApp extends ElementNode>(
271
282
  e.clientX,
272
283
  e.clientY,
273
284
  customStates.hoverState,
285
+ respectClipping,
274
286
  );
275
287
 
276
288
  if (!pressedElement) {
@@ -318,16 +330,64 @@ function isNodeAtPosition(
318
330
  );
319
331
  }
320
332
 
333
+ // Depth-first search for the topmost hit, used when `respectClipping` is on.
334
+ // Descends through nodes the pointer misses unless they clip; pushes only
335
+ // nodes the pointer is over onto `path` (ancestors first). Returns whether
336
+ // anything in this subtree was hit.
337
+ function collectHitPath(
338
+ node: ElementNode | ElementText | TextNode,
339
+ px: number,
340
+ py: number,
341
+ path: ElementNode[],
342
+ ): boolean {
343
+ if (!isElementNode(node) || node.alpha === 0 || node.skipFocus) {
344
+ return false;
345
+ }
346
+
347
+ const hit = testCollision(
348
+ px,
349
+ py,
350
+ (node.lng.absX as number) || 0,
351
+ (node.lng.absY as number) || 0,
352
+ node.width || 0,
353
+ node.height || 0,
354
+ );
355
+ if (!hit && node.clipping) return false;
356
+ if (hit) path.push(node);
357
+
358
+ // Topmost first: higher zIndex, then later sibling (as in the default
359
+ // search). Index tiebreak, since old webOS engines have no stable sort.
360
+ const children = node.children;
361
+ const order: number[] = [];
362
+ for (let i = children.length - 1; i >= 0; i--) order.push(i);
363
+ order.sort(
364
+ (a, b) =>
365
+ ((children[b] as ElementNode).zIndex ?? -1) -
366
+ ((children[a] as ElementNode).zIndex ?? -1) || b - a,
367
+ );
368
+ for (const i of order) {
369
+ if (collectHitPath(children[i]!, px, py, path)) return true;
370
+ }
371
+
372
+ return hit;
373
+ }
374
+
321
375
  function getChildrenByPosition<TElement extends ElementNode = ElementNode>(
322
376
  node: TElement,
323
377
  x: number,
324
378
  y: number,
379
+ respectClipping = false,
325
380
  ): TElement[] {
326
381
  const result: TElement[] = [];
327
382
  const precision = Config.rendererOptions?.deviceLogicalPixelRatio || 1;
328
383
  const px = x / precision;
329
384
  const py = y / precision;
330
385
 
386
+ if (respectClipping) {
387
+ collectHitPath(node, px, py, result);
388
+ return result;
389
+ }
390
+
331
391
  let current: ElementNode | ElementText | TextNode | undefined = node;
332
392
  while (current && isNodeAtPosition(current, px, py)) {
333
393
  result.push(current as TElement);
@@ -360,9 +420,16 @@ export function useMouse<TApp extends ElementNode = ElementNode>(
360
420
  const pressedElementRef: { current: ElementNode | null } = { current: null };
361
421
  const customStates = options?.customStates;
362
422
  const hoverState = customStates?.hoverState;
363
- const handleClick = createHandleClick(myApp, customStates, pressedElementRef);
423
+ const respectClipping = options?.respectClipping ?? false;
424
+ const handleClick = createHandleClick(
425
+ myApp,
426
+ respectClipping,
427
+ customStates,
428
+ pressedElementRef,
429
+ );
364
430
  const handleMouseDown = createHandleMouseDown(
365
431
  myApp,
432
+ respectClipping,
366
433
  customStates,
367
434
  pressedElementRef,
368
435
  );
@@ -382,7 +449,7 @@ export function useMouse<TApp extends ElementNode = ElementNode>(
382
449
  createEffect(() => {
383
450
  if (!scheduled()) return;
384
451
 
385
- const path = getChildrenByPosition(myApp, pos.x, pos.y);
452
+ const path = getChildrenByPosition(myApp, pos.x, pos.y, respectClipping);
386
453
  let activeElm: ElementNode | undefined;
387
454
  for (let i = path.length - 1; i >= 0; i--) {
388
455
  const el = path[i]!;