@solidtv/solid 1.4.0 → 1.4.1-0

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.4.0",
3
+ "version": "1.4.1-0",
4
4
  "description": "SolidTV",
5
5
  "type": "module",
6
6
  "exports": {
@@ -448,13 +448,77 @@ const propagateKeyPress = (
448
448
  const DEFAULT_KEY_HOLD_THRESHOLD = 500; // ms
449
449
  const keyHoldTimeouts: { [key: KeyNameOrKeyCode]: number | true } = {};
450
450
 
451
+ const keyOf = (e: KeyboardEvent): KeyNameOrKeyCode => e.key || e.keyCode;
452
+
453
+ // Keys whose auto-repeat key-downs are dropped before propagation, mapped to an
454
+ // optional callback run when the key is finally released.
455
+ //
456
+ // A hold action typically moves focus (opening a context menu, say) while the
457
+ // key is still physically down. Two things then go wrong, and neither can be
458
+ // fixed by the element that owns the hold, because it is no longer in the focus
459
+ // path: the trailing auto-repeats propagate down the *new* focus path and fire
460
+ // whatever just took focus, and the key-up goes there too, so the owner's
461
+ // release handler never runs.
462
+ //
463
+ // Both are propagation concerns, so the latch lives here. The release callback
464
+ // is what lets a suppressor still learn about a key-up it can no longer receive
465
+ // through the focus path.
466
+ const suppressedKeys = new Map<KeyNameOrKeyCode, (() => void) | undefined>();
467
+
468
+ const liftSuppression = (key: KeyNameOrKeyCode): void => {
469
+ const onRelease = suppressedKeys.get(key);
470
+ suppressedKeys.delete(key);
471
+ onRelease?.();
472
+ };
473
+
474
+ /**
475
+ * Drop auto-repeat key-downs for `keyOrEvent` until the key is released.
476
+ *
477
+ * Suppression is lifted by the key's key-up, or by the next fresh (non-repeat)
478
+ * key-down — the latter so platforms that swallow key-up (webOS) can't wedge a
479
+ * key permanently. Non-repeat key-downs are never suppressed. `onRelease` runs
480
+ * when suppression lifts, whichever way it lifts, and is delivered regardless of
481
+ * where focus has moved in the meantime.
482
+ *
483
+ * `useHold` calls this itself when a hold fires; call it directly only when
484
+ * implementing hold behavior outside that primitive.
485
+ */
486
+ export const suppressKeyUntilRelease = (
487
+ keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
488
+ onRelease?: () => void,
489
+ ): void => {
490
+ suppressedKeys.set(
491
+ typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
492
+ onRelease,
493
+ );
494
+ };
495
+
496
+ /**
497
+ * Lift suppression added by {@link suppressKeyUntilRelease} early, running its
498
+ * release callback.
499
+ */
500
+ export const releaseKeySuppression = (
501
+ keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
502
+ ): void => {
503
+ liftSuppression(
504
+ typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
505
+ );
506
+ };
507
+
451
508
  const handleKeyEvents = (
452
509
  delay: number,
453
510
  keydown?: KeyboardEvent,
454
511
  keyup?: KeyboardEvent,
455
512
  ) => {
456
513
  if (keydown) {
457
- const key: KeyNameOrKeyCode = keydown.key || keydown.keyCode;
514
+ const key: KeyNameOrKeyCode = keyOf(keydown);
515
+ if (keydown.repeat) {
516
+ if (suppressedKeys.has(key)) return;
517
+ } else if (suppressedKeys.has(key)) {
518
+ // A fresh press starts a new gesture, so the previous one is over even
519
+ // though its key-up never arrived. Settle it before handling this press.
520
+ liftSuppression(key);
521
+ }
458
522
  const mappedKeyHoldEvent =
459
523
  keyHoldMapEntries[keydown.key] || keyHoldMapEntries[keydown.keyCode];
460
524
  const mappedKeyEvent =
@@ -471,7 +535,11 @@ const handleKeyEvents = (
471
535
 
472
536
  propagateKeyPress(keydown, mappedKeyEvent, false);
473
537
  } else if (keyup) {
474
- const key: KeyNameOrKeyCode = keyup.key || keyup.keyCode;
538
+ const key: KeyNameOrKeyCode = keyOf(keyup);
539
+ // The key is up: whatever was suppressing its repeats is done. Settle it
540
+ // before propagating, so a suppressor that is still in the focus path sees
541
+ // its own release callback rather than a second one via the key-up below.
542
+ if (suppressedKeys.has(key)) liftSuppression(key);
475
543
  const mappedKeyEvent =
476
544
  keyMapEntries[keyup.key] || keyMapEntries[keyup.keyCode];
477
545
  if (keyHoldTimeouts[key] === true) {
@@ -529,5 +597,6 @@ export const useFocusManager = (
529
597
  for (const timeout of Object.values(keyHoldTimeouts)) {
530
598
  if (timeout && timeout !== true) clearTimeout(timeout);
531
599
  }
600
+ suppressedKeys.clear();
532
601
  });
533
602
  };
@@ -2,6 +2,8 @@ export {
2
2
  focusPath,
3
3
  useFocusManager,
4
4
  setActiveElementCore,
5
+ suppressKeyUntilRelease,
6
+ releaseKeySuppression,
5
7
  type KeyMap,
6
8
  type KeyHoldOptions,
7
9
  } from '../core/focusManager.js';
@@ -1,11 +1,41 @@
1
1
  import { createMemo } from 'solid-js';
2
+ import { suppressKeyUntilRelease } from '../core/focusManager.js';
3
+ import type { ElementNode } from '../core/elementNode.js';
4
+
5
+ /**
6
+ * A hold callback. Receives the `KeyboardEvent` that began the press, the
7
+ * element whose handler was invoked, and the focused (leaf) element at that
8
+ * moment — the same arguments a `KeyHandler` gets. All three are `undefined`
9
+ * when `startHold` was called without them.
10
+ */
11
+ export type HoldCallback = (
12
+ e?: KeyboardEvent,
13
+ target?: ElementNode,
14
+ handlerElm?: ElementNode,
15
+ ) => void;
16
+
17
+ export type HoldHandler = (
18
+ e?: KeyboardEvent,
19
+ target?: ElementNode,
20
+ handlerElm?: ElementNode,
21
+ ) => boolean;
2
22
 
3
23
  export type UseHoldProps = {
4
- onHold: () => void;
5
- onEnter: () => void;
6
- onRelease?: () => void;
24
+ onHold: HoldCallback;
25
+ onEnter: HoldCallback;
26
+ onRelease?: HoldCallback;
7
27
  holdThreshold?: number;
8
28
  performOnEnterImmediately?: boolean;
29
+ /**
30
+ * Whether a hold must be confirmed by an auto-repeat key-down.
31
+ *
32
+ * `true` (default): a press with neither key-up nor auto-repeat by the
33
+ * threshold resolves as a tap. `false`: it resolves as a hold, matching the
34
+ * legacy `keyHoldOptions` behavior. Set this to `false` on platforms whose
35
+ * remote input layer delivers no auto-repeat, where a hold gesture would
36
+ * otherwise be unreachable.
37
+ */
38
+ holdRequiresRepeat?: boolean;
9
39
  };
10
40
 
11
41
  /**
@@ -23,10 +53,23 @@ export type UseHoldProps = {
23
53
  * - if neither key-up nor auto-repeat arrives, the timer resolves to a tap →
24
54
  * `onEnter` after `holdThreshold` ms. This is the key-up-independent path that
25
55
  * keeps taps working on webOS, at the cost of ~`holdThreshold` ms latency.
56
+ * Set `holdRequiresRepeat: false` to resolve this ambiguous case as a hold
57
+ * instead, on platforms that deliver no auto-repeat at all.
58
+ *
59
+ * Once `onHold` fires the key is still physically down, so the focus manager
60
+ * drops its remaining auto-repeats until it is released. Without that, a hold
61
+ * that moves focus — the canonical case, opening a context menu — would have
62
+ * its trailing repeats fire whatever it just focused. The same latch delivers
63
+ * `onRelease` even though the key-up now propagates somewhere else entirely.
26
64
  *
27
65
  * `performOnEnterImmediately` keeps the legacy behavior of firing `onEnter` on
28
66
  * key-down; a long-press then fires both `onEnter` and `onHold`.
29
67
  *
68
+ * Note that `startHold` returns `true`, which ends the bubble phase: ancestor
69
+ * handlers for that key will not run, and whether the press was a tap isn't
70
+ * known until after propagation is over. If an ancestor performs navigation,
71
+ * invoke it from `onEnter` yourself. See the caveat in the docs.
72
+ *
30
73
  * @example
31
74
  * const [holdRight, releaseRight] = useHold({
32
75
  * onHold: handleHoldRight,
@@ -40,21 +83,31 @@ export type UseHoldProps = {
40
83
  * onRightRelease={releaseRight}
41
84
  * />
42
85
  *
86
+ * `startHold` reads `e.repeat` to detect a hold. Used directly as a `KeyHandler`
87
+ * (above) it gets the event; if you wrap it, forward all of the arguments, or
88
+ * hold detection silently never triggers.
89
+ *
43
90
  * @param {UseHoldProps} props - The properties for configuring the hold behavior.
44
- * @returns {[(e?: KeyboardEvent) => boolean, () => boolean]} A tuple containing `startHold` and `releaseHold` functions.
91
+ * @returns {[HoldHandler, HoldHandler]} A tuple of `startHold` and `releaseHold`.
45
92
  */
46
93
 
47
- export function useHold(props: UseHoldProps) {
94
+ export function useHold(props: UseHoldProps): [HoldHandler, HoldHandler] {
48
95
  const holdThreshold = createMemo(() => props.holdThreshold ?? 500);
49
96
  const performOnEnterImmediately = createMemo(
50
97
  () => props.performOnEnterImmediately ?? false,
51
98
  );
99
+ const holdRequiresRepeat = createMemo(() => props.holdRequiresRepeat ?? true);
52
100
 
53
101
  let holdTimeout = -1;
54
102
  let enterFired = false; // onEnter already fired for this press
55
103
  let holdFired = false; // onHold already fired for this press
56
104
  let repeated = false; // an auto-repeat key-down was seen (key still held)
57
105
 
106
+ // Context from the key-down that began the press, replayed to whichever
107
+ // callback resolves it — the timer path has no event of its own, and after a
108
+ // focus-moving hold neither does the release.
109
+ let press: Parameters<HoldCallback> = [];
110
+
58
111
  const reset = () => {
59
112
  if (holdTimeout !== -1) {
60
113
  clearTimeout(holdTimeout);
@@ -63,9 +116,17 @@ export function useHold(props: UseHoldProps) {
63
116
  enterFired = false;
64
117
  holdFired = false;
65
118
  repeated = false;
119
+ press = [];
66
120
  };
67
121
 
68
- const startHold = (e?: KeyboardEvent) => {
122
+ const fireRelease = () => {
123
+ if (!holdFired) return;
124
+ const args = press;
125
+ reset(); // before the callback, so a re-entrant press isn't clobbered
126
+ props.onRelease?.(...args);
127
+ };
128
+
129
+ const startHold: HoldHandler = (e, target, handlerElm) => {
69
130
  // Auto-repeat key-down: the key is still held. Record it so the timer
70
131
  // resolves to a hold even if the key-up event never arrives (webOS).
71
132
  if (e?.repeat) {
@@ -76,30 +137,35 @@ export function useHold(props: UseHoldProps) {
76
137
  // Fresh key-down begins a new press. Reset first so a previous press whose
77
138
  // key-up was never delivered doesn't leave us wedged for this one.
78
139
  reset();
140
+ press = [e, target, handlerElm];
79
141
 
80
142
  if (performOnEnterImmediately()) {
81
143
  enterFired = true;
82
- props.onEnter();
144
+ props.onEnter(...press);
83
145
  }
84
146
 
85
147
  holdTimeout = setTimeout(() => {
86
148
  holdTimeout = -1;
87
- if (repeated) {
149
+ if (repeated || !holdRequiresRepeat()) {
88
150
  // Held past the threshold → hold gesture.
89
151
  holdFired = true;
90
- props.onHold();
152
+ // The key is still down and will keep repeating. Drop those repeats so
153
+ // they can't reach whatever onHold focuses, and route the key-up back
154
+ // here even if this element is no longer in the focus path.
155
+ if (e) suppressKeyUntilRelease(e, fireRelease);
156
+ props.onHold(...press);
91
157
  } else if (!enterFired) {
92
158
  // No key-up and no auto-repeat arrived: resolve as a tap so the
93
159
  // primary action still fires on remotes that swallow key-up.
94
160
  enterFired = true;
95
- props.onEnter();
161
+ props.onEnter(...press);
96
162
  }
97
163
  }, holdThreshold()) as unknown as number;
98
164
 
99
165
  return true;
100
166
  };
101
167
 
102
- const releaseHold = () => {
168
+ const releaseHold: HoldHandler = (e, target, handlerElm) => {
103
169
  if (holdTimeout !== -1) {
104
170
  // Released before the threshold → tap. Fires immediately where key-up is
105
171
  // delivered, avoiding the timer latency.
@@ -107,12 +173,19 @@ export function useHold(props: UseHoldProps) {
107
173
  holdTimeout = -1;
108
174
  if (!enterFired) {
109
175
  enterFired = true;
110
- props.onEnter();
176
+ props.onEnter(
177
+ e ?? press[0],
178
+ target ?? press[1],
179
+ handlerElm ?? press[2],
180
+ );
111
181
  }
112
- } else if (holdFired) {
113
- props.onRelease?.();
182
+ reset();
183
+ } else {
184
+ // After a hold, the suppression latch has usually already fired onRelease
185
+ // and reset — `holdFired` is false and this is a no-op.
186
+ fireRelease();
187
+ reset();
114
188
  }
115
- reset();
116
189
  return true;
117
190
  };
118
191