@solidtv/solid 1.4.0 → 1.4.1-1

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-1",
4
4
  "description": "SolidTV",
5
5
  "type": "module",
6
6
  "exports": {
@@ -448,13 +448,116 @@ 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 this platform has actually been seen delivering a key-up for.
454
+ //
455
+ // Whether a hold can be detected by timer alone comes down to this. If a key
456
+ // delivers key-up, reaching the hold threshold without one means the key is
457
+ // genuinely still down, and no auto-repeat is needed to confirm the hold. If it
458
+ // does not (webOS OK), a press that is already over is indistinguishable from
459
+ // one still held, so only an auto-repeat can confirm it.
460
+ //
461
+ // This is per-key, not per-platform: webOS emits auto-repeat for OK but not for
462
+ // Back, and swallows key-up for OK but not for Back.
463
+ const keysSeenReleasing = new Set<KeyNameOrKeyCode>();
464
+
465
+ /**
466
+ * Record that `keyOrEvent` delivered a key-up. The focus manager calls this for
467
+ * every key-up it sees; call it directly only when driving key events without
468
+ * {@link useFocusManager}.
469
+ */
470
+ export const noteKeyUpDelivered = (
471
+ keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
472
+ ): void => {
473
+ keysSeenReleasing.add(
474
+ typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
475
+ );
476
+ };
477
+
478
+ /**
479
+ * Whether this key has been observed delivering a key-up in this session.
480
+ *
481
+ * `false` for a key not yet pressed — absence of evidence, not evidence that the
482
+ * key never releases. Callers that need a decision before any press has been
483
+ * observed should be told the answer explicitly rather than inferring it.
484
+ */
485
+ export const keyDeliversKeyUp = (
486
+ keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
487
+ ): boolean =>
488
+ keysSeenReleasing.has(
489
+ typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
490
+ );
491
+
492
+ // Keys whose auto-repeat key-downs are dropped before propagation, mapped to an
493
+ // optional callback run when the key is finally released.
494
+ //
495
+ // A hold action typically moves focus (opening a context menu, say) while the
496
+ // key is still physically down. Two things then go wrong, and neither can be
497
+ // fixed by the element that owns the hold, because it is no longer in the focus
498
+ // path: the trailing auto-repeats propagate down the *new* focus path and fire
499
+ // whatever just took focus, and the key-up goes there too, so the owner's
500
+ // release handler never runs.
501
+ //
502
+ // Both are propagation concerns, so the latch lives here. The release callback
503
+ // is what lets a suppressor still learn about a key-up it can no longer receive
504
+ // through the focus path.
505
+ const suppressedKeys = new Map<KeyNameOrKeyCode, (() => void) | undefined>();
506
+
507
+ const liftSuppression = (key: KeyNameOrKeyCode): void => {
508
+ const onRelease = suppressedKeys.get(key);
509
+ suppressedKeys.delete(key);
510
+ onRelease?.();
511
+ };
512
+
513
+ /**
514
+ * Drop auto-repeat key-downs for `keyOrEvent` until the key is released.
515
+ *
516
+ * Suppression is lifted by the key's key-up, or by the next fresh (non-repeat)
517
+ * key-down — the latter so platforms that swallow key-up (webOS) can't wedge a
518
+ * key permanently. Non-repeat key-downs are never suppressed. `onRelease` runs
519
+ * when suppression lifts, whichever way it lifts, and is delivered regardless of
520
+ * where focus has moved in the meantime.
521
+ *
522
+ * `useHold` calls this itself when a hold fires; call it directly only when
523
+ * implementing hold behavior outside that primitive.
524
+ */
525
+ export const suppressKeyUntilRelease = (
526
+ keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
527
+ onRelease?: () => void,
528
+ ): void => {
529
+ suppressedKeys.set(
530
+ typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
531
+ onRelease,
532
+ );
533
+ };
534
+
535
+ /**
536
+ * Lift suppression added by {@link suppressKeyUntilRelease} early, running its
537
+ * release callback.
538
+ */
539
+ export const releaseKeySuppression = (
540
+ keyOrEvent: KeyboardEvent | KeyNameOrKeyCode,
541
+ ): void => {
542
+ liftSuppression(
543
+ typeof keyOrEvent === 'object' ? keyOf(keyOrEvent) : keyOrEvent,
544
+ );
545
+ };
546
+
451
547
  const handleKeyEvents = (
452
548
  delay: number,
453
549
  keydown?: KeyboardEvent,
454
550
  keyup?: KeyboardEvent,
455
551
  ) => {
456
552
  if (keydown) {
457
- const key: KeyNameOrKeyCode = keydown.key || keydown.keyCode;
553
+ const key: KeyNameOrKeyCode = keyOf(keydown);
554
+ if (keydown.repeat) {
555
+ if (suppressedKeys.has(key)) return;
556
+ } else if (suppressedKeys.has(key)) {
557
+ // A fresh press starts a new gesture, so the previous one is over even
558
+ // though its key-up never arrived. Settle it before handling this press.
559
+ liftSuppression(key);
560
+ }
458
561
  const mappedKeyHoldEvent =
459
562
  keyHoldMapEntries[keydown.key] || keyHoldMapEntries[keydown.keyCode];
460
563
  const mappedKeyEvent =
@@ -471,7 +574,12 @@ const handleKeyEvents = (
471
574
 
472
575
  propagateKeyPress(keydown, mappedKeyEvent, false);
473
576
  } else if (keyup) {
474
- const key: KeyNameOrKeyCode = keyup.key || keyup.keyCode;
577
+ const key: KeyNameOrKeyCode = keyOf(keyup);
578
+ keysSeenReleasing.add(key);
579
+ // The key is up: whatever was suppressing its repeats is done. Settle it
580
+ // before propagating, so a suppressor that is still in the focus path sees
581
+ // its own release callback rather than a second one via the key-up below.
582
+ if (suppressedKeys.has(key)) liftSuppression(key);
475
583
  const mappedKeyEvent =
476
584
  keyMapEntries[keyup.key] || keyMapEntries[keyup.keyCode];
477
585
  if (keyHoldTimeouts[key] === true) {
@@ -529,5 +637,6 @@ export const useFocusManager = (
529
637
  for (const timeout of Object.values(keyHoldTimeouts)) {
530
638
  if (timeout && timeout !== true) clearTimeout(timeout);
531
639
  }
640
+ suppressedKeys.clear();
532
641
  });
533
642
  };
@@ -2,6 +2,10 @@ export {
2
2
  focusPath,
3
3
  useFocusManager,
4
4
  setActiveElementCore,
5
+ suppressKeyUntilRelease,
6
+ releaseKeySuppression,
7
+ keyDeliversKeyUp,
8
+ noteKeyUpDelivered,
5
9
  type KeyMap,
6
10
  type KeyHoldOptions,
7
11
  } from '../core/focusManager.js';
@@ -1,11 +1,53 @@
1
1
  import { createMemo } from 'solid-js';
2
+ import {
3
+ suppressKeyUntilRelease,
4
+ keyDeliversKeyUp,
5
+ noteKeyUpDelivered,
6
+ } from '../core/focusManager.js';
7
+ import type { ElementNode } from '../core/elementNode.js';
8
+
9
+ /**
10
+ * A hold callback. Receives the `KeyboardEvent` that began the press, the
11
+ * element whose handler was invoked, and the focused (leaf) element at that
12
+ * moment — the same arguments a `KeyHandler` gets. All three are `undefined`
13
+ * when `startHold` was called without them.
14
+ */
15
+ export type HoldCallback = (
16
+ e?: KeyboardEvent,
17
+ target?: ElementNode,
18
+ handlerElm?: ElementNode,
19
+ ) => void;
20
+
21
+ export type HoldHandler = (
22
+ e?: KeyboardEvent,
23
+ target?: ElementNode,
24
+ handlerElm?: ElementNode,
25
+ ) => boolean;
2
26
 
3
27
  export type UseHoldProps = {
4
- onHold: () => void;
5
- onEnter: () => void;
6
- onRelease?: () => void;
28
+ onHold: HoldCallback;
29
+ onEnter: HoldCallback;
30
+ onRelease?: HoldCallback;
7
31
  holdThreshold?: number;
8
32
  performOnEnterImmediately?: boolean;
33
+ /**
34
+ * Whether a hold must be confirmed by an auto-repeat key-down.
35
+ *
36
+ * - `'auto'` (default): use auto-repeat if the key needs it, otherwise resolve
37
+ * by timer. A key that has been seen delivering key-up needs no auto-repeat —
38
+ * reaching the threshold without a key-up means it is still down. A key that
39
+ * swallows key-up does need one, since a finished press is otherwise
40
+ * indistinguishable from one still held.
41
+ * - `false`: always resolve by timer, matching the legacy `keyHoldOptions`
42
+ * behavior. Deterministic from the very first press.
43
+ * - `true`: require an auto-repeat, never resolving a repeat-less press as a
44
+ * hold.
45
+ *
46
+ * `'auto'` infers from keys seen earlier in the session, so the first press of
47
+ * a key resolves as a tap. Set this explicitly for a key you already know —
48
+ * e.g. `false` for Back on webOS, which emits no auto-repeat.
49
+ */
50
+ holdRequiresRepeat?: boolean | 'auto';
9
51
  };
10
52
 
11
53
  /**
@@ -20,13 +62,31 @@ export type UseHoldProps = {
20
62
  * when the timer fires it resolves to a hold → `onHold`.
21
63
  * - if key-up arrives before the timer, it's a tap → `onEnter` (fires
22
64
  * immediately, no latency, on platforms that deliver key-up).
23
- * - if neither key-up nor auto-repeat arrives, the timer resolves to a tap →
24
- * `onEnter` after `holdThreshold` ms. This is the key-up-independent path that
25
- * keeps taps working on webOS, at the cost of ~`holdThreshold` ms latency.
65
+ * - if the timer fires with neither, the key is either still held or already
66
+ * released with its key-up swallowed. `holdRequiresRepeat` decides which, and
67
+ * by default (`'auto'`) infers it: a key known to deliver key-up must still be
68
+ * down, so it resolves to a hold by timer alone; a key that swallows key-up
69
+ * resolves to a tap, keeping the primary action working on webOS OK at the
70
+ * cost of ~`holdThreshold` ms latency.
71
+ *
72
+ * Auto-repeat is therefore used where it exists and not required where it does
73
+ * not — webOS emits it for OK but not for Back, so Back holds resolve by timer
74
+ * while OK holds are confirmed by repeat.
75
+ *
76
+ * Once `onHold` fires the key is still physically down, so the focus manager
77
+ * drops its remaining auto-repeats until it is released. Without that, a hold
78
+ * that moves focus — the canonical case, opening a context menu — would have
79
+ * its trailing repeats fire whatever it just focused. The same latch delivers
80
+ * `onRelease` even though the key-up now propagates somewhere else entirely.
26
81
  *
27
82
  * `performOnEnterImmediately` keeps the legacy behavior of firing `onEnter` on
28
83
  * key-down; a long-press then fires both `onEnter` and `onHold`.
29
84
  *
85
+ * Note that `startHold` returns `true`, which ends the bubble phase: ancestor
86
+ * handlers for that key will not run, and whether the press was a tap isn't
87
+ * known until after propagation is over. If an ancestor performs navigation,
88
+ * invoke it from `onEnter` yourself. See the caveat in the docs.
89
+ *
30
90
  * @example
31
91
  * const [holdRight, releaseRight] = useHold({
32
92
  * onHold: handleHoldRight,
@@ -40,21 +100,42 @@ export type UseHoldProps = {
40
100
  * onRightRelease={releaseRight}
41
101
  * />
42
102
  *
103
+ * `startHold` reads `e.repeat` to detect a hold. Used directly as a `KeyHandler`
104
+ * (above) it gets the event; if you wrap it, forward all of the arguments, or
105
+ * hold detection silently never triggers.
106
+ *
43
107
  * @param {UseHoldProps} props - The properties for configuring the hold behavior.
44
- * @returns {[(e?: KeyboardEvent) => boolean, () => boolean]} A tuple containing `startHold` and `releaseHold` functions.
108
+ * @returns {[HoldHandler, HoldHandler]} A tuple of `startHold` and `releaseHold`.
45
109
  */
46
110
 
47
- export function useHold(props: UseHoldProps) {
111
+ export function useHold(props: UseHoldProps): [HoldHandler, HoldHandler] {
48
112
  const holdThreshold = createMemo(() => props.holdThreshold ?? 500);
49
113
  const performOnEnterImmediately = createMemo(
50
114
  () => props.performOnEnterImmediately ?? false,
51
115
  );
116
+ const holdRequiresRepeat = createMemo(
117
+ () => props.holdRequiresRepeat ?? 'auto',
118
+ );
119
+
120
+ // At the threshold with no auto-repeat seen, is this press still held (a hold)
121
+ // or already over with its key-up swallowed (a tap)? Only a key that delivers
122
+ // key-up can answer that on its own.
123
+ const requiresRepeat = (e: KeyboardEvent | undefined) => {
124
+ const mode = holdRequiresRepeat();
125
+ if (mode !== 'auto') return mode;
126
+ return !(e !== undefined && keyDeliversKeyUp(e));
127
+ };
52
128
 
53
129
  let holdTimeout = -1;
54
130
  let enterFired = false; // onEnter already fired for this press
55
131
  let holdFired = false; // onHold already fired for this press
56
132
  let repeated = false; // an auto-repeat key-down was seen (key still held)
57
133
 
134
+ // Context from the key-down that began the press, replayed to whichever
135
+ // callback resolves it — the timer path has no event of its own, and after a
136
+ // focus-moving hold neither does the release.
137
+ let press: Parameters<HoldCallback> = [];
138
+
58
139
  const reset = () => {
59
140
  if (holdTimeout !== -1) {
60
141
  clearTimeout(holdTimeout);
@@ -63,9 +144,17 @@ export function useHold(props: UseHoldProps) {
63
144
  enterFired = false;
64
145
  holdFired = false;
65
146
  repeated = false;
147
+ press = [];
66
148
  };
67
149
 
68
- const startHold = (e?: KeyboardEvent) => {
150
+ const fireRelease = () => {
151
+ if (!holdFired) return;
152
+ const args = press;
153
+ reset(); // before the callback, so a re-entrant press isn't clobbered
154
+ props.onRelease?.(...args);
155
+ };
156
+
157
+ const startHold: HoldHandler = (e, target, handlerElm) => {
69
158
  // Auto-repeat key-down: the key is still held. Record it so the timer
70
159
  // resolves to a hold even if the key-up event never arrives (webOS).
71
160
  if (e?.repeat) {
@@ -76,30 +165,40 @@ export function useHold(props: UseHoldProps) {
76
165
  // Fresh key-down begins a new press. Reset first so a previous press whose
77
166
  // key-up was never delivered doesn't leave us wedged for this one.
78
167
  reset();
168
+ press = [e, target, handlerElm];
79
169
 
80
170
  if (performOnEnterImmediately()) {
81
171
  enterFired = true;
82
- props.onEnter();
172
+ props.onEnter(...press);
83
173
  }
84
174
 
85
175
  holdTimeout = setTimeout(() => {
86
176
  holdTimeout = -1;
87
- if (repeated) {
177
+ if (repeated || !requiresRepeat(e)) {
88
178
  // Held past the threshold → hold gesture.
89
179
  holdFired = true;
90
- props.onHold();
180
+ // The key is still down and will keep repeating. Drop those repeats so
181
+ // they can't reach whatever onHold focuses, and route the key-up back
182
+ // here even if this element is no longer in the focus path.
183
+ if (e) suppressKeyUntilRelease(e, fireRelease);
184
+ props.onHold(...press);
91
185
  } else if (!enterFired) {
92
186
  // No key-up and no auto-repeat arrived: resolve as a tap so the
93
187
  // primary action still fires on remotes that swallow key-up.
94
188
  enterFired = true;
95
- props.onEnter();
189
+ props.onEnter(...press);
96
190
  }
97
191
  }, holdThreshold()) as unknown as number;
98
192
 
99
193
  return true;
100
194
  };
101
195
 
102
- const releaseHold = () => {
196
+ const releaseHold: HoldHandler = (e, target, handlerElm) => {
197
+ // Reaching here at all is proof this key delivers key-up, which is what
198
+ // `'auto'` reads. The focus manager records this too; doing it here as well
199
+ // keeps `'auto'` working behind a custom focus manager.
200
+ if (e) noteKeyUpDelivered(e);
201
+
103
202
  if (holdTimeout !== -1) {
104
203
  // Released before the threshold → tap. Fires immediately where key-up is
105
204
  // delivered, avoiding the timer latency.
@@ -107,12 +206,19 @@ export function useHold(props: UseHoldProps) {
107
206
  holdTimeout = -1;
108
207
  if (!enterFired) {
109
208
  enterFired = true;
110
- props.onEnter();
209
+ props.onEnter(
210
+ e ?? press[0],
211
+ target ?? press[1],
212
+ handlerElm ?? press[2],
213
+ );
111
214
  }
112
- } else if (holdFired) {
113
- props.onRelease?.();
215
+ reset();
216
+ } else {
217
+ // After a hold, the suppression latch has usually already fired onRelease
218
+ // and reset — `holdFired` is false and this is a no-op.
219
+ fireRelease();
220
+ reset();
114
221
  }
115
- reset();
116
222
  return true;
117
223
  };
118
224