@human-kit/ui 1.5.0 → 1.5.2
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.
- package/dist/button/root/button-root.svelte +8 -1
- package/dist/listbox/item/listbox-item.svelte +39 -17
- package/dist/table/body/table-body.svelte +9 -1
- package/dist/table/cell/table-cell.svelte +5 -15
- package/dist/table/interactive-cell/table-interactive-cell.svelte +6 -2
- package/dist/table/root/context.svelte.d.ts +10 -0
- package/dist/table/root/context.svelte.js +73 -1
- package/dist/table/root/table-root.svelte +6 -0
- package/dist/table/row/table-row.svelte +4 -3
- package/package.json +1 -1
|
@@ -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
|
-
|
|
163
|
-
window.addEventListener('pointercancel', handleGlobalPointerEnd);
|
|
164
|
-
window.addEventListener('mouseup', handleGlobalPointerEnd);
|
|
198
|
+
acquireGlobalPointerEndListeners();
|
|
165
199
|
});
|
|
166
200
|
|
|
167
201
|
onDestroy(() => {
|
|
168
|
-
|
|
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);
|
|
@@ -182,7 +182,15 @@
|
|
|
182
182
|
const rawFrom = Math.max(0, startIndex - overscan);
|
|
183
183
|
const rawTo = Math.min(rowCount - 1, endIndex + overscan);
|
|
184
184
|
const from = Math.max(0, Math.floor(rawFrom / block) * block);
|
|
185
|
-
|
|
185
|
+
// With `recycle` the window keeps one length and moves by whole blocks. Its two edges then move
|
|
186
|
+
// in the same step, so the rows that leave one edge are the rows that enter the other. With
|
|
187
|
+
// edges that snap apart, the window grew at one edge and shrank at the other at different
|
|
188
|
+
// steps, and each growth built a row that the next shrink destroyed.
|
|
189
|
+
const length = visibleCount + 2 * overscan + block + 2;
|
|
190
|
+
const to =
|
|
191
|
+
virtualizer.recycle === true
|
|
192
|
+
? Math.min(rowCount - 1, from + length - 1)
|
|
193
|
+
: Math.max(from, Math.min(rowCount - 1, Math.ceil((rawTo + 1) / block) * block - 1));
|
|
186
194
|
|
|
187
195
|
const topSpacerHeight = offsets ? offsets[from] : from * rowHeight;
|
|
188
196
|
const bottomSpacerHeight = offsets
|
|
@@ -87,9 +87,12 @@
|
|
|
87
87
|
const visibleColumnIndex = $derived(column?.visibleColumnIndex ?? -1);
|
|
88
88
|
const pinState = $derived.by(() => {
|
|
89
89
|
void table.layoutEpoch;
|
|
90
|
-
void table.widthEpoch;
|
|
91
90
|
const columnId = resolvedColumn?.id;
|
|
92
|
-
|
|
91
|
+
// A pinned cell follows the column widths, as its offset is made of them. A cell that is not
|
|
92
|
+
// pinned has no offset, so a column drag does not have to ask it again at every move.
|
|
93
|
+
if (!columnId || table.getColumnPin(columnId) === undefined) return null;
|
|
94
|
+
void table.widthEpoch;
|
|
95
|
+
return table.getColumnPinState(columnId);
|
|
93
96
|
});
|
|
94
97
|
const tagName = $derived(row.section === 'body' && resolvedColumn?.isRowHeader ? 'th' : 'td');
|
|
95
98
|
const role = $derived.by(() => {
|
|
@@ -164,18 +167,6 @@
|
|
|
164
167
|
);
|
|
165
168
|
}
|
|
166
169
|
|
|
167
|
-
function handleDoubleClick() {
|
|
168
|
-
if (row.section !== 'body') return;
|
|
169
|
-
if (isRowDisabled) return;
|
|
170
|
-
focusPressTarget();
|
|
171
|
-
table.pressRow(
|
|
172
|
-
row.rowId as TableSelectionKey | undefined,
|
|
173
|
-
'pointer-double',
|
|
174
|
-
{},
|
|
175
|
-
row.isDisabled
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
170
|
function handleMouseDown(event: MouseEvent) {
|
|
180
171
|
trackInteractionModality(event, element ?? null);
|
|
181
172
|
table.setFocusVisible(false);
|
|
@@ -251,7 +242,6 @@
|
|
|
251
242
|
style:display={isColumnHidden ? 'none' : 'table-cell'}
|
|
252
243
|
onfocus={isFocusTarget ? handleFocus : undefined}
|
|
253
244
|
onclick={row.section === 'body' ? handleClick : undefined}
|
|
254
|
-
ondblclick={row.section === 'body' ? handleDoubleClick : undefined}
|
|
255
245
|
onmousedown={row.section === 'body' ? handleMouseDown : undefined}
|
|
256
246
|
onkeydown={isFocusTarget ? handleKeyDown : undefined}
|
|
257
247
|
{...restProps}
|
|
@@ -82,9 +82,12 @@
|
|
|
82
82
|
const visibleColumnIndex = $derived(column?.visibleColumnIndex ?? -1);
|
|
83
83
|
const pinState = $derived.by(() => {
|
|
84
84
|
void table.layoutEpoch;
|
|
85
|
-
void table.widthEpoch;
|
|
86
85
|
const columnId = resolvedColumn?.id;
|
|
87
|
-
|
|
86
|
+
// A pinned cell follows the column widths, as its offset is made of them. A cell that is not
|
|
87
|
+
// pinned has no offset, so a column drag does not have to ask it again at every move.
|
|
88
|
+
if (!columnId || table.getColumnPin(columnId) === undefined) return null;
|
|
89
|
+
void table.widthEpoch;
|
|
90
|
+
return table.getColumnPinState(columnId);
|
|
88
91
|
});
|
|
89
92
|
const tagName = $derived(row.section === 'body' && resolvedColumn?.isRowHeader ? 'th' : 'td');
|
|
90
93
|
const role = $derived.by(() => {
|
|
@@ -244,6 +247,7 @@
|
|
|
244
247
|
? 'true'
|
|
245
248
|
: undefined}
|
|
246
249
|
data-disabled={isRowDisabled || undefined}
|
|
250
|
+
data-table-interactive-cell=""
|
|
247
251
|
data-column-index={visibleColumnIndex >= 0 ? visibleColumnIndex : undefined}
|
|
248
252
|
data-pinned={pinState?.side}
|
|
249
253
|
data-pin-edge={pinState?.isEdge ? 'true' : undefined}
|
|
@@ -211,6 +211,16 @@ export type TableContext = {
|
|
|
211
211
|
hasResizableColumns: () => boolean;
|
|
212
212
|
registerRow: (row: TableRowRegistration) => void;
|
|
213
213
|
unregisterRow: (token: string) => void;
|
|
214
|
+
/**
|
|
215
|
+
* Calls `callback` when the children of `element` (a row) change, and returns the function that
|
|
216
|
+
* stops it. Every row shares one `MutationObserver`.
|
|
217
|
+
*/
|
|
218
|
+
observeRowChildren: (element: HTMLElement, callback: () => void) => () => void;
|
|
219
|
+
/**
|
|
220
|
+
* Presses the body row that holds `target` as a double click does. `Table.Root` listens once
|
|
221
|
+
* for `dblclick` and passes what it hit here, instead of every cell carrying a listener.
|
|
222
|
+
*/
|
|
223
|
+
pressRowFromDoubleClick: (target: Element) => void;
|
|
214
224
|
setLogicalBodyRows: (ids?: Iterable<TableSelectionKey>) => void;
|
|
215
225
|
/**
|
|
216
226
|
* Declares which logical body rows are section rows. `Table.Body` calls it
|
|
@@ -918,6 +918,8 @@ export function createTableContext(options = {}) {
|
|
|
918
918
|
})
|
|
919
919
|
.at(-1)?.id;
|
|
920
920
|
}
|
|
921
|
+
// The column whose drag already found nothing to freeze.
|
|
922
|
+
let preparedResizeColumnId = null;
|
|
921
923
|
function prepareColumnWidthsForResize(activeColumnId) {
|
|
922
924
|
const next = new SvelteMap(columnWidths);
|
|
923
925
|
const baselineWidths = new SvelteMap();
|
|
@@ -950,8 +952,12 @@ export function createTableContext(options = {}) {
|
|
|
950
952
|
}
|
|
951
953
|
}
|
|
952
954
|
}
|
|
953
|
-
if (!changed)
|
|
955
|
+
if (!changed) {
|
|
956
|
+
// Nothing to freeze: the widths are already the ones the drag starts from. Said once, or
|
|
957
|
+
// every move of the drag measured every header again.
|
|
958
|
+
preparedResizeColumnId = activeColumnId;
|
|
954
959
|
return;
|
|
960
|
+
}
|
|
955
961
|
columnWidths.clear();
|
|
956
962
|
for (const [columnId, width] of next) {
|
|
957
963
|
columnWidths.set(columnId, width);
|
|
@@ -979,6 +985,8 @@ export function createTableContext(options = {}) {
|
|
|
979
985
|
return false;
|
|
980
986
|
if (!hasRelativeVisibleColumnWidths())
|
|
981
987
|
return false;
|
|
988
|
+
if (preparedResizeColumnId === columnId)
|
|
989
|
+
return false;
|
|
982
990
|
return resizeSession?.activeColumnId !== columnId;
|
|
983
991
|
}
|
|
984
992
|
function setColumnWidths(widths) {
|
|
@@ -1218,6 +1226,7 @@ export function createTableContext(options = {}) {
|
|
|
1218
1226
|
if (!isColumnResizable(columnId) || resizingColumnId === columnId)
|
|
1219
1227
|
return;
|
|
1220
1228
|
resizeSession = null;
|
|
1229
|
+
preparedResizeColumnId = null;
|
|
1221
1230
|
resizingColumnId = columnId;
|
|
1222
1231
|
options.onColumnResizeStart?.(columnId);
|
|
1223
1232
|
}
|
|
@@ -1236,6 +1245,7 @@ export function createTableContext(options = {}) {
|
|
|
1236
1245
|
restoredFlexibleTail = true;
|
|
1237
1246
|
}
|
|
1238
1247
|
resizingColumnId = null;
|
|
1248
|
+
preparedResizeColumnId = null;
|
|
1239
1249
|
if (restoredFlexibleTail) {
|
|
1240
1250
|
options.onColumnWidthsChange?.(getColumnWidths());
|
|
1241
1251
|
}
|
|
@@ -1375,6 +1385,66 @@ export function createTableContext(options = {}) {
|
|
|
1375
1385
|
return false;
|
|
1376
1386
|
return declaredSectionRowKeys.has(id) || registeredSectionRowKeys.has(id);
|
|
1377
1387
|
}
|
|
1388
|
+
let rowChildrenObserver;
|
|
1389
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- plain bookkeeping, not state
|
|
1390
|
+
const rowChildrenCallbacks = new Map();
|
|
1391
|
+
function observeRowChildren(element, callback) {
|
|
1392
|
+
rowChildrenObserver ??= new MutationObserver((records) => {
|
|
1393
|
+
// A row that changed several times in one batch is told once.
|
|
1394
|
+
// eslint-disable-next-line svelte/prefer-svelte-reactivity -- a throwaway set, not state
|
|
1395
|
+
const told = new Set();
|
|
1396
|
+
for (const record of records) {
|
|
1397
|
+
if (told.has(record.target))
|
|
1398
|
+
continue;
|
|
1399
|
+
told.add(record.target);
|
|
1400
|
+
rowChildrenCallbacks.get(record.target)?.();
|
|
1401
|
+
}
|
|
1402
|
+
});
|
|
1403
|
+
rowChildrenCallbacks.set(element, callback);
|
|
1404
|
+
rowChildrenObserver.observe(element, { childList: true });
|
|
1405
|
+
return () => {
|
|
1406
|
+
rowChildrenCallbacks.delete(element);
|
|
1407
|
+
// An observer cannot let go of one node, only of all: a gone row has no callback, so the
|
|
1408
|
+
// records it still queues are dropped, and the observer ends with the last row.
|
|
1409
|
+
if (rowChildrenCallbacks.size === 0) {
|
|
1410
|
+
rowChildrenObserver?.disconnect();
|
|
1411
|
+
rowChildrenObserver = undefined;
|
|
1412
|
+
}
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
function pressRowFromDoubleClick(target) {
|
|
1416
|
+
const cellElement = target.closest('td, th');
|
|
1417
|
+
// An interactive cell answers its own double click, and only when it is the target.
|
|
1418
|
+
if (cellElement?.hasAttribute('data-table-interactive-cell'))
|
|
1419
|
+
return;
|
|
1420
|
+
const rowElement = target.closest('tr');
|
|
1421
|
+
if (!rowElement)
|
|
1422
|
+
return;
|
|
1423
|
+
let found;
|
|
1424
|
+
for (const row of rows.values()) {
|
|
1425
|
+
if (row.element === rowElement) {
|
|
1426
|
+
found = row;
|
|
1427
|
+
break;
|
|
1428
|
+
}
|
|
1429
|
+
}
|
|
1430
|
+
if (!found || found.section !== 'body' || found.isSection)
|
|
1431
|
+
return;
|
|
1432
|
+
if (isRowDisabled(found.id, found.disabled))
|
|
1433
|
+
return;
|
|
1434
|
+
// What a press on the cell did first: the focus goes where the keyboard carries on from.
|
|
1435
|
+
if (keyboardNavigation === 'grid') {
|
|
1436
|
+
for (const cell of cells.values()) {
|
|
1437
|
+
if (cell.element === cellElement) {
|
|
1438
|
+
focusCellByKey(cell.key);
|
|
1439
|
+
break;
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
else if (keyboardNavigation === 'row') {
|
|
1444
|
+
focusRowByToken(found.token, 'start');
|
|
1445
|
+
}
|
|
1446
|
+
pressRow(found.id, 'pointer-double', {}, found.disabled);
|
|
1447
|
+
}
|
|
1378
1448
|
function unregisterRow(token) {
|
|
1379
1449
|
defaultFocusKeyCache = undefined;
|
|
1380
1450
|
const row = rows.get(token);
|
|
@@ -2546,6 +2616,8 @@ export function createTableContext(options = {}) {
|
|
|
2546
2616
|
hasResizableColumns,
|
|
2547
2617
|
registerRow: asCommand(registerRow),
|
|
2548
2618
|
unregisterRow: asCommand(unregisterRow),
|
|
2619
|
+
observeRowChildren,
|
|
2620
|
+
pressRowFromDoubleClick: asCommand(pressRowFromDoubleClick),
|
|
2549
2621
|
setLogicalBodyRows: asCommand(setLogicalBodyRows),
|
|
2550
2622
|
setSectionRowKeys: asCommand(setSectionRowKeys),
|
|
2551
2623
|
isSectionRowKey,
|
|
@@ -547,6 +547,11 @@
|
|
|
547
547
|
function handleKeyDown(event: KeyboardEvent) {
|
|
548
548
|
trackInteractionModality(event, event.target as HTMLElement | null);
|
|
549
549
|
}
|
|
550
|
+
|
|
551
|
+
// One listener for the table: a body cell used to carry its own, and a table draws thousands.
|
|
552
|
+
function handleDoubleClick(event: MouseEvent) {
|
|
553
|
+
if (event.target instanceof Element) ctx.pressRowFromDoubleClick(event.target);
|
|
554
|
+
}
|
|
550
555
|
</script>
|
|
551
556
|
|
|
552
557
|
<table
|
|
@@ -585,6 +590,7 @@
|
|
|
585
590
|
onfocusout={handleFocusOut}
|
|
586
591
|
onmousedown={handleMouseDown}
|
|
587
592
|
onkeydown={handleKeyDown}
|
|
593
|
+
ondblclick={handleDoubleClick}
|
|
588
594
|
{...restProps}
|
|
589
595
|
>
|
|
590
596
|
{#if layoutColumns.length > 0}
|
|
@@ -214,7 +214,9 @@
|
|
|
214
214
|
});
|
|
215
215
|
}
|
|
216
216
|
|
|
217
|
-
|
|
217
|
+
// One observer for every row of the table: a body row used to make its own, and a table keeps
|
|
218
|
+
// a few dozen mounted and swaps them as it scrolls.
|
|
219
|
+
const stopObserving = table.observeRowChildren(rowElement, () => {
|
|
218
220
|
if (isBodyRow && !bodyCellOrderSealed) {
|
|
219
221
|
return;
|
|
220
222
|
}
|
|
@@ -231,11 +233,10 @@
|
|
|
231
233
|
|
|
232
234
|
scheduleCellOrderNotify();
|
|
233
235
|
});
|
|
234
|
-
observer.observe(rowElement, { childList: true });
|
|
235
236
|
|
|
236
237
|
return () => {
|
|
237
238
|
disposed = true;
|
|
238
|
-
|
|
239
|
+
stopObserving();
|
|
239
240
|
};
|
|
240
241
|
});
|
|
241
242
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@human-kit/ui",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.2",
|
|
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>",
|