@solidtv/solid 1.6.7 → 1.6.9

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.7",
3
+ "version": "1.6.9",
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 (
@@ -128,7 +128,13 @@ function createVirtual<T>(
128
128
  cursor: 0,
129
129
  };
130
130
 
131
- if (total <= props.displaySize) {
131
+ // Everything is mounted and the row does not scroll. Not for auto without
132
+ // wrap at exactly displaySize items: there, as for a longer list, the
133
+ // last item ends one slot short of displaySize, so the row scrolls.
134
+ if (
135
+ total < props.displaySize ||
136
+ (total === props.displaySize && (props.wrap || scrollType() !== 'auto'))
137
+ ) {
132
138
  return {
133
139
  start: 0,
134
140
  slice: items() as T[],
@@ -102,6 +102,10 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
102
102
  setCursor(prevStart + idx);
103
103
  if (newRowIndex === prevRowIndex) return;
104
104
 
105
+ // 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;
105
109
  setSlice(items().slice(start(), end()));
106
110
 
107
111
  // this.selected is relative to the slice
@@ -116,7 +120,6 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
116
120
  }
117
121
 
118
122
  queueMicrotask(() => {
119
- const prevRowY = this.y + active.y;
120
123
  this.updateLayout();
121
124
  this.lng.y = prevRowY - active.y;
122
125
  columnScroll(idx, elm, active, lastIdx);
@@ -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]!;