@human-kit/ui 1.4.7 → 1.4.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.
@@ -30,7 +30,7 @@ primitives as `Popover`.
30
30
  typed text.
31
31
  - Escape closes the current (topmost) menu and returns focus to its trigger. `Tab` and outside
32
32
  interaction close the whole menu chain. Within a submenu, `ArrowLeft` closes just that level.
33
- - A submenu uses a safe triangle for the pointer. While the pointer moves at an angle to the open submenu, the items that the pointer goes over do not close it. This continues for a short time. Thus the user gets to the submenu, and the submenu stays open.
33
+ - A submenu uses a safe triangle for the pointer. While the pointer moves at an angle to the open submenu, the items that the pointer goes over do not close it. Thus the user gets to the submenu, and the submenu stays open. The submenu closes when the pointer stops on an item. It also closes when the pointer moves away from it two times in sequence. Touch and pen do not use the triangle.
34
34
 
35
35
  ## Context menus
36
36
 
@@ -67,7 +67,14 @@
67
67
  // (`bottom-start`) already matches how a native context menu unfolds down and to the
68
68
  // right, and `flip`/`shift` handle the viewport edges.
69
69
  const resolvedOffset = $derived(offset ?? (ctx.isContextMenu ? 0 : 4));
70
- const anchorPoint = $derived(ctx.anchorPoint);
70
+ // The point the menu is shown at, held through its exit: the state drops the point as soon as
71
+ // the menu closes (a later keyboard open must land on the trigger), and following it then
72
+ // moved a leaving context menu onto its surface for the frames of its exit animation.
73
+ let shownAnchorPoint: typeof ctx.anchorPoint = null;
74
+ const anchorPoint = $derived.by(() => {
75
+ if (ctx.isOpen) shownAnchorPoint = ctx.anchorPoint;
76
+ return shownAnchorPoint;
77
+ });
71
78
  const resolvedAnchor = $derived(
72
79
  anchorPoint ? createPointAnchor(anchorPoint.x, anchorPoint.y, triggerRef) : triggerRef
73
80
  );
@@ -187,7 +187,7 @@
187
187
  if (disabled) return;
188
188
  // Hovering a sibling collapses an open submenu — but defer while the pointer is
189
189
  // travelling toward it (safe-triangle intent), so diagonal moves aren't interrupted.
190
- ctx.highlightItemFromPointer(itemId, event.clientX, event.clientY);
190
+ ctx.highlightItemFromPointer(itemId, event.clientX, event.clientY, event.pointerType);
191
191
 
192
192
  // Re-entering the item that started the press (button still held) restores its
193
193
  // pressed look; arriving on an item that didn't start the press leaves it alone.
@@ -122,8 +122,8 @@ export type MenuContext = {
122
122
  /** Records pointer movement over the content (drives submenu "safe triangle" intent). */
123
123
  trackPointer: (x: number, y: number) => void;
124
124
  /** Highlights an item from a pointer hover, deferring the close of an open submenu while
125
- * the pointer is travelling toward it. */
126
- highlightItemFromPointer: (id: string | number, x: number, y: number) => void;
125
+ * a mouse pointer is travelling toward it. */
126
+ highlightItemFromPointer: (id: string | number, x: number, y: number, pointerType?: string) => void;
127
127
  /** Cancels a pending deferred highlight (pointer reached the submenu). */
128
128
  cancelPendingHighlight: () => void;
129
129
  };
@@ -202,11 +202,11 @@ export function createMenuState(options) {
202
202
  function trackPointer(x, y) {
203
203
  submenuIntent.track(x, y);
204
204
  }
205
- function highlightItemFromPointer(id, x, y) {
205
+ function highlightItemFromPointer(id, x, y, pointerType) {
206
206
  submenuIntent.request({ x, y }, () => {
207
207
  closeActiveChild();
208
208
  keyboardNav.focusById(id);
209
- });
209
+ }, pointerType);
210
210
  }
211
211
  function cancelPendingHighlight() {
212
212
  submenuIntent.cancel();
@@ -5,18 +5,30 @@ import type { MenuContext } from './context';
5
5
  * When a submenu is open and the pointer moves diagonally from the trigger toward the
6
6
  * submenu, the path crosses sibling items. Naively, each sibling's `pointerenter` would
7
7
  * close the submenu before the pointer arrives. This tracks the pointer trajectory and,
8
- * while the pointer is aimed at the open submenu, defers the sibling highlight/close for a
9
- * short grace period so the user can reach the submenu.
8
+ * while the pointer keeps moving toward the open submenu, defers the sibling highlight/close
9
+ * so the user can reach the submenu.
10
+ *
11
+ * The deferral ends as soon as the pointer shows it is not on its way:
12
+ * - it stops: a still pointer sends no `pointermove`, so a short timer stands in for it.
13
+ * A fixed long timeout would make everyone who wants the sibling wait for it.
14
+ * - it leaves the triangle on two moves in a row. One stray move is forgiven, so a hand
15
+ * tremor or a jittery trackpad does not drop the submenu mid-travel.
16
+ *
17
+ * Only a mouse aims. Touch and pen have no hover travel, so they highlight at once.
10
18
  */
11
19
  type Point = {
12
20
  x: number;
13
21
  y: number;
14
22
  };
23
+ /** How long the pointer may go without moving before a deferred highlight commits. Pointer
24
+ * events arrive once per frame while the pointer moves; 100 ms still holds when a busy page
25
+ * drops to 30 fps and loses a frame, and it is a third of a fixed 300 ms timeout. */
26
+ export declare const SUBMENU_INTENT_STOP_MS = 100;
15
27
  export type SubmenuIntent = {
16
28
  /** Records a pointer move; flushes a pending highlight if the pointer stops aiming. */
17
29
  track: (x: number, y: number) => void;
18
- /** Highlights an item, deferring if the pointer is aimed at the open submenu. */
19
- request: (point: Point, commit: () => void) => void;
30
+ /** Highlights an item, deferring if a mouse pointer is aimed at the open submenu. */
31
+ request: (point: Point, commit: () => void, pointerType?: string) => void;
20
32
  /** Drops a pending highlight without committing (pointer reached the submenu). */
21
33
  cancel: () => void;
22
34
  };
@@ -1,4 +1,9 @@
1
- const GRACE_MS = 300;
1
+ /** How long the pointer may go without moving before a deferred highlight commits. Pointer
2
+ * events arrive once per frame while the pointer moves; 100 ms still holds when a busy page
3
+ * drops to 30 fps and loses a frame, and it is a third of a fixed 300 ms timeout. */
4
+ export const SUBMENU_INTENT_STOP_MS = 100;
5
+ /** Consecutive moves outside the triangle that commit a deferred highlight. */
6
+ const MAX_MISSES = 2;
2
7
  /** Vertical padding added to the submenu's near edge so the triangle is forgiving. */
3
8
  const EDGE_PADDING = 16;
4
9
  function cross(p1, p2, p3) {
@@ -15,6 +20,7 @@ function pointInTriangle(p, a, b, c) {
15
20
  export function createSubmenuIntent(getActiveChild) {
16
21
  let previous = null;
17
22
  let pending = null;
23
+ let misses = 0;
18
24
  let timer = null;
19
25
  function clearTimer() {
20
26
  if (timer !== null) {
@@ -22,9 +28,14 @@ export function createSubmenuIntent(getActiveChild) {
22
28
  timer = null;
23
29
  }
24
30
  }
31
+ function armStopTimer() {
32
+ clearTimer();
33
+ timer = setTimeout(commit, SUBMENU_INTENT_STOP_MS);
34
+ }
25
35
  function commit() {
26
36
  const current = pending;
27
37
  pending = null;
38
+ misses = 0;
28
39
  clearTimer();
29
40
  current?.commit();
30
41
  }
@@ -47,27 +58,34 @@ export function createSubmenuIntent(getActiveChild) {
47
58
  return {
48
59
  track(x, y) {
49
60
  const point = { x, y };
61
+ // The browser sends a `pointermove` at the `pointerenter` coordinates. It has no
62
+ // direction, so it is neither a miss nor progress. The stop timer still runs.
63
+ if (previous && previous.x === x && previous.y === y)
64
+ return;
50
65
  if (pending) {
51
66
  if (isAimingAtSubmenu(point)) {
52
- // Still aiming at the submenu: re-arm the grace timer so a long diagonal
53
- // travel doesn't expire (and commit the highlight/close) mid-flight.
54
- clearTimer();
55
- timer = setTimeout(commit, GRACE_MS);
67
+ misses = 0;
68
+ armStopTimer();
56
69
  }
57
70
  else {
58
- commit();
71
+ misses += 1;
72
+ if (misses >= MAX_MISSES)
73
+ commit();
74
+ // A forgiven miss keeps the stop timer running: it is not progress.
59
75
  }
60
76
  }
61
77
  previous = point;
62
78
  },
63
- request(point, commitFn) {
64
- if (getActiveChild() && isAimingAtSubmenu(point)) {
65
- clearTimer();
79
+ request(point, commitFn, pointerType) {
80
+ const canAim = pointerType !== 'touch' && pointerType !== 'pen';
81
+ if (canAim && getActiveChild() && isAimingAtSubmenu(point)) {
66
82
  pending = { commit: commitFn };
67
- timer = setTimeout(commit, GRACE_MS);
83
+ misses = 0;
84
+ armStopTimer();
68
85
  }
69
86
  else {
70
87
  pending = null;
88
+ misses = 0;
71
89
  clearTimer();
72
90
  commitFn();
73
91
  }
@@ -75,6 +93,7 @@ export function createSubmenuIntent(getActiveChild) {
75
93
  },
76
94
  cancel() {
77
95
  pending = null;
96
+ misses = 0;
78
97
  clearTimer();
79
98
  }
80
99
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.4.7",
3
+ "version": "1.4.9",
4
4
  "description": "Accessible, typed UI components for Svelte 5 — shipped as native ESM with per-component subpath exports so bundlers only include what you import.",
5
5
  "license": "MIT",
6
6
  "author": "Agustin Delgado <agustindariodelgado@gmail.com>",