@human-kit/ui 1.4.9 → 1.5.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.
@@ -198,8 +198,15 @@
198
198
  buttonRef?.getAttribute('aria-expanded') === 'true';
199
199
  }
200
200
 
201
+ // Only a button that opts in with `data-pressed-when-expanded` needs to watch `aria-expanded`.
202
+ // The attribute comes in through the props, so the check is reactive. Before, every button
203
+ // made a `MutationObserver` for nothing, and a page with 1000 buttons made 1000 of them.
204
+ const watchesExpanded = $derived(
205
+ (restProps as Record<string, unknown>)['data-pressed-when-expanded'] === 'true'
206
+ );
207
+
201
208
  $effect(() => {
202
- if (!buttonRef) {
209
+ if (!buttonRef || !watchesExpanded) {
203
210
  expandedPressed = false;
204
211
  return;
205
212
  }
@@ -2,9 +2,45 @@
2
2
  type PointerPressOwner = {
3
3
  key: symbol;
4
4
  pointerId: number | null;
5
+ /** Clears the pressed state of the owning item. */
6
+ release: () => void;
5
7
  };
6
8
 
7
9
  let pointerPressOwner: PointerPressOwner | null = null;
10
+
11
+ // A press can end anywhere on the page, so the end of the press is seen on `window`. One
12
+ // handler, shared by all the items and counted by the mounted items, serves them all. Before,
13
+ // each item added three listeners, and a list of 1000 items ran 3000 handlers on every
14
+ // pointer end.
15
+ let pointerEndListenerRefCount = 0;
16
+
17
+ function handleGlobalPointerEnd(event: PointerEvent | MouseEvent) {
18
+ const owner = pointerPressOwner;
19
+ if (!owner) return;
20
+ const pointerId = 'pointerId' in event ? event.pointerId : null;
21
+ if (owner.pointerId !== null && pointerId !== null && owner.pointerId !== pointerId) return;
22
+ owner.release();
23
+ }
24
+
25
+ function acquireGlobalPointerEndListeners() {
26
+ if (typeof window === 'undefined') return;
27
+ if (pointerEndListenerRefCount === 0) {
28
+ window.addEventListener('pointerup', handleGlobalPointerEnd);
29
+ window.addEventListener('pointercancel', handleGlobalPointerEnd);
30
+ window.addEventListener('mouseup', handleGlobalPointerEnd);
31
+ }
32
+ pointerEndListenerRefCount += 1;
33
+ }
34
+
35
+ function releaseGlobalPointerEndListeners() {
36
+ if (typeof window === 'undefined') return;
37
+ pointerEndListenerRefCount = Math.max(0, pointerEndListenerRefCount - 1);
38
+ if (pointerEndListenerRefCount === 0) {
39
+ window.removeEventListener('pointerup', handleGlobalPointerEnd);
40
+ window.removeEventListener('pointercancel', handleGlobalPointerEnd);
41
+ window.removeEventListener('mouseup', handleGlobalPointerEnd);
42
+ }
43
+ }
8
44
  </script>
9
45
 
10
46
  <script lang="ts">
@@ -159,17 +195,11 @@
159
195
  listboxCtx.keyboardNav.updateItems();
160
196
  }
161
197
 
162
- window.addEventListener('pointerup', handleGlobalPointerEnd);
163
- window.addEventListener('pointercancel', handleGlobalPointerEnd);
164
- window.addEventListener('mouseup', handleGlobalPointerEnd);
198
+ acquireGlobalPointerEndListeners();
165
199
  });
166
200
 
167
201
  onDestroy(() => {
168
- if (typeof window !== 'undefined') {
169
- window.removeEventListener('pointerup', handleGlobalPointerEnd);
170
- window.removeEventListener('pointercancel', handleGlobalPointerEnd);
171
- window.removeEventListener('mouseup', handleGlobalPointerEnd);
172
- }
202
+ releaseGlobalPointerEndListeners();
173
203
  clearOwnedPointerPress();
174
204
  listboxCtx.unregisterItem(id);
175
205
  unsubscribeSelection?.();
@@ -238,7 +268,7 @@
238
268
  }
239
269
 
240
270
  function startPointerPress(pointerId: number | null) {
241
- pointerPressOwner = { key: pointerPressOwnerKey, pointerId };
271
+ pointerPressOwner = { key: pointerPressOwnerKey, pointerId, release: clearPressedState };
242
272
  isPressed = true;
243
273
  pressedKey = null;
244
274
  }
@@ -249,14 +279,6 @@
249
279
  clearOwnedPointerPress();
250
280
  }
251
281
 
252
- function handleGlobalPointerEnd(event: PointerEvent | MouseEvent) {
253
- if (!ownsPointerPress(event)) {
254
- return;
255
- }
256
-
257
- clearPressedState();
258
- }
259
-
260
282
  function listboxHasDomFocus() {
261
283
  const root = elementRef?.closest('[role="listbox"]');
262
284
  return !!root && !!document.activeElement && root.contains(document.activeElement);
@@ -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.9",
3
+ "version": "1.5.1",
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>",