@zhangt58/svelte-vtable 0.4.3 → 0.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.
- package/README.md +149 -29
- package/dist/components/DataTable.svelte +528 -16
- package/dist/components/DataTable.svelte.d.ts +13 -1
- package/dist/components/DataTableControls.svelte +142 -7
- package/dist/components/flowbite/DataTableControls.svelte +142 -7
- package/dist/index.d.ts +29 -0
- package/dist/index.js +14 -0
- package/dist/styles.css +215 -61
- package/package.json +3 -3
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
<script>
|
|
2
|
+
import { untrack } from 'svelte';
|
|
2
3
|
import { VirtualList } from 'svelte-virtuallists';
|
|
3
4
|
import ColumnHeaderFilter from './filters/ColumnHeaderFilter.svelte';
|
|
4
5
|
import { useFilterState } from './filters/useFilterState.svelte.js';
|
|
@@ -51,6 +52,21 @@
|
|
|
51
52
|
// If `virtualize` is false we ask the wrapped VirtualList to render as a normal list/table
|
|
52
53
|
// by setting its `isDisabled` prop. Default: true (virtualization enabled).
|
|
53
54
|
virtualize = true,
|
|
55
|
+
// Row density: 'comfortable' (default) or 'compact' (tighter cell padding).
|
|
56
|
+
density = 'comfortable',
|
|
57
|
+
// Drag a header's right edge to resize its column (ColumnDef.resizable: false opts out).
|
|
58
|
+
resizable = false,
|
|
59
|
+
// User-set column widths in px, keyed by column key. Bindable; empty means
|
|
60
|
+
// the declared widths. Once any column is resized the table switches to a
|
|
61
|
+
// fixed layout whose width is the sum of the column widths.
|
|
62
|
+
columnWidths = $bindable({}),
|
|
63
|
+
// oncolumnresize({ key, width, widths }) — called when a resize ends.
|
|
64
|
+
oncolumnresize = undefined,
|
|
65
|
+
// When set, columnWidths are saved to and restored from localStorage under this key.
|
|
66
|
+
storageKey = undefined,
|
|
67
|
+
// Wrap long cell text (default). When false, cells stay on one line and
|
|
68
|
+
// truncated text is shown in a tooltip. ColumnDef.wrap overrides per column.
|
|
69
|
+
wrap = true,
|
|
54
70
|
} = $props();
|
|
55
71
|
|
|
56
72
|
// --- Derived values from ColumnDef[] ---
|
|
@@ -66,6 +82,300 @@
|
|
|
66
82
|
// Fast lookup map: key → ColumnDef.
|
|
67
83
|
const colDefMap = $derived(Object.fromEntries(columns.map((c) => [c.key, c])));
|
|
68
84
|
|
|
85
|
+
// --- Sticky columns ---
|
|
86
|
+
// Header cell widths, measured (bind:offsetWidth), so the sticky offsets hold
|
|
87
|
+
// whatever the width type (stretch weight or CSS length). Also the starting
|
|
88
|
+
// point of the first column resize.
|
|
89
|
+
let headerWidths = $state({});
|
|
90
|
+
|
|
91
|
+
// key → { side, offset, edge } for columns with `sticky: 'left' | 'right'`.
|
|
92
|
+
// A left column's offset is the width of the left sticky columns before it;
|
|
93
|
+
// a right column's, of the right sticky columns after it. `edge` marks the
|
|
94
|
+
// innermost column of each side, which draws the divider shadow.
|
|
95
|
+
const stickyMap = $derived.by(() => {
|
|
96
|
+
const map = {};
|
|
97
|
+
const left = columns.filter((c) => c.sticky === 'left');
|
|
98
|
+
const right = columns.filter((c) => c.sticky === 'right').reverse();
|
|
99
|
+
let acc = 0;
|
|
100
|
+
left.forEach((c, i) => {
|
|
101
|
+
map[c.key] = { side: 'left', offset: acc, edge: i === left.length - 1 };
|
|
102
|
+
acc += headerWidths[c.key] ?? 0;
|
|
103
|
+
});
|
|
104
|
+
acc = 0;
|
|
105
|
+
right.forEach((c, i) => {
|
|
106
|
+
map[c.key] = { side: 'right', offset: acc, edge: i === right.length - 1 };
|
|
107
|
+
acc += headerWidths[c.key] ?? 0;
|
|
108
|
+
});
|
|
109
|
+
return map;
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
function stickyClass(key) {
|
|
113
|
+
const st = stickyMap[key];
|
|
114
|
+
if (!st) return '';
|
|
115
|
+
return `vtable-sticky vtable-sticky-${st.side}${st.edge ? ` vtable-sticky-edge-${st.side}` : ''}`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// A column's minimum width: ColumnDef.minWidth, else a CSS-length width.
|
|
119
|
+
// Set on the header content, it is a real minimum (a <col> width is only a
|
|
120
|
+
// hint under automatic table layout), so wide tables scroll sideways
|
|
121
|
+
// instead of squeezing their columns.
|
|
122
|
+
function declaredMinWidth(key) {
|
|
123
|
+
const c = colDefMap[key];
|
|
124
|
+
return (
|
|
125
|
+
c?.minWidth ??
|
|
126
|
+
(typeof c?.width === 'string' && !c.width.trim().endsWith('%') ? c.width : null)
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function minWidthStyle(key) {
|
|
131
|
+
// Under the fixed layout the column width already includes the minimum.
|
|
132
|
+
if (fixedLayout) return '';
|
|
133
|
+
const w = declaredMinWidth(key);
|
|
134
|
+
return w ? `min-width: ${w};` : '';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function stickyStyle(key) {
|
|
138
|
+
const st = stickyMap[key];
|
|
139
|
+
return st ? `${st.side}: ${st.offset}px;` : '';
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Horizontal scroll position, so the sticky edges cast a shadow only while
|
|
143
|
+
// columns are scrolled under them.
|
|
144
|
+
let scroller = $state(null);
|
|
145
|
+
let scrolledFromStart = $state(false);
|
|
146
|
+
let scrolledToEnd = $state(true);
|
|
147
|
+
|
|
148
|
+
function updateHorizontalScroll() {
|
|
149
|
+
if (!scroller) return;
|
|
150
|
+
const { scrollLeft, scrollWidth, clientWidth } = scroller;
|
|
151
|
+
scrolledFromStart = scrollLeft > 0;
|
|
152
|
+
scrolledToEnd = scrollLeft + clientWidth >= scrollWidth - 1;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
$effect(() => {
|
|
156
|
+
if (!scroller || typeof ResizeObserver === 'undefined') return;
|
|
157
|
+
const ro = new ResizeObserver(updateHorizontalScroll);
|
|
158
|
+
ro.observe(scroller);
|
|
159
|
+
const table = scroller.querySelector('table');
|
|
160
|
+
if (table) ro.observe(table);
|
|
161
|
+
updateHorizontalScroll();
|
|
162
|
+
return () => ro.disconnect();
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// --- Resizable columns ---
|
|
166
|
+
// Floor for user widths, so a column stays wide enough to grab again. A
|
|
167
|
+
// declared minWidth does not apply: it sizes the declared layout only, and
|
|
168
|
+
// the user may resize below it.
|
|
169
|
+
const MIN_RESIZE_WIDTH = 40;
|
|
170
|
+
// Width of a column that has no user width under the fixed layout and
|
|
171
|
+
// declares no CSS width either (a stretch weight has no meaning there).
|
|
172
|
+
const DEFAULT_FIXED_WIDTH = '10rem';
|
|
173
|
+
const STORAGE_PREFIX = 'svelte-vtable:column-widths:';
|
|
174
|
+
|
|
175
|
+
// { key, startX, startWidth } while a header edge is dragged.
|
|
176
|
+
let resizing = $state(null);
|
|
177
|
+
|
|
178
|
+
const fixedLayout = $derived(resizable && Object.keys(columnWidths ?? {}).length > 0);
|
|
179
|
+
|
|
180
|
+
function isResizable(key) {
|
|
181
|
+
return resizable && colDefMap[key]?.resizable !== false;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Column width under the fixed layout: the user width, else the declared
|
|
185
|
+
// CSS width or minimum.
|
|
186
|
+
function fixedColWidth(key) {
|
|
187
|
+
const px = columnWidths?.[key];
|
|
188
|
+
if (typeof px === 'number' && px > 0) return `${Math.max(MIN_RESIZE_WIDTH, px)}px`;
|
|
189
|
+
return declaredMinWidth(key) ?? DEFAULT_FIXED_WIDTH;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const fixedTableWidth = $derived(
|
|
193
|
+
fixedLayout ? `calc(${effectiveVisibleKeys.map(fixedColWidth).join(' + ')})` : '',
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
function readStoredWidths(key) {
|
|
197
|
+
try {
|
|
198
|
+
const parsed = JSON.parse(localStorage.getItem(STORAGE_PREFIX + key) ?? 'null');
|
|
199
|
+
return parsed && typeof parsed === 'object' && !Array.isArray(parsed) ? parsed : null;
|
|
200
|
+
} catch (e) {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
function writeStoredWidths(key, widths) {
|
|
206
|
+
try {
|
|
207
|
+
if (Object.keys(widths).length === 0) localStorage.removeItem(STORAGE_PREFIX + key);
|
|
208
|
+
else localStorage.setItem(STORAGE_PREFIX + key, JSON.stringify(widths));
|
|
209
|
+
} catch (e) {}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Restore saved widths before the first paint (and when the key changes).
|
|
213
|
+
$effect.pre(() => {
|
|
214
|
+
if (!storageKey) return;
|
|
215
|
+
const saved = readStoredWidths(storageKey);
|
|
216
|
+
untrack(() => {
|
|
217
|
+
columnWidths = saved ?? {};
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// Save widths when they change, but not on every pointer move of a drag.
|
|
222
|
+
$effect(() => {
|
|
223
|
+
if (!storageKey || resizing) return;
|
|
224
|
+
writeStoredWidths(storageKey, $state.snapshot(columnWidths) ?? {});
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// The first resize pins every column at its current width, so switching to
|
|
228
|
+
// the fixed layout does not move the other columns.
|
|
229
|
+
function ensureFixedWidths() {
|
|
230
|
+
if (fixedLayout) return;
|
|
231
|
+
columnWidths = Object.fromEntries(
|
|
232
|
+
effectiveVisibleKeys.map((k) => [k, Math.round(headerWidths[k] ?? 0)]).filter(([, w]) => w),
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function setColumnWidth(key, width) {
|
|
237
|
+
columnWidths = { ...columnWidths, [key]: width };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function emitResize(key) {
|
|
241
|
+
try {
|
|
242
|
+
if (oncolumnresize)
|
|
243
|
+
oncolumnresize({ key, width: columnWidths[key], widths: $state.snapshot(columnWidths) });
|
|
244
|
+
} catch (err) {
|
|
245
|
+
try {
|
|
246
|
+
console.error('oncolumnresize threw:', err);
|
|
247
|
+
} catch (e) {}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function startResize(event, key) {
|
|
252
|
+
if (event.button !== 0) return;
|
|
253
|
+
event.preventDefault();
|
|
254
|
+
event.stopPropagation();
|
|
255
|
+
const handle = event.currentTarget;
|
|
256
|
+
const th = handle.closest('th');
|
|
257
|
+
ensureFixedWidths();
|
|
258
|
+
handle.setPointerCapture?.(event.pointerId);
|
|
259
|
+
resizing = {
|
|
260
|
+
key,
|
|
261
|
+
startX: event.clientX,
|
|
262
|
+
startWidth: th.offsetWidth,
|
|
263
|
+
};
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function moveResize(event) {
|
|
267
|
+
if (!resizing) return;
|
|
268
|
+
const { key, startX, startWidth } = resizing;
|
|
269
|
+
setColumnWidth(
|
|
270
|
+
key,
|
|
271
|
+
Math.max(MIN_RESIZE_WIDTH, Math.round(startWidth + event.clientX - startX)),
|
|
272
|
+
);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function endResize() {
|
|
276
|
+
if (!resizing) return;
|
|
277
|
+
const { key } = resizing;
|
|
278
|
+
resizing = null;
|
|
279
|
+
emitResize(key);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
// Double-clicking a header edge drops the user widths: the table returns to
|
|
283
|
+
// its declared layout. (A single column has no width to return to under the
|
|
284
|
+
// fixed layout when it declares only a stretch weight.)
|
|
285
|
+
function resetColumnWidths(key) {
|
|
286
|
+
if (!fixedLayout) return;
|
|
287
|
+
columnWidths = {};
|
|
288
|
+
emitResize(key);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// --- Grouped headers ---
|
|
292
|
+
// Runs of adjacent columns sharing ColumnDef.group, rendered as a header row
|
|
293
|
+
// above the column headers. A run is split where its columns' sticky side
|
|
294
|
+
// changes, so the sticky part can stick with its columns; only the first
|
|
295
|
+
// part shows the label.
|
|
296
|
+
const groupSegments = $derived.by(() => {
|
|
297
|
+
if (!columns.some((c) => c.group)) return [];
|
|
298
|
+
const segments = [];
|
|
299
|
+
for (const c of columns) {
|
|
300
|
+
const group = c.group || null;
|
|
301
|
+
const side = stickyMap[c.key]?.side ?? null;
|
|
302
|
+
const last = segments[segments.length - 1];
|
|
303
|
+
if (last && last.group === group && last.side === side) {
|
|
304
|
+
last.keys.push(c.key);
|
|
305
|
+
} else {
|
|
306
|
+
const cont = !!last && group !== null && last.group === group;
|
|
307
|
+
segments.push({ group, side, keys: [c.key], cont });
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return segments;
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
// Height of the group header row: the column header row sticks below it.
|
|
314
|
+
let groupRowHeight = $state(0);
|
|
315
|
+
|
|
316
|
+
function groupStickyKey(segment) {
|
|
317
|
+
if (!segment.side) return null;
|
|
318
|
+
return segment.side === 'left' ? segment.keys[0] : segment.keys[segment.keys.length - 1];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
function groupClass(segment) {
|
|
322
|
+
const base = segment.group
|
|
323
|
+
? `vtable-group-header${segment.cont ? ' vtable-group-cont' : ''}`
|
|
324
|
+
: 'vtable-group-header vtable-group-empty';
|
|
325
|
+
if (!segment.side) return base;
|
|
326
|
+
const edge = segment.keys.some((k) => stickyMap[k]?.edge);
|
|
327
|
+
return `${base} vtable-sticky vtable-sticky-${segment.side}${edge ? ` vtable-sticky-edge-${segment.side}` : ''}`;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// --- Truncated text ---
|
|
331
|
+
function cellWraps(col) {
|
|
332
|
+
return col.wrap ?? wrap;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function isTruncated(el) {
|
|
336
|
+
return (
|
|
337
|
+
el.clientWidth > 0 &&
|
|
338
|
+
(el.scrollWidth > el.clientWidth + 1 || el.scrollHeight > el.clientHeight + 1)
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// On hover, give a body cell (or header label) whose text is cut off a
|
|
343
|
+
// tooltip with the full text. Checks the hovered element and its ancestors
|
|
344
|
+
// up to the cell, so truncation inside custom cell snippets counts too.
|
|
345
|
+
function revealTruncated(event) {
|
|
346
|
+
const target = /** @type {Element} */ (event.target);
|
|
347
|
+
if (!target?.closest) return;
|
|
348
|
+
const label = target.closest('.vtable-header-label');
|
|
349
|
+
if (label) {
|
|
350
|
+
setAutoTitle(label, isTruncated(label) ? label.textContent : null);
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const td = target.closest('td');
|
|
354
|
+
if (!td || !scroller?.contains(td)) return;
|
|
355
|
+
let text = null;
|
|
356
|
+
for (let el = target; el; el = el.parentElement) {
|
|
357
|
+
if (isTruncated(el)) {
|
|
358
|
+
text = el.textContent;
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
if (el === td) break;
|
|
362
|
+
}
|
|
363
|
+
setAutoTitle(td, text);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Only touch titles this component set, never a consumer's.
|
|
367
|
+
function setAutoTitle(el, text) {
|
|
368
|
+
const full = text?.replace(/\s+/g, ' ').trim();
|
|
369
|
+
if (full) {
|
|
370
|
+
if (el.hasAttribute('title') && !el.hasAttribute('data-vtable-auto-title')) return;
|
|
371
|
+
el.setAttribute('title', full);
|
|
372
|
+
el.setAttribute('data-vtable-auto-title', '');
|
|
373
|
+
} else if (el.hasAttribute('data-vtable-auto-title')) {
|
|
374
|
+
el.removeAttribute('title');
|
|
375
|
+
el.removeAttribute('data-vtable-auto-title');
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
69
379
|
const effectiveColumnFilters = $derived.by(() => {
|
|
70
380
|
return resolveColumnFilters({
|
|
71
381
|
columnFilters,
|
|
@@ -230,6 +540,7 @@
|
|
|
230
540
|
|
|
231
541
|
// Compute column width by key (fallback to even distribution)
|
|
232
542
|
function calcColWidth(key) {
|
|
543
|
+
if (fixedLayout) return fixedColWidth(key);
|
|
233
544
|
if (colWidthMap[key] !== undefined) return String(colWidthMap[key]);
|
|
234
545
|
const n = Math.max(1, effectiveVisibleKeys.length);
|
|
235
546
|
return Math.floor(100 / n) + '%';
|
|
@@ -238,7 +549,19 @@
|
|
|
238
549
|
|
|
239
550
|
<svelte:window onclick={closeFilter} onkeydown={handleKeydown} />
|
|
240
551
|
|
|
241
|
-
|
|
552
|
+
<!-- pointerover only adds tooltips to truncated text -->
|
|
553
|
+
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
|
554
|
+
<section
|
|
555
|
+
class="virtual-data-table {className}{fixedLayout ? ' vtable-fixed-layout' : ''}{resizing
|
|
556
|
+
? ' vtable-resizing'
|
|
557
|
+
: ''}"
|
|
558
|
+
style="{style}{fixedTableWidth ? `; --vtable-table-width: ${fixedTableWidth}` : ''}"
|
|
559
|
+
bind:this={scroller}
|
|
560
|
+
onscroll={updateHorizontalScroll}
|
|
561
|
+
onpointerover={revealTruncated}
|
|
562
|
+
data-scrolled-start={scrolledFromStart ? '' : undefined}
|
|
563
|
+
data-scrolled-end={scrolledToEnd ? undefined : ''}
|
|
564
|
+
>
|
|
242
565
|
{#if columns.length > 0}
|
|
243
566
|
<!-- When `virtualize` is false we instruct VirtualList to render as a normal list/table
|
|
244
567
|
by setting `isDisabled`. This ensures pagination based on item counts shows the
|
|
@@ -247,7 +570,9 @@
|
|
|
247
570
|
items={Array.isArray(sortedItems) ? sortedItems : []}
|
|
248
571
|
isTable
|
|
249
572
|
isDisabled={!virtualize}
|
|
250
|
-
class="datatable-table max-w-full max-h-full overflow-auto
|
|
573
|
+
class="datatable-table max-w-full max-h-full overflow-auto{density === 'compact'
|
|
574
|
+
? ' compact'
|
|
575
|
+
: ''}"
|
|
251
576
|
>
|
|
252
577
|
<!-- colgroup enforces the column widths so table-layout: fixed distributes as intended -->
|
|
253
578
|
{#snippet header()}
|
|
@@ -257,6 +582,23 @@
|
|
|
257
582
|
{/each}
|
|
258
583
|
</colgroup>
|
|
259
584
|
<thead class="sticky-header">
|
|
585
|
+
{#if groupSegments.length > 0}
|
|
586
|
+
<tr class="vtable-group-row" bind:offsetHeight={groupRowHeight}>
|
|
587
|
+
{#each groupSegments as segment}
|
|
588
|
+
{@const stickyKey = groupStickyKey(segment)}
|
|
589
|
+
<th
|
|
590
|
+
colspan={segment.keys.length}
|
|
591
|
+
class={groupClass(segment)}
|
|
592
|
+
style={stickyKey ? stickyStyle(stickyKey) : undefined}
|
|
593
|
+
scope={segment.group ? 'colgroup' : undefined}
|
|
594
|
+
>
|
|
595
|
+
{#if segment.group && !segment.cont}
|
|
596
|
+
<span class="vtable-header-label">{segment.group}</span>
|
|
597
|
+
{/if}
|
|
598
|
+
</th>
|
|
599
|
+
{/each}
|
|
600
|
+
</tr>
|
|
601
|
+
{/if}
|
|
260
602
|
<tr>
|
|
261
603
|
{#each effectiveVisibleKeys as key, columnIndex}
|
|
262
604
|
{@const colDef = colDefMap[key]}
|
|
@@ -266,15 +608,21 @@
|
|
|
266
608
|
{@const selection = filterDef ? filterState.selections[filterDef.key] : undefined}
|
|
267
609
|
{@const isFilterActive = hasActiveFilterValue(selection)}
|
|
268
610
|
<th
|
|
269
|
-
style="width: {calcColWidth(key)}
|
|
270
|
-
|
|
611
|
+
style="width: {calcColWidth(key)}; {stickyStyle(key)}{groupSegments.length > 0
|
|
612
|
+
? ` top: ${groupRowHeight}px;`
|
|
613
|
+
: ''}"
|
|
614
|
+
class="select-none {stickyClass(key)}{filterDef && openFilterKey === filterDef.key
|
|
615
|
+
? ' vtable-filter-open'
|
|
616
|
+
: ''}"
|
|
617
|
+
data-key={key}
|
|
618
|
+
bind:offsetWidth={headerWidths[key]}
|
|
271
619
|
aria-sort={sortKey === key
|
|
272
620
|
? sortDir === 'asc'
|
|
273
621
|
? 'ascending'
|
|
274
622
|
: 'descending'
|
|
275
623
|
: 'none'}
|
|
276
624
|
>
|
|
277
|
-
<div class="vtable-header-content">
|
|
625
|
+
<div class="vtable-header-content" style={minWidthStyle(key) || undefined}>
|
|
278
626
|
{#if colDef?.headerSnippet}
|
|
279
627
|
<div class="min-w-0 flex-1">
|
|
280
628
|
{@render colDef.headerSnippet({ key, label, sortKey, sortDir })}
|
|
@@ -285,13 +633,13 @@
|
|
|
285
633
|
class="vtable-header-sort-button min-w-0 flex-1 cursor-pointer"
|
|
286
634
|
onclick={() => handleSort(key)}
|
|
287
635
|
>
|
|
288
|
-
<span class="min-w-0 truncate">{label}</span>
|
|
636
|
+
<span class="vtable-header-label min-w-0 truncate">{label}</span>
|
|
289
637
|
{#if sortKey === key}
|
|
290
638
|
<span aria-hidden="true">{sortDir === 'asc' ? '▲' : '▼'}</span>
|
|
291
639
|
{/if}
|
|
292
640
|
</button>
|
|
293
641
|
{:else}
|
|
294
|
-
<span class="min-w-0 flex-1 truncate">{label}</span>
|
|
642
|
+
<span class="vtable-header-label min-w-0 flex-1 truncate">{label}</span>
|
|
295
643
|
{/if}
|
|
296
644
|
|
|
297
645
|
{#if inlineFilters && filterDef}
|
|
@@ -319,6 +667,27 @@
|
|
|
319
667
|
/>
|
|
320
668
|
{/if}
|
|
321
669
|
</div>
|
|
670
|
+
{#if isResizable(key)}
|
|
671
|
+
<!-- Pointer-only affordance, hidden from assistive technology -->
|
|
672
|
+
<!-- svelte-ignore a11y_no_static_element_interactions, a11y_click_events_have_key_events -->
|
|
673
|
+
<span
|
|
674
|
+
class="vtable-resize-handle{resizing?.key === key
|
|
675
|
+
? ' vtable-resize-active'
|
|
676
|
+
: ''}"
|
|
677
|
+
aria-hidden="true"
|
|
678
|
+
title="Drag to resize, double-click to reset all widths"
|
|
679
|
+
onpointerdown={(e) => startResize(e, key)}
|
|
680
|
+
onpointermove={moveResize}
|
|
681
|
+
onpointerup={endResize}
|
|
682
|
+
onpointercancel={endResize}
|
|
683
|
+
onlostpointercapture={endResize}
|
|
684
|
+
onclick={(e) => e.stopPropagation()}
|
|
685
|
+
ondblclick={(e) => {
|
|
686
|
+
e.stopPropagation();
|
|
687
|
+
resetColumnWidths(key);
|
|
688
|
+
}}
|
|
689
|
+
></span>
|
|
690
|
+
{/if}
|
|
322
691
|
</th>
|
|
323
692
|
{/each}
|
|
324
693
|
</tr>
|
|
@@ -327,9 +696,17 @@
|
|
|
327
696
|
|
|
328
697
|
{#snippet vl_slot({ index, item })}
|
|
329
698
|
<!-- Default row rendering using each column's cellSnippet or raw value -->
|
|
330
|
-
<tr
|
|
699
|
+
<tr
|
|
700
|
+
onclick={() => selectItem(item, index)}
|
|
701
|
+
class={selected === item ? 'selected selected-row' : ''}
|
|
702
|
+
>
|
|
331
703
|
{#each columns as col}
|
|
332
|
-
<td
|
|
704
|
+
<td
|
|
705
|
+
class={[stickyClass(col.key), cellWraps(col) ? '' : 'vtable-truncate']
|
|
706
|
+
.filter(Boolean)
|
|
707
|
+
.join(' ') || undefined}
|
|
708
|
+
style={stickyStyle(col.key) || undefined}
|
|
709
|
+
>
|
|
333
710
|
{#if col.cellSnippet}
|
|
334
711
|
{@render col.cellSnippet({ item, value: item[col.key], index })}
|
|
335
712
|
{:else}
|
|
@@ -359,6 +736,9 @@
|
|
|
359
736
|
--vtable-color-info: #3b82f6; /* blue-500 */
|
|
360
737
|
|
|
361
738
|
--vtable-radius: 0.375rem; /* rounded-md */
|
|
739
|
+
|
|
740
|
+
--vtable-resize-line: #cbd5e1; /* slate-300: resize separator at rest */
|
|
741
|
+
--vtable-resize-line-hover: #64748b; /* slate-500: while its header is hovered */
|
|
362
742
|
--vtable-row-height: 2.25rem; /* default row height hint for virtual scroll */
|
|
363
743
|
}
|
|
364
744
|
.virtual-data-table {
|
|
@@ -372,7 +752,6 @@
|
|
|
372
752
|
display: table;
|
|
373
753
|
width: 100%;
|
|
374
754
|
min-width: 0;
|
|
375
|
-
border-collapse: collapse;
|
|
376
755
|
box-sizing: border-box;
|
|
377
756
|
font-size: 0.875rem; /* 14px - smaller, more compact */
|
|
378
757
|
font-family:
|
|
@@ -380,12 +759,106 @@
|
|
|
380
759
|
line-height: 1.4;
|
|
381
760
|
border: none;
|
|
382
761
|
}
|
|
383
|
-
/* Sticky header
|
|
384
|
-
|
|
762
|
+
/* Sticky header: each header cell sticks (rather than the <thead>), so a
|
|
763
|
+
cell can also stick horizontally for sticky columns. */
|
|
764
|
+
:global(.datatable-table thead.sticky-header th) {
|
|
385
765
|
position: sticky;
|
|
386
766
|
top: 0;
|
|
387
767
|
z-index: 2;
|
|
388
768
|
}
|
|
769
|
+
/* separate + 0 spacing looks like collapse here (bottom borders only) but
|
|
770
|
+
keeps each border on its cell, so borders move with sticky cells. Set on
|
|
771
|
+
the inner <table> itself: Tailwind's preflight collapses every table. */
|
|
772
|
+
:global(.datatable-table table.vtlist-inner) {
|
|
773
|
+
border-collapse: separate;
|
|
774
|
+
border-spacing: 0;
|
|
775
|
+
}
|
|
776
|
+
/* Sticky columns (ColumnDef.sticky). Body cells take the row's background
|
|
777
|
+
(striping, hover, selection) so scrolled cells do not show through. The
|
|
778
|
+
<tbody> is rendered by VirtualList, hence fully global. */
|
|
779
|
+
:global(.datatable-table tbody td.vtable-sticky) {
|
|
780
|
+
position: sticky;
|
|
781
|
+
z-index: 1;
|
|
782
|
+
background-color: inherit;
|
|
783
|
+
}
|
|
784
|
+
:global(.datatable-table thead.sticky-header th.vtable-sticky) {
|
|
785
|
+
z-index: 3;
|
|
786
|
+
}
|
|
787
|
+
/* Keep an open header filter above the sticky header cells */
|
|
788
|
+
:global(.datatable-table thead.sticky-header th.vtable-filter-open) {
|
|
789
|
+
z-index: 4;
|
|
790
|
+
}
|
|
791
|
+
/* Divider shadow on the innermost sticky column, only while columns are
|
|
792
|
+
scrolled under it */
|
|
793
|
+
:global(.virtual-data-table[data-scrolled-start] .vtable-sticky-edge-left) {
|
|
794
|
+
box-shadow: 6px 0 6px -4px rgba(15, 23, 42, 0.18);
|
|
795
|
+
}
|
|
796
|
+
:global(.virtual-data-table[data-scrolled-end] .vtable-sticky-edge-right) {
|
|
797
|
+
box-shadow: -6px 0 6px -4px rgba(15, 23, 42, 0.18);
|
|
798
|
+
}
|
|
799
|
+
:global(.dark .virtual-data-table[data-scrolled-start] .vtable-sticky-edge-left) {
|
|
800
|
+
box-shadow: 6px 0 6px -4px rgba(0, 0, 0, 0.6);
|
|
801
|
+
}
|
|
802
|
+
:global(.dark .virtual-data-table[data-scrolled-end] .vtable-sticky-edge-right) {
|
|
803
|
+
box-shadow: -6px 0 6px -4px rgba(0, 0, 0, 0.6);
|
|
804
|
+
}
|
|
805
|
+
/* Resizable columns: once a column is resized the table switches to a
|
|
806
|
+
fixed layout whose width is the sum of the column widths (set by DataTable
|
|
807
|
+
as --vtable-table-width). */
|
|
808
|
+
:global(.virtual-data-table.vtable-fixed-layout table.vtlist-inner) {
|
|
809
|
+
table-layout: fixed;
|
|
810
|
+
width: var(--vtable-table-width);
|
|
811
|
+
}
|
|
812
|
+
:global(.datatable-table thead th .vtable-resize-handle) {
|
|
813
|
+
position: absolute;
|
|
814
|
+
top: 0;
|
|
815
|
+
right: 0;
|
|
816
|
+
bottom: 0;
|
|
817
|
+
width: 0.6rem;
|
|
818
|
+
cursor: col-resize;
|
|
819
|
+
touch-action: none;
|
|
820
|
+
z-index: 1;
|
|
821
|
+
}
|
|
822
|
+
/* Separator line on the header's right edge: always shown on resizable
|
|
823
|
+
columns, stronger while the header is hovered, accent-coloured and full
|
|
824
|
+
height on the handle itself or while dragging. */
|
|
825
|
+
:global(.datatable-table thead th .vtable-resize-handle::after) {
|
|
826
|
+
content: '';
|
|
827
|
+
position: absolute;
|
|
828
|
+
top: 22%;
|
|
829
|
+
bottom: 22%;
|
|
830
|
+
right: 0;
|
|
831
|
+
width: 2px;
|
|
832
|
+
border-radius: 1px;
|
|
833
|
+
background-color: var(--vtable-resize-line);
|
|
834
|
+
transition:
|
|
835
|
+
background-color 0.12s ease-in-out,
|
|
836
|
+
top 0.12s ease-in-out,
|
|
837
|
+
bottom 0.12s ease-in-out,
|
|
838
|
+
width 0.12s ease-in-out;
|
|
839
|
+
}
|
|
840
|
+
:global(.datatable-table thead th:hover .vtable-resize-handle::after) {
|
|
841
|
+
top: 12%;
|
|
842
|
+
bottom: 12%;
|
|
843
|
+
background-color: var(--vtable-resize-line-hover);
|
|
844
|
+
}
|
|
845
|
+
:global(.datatable-table thead th .vtable-resize-handle:hover::after),
|
|
846
|
+
:global(.datatable-table thead th .vtable-resize-handle.vtable-resize-active::after) {
|
|
847
|
+
top: 0;
|
|
848
|
+
bottom: 0;
|
|
849
|
+
width: 3px;
|
|
850
|
+
background-color: var(--vtable-color-accent-border);
|
|
851
|
+
}
|
|
852
|
+
:global(.dark) :global(.datatable-table thead th .vtable-resize-handle) {
|
|
853
|
+
--vtable-resize-line: #4b5563; /* gray-600 */
|
|
854
|
+
--vtable-resize-line-hover: #9ca3af; /* gray-400 */
|
|
855
|
+
}
|
|
856
|
+
/* Keep the resize cursor and stop text selection while dragging */
|
|
857
|
+
:global(.virtual-data-table.vtable-resizing),
|
|
858
|
+
:global(.virtual-data-table.vtable-resizing *) {
|
|
859
|
+
cursor: col-resize !important;
|
|
860
|
+
user-select: none;
|
|
861
|
+
}
|
|
389
862
|
/* Header visual styles - only bottom border */
|
|
390
863
|
:global(.datatable-table) thead th {
|
|
391
864
|
background-color: #f8f9fa;
|
|
@@ -464,7 +937,7 @@
|
|
|
464
937
|
}
|
|
465
938
|
/* Compact variant */
|
|
466
939
|
:global(.datatable-table.compact tbody td) {
|
|
467
|
-
padding: 0.
|
|
940
|
+
padding: 0.25rem 0.5rem;
|
|
468
941
|
}
|
|
469
942
|
/* Header content alignment helper */
|
|
470
943
|
:global(.datatable-table thead th > div) {
|
|
@@ -472,9 +945,6 @@
|
|
|
472
945
|
align-items: center;
|
|
473
946
|
gap: 0.25rem;
|
|
474
947
|
}
|
|
475
|
-
:global(.datatable-table thead th) {
|
|
476
|
-
position: relative;
|
|
477
|
-
}
|
|
478
948
|
:global(.datatable-table thead th .vtable-header-content) {
|
|
479
949
|
display: flex;
|
|
480
950
|
align-items: center;
|
|
@@ -497,6 +967,48 @@
|
|
|
497
967
|
gap: 0.25rem;
|
|
498
968
|
text-align: left;
|
|
499
969
|
}
|
|
970
|
+
/* Grouped column headers (ColumnDef.group): a header row above the column
|
|
971
|
+
headers, one cell per run of columns sharing a group. */
|
|
972
|
+
:global(.datatable-table thead th.vtable-group-header) {
|
|
973
|
+
text-align: center;
|
|
974
|
+
padding-top: 0.25rem;
|
|
975
|
+
padding-bottom: 0.25rem;
|
|
976
|
+
font-size: 0.75rem;
|
|
977
|
+
letter-spacing: 0.05em;
|
|
978
|
+
color: #475569;
|
|
979
|
+
box-shadow: none;
|
|
980
|
+
}
|
|
981
|
+
/* Divider between groups; a run continued past a sticky boundary has none */
|
|
982
|
+
:global(
|
|
983
|
+
.datatable-table thead th.vtable-group-header + th.vtable-group-header:not(.vtable-group-cont)
|
|
984
|
+
) {
|
|
985
|
+
border-left: 1px solid #dee2e6;
|
|
986
|
+
}
|
|
987
|
+
:global(.datatable-table thead th.vtable-group-empty) {
|
|
988
|
+
border-bottom-color: transparent;
|
|
989
|
+
}
|
|
990
|
+
:global(.dark) :global(.datatable-table thead th.vtable-group-header) {
|
|
991
|
+
color: #cbd5e1;
|
|
992
|
+
box-shadow: none;
|
|
993
|
+
}
|
|
994
|
+
:global(.dark)
|
|
995
|
+
:global(
|
|
996
|
+
.datatable-table thead th.vtable-group-header + th.vtable-group-header:not(.vtable-group-cont)
|
|
997
|
+
) {
|
|
998
|
+
border-left-color: #374151;
|
|
999
|
+
}
|
|
1000
|
+
:global(.dark) :global(.datatable-table thead th.vtable-group-empty) {
|
|
1001
|
+
border-bottom-color: transparent;
|
|
1002
|
+
}
|
|
1003
|
+
/* Single-line cells (DataTable wrap={false} or ColumnDef.wrap: false). The
|
|
1004
|
+
zero max-width keeps cell text out of the automatic column sizing, so
|
|
1005
|
+
columns keep their declared widths and long text ends in an ellipsis
|
|
1006
|
+
(revealed in a tooltip on hover). */
|
|
1007
|
+
:global(.datatable-table tbody td.vtable-truncate) {
|
|
1008
|
+
white-space: nowrap;
|
|
1009
|
+
text-overflow: ellipsis;
|
|
1010
|
+
max-width: 0;
|
|
1011
|
+
}
|
|
500
1012
|
/* Allow certain long cells to wrap fully if explicitly requested */
|
|
501
1013
|
:global(.datatable-table td.wrap) {
|
|
502
1014
|
white-space: normal;
|
|
@@ -28,7 +28,13 @@ declare const DataTable: import("svelte").Component<{
|
|
|
28
28
|
emitDebounce?: number;
|
|
29
29
|
relativeRangePresets?: typeof DEFAULT_RELATIVE_RANGE_PRESETS;
|
|
30
30
|
virtualize?: boolean;
|
|
31
|
-
|
|
31
|
+
density?: string;
|
|
32
|
+
resizable?: boolean;
|
|
33
|
+
columnWidths?: Record<string, any>;
|
|
34
|
+
oncolumnresize?: any;
|
|
35
|
+
storageKey?: any;
|
|
36
|
+
wrap?: boolean;
|
|
37
|
+
}, {}, "sortKey" | "sortDir" | "columnWidths">;
|
|
32
38
|
type $$ComponentProps = {
|
|
33
39
|
items?: any[];
|
|
34
40
|
columns?: any[];
|
|
@@ -54,5 +60,11 @@ type $$ComponentProps = {
|
|
|
54
60
|
emitDebounce?: number;
|
|
55
61
|
relativeRangePresets?: typeof DEFAULT_RELATIVE_RANGE_PRESETS;
|
|
56
62
|
virtualize?: boolean;
|
|
63
|
+
density?: string;
|
|
64
|
+
resizable?: boolean;
|
|
65
|
+
columnWidths?: Record<string, any>;
|
|
66
|
+
oncolumnresize?: any;
|
|
67
|
+
storageKey?: any;
|
|
68
|
+
wrap?: boolean;
|
|
57
69
|
};
|
|
58
70
|
import { DEFAULT_RELATIVE_RANGE_PRESETS } from '../filterUtils.js';
|