@human-kit/ui 1.4.8 → 1.5.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.
@@ -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
 
@@ -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
  };
@@ -24,10 +24,11 @@ Public prop type: `TableBodyProps`
24
24
  Name: `TableBodyVirtualizer`
25
25
  Description: Fixed-height body virtualization settings.
26
26
 
27
- | Prop | Type | Default | Description |
28
- | ----------- | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
29
- | `rowHeight` | `number` | `-` | Required fixed pixel height used to compute the visible range. |
30
- | `overscan` | `number` | `18` | Optional explicit override for the extra rows rendered above and below the viewport. When omitted, the body renders 18 extra rows above and below the visible range. |
27
+ | Prop | Type | Default | Description |
28
+ | ----------- | --------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
29
+ | `rowHeight` | `number` | `-` | Required fixed pixel height used to compute the visible range. |
30
+ | `overscan` | `number` | `18` | Optional explicit override for the extra rows rendered above and below the viewport. When omitted, the body renders 18 extra rows above and below the visible range. |
31
+ | `recycle` | `boolean` | `false` | Reuses the mounted rows. A row that leaves the window is given to an item that enters. A cell that keeps its own state must reset it when its item changes. |
31
32
 
32
33
  ### Context utilities
33
34
 
@@ -212,6 +212,33 @@
212
212
  return itemList.slice(visibleRange.from, visibleRange.to + 1);
213
213
  });
214
214
 
215
+ // With `virtualizer.recycle`, each rendered item is drawn by a numbered row slot. An item that
216
+ // is already drawn keeps its slot. An item that enters takes the slot of an item that left, so
217
+ // the keyed `each` updates that row to the new item and does not destroy one row and build
218
+ // another. This is plain bookkeeping and not state: it answers only for the items it gets, and
219
+ // it gives the same answer when asked twice for the same items.
220
+ // eslint-disable-next-line svelte/prefer-svelte-reactivity -- not state, see above
221
+ const slotOf = new Map<string | number, number>();
222
+ const freeSlots: number[] = [];
223
+ let slotCount = 0;
224
+ const slotKeys = $derived.by(() => {
225
+ if (!virtualizerEnabled || virtualizer?.recycle !== true) return undefined;
226
+ const present = new Set(renderedItems.map((item) => item.id));
227
+ for (const [id, slot] of slotOf) {
228
+ if (present.has(id)) continue;
229
+ slotOf.delete(id);
230
+ freeSlots.push(slot);
231
+ }
232
+ return renderedItems.map((item) => {
233
+ let slot = slotOf.get(item.id);
234
+ if (slot === undefined) {
235
+ slot = freeSlots.pop() ?? slotCount++;
236
+ slotOf.set(item.id, slot);
237
+ }
238
+ return slot;
239
+ });
240
+ });
241
+
215
242
  const isEmpty = $derived.by(() => {
216
243
  void table.layoutEpoch;
217
244
  return table.getLogicalBodyRowCount() === 0;
@@ -285,7 +312,7 @@
285
312
  </tr>
286
313
  {/if}
287
314
 
288
- {#each renderedItems as item (item.id)}
315
+ {#each renderedItems as item, index (slotKeys ? slotKeys[index] : item.id)}
289
316
  {#if children}
290
317
  {@render (children as Snippet<[T]>)(item)}
291
318
  {/if}
@@ -11,10 +11,11 @@ Description: Table data cell part. In body scope it participates in roving focus
11
11
 
12
12
  Public prop type: `TableCellProps`
13
13
 
14
- | Prop | Type | Default | Description |
15
- | ---------- | --------- | ----------- | ------------------------------------------ |
16
- | `class` | `string` | `''` | Class names for the rendered `td` or `th`. |
17
- | `children` | `Snippet` | `undefined` | Cell content. |
14
+ | Prop | Type | Default | Description |
15
+ | ------------- | --------- | ----------- | ------------------------------------------------------------------- |
16
+ | `class` | `string` | `''` | Class names for the rendered `td` or `th`. |
17
+ | `children` | `Snippet` | `undefined` | Cell content. |
18
+ | `columnIndex` | `number` | `undefined` | Index of the column of the cell. Set it when the row skips columns. |
18
19
 
19
20
  ### Context utilities
20
21
 
@@ -13,7 +13,7 @@
13
13
  } from '../../primitives/input-modality';
14
14
  import { handleTableBodyKeydown } from '../utils/handle-body-keydown';
15
15
 
16
- let { children, class: className = '', ...restProps }: TableCellProps = $props();
16
+ let { children, class: className = '', columnIndex, ...restProps }: TableCellProps = $props();
17
17
 
18
18
  const table = useTableContext();
19
19
  const row = useTableRowContext();
@@ -22,7 +22,11 @@
22
22
  let element = $state<HTMLElement | undefined>(undefined);
23
23
  let focusDelegate = $state<(() => HTMLElement | undefined) | undefined>(undefined);
24
24
  row.registerCellToken(key, () => element);
25
+ // `columnIndex` is for a row that does not draw every cell, for example one that uses a
26
+ // `colspan` filler for the columns far from the screen. Counting the cells of such a row gives
27
+ // the wrong columns. It also skips the scan of the row that each cell would do.
25
28
  const cellIndex = $derived.by(() => {
29
+ if (columnIndex !== undefined) return columnIndex;
26
30
  void row.cellOrderEpoch;
27
31
  return row.getCellIndex(key);
28
32
  });
@@ -75,6 +75,14 @@ export type TableBodyVirtualizer<T extends TableRowItem = TableRowItem> = {
75
75
  * as blank. Raise it for expensive rows or very fast scrolling.
76
76
  */
77
77
  overscan?: number;
78
+ /**
79
+ * Reuse the mounted rows. When the window moves, a row that leaves it is given to an item that
80
+ * enters, and the body does not destroy that row to build another. Building a row of a wide
81
+ * table is most of the cost of a scroll, and updating one costs much less. Whatever a cell
82
+ * keeps in its own state stays after the item changes, so a cell that keeps state must reset
83
+ * it when its item changes.
84
+ */
85
+ recycle?: boolean;
78
86
  };
79
87
  type TableBodyBaseProps = Omit<HTMLAttributes<HTMLTableSectionElement>, 'children'> & {
80
88
  class?: string;
@@ -196,6 +204,12 @@ export type TableColumnResizerProps = Omit<HTMLAttributes<HTMLDivElement>, 'chil
196
204
  export type TableCellProps = Omit<HTMLAttributes<HTMLTableCellElement>, 'children'> & {
197
205
  children?: Snippet;
198
206
  class?: string;
207
+ /**
208
+ * Index of the column this cell belongs to. By default the cell takes the index from its place
209
+ * among the cells of its row. Set it when the row skips columns, for example with a `colspan`
210
+ * filler cell.
211
+ */
212
+ columnIndex?: number;
199
213
  };
200
214
  export type TableInteractiveCellProps = TableCellProps;
201
215
  export type TableEmptyStateProps = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@human-kit/ui",
3
- "version": "1.4.8",
3
+ "version": "1.5.0",
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>",