@human-kit/ui 1.4.9 → 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.
@@ -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.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>",