@solidtv/solid 1.7.1 → 1.7.3

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.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "SolidTV",
5
5
  "type": "module",
6
6
  "exports": {
@@ -73,6 +73,17 @@ export interface Config {
73
73
  lockStyles?: boolean;
74
74
  fontWeightAlias?: Record<string, number | string>;
75
75
  throttleInput?: number;
76
+ /**
77
+ * For devices that never set `KeyboardEvent.repeat`: while a key is held
78
+ * they send a stream of `repeat=false` key-downs, and one key-up on release.
79
+ * With this set (in ms), a key-down of a key that has had no key-up since
80
+ * its previous key-down, arriving within this many ms of it, is marked as a
81
+ * repeat (`repeat` reads `true`) before anything handles it. It must cover
82
+ * the device's initial auto-repeat delay. A key-up that never arrives stops
83
+ * mattering once the window passes. Off by default: on a key that swallows
84
+ * key-up (webOS OK), a second tap inside the window would read as a repeat.
85
+ */
86
+ inferKeyRepeat?: number;
76
87
  /**
77
88
  * Call `preventDefault()` on every key event the app consumed: a handler
78
89
  * returned `true`, or the focus manager dropped the press itself (a
@@ -334,7 +334,12 @@ function updateNodeStyles(node: DOMNode | DOMText) {
334
334
 
335
335
  if (props.alpha !== 1) style += `opacity: ${props.alpha};`;
336
336
 
337
- if (props.clipping) {
337
+ if (Array.isArray(props.clipping)) {
338
+ // [top, right, bottom, left] margins grow the clip box past the node's
339
+ // bounds, as in the WebGL renderer. Legacy `clip` (every node is absolute)
340
+ const [t, r, b, l] = props.clipping;
341
+ style += `clip: rect(${-t}px, ${props.w + r}px, ${props.h + b}px, ${-l}px);`;
342
+ } else if (props.clipping) {
338
343
  style += `overflow: hidden;`;
339
344
  }
340
345
 
@@ -488,18 +488,62 @@ type Suppression = {
488
488
  // key differently still finds it. Entries for one key share a Suppression.
489
489
  const suppressedKeys = new Map<KeyNameOrKeyCode, Suppression>();
490
490
 
491
- // The suppression of an event's key: looked up under its name, then its
492
- // keyCode (keyNameId, keyCodeId), either of which may be undefined.
491
+ // An event's entry in a map keyed by key identity: looked up under its name,
492
+ // then its keyCode (keyNameId, keyCodeId), either of which may be undefined.
493
+ const findByIdentity = <T>(
494
+ map: Map<KeyNameOrKeyCode, T>,
495
+ name: KeyNameOrKeyCode | undefined,
496
+ code: KeyNameOrKeyCode | undefined,
497
+ ): T | undefined => {
498
+ let found: T | undefined;
499
+ if (name !== undefined) found = map.get(name);
500
+ if (found === undefined && code !== undefined) found = map.get(code);
501
+ return found;
502
+ };
503
+
493
504
  const findSuppression = (
494
505
  name: KeyNameOrKeyCode | undefined,
495
506
  code: KeyNameOrKeyCode | undefined,
496
- ): Suppression | undefined => {
497
- let found: Suppression | undefined;
498
- if (name !== undefined) found = suppressedKeys.get(name);
499
- if (found === undefined && code !== undefined) {
500
- found = suppressedKeys.get(code);
507
+ ): Suppression | undefined => findByIdentity(suppressedKeys, name, code);
508
+
509
+ // Keys down with no key-up yet, for Config.inferKeyRepeat: when each last sent
510
+ // a key-down. Indexed under every identity like suppressedKeys, so a key-up
511
+ // naming the key differently (webOS Back) still ends the press.
512
+ type HeldKey = { ids: KeyNameOrKeyCode[]; time: number };
513
+ const heldKeys = new Map<KeyNameOrKeyCode, HeldKey>();
514
+
515
+ // Marks a key-down as a repeat when it follows a key-down of the same key,
516
+ // with no key-up between, by less than `windowMs`. KeyboardEvent.repeat is a
517
+ // read-only getter; an own property shadows it, so handlers, useHold and the
518
+ // suppression check below all read the same value.
519
+ const inferRepeat = (
520
+ e: KeyboardEvent,
521
+ name: KeyNameOrKeyCode | undefined,
522
+ code: KeyNameOrKeyCode | undefined,
523
+ windowMs: number,
524
+ ): void => {
525
+ const now = performance.now();
526
+ const held = findByIdentity(heldKeys, name, code);
527
+ if (held === undefined) {
528
+ const ids = keyIdentities(e);
529
+ const entry: HeldKey = { ids, time: now };
530
+ for (let i = 0; i < ids.length; i++) heldKeys.set(ids[i]!, entry);
531
+ return;
501
532
  }
502
- return found;
533
+ if (!e.repeat && now - held.time < windowMs) {
534
+ Object.defineProperty(e, 'repeat', { value: true });
535
+ }
536
+ held.time = now;
537
+ };
538
+
539
+ const releaseHeldKey = (
540
+ name: KeyNameOrKeyCode | undefined,
541
+ code: KeyNameOrKeyCode | undefined,
542
+ ): void => {
543
+ const held = findByIdentity(heldKeys, name, code);
544
+ if (!held) return;
545
+ const ids = held.ids;
546
+ for (let i = 0; i < ids.length; i++) heldKeys.delete(ids[i]!);
503
547
  };
504
548
 
505
549
  const liftSuppression = (found: Suppression | undefined): void => {
@@ -559,6 +603,9 @@ const handleKeyEvents = (
559
603
  if (keydown) {
560
604
  const name = keyNameId(keydown);
561
605
  const code = keyCodeId(keydown);
606
+ if (Config.inferKeyRepeat) {
607
+ inferRepeat(keydown, name, code, Config.inferKeyRepeat);
608
+ }
562
609
  if (keydown.repeat) {
563
610
  if (findSuppression(name, code)) return true;
564
611
  } else {
@@ -576,7 +623,10 @@ const handleKeyEvents = (
576
623
  // The key is up: whatever was suppressing its repeats is done. Settle it
577
624
  // before propagating, so a suppressor that is still in the focus path sees
578
625
  // its own release callback rather than a second one via the key-up below.
579
- liftSuppression(findSuppression(keyNameId(keyup), keyCodeId(keyup)));
626
+ const name = keyNameId(keyup);
627
+ const code = keyCodeId(keyup);
628
+ releaseHeldKey(name, code);
629
+ liftSuppression(findSuppression(name, code));
580
630
 
581
631
  return propagateKeyPress(
582
632
  keyup,
@@ -696,5 +746,6 @@ export const useFocusManager = (
696
746
  eventTarget.removeEventListener('keydown', keyPressHandler);
697
747
  eventTarget.removeEventListener('keyup', keyUpHandler);
698
748
  suppressedKeys.clear();
749
+ heldKeys.clear();
699
750
  });
700
751
  };
@@ -147,7 +147,13 @@ function createVirtual<T>(
147
147
  const total = itemCount();
148
148
  if (total === 0) return applySlice(0, noItems, 0, delta, 0, true, 0);
149
149
 
150
- if (total <= props.displaySize) {
150
+ // Everything is mounted and the row does not scroll. Not for auto without
151
+ // wrap at exactly displaySize items: there, as for a longer list, the
152
+ // last item ends one slot short of displaySize, so the row scrolls.
153
+ if (
154
+ total < props.displaySize ||
155
+ (total === props.displaySize && (props.wrap || scrollType() !== 'auto'))
156
+ ) {
151
157
  const clamped = utils.clamp(c, 0, total - 1);
152
158
  return applySlice(
153
159
  0,
@@ -254,9 +260,12 @@ function createVirtual<T>(
254
260
  } else {
255
261
  // Initial setup
256
262
  if (c > 0) {
263
+ // Near the end, the window navigation reaches there: it starts
264
+ // at total - displaySize. Subtracting bufferSize as well put
265
+ // the focused item past the last visible slot.
257
266
  start = Math.min(
258
267
  c - (scrollIndex() || 1),
259
- total - props.displaySize - bufferSize(),
268
+ total - props.displaySize,
260
269
  );
261
270
  selected = Math.max(scrollIndex() || 1, c - start);
262
271
  shiftBy = total - c < 3 ? c - total : -1;
@@ -313,7 +322,8 @@ function createVirtual<T>(
313
322
  if (delta === 0 && c > 0) {
314
323
  //initial setup
315
324
  selected = c > startScrolling ? startScrolling : c;
316
- start = Math.max(0, c - startScrolling + 1);
325
+ // The window slot `selected` holds item c (start was one more).
326
+ start = c - selected;
317
327
  shiftBy = c > startScrolling ? -1 : 0;
318
328
  atStart = c < startScrolling;
319
329
  } else if (delta > 0) {
@@ -492,6 +502,7 @@ function createVirtual<T>(
492
502
  } else {
493
503
  view.lng[axis] =
494
504
  shiftPrevChildPos - active[axis] + childSize * slice.shiftBy;
505
+ targetPosition = view.lng[axis];
495
506
  }
496
507
  }
497
508
 
@@ -573,14 +584,20 @@ function createVirtual<T>(
573
584
  viewRef.children[activeIndex]?.setFocus();
574
585
  }
575
586
 
576
- if (shiftBy === 0) return;
577
-
578
- const childSize = computeSize(slice.selected);
579
587
  // Original Position is offset to support scrollToIndex
580
- originalPosition = originalPosition ?? viewRef.lng[axis];
581
- targetPosition = targetPosition ?? viewRef.lng[axis];
582
-
583
- viewRef.lng[axis] = (viewRef.lng[axis] || 0) + childSize * -1;
588
+ originalPosition = originalPosition ?? viewRef.lng[axis] ?? 0;
589
+ // The window is laid out from scratch (delta 0), so the row goes to
590
+ // its original position, one slot further with a shift. Shifting from
591
+ // the current position moved an already scrolled row a second slot.
592
+ let position: number = originalPosition;
593
+ if (shiftBy !== 0) position -= computeSize(slice.selected);
594
+ if (cachedAnimationController?.state === 'running') {
595
+ cachedAnimationController.stop();
596
+ }
597
+ viewRef.lng[axis] = position;
598
+ // The next press shifts from targetPosition: left at the position
599
+ // before the shift, it moved the row one slot too far.
600
+ targetPosition = position;
584
601
  });
585
602
  };
586
603
 
@@ -617,6 +634,7 @@ function createVirtual<T>(
617
634
  c = Math.max(0, itemCount() - 1);
618
635
  setCursor(c);
619
636
  }
637
+ slice.slice = noItems;
620
638
  viewRef.selected = computeSlice(c, 0).selected;
621
639
  }),
622
640
  );
@@ -121,6 +121,10 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
121
121
  setCursor(prevStart + idx);
122
122
  if (newRowIndex === prevRowIndex) return;
123
123
 
124
+ // Read before the window changes: the post-mutation flex pass can run
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;
124
128
  setSlice(items().slice(start(), end()));
125
129
 
126
130
  // this.selected is relative to the slice
@@ -149,14 +153,14 @@ export function VirtualGrid<T>(props: VirtualGridProps<T>): s.JSX.Element {
149
153
  let shiftActive!: lng.ElementNode;
150
154
  let shiftIdx = 0;
151
155
  let shiftLastIdx: number | undefined;
156
+ let shiftPrevRowY = 0;
152
157
  function applyShift() {
153
158
  const view = shiftView;
154
159
  const active = shiftActive;
155
- const prevRowY = view.y + active.y;
156
160
  // One flex pass per window shift: lay out here only if the post-mutation
157
161
  // layout has not already run since the window changed.
158
162
  if (layoutPending) view.updateLayout();
159
- view.lng.y = prevRowY - active.y;
163
+ view.lng.y = shiftPrevRowY - active.y;
160
164
  columnScroll(shiftIdx, view, active, shiftLastIdx);
161
165
  }
162
166