@torpor/ui 1.3.3 → 1.3.5

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.
@@ -63,8 +63,15 @@ export default function ColorPickerInput($props: ColorPickerInputProps | undefin
63
63
 
64
64
  function handleInput(e: Event) {
65
65
  const el = e.currentTarget as HTMLInputElement;
66
- $state.editing = true;
66
+ // Order matters: while `editing` is false, `display()` only reads the
67
+ // committed value, so assigning `text` first doesn't re-trigger the
68
+ // reactive `value` attribute. Flipping `editing` last re-applies
69
+ // `display()` — which by then returns the new text. The other order
70
+ // flushes the attribute effect with `text` still at its old value,
71
+ // wiping the input mid-keystroke (the bug that made hex commits
72
+ // silently read "" and never fire onchange)
67
73
  $state.text = el.value;
74
+ $state.editing = true;
68
75
  }
69
76
 
70
77
  function handleKey(e: KeyboardEvent) {
@@ -19,6 +19,14 @@ interface ContextualHoverProps {
19
19
  exitGrace?: number;
20
20
  /** Whether hovering and focusing are ignored */
21
21
  disabled?: boolean;
22
+ /**
23
+ * How taps on touch screens open the contextual window (hover never
24
+ * fires on touch). "auto" (default): tapping the trigger's inert surface
25
+ * opens it, but tapping a control inside the trigger — a button, link,
26
+ * … — runs that control's action without opening. "open": any tap opens
27
+ * it, even over embedded controls. "ignore": taps never open it.
28
+ */
29
+ touchBehavior?: "auto" | "open" | "ignore";
22
30
  }
23
31
 
24
32
  /**
@@ -77,6 +85,7 @@ export default function ContextualHover($props?: ContextualHoverProps) {
77
85
  getContent: () => ($state.contentId ? document.getElementById($state.contentId) : undefined),
78
86
  hoverDelay: $props?.hoverDelay,
79
87
  exitGrace: $props?.exitGrace,
88
+ touchBehavior: $props?.touchBehavior,
80
89
  });
81
90
 
82
91
  $run(() => {
@@ -84,8 +93,10 @@ export default function ContextualHover($props?: ContextualHoverProps) {
84
93
  });
85
94
 
86
95
  function handleEnter() {
96
+ // Suppressed touch taps (a tap on an embedded control) don't anchor
97
+ // the content either — the tap belongs to the control's action
98
+ if (!hover.handleEnter()) return;
87
99
  activate({ clientX: node.offsetWidth / 2, clientY: node.offsetHeight / 2 } as MouseEvent);
88
- hover.handleEnter();
89
100
  }
90
101
 
91
102
  @render {
@@ -99,6 +110,7 @@ export default function ContextualHover($props?: ContextualHoverProps) {
99
110
  aria-controls={$state.contentId}
100
111
  onmouseenter={handleEnter}
101
112
  onmouseleave={(e: MouseEvent) => hover.handleLeave(e)}
113
+ onpointerdown={(e: PointerEvent) => hover.handlePointerDown(e)}
102
114
  onfocus={() => hover.handleFocus()}
103
115
  onblur={(e: FocusEvent) => hover.handleBlur(e)}
104
116
  onmount={(el) => {
@@ -16,6 +16,14 @@ interface PopoverHoverProps {
16
16
  exitGrace?: number;
17
17
  /** Whether hovering and focusing are ignored */
18
18
  disabled?: boolean;
19
+ /**
20
+ * How taps on touch screens open the popover (hover never fires on
21
+ * touch). "auto" (default): tapping the trigger's inert surface opens it,
22
+ * but tapping a control inside the trigger — a button, link, … — runs
23
+ * that control's action without opening. "open": any tap opens it, even
24
+ * over embedded controls. "ignore": taps never open it.
25
+ */
26
+ touchBehavior?: "auto" | "open" | "ignore";
19
27
  }
20
28
 
21
29
  /**
@@ -59,6 +67,7 @@ export default function PopoverHover($props?: PopoverHoverProps) {
59
67
  getContent: () => ($state.contentId ? document.getElementById($state.contentId) : undefined),
60
68
  hoverDelay: $props?.hoverDelay,
61
69
  exitGrace: $props?.exitGrace,
70
+ touchBehavior: $props?.touchBehavior,
62
71
  });
63
72
 
64
73
  $run(() => {
@@ -80,6 +89,7 @@ export default function PopoverHover($props?: PopoverHoverProps) {
80
89
  aria-controls={$state.contentId}
81
90
  onmouseenter={() => hover.handleEnter()}
82
91
  onmouseleave={(e: MouseEvent) => hover.handleLeave(e)}
92
+ onpointerdown={(e: PointerEvent) => hover.handlePointerDown(e)}
83
93
  onfocus={() => hover.handleFocus()}
84
94
  onblur={(e: FocusEvent) => hover.handleBlur(e)}
85
95
  onmount={anchor}
@@ -1,4 +1,4 @@
1
- import { $watch } from "@torpor/view";
1
+ import { $batch, $watch } from "@torpor/view";
2
2
  import type DataSeries from "../Charts/DataSeries";
3
3
  import {
4
4
  calculateItemWidth,
@@ -196,20 +196,29 @@ export function createChartLayout(options: ChartLayoutOptions): ChartLayout {
196
196
  const claims = this.claims as Record<string, SpaceClaim>;
197
197
  const current = claims[id];
198
198
  if (current && current.side === side && current.size === size) return;
199
- claims[id] = { side, size };
200
- this.claimsVersion++;
199
+ // One claim change is one state transition: adding a key to the
200
+ // claims object notifies both the per-key signal and the key-set
201
+ // (ownKeys) signal, so the writes are batched to a single flush
202
+ $batch(() => {
203
+ claims[id] = { side, size };
204
+ this.claimsVersion++;
205
+ });
201
206
  },
202
207
  release(id: string) {
203
208
  const claims = this.claims as Record<string, SpaceClaim>;
204
209
  if (!claims[id]) return;
205
- delete claims[id];
206
- this.claimsVersion++;
210
+ $batch(() => {
211
+ delete claims[id];
212
+ this.claimsVersion++;
213
+ });
207
214
  },
208
215
  announceMax(id: string, value: number) {
209
216
  const claims = this.maxClaims as Record<string, number>;
210
217
  if (claims[id] === value) return;
211
- claims[id] = value;
212
- this.maxVersion++;
218
+ $batch(() => {
219
+ claims[id] = value;
220
+ this.maxVersion++;
221
+ });
213
222
  },
214
223
 
215
224
  setTextSize(width: number, height: number) {
@@ -27,17 +27,42 @@ export interface HoverRevealOptions {
27
27
  hoverDelay?: number;
28
28
  /** Milliseconds to wait after the pointer leaves before closing (default 300) */
29
29
  exitGrace?: number;
30
+ /**
31
+ * How taps on touch screens open the popout (hover never fires on touch).
32
+ * - "auto" (default): tapping the trigger's inert surface opens it;
33
+ * tapping an interactive control inside the trigger (a button, link,
34
+ * …) runs the control's action and does NOT open the popout.
35
+ * - "open": any tap opens it, even over embedded controls — for pure
36
+ * hovercards whose content is the whole point.
37
+ * - "ignore": taps never open it — the component provides its own touch
38
+ * trigger.
39
+ */
40
+ touchBehavior?: "auto" | "open" | "ignore";
30
41
  }
31
42
 
32
43
  export interface HoverRevealHandlers {
33
- /** Handles the pointer entering the trigger */
34
- handleEnter: () => void;
44
+ /**
45
+ * Handles the pointer entering the trigger. Returns false when the enter
46
+ * was suppressed (a touch tap that shouldn't open the popout), so callers
47
+ * with their own enter bookkeeping (e.g. Contextual's anchor positioning)
48
+ * can skip it too.
49
+ */
50
+ handleEnter: () => boolean;
35
51
  /** Handles the pointer leaving the trigger; closes unless it moved into the content element */
36
52
  handleLeave: (e: MouseEvent | FocusEvent) => void;
37
53
  /** Handles focus landing on the trigger (opens immediately) */
38
54
  handleFocus: () => void;
39
55
  /** Handles focus leaving the trigger (closes unless focus moved into the content element) */
40
56
  handleBlur: (e: FocusEvent) => void;
57
+ /**
58
+ * Handles a pointerdown on the trigger. Touch taps synthesize the
59
+ * mouseenter/focus sequence the hover machine reacts to — but a tap on an
60
+ * interactive control is an *action*, not a request to see the popout, so
61
+ * this records the modality and target for `handleEnter`/`handleFocus` to
62
+ * discriminate (the compatibility mouse events carry no pointerType, and
63
+ * focus carries none at all).
64
+ */
65
+ handlePointerDown: (e: PointerEvent) => void;
41
66
  /** Cancels pending open/close timers */
42
67
  dispose: () => void;
43
68
  }
@@ -50,10 +75,45 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
50
75
  const getContent = options.getContent ?? (() => undefined);
51
76
  const hoverDelay = options.hoverDelay ?? 500;
52
77
  const exitGrace = options.exitGrace ?? 300;
78
+ const touchBehavior = options.touchBehavior ?? "auto";
53
79
 
54
80
  let openTimer: ReturnType<typeof setTimeout> | undefined;
55
81
  let closeTimer: ReturnType<typeof setTimeout> | undefined;
56
82
 
83
+ // The last touch/pen pointerdown on the trigger: a tap synthesizes the
84
+ // mouseenter and focus the machine reacts to, so the pointerdown is the
85
+ // only place where the input modality — and the tapped control — is
86
+ // observable. Time-boxed rather than reset manually, so a stale tap can't
87
+ // suppress a later real mouse hover for more than a moment.
88
+ const TOUCH_WINDOW = 1000;
89
+ let touchTapAt = 0;
90
+ let touchTapInteractive = false;
91
+
92
+ // Controls whose activation is an action in its own right: tapping them
93
+ // must not also reveal the popout ("don't punish the action"). Inert
94
+ // surfaces fall through, so the tap fallback still works for pure content.
95
+ const INTERACTIVE =
96
+ "button, a, input, select, textarea, label, [role='button'], [role='link'], [role='menuitem'], [role='tab'], [role='option'], [role='checkbox'], [role='radio'], [role='switch']";
97
+
98
+ function touchSuppressesEnter(): boolean {
99
+ if (Date.now() - touchTapAt > TOUCH_WINDOW) return false;
100
+ if (touchBehavior === "open") return false;
101
+ if (touchBehavior === "ignore") return true;
102
+ return touchTapInteractive;
103
+ }
104
+
105
+ /**
106
+ * Focus is only suppressed in "ignore" mode: in "auto", a tap on an
107
+ * embedded control focuses the *control* (focus doesn't bubble to the
108
+ * trigger), so the trigger's focus handler only ever runs for inert
109
+ * taps — which are exactly the fallback that should open — and for
110
+ * keyboard focus, which must always open.
111
+ */
112
+ function touchSuppressesFocus(): boolean {
113
+ if (touchBehavior !== "ignore") return false;
114
+ return Date.now() - touchTapAt <= TOUCH_WINDOW;
115
+ }
116
+
57
117
  // The content element currently watched for pointer/focus leaving it. It
58
118
  // is attached once the pointer or focus enters the content (which cancels
59
119
  // the trigger's close timer), so that leaving the content arms a close
@@ -183,7 +243,13 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
183
243
 
184
244
  return {
185
245
  handleEnter() {
186
- if (disabled()) return;
246
+ if (disabled()) return false;
247
+ if (touchSuppressesEnter()) {
248
+ // A touch tap on an interactive control (or with "ignore"):
249
+ // the tap runs the control's action; it doesn't request the
250
+ // popout. Don't resume a closing popout or arm the timer
251
+ return false;
252
+ }
187
253
 
188
254
  if (closeTimer !== undefined) {
189
255
  clearTimeout(closeTimer);
@@ -191,11 +257,11 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
191
257
  stopListening();
192
258
  unwatchContent();
193
259
  }
194
- if (isVisible()) return;
260
+ if (isVisible()) return true;
195
261
 
196
262
  if (hoverDelay <= 0) {
197
263
  reveal();
198
- return;
264
+ return true;
199
265
  }
200
266
  if (openTimer === undefined) {
201
267
  openTimer = setTimeout(() => {
@@ -203,6 +269,7 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
203
269
  reveal();
204
270
  }, hoverDelay);
205
271
  }
272
+ return true;
206
273
  },
207
274
 
208
275
  handleLeave(_e) {
@@ -218,6 +285,21 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
218
285
  scheduleClose();
219
286
  },
220
287
 
288
+ handlePointerDown(e: PointerEvent) {
289
+ if (disabled()) return;
290
+ if (e.pointerType !== "touch" && e.pointerType !== "pen") return;
291
+
292
+ const target = e.target as Element | null;
293
+ const interactive = target?.closest?.(INTERACTIVE) != null;
294
+ touchTapAt = Date.now();
295
+ touchTapInteractive = interactive;
296
+ if (touchSuppressesEnter()) {
297
+ // A tap that shouldn't reveal the popout must also cancel an
298
+ // open armed by the synthesized mouseenter that precedes it
299
+ cancelPendingOpen();
300
+ }
301
+ },
302
+
221
303
  handleFocus() {
222
304
  if (disabled()) return;
223
305
 
@@ -228,6 +310,12 @@ export function createHoverReveal(options: HoverRevealOptions): HoverRevealHandl
228
310
  return;
229
311
  }
230
312
 
313
+ // A touch tap that shouldn't reveal the popout focuses the
314
+ // trigger too (tapping its inert surface); don't open
315
+ if (touchSuppressesFocus()) {
316
+ return;
317
+ }
318
+
231
319
  if (closeTimer !== undefined) {
232
320
  clearTimeout(closeTimer);
233
321
  closeTimer = undefined;
@@ -47,6 +47,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
47
47
  // mouse-opened popover never takes focus, so it must not yank focus back to
48
48
  // its anchor when it hides.
49
49
  let hadFocus = false;
50
+ let resizeObserver: ResizeObserver | null = null;
50
51
 
51
52
  function setPosition() {
52
53
  const anchor = options.getAnchor();
@@ -60,6 +61,22 @@ export function createPopoutContent(options: PopoutContentOptions): {
60
61
  }
61
62
  }
62
63
 
64
+ /**
65
+ * Content often renders before its data (or images/layout) arrive, which
66
+ * changes the size the placement was computed from. Watch the content and
67
+ * reposition whenever it resizes while open.
68
+ */
69
+ function observeContent() {
70
+ if (typeof ResizeObserver === "undefined") return;
71
+ const content = options.getContent();
72
+ if (!content) return;
73
+ resizeObserver ??= new ResizeObserver(() => {
74
+ if (shown) setPosition();
75
+ });
76
+ resizeObserver.disconnect();
77
+ resizeObserver.observe(content);
78
+ }
79
+
63
80
  function handleDocumentMouseDown() {
64
81
  closeOnClick = true;
65
82
  }
@@ -108,6 +125,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
108
125
 
109
126
  // Set the position and listen for window resize and scroll to reset the position
110
127
  setPosition();
128
+ observeContent();
111
129
  addEventListener("resize", setPosition);
112
130
  const anchor = options.getAnchor();
113
131
  if (anchor) {
@@ -125,6 +143,7 @@ export function createPopoutContent(options: PopoutContentOptions): {
125
143
  removeDocumentEvent("click", handleDocumentClick);
126
144
  removeDocumentEvent("focusin", handleDocumentFocusIn);
127
145
  removeDocumentEvent("keydown", handleDocumentKeyDown);
146
+ resizeObserver?.disconnect();
128
147
  removeEventListener("resize", setPosition);
129
148
  const anchor = options.getAnchor();
130
149
  if (anchor) {
@@ -54,22 +54,26 @@ export default function setPopoverPosition(
54
54
 
55
55
  switch (calculatedSide) {
56
56
  case "top": {
57
- content.style.top = anchorRect.top - contentRect.height + "px";
57
+ // Clamp the axis the placement chose too: when neither side has
58
+ // enough room, the requested side is kept and must stay on screen.
59
+ content.style.top =
60
+ clamp(anchorRect.top - contentRect.height, window.innerHeight - contentRect.height) + "px";
58
61
  setVerticalAlignment(content, anchorRect, contentRect, alignment);
59
62
  break;
60
63
  }
61
64
  case "right": {
62
- content.style.left = anchorRect.right + "px";
65
+ content.style.left = clamp(anchorRect.right, window.innerWidth - contentRect.width) + "px";
63
66
  setHorizontalAlignment(content, anchorRect, contentRect, alignment);
64
67
  break;
65
68
  }
66
69
  case "bottom": {
67
- content.style.top = anchorRect.bottom + "px";
70
+ content.style.top = clamp(anchorRect.bottom, window.innerHeight - contentRect.height) + "px";
68
71
  setVerticalAlignment(content, anchorRect, contentRect, alignment);
69
72
  break;
70
73
  }
71
74
  case "left": {
72
- content.style.left = anchorRect.left - contentRect.width + "px";
75
+ content.style.left =
76
+ clamp(anchorRect.left - contentRect.width, window.innerWidth - contentRect.width) + "px";
73
77
  setHorizontalAlignment(content, anchorRect, contentRect, alignment);
74
78
  break;
75
79
  }
@@ -80,6 +84,11 @@ export default function setPopoverPosition(
80
84
  }
81
85
  }
82
86
 
87
+ /** Clamps a position to the viewport, keeping it non-negative. */
88
+ function clamp(value: number, max: number): number {
89
+ return Math.max(Math.min(value, max), 0);
90
+ }
91
+
83
92
  function setVerticalAlignment(
84
93
  content: HTMLElement,
85
94
  anchorRect: DOMRect,
@@ -107,10 +116,7 @@ function setVerticalAlignment(
107
116
  }
108
117
 
109
118
  // Try to make sure the content is entirely on the screen horizontally
110
- left = Math.min(left, window.innerWidth - contentRect.width);
111
- left = Math.max(left, 0);
112
-
113
- content.style.left = left + "px";
119
+ content.style.left = clamp(left, window.innerWidth - contentRect.width) + "px";
114
120
  }
115
121
 
116
122
  function setHorizontalAlignment(
@@ -140,8 +146,5 @@ function setHorizontalAlignment(
140
146
  }
141
147
 
142
148
  // Try to make sure the content is entirely on the screen vertically
143
- top = Math.min(top, window.innerHeight - contentRect.height);
144
- top = Math.max(top, 0);
145
-
146
- content.style.top = top + "px";
149
+ content.style.top = clamp(top, window.innerHeight - contentRect.height) + "px";
147
150
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@torpor/ui",
3
- "version": "1.3.3",
3
+ "version": "1.3.5",
4
4
  "description": "Accessible, unstyled components for use in your Torpor front-end",
5
5
  "type": "module",
6
6
  "torpor": true,
@@ -181,15 +181,15 @@
181
181
  "author": "",
182
182
  "license": "ISC",
183
183
  "dependencies": {
184
- "@torpor/unplugin": "^1.2.8",
185
- "@torpor/view": "^1.2.5"
184
+ "@torpor/unplugin": "^1.2.10",
185
+ "@torpor/view": "^1.3.0"
186
186
  },
187
187
  "devDependencies": {
188
188
  "@testing-library/dom": "^10.4.2",
189
189
  "@testing-library/jest-dom": "^7.0.1",
190
190
  "@testing-library/user-event": "^14.6.8",
191
- "@torpor/check": "^1.0.13",
192
- "@torpor/prettier": "^1.0.13",
191
+ "@torpor/check": "^1.0.15",
192
+ "@torpor/prettier": "^1.0.15",
193
193
  "@types/jsdom": "^30.0.0",
194
194
  "@types/node": "^26.6.5",
195
195
  "@typescript/native-preview": "7.0.0-dev.20260707.2",