@witchcraft/ui 0.3.13 → 0.3.15
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/module.json +1 -1
- package/dist/runtime/assets/utils.css +1 -1
- package/dist/runtime/components/LibColorInput/LibColorInput.d.vue.ts +2 -2
- package/dist/runtime/components/LibColorInput/LibColorInput.vue.d.ts +2 -2
- package/dist/runtime/components/LibColorPicker/LibColorPicker.d.vue.ts +3 -3
- package/dist/runtime/components/LibColorPicker/LibColorPicker.vue.d.ts +3 -3
- package/dist/runtime/components/LibInputDeprecated/LibInputDeprecated.d.vue.ts +3 -3
- package/dist/runtime/components/LibInputDeprecated/LibInputDeprecated.vue.d.ts +3 -3
- package/dist/runtime/components/LibPopup/LibPopup.d.vue.ts +1 -1
- package/dist/runtime/components/LibPopup/LibPopup.vue.d.ts +1 -1
- package/dist/runtime/components/LibTable/LibTable.d.vue.ts +63 -5
- package/dist/runtime/components/LibTable/LibTable.vue +302 -99
- package/dist/runtime/components/LibTable/LibTable.vue.d.ts +63 -5
- package/dist/runtime/directives/vResizableCols.js +8 -5
- package/dist/runtime/types/index.d.ts +7 -3
- package/dist/runtime/utils/twMerge.d.ts +1 -0
- package/dist/runtime/utils/twMerge.js +2 -1
- package/package.json +3 -1
- package/src/runtime/assets/utils.css +5 -0
- package/src/runtime/components/LibTable/LibTable.stories.ts +174 -10
- package/src/runtime/components/LibTable/LibTable.vue +378 -107
- package/src/runtime/directives/vResizableCols.ts +9 -5
- package/src/runtime/types/index.ts +7 -3
- package/src/runtime/utils/twMerge.ts +2 -1
|
@@ -1,134 +1,321 @@
|
|
|
1
1
|
<template>
|
|
2
|
-
<!--
|
|
3
|
-
|
|
2
|
+
<!--
|
|
3
|
+
- moving the border to the wrapper is to hide the little bits of border sticking out
|
|
4
|
+
added back the right straight border otherwise the scrollbar looks ass
|
|
5
|
+
this is ever so slightly visible if there is no scrollbar
|
|
6
|
+
|
|
7
|
+
- relative is for the sticky header in dynamic mode
|
|
8
|
+
|
|
9
|
+
- dynamic mode REQUIRES grid since otherwise the transforms don't work because of how tanstack calculates them
|
|
10
|
+
- tried pre-calculating the transforms to take into account the previous elements (e.g. virtual.start - (height of previous rows)) but this was way to slow and buggy
|
|
11
|
+
-->
|
|
12
|
+
<div
|
|
4
13
|
:class="twMerge(
|
|
5
|
-
`
|
|
6
|
-
table
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
[&
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
[&.resizable-cols-error]:cursor-not-allowed
|
|
19
|
-
[&.resizable-cols-error]:user-select-none
|
|
20
|
-
`,
|
|
21
|
-
cellBorder && `
|
|
22
|
-
[&_td]:border-neutral-500
|
|
23
|
-
[&_td:not(.last-row)]:border-b
|
|
24
|
-
[&_td:not(.first-col)]:border-l
|
|
25
|
-
`,
|
|
14
|
+
`
|
|
15
|
+
table--container
|
|
16
|
+
overflow-auto
|
|
17
|
+
`,
|
|
18
|
+
hasScrollbar.vertical && `has-scrollbar-vertical`,
|
|
19
|
+
hasScrollbar.horizontal && `has-scrollbar-horizontal`,
|
|
20
|
+
stickyHeader && `
|
|
21
|
+
[&_thead]:sticky
|
|
22
|
+
[&_thead]:top-0
|
|
23
|
+
[&_thead]:z-1
|
|
24
|
+
[&_.grip]:z-2
|
|
25
|
+
`,
|
|
26
|
+
isPostSetup && `resizable-cols-setup`,
|
|
26
27
|
border && `
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
28
|
+
border
|
|
29
|
+
border-neutral-500
|
|
30
|
+
`,
|
|
31
|
+
border && cellBorder && `
|
|
32
|
+
[&.has-scrollbar-horizontal_.last-row]:border-b
|
|
33
|
+
[&.has-scrollbar-horizontal_.last-row]:border-neutral-500
|
|
34
|
+
[&.has-scrollbar-vertical_.last-col]:border-r
|
|
35
|
+
[&.has-scrollbar-vertical_.last-col]:border-neutral-500
|
|
36
|
+
`,
|
|
37
|
+
(!resizableOptions.fitWidth || stickyHeader) && `
|
|
38
|
+
[&_td.tr]:rounded-tr-none!
|
|
39
|
+
[&_td.br]:rounded-br-none!
|
|
40
|
+
`,
|
|
41
|
+
// this combo prevents the x-scrollbar from showing up when it shouldn\'t
|
|
42
|
+
// and max-w-fit allows the border to shrink with the table columns
|
|
43
|
+
resizableOptions.fitWidth === false && `
|
|
44
|
+
[&_.grip]:last:translate-x-[-5px]
|
|
45
|
+
mr-1
|
|
46
|
+
max-w-fit
|
|
47
|
+
`,
|
|
36
48
|
rounded && `
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
49
|
+
rounded-md
|
|
50
|
+
`,
|
|
51
|
+
mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'dynamic' && `
|
|
52
|
+
relative
|
|
41
53
|
`,
|
|
42
|
-
$attrs.
|
|
54
|
+
$attrs.wrapperClass
|
|
43
55
|
)"
|
|
44
|
-
|
|
56
|
+
ref="parentRef"
|
|
45
57
|
>
|
|
46
|
-
<
|
|
47
|
-
|
|
48
|
-
|
|
58
|
+
<div
|
|
59
|
+
class="table--inner-container"
|
|
60
|
+
:style="{
|
|
61
|
+
...mergedVirtualizerOpts.enabled ? { height: `${totalSize}px` } : {}
|
|
62
|
+
}"
|
|
49
63
|
>
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
64
|
+
<!-- https://github.com/TanStack/virtual/issues/640#issuecomment-2795731690 -->
|
|
65
|
+
<table
|
|
66
|
+
:style="{
|
|
67
|
+
...stickyHeader && mergedVirtualizerOpts.enabled ? { '--table-sticky-fix': `${totalSize - tableHeight}px` } : {},
|
|
68
|
+
...$attrs.style ?? {}
|
|
69
|
+
}"
|
|
70
|
+
:class="twMerge(
|
|
71
|
+
`
|
|
72
|
+
table
|
|
73
|
+
table-fixed
|
|
74
|
+
border-separate
|
|
75
|
+
border-spacing-0
|
|
76
|
+
scrollbar-hidden
|
|
77
|
+
[&_.grip]:w-[5px]
|
|
78
|
+
relative
|
|
79
|
+
w-full
|
|
80
|
+
box-content
|
|
81
|
+
[&_thead]:font-bold
|
|
82
|
+
[&_td]:p-1
|
|
83
|
+
[&_th]:p-1
|
|
84
|
+
[&.resizable-cols-error]:cursor-not-allowed
|
|
85
|
+
[&.resizable-cols-error]:user-select-none
|
|
86
|
+
[&_thead_th]:bg-neutral-200
|
|
87
|
+
dark:[&_thead_th]:bg-neutral-800
|
|
88
|
+
`,
|
|
89
|
+
isPostSetup && mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'dynamic' && `
|
|
90
|
+
grid
|
|
91
|
+
`,
|
|
92
|
+
stickyHeader && mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'fixed' && `
|
|
93
|
+
after:inline-block
|
|
94
|
+
after:h-(--table-sticky-fix)
|
|
95
|
+
`,
|
|
96
|
+
cellBorder && `
|
|
97
|
+
[&_td]:border-neutral-500
|
|
98
|
+
[&_td:not(.last-row)]:border-b
|
|
99
|
+
[&_td:not(.first-col)]:border-l
|
|
100
|
+
[&_th]:border-neutral-500
|
|
101
|
+
[&_th:not(.last-row)]:border-b
|
|
102
|
+
[&_th:not(.first-col)]:border-l
|
|
103
|
+
`,
|
|
104
|
+
!cellBorder && `
|
|
105
|
+
[&_.grip]:hover:bg-neutral-300
|
|
106
|
+
dark:[&_.grip]:hover:bg-neutral-700
|
|
107
|
+
`,
|
|
108
|
+
$attrs.class
|
|
109
|
+
)"
|
|
110
|
+
v-resizable-cols="resizableOptions"
|
|
111
|
+
>
|
|
112
|
+
<thead
|
|
113
|
+
v-if="header"
|
|
114
|
+
:class="twMerge(
|
|
115
|
+
`table--header`,
|
|
116
|
+
isPostSetup && mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'dynamic' && `
|
|
117
|
+
grid
|
|
118
|
+
top-0
|
|
119
|
+
`
|
|
120
|
+
)"
|
|
54
121
|
>
|
|
55
|
-
<
|
|
56
|
-
:
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
colConfig[col]?.resizable === false ? 'no-resize' : ''
|
|
61
|
-
].join(' ')"
|
|
62
|
-
:style="`width:${widths.length > 0 ? widths[i] : ``}; `"
|
|
63
|
-
:col-key="col"
|
|
122
|
+
<tr
|
|
123
|
+
:class="twMerge(
|
|
124
|
+
`table--row`,
|
|
125
|
+
isPostSetup && mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'dynamic' && `flex w-full`
|
|
126
|
+
)"
|
|
64
127
|
>
|
|
65
|
-
<
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
'cell table--header-cell',
|
|
69
|
-
colConfig[col]?.resizable === false ? 'no-resize' : ''
|
|
70
|
-
].join(' ')"
|
|
71
|
-
:style="`width:${widths.length > 0 ? widths[i] : ``}; `"
|
|
128
|
+
<template
|
|
129
|
+
v-for="col, i of cols"
|
|
130
|
+
:key="col"
|
|
72
131
|
>
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
132
|
+
<slot
|
|
133
|
+
:name="`header-${col.toString()}`"
|
|
134
|
+
:class="classes[`-1-${i}`]"
|
|
135
|
+
:style="{ width: widths[i] }"
|
|
136
|
+
:col-key="col"
|
|
137
|
+
:config="colConfig[col]"
|
|
138
|
+
>
|
|
139
|
+
<th
|
|
140
|
+
:class="classes[`-1-${i}`]"
|
|
141
|
+
:style="{ width: widths[i] }"
|
|
142
|
+
>
|
|
143
|
+
{{ colConfig[col]?.name ?? col }}
|
|
144
|
+
</th>
|
|
145
|
+
</slot>
|
|
146
|
+
</template>
|
|
147
|
+
</tr>
|
|
148
|
+
</thead>
|
|
149
|
+
<tbody
|
|
150
|
+
:class="twMerge(
|
|
151
|
+
`table--body`,
|
|
152
|
+
isPostSetup && mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'dynamic' && `grid relative`
|
|
153
|
+
)"
|
|
154
|
+
:style="{
|
|
155
|
+
...mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'dynamic' ? { height: `${totalSize}px` } : {}
|
|
156
|
+
}"
|
|
157
|
+
>
|
|
85
158
|
<template
|
|
86
|
-
v-for="
|
|
87
|
-
:key="
|
|
159
|
+
v-for="(virtual, index) in virtualList"
|
|
160
|
+
:key="virtual.key"
|
|
88
161
|
>
|
|
89
|
-
<
|
|
90
|
-
:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
162
|
+
<tr
|
|
163
|
+
:class="twMerge(
|
|
164
|
+
`
|
|
165
|
+
table--row
|
|
166
|
+
`,
|
|
167
|
+
isPostSetup && mergedVirtualizerOpts.enabled && mergedVirtualizerOpts.method === 'dynamic' && `flex absolute w-full`
|
|
168
|
+
)"
|
|
169
|
+
:style="{
|
|
170
|
+
...mergedVirtualizerOpts.enabled ? {
|
|
171
|
+
transform: mergedVirtualizerOpts.method === 'fixed' ? `translateY(${virtual.start - index * virtual.size}px)` : `translateY(${virtual.start}px)`,
|
|
172
|
+
height: virtual.size
|
|
173
|
+
} : {}
|
|
174
|
+
}"
|
|
175
|
+
:data-index="virtual.index"
|
|
176
|
+
:ref="measureElement"
|
|
94
177
|
>
|
|
95
|
-
<
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
178
|
+
<template
|
|
179
|
+
v-for="col, j of cols"
|
|
180
|
+
:key="virtual.key + '-' + col.toString()"
|
|
181
|
+
>
|
|
182
|
+
<slot
|
|
183
|
+
:name="col"
|
|
184
|
+
:item="values[virtual.index]"
|
|
185
|
+
:value="values[virtual.index][col]"
|
|
186
|
+
:style="{ width: widths[j] }"
|
|
187
|
+
:class="classes[`${virtual.index}-${j}`]"
|
|
188
|
+
>
|
|
189
|
+
<td
|
|
190
|
+
:style="{ width: widths[j] }"
|
|
191
|
+
:class="classes[`${virtual.index}-${j}`]"
|
|
192
|
+
>
|
|
193
|
+
{{ values[virtual.index][col] }}
|
|
194
|
+
</td>
|
|
195
|
+
</slot>
|
|
196
|
+
</template>
|
|
197
|
+
</tr>
|
|
99
198
|
</template>
|
|
100
|
-
</
|
|
101
|
-
</
|
|
102
|
-
</
|
|
103
|
-
</
|
|
199
|
+
</tbody>
|
|
200
|
+
</table>
|
|
201
|
+
</div>
|
|
202
|
+
</div>
|
|
104
203
|
</template>
|
|
105
204
|
|
|
106
205
|
<script setup>
|
|
107
206
|
import { keys } from "@alanscodelog/utils/keys";
|
|
108
|
-
import {
|
|
207
|
+
import { throttle } from "@alanscodelog/utils/throttle";
|
|
208
|
+
import { useVirtualizer } from "@tanstack/vue-virtual";
|
|
209
|
+
import { computed, onMounted, ref } from "vue";
|
|
210
|
+
import { useGlobalResizeObserver } from "../../composables/useGlobalResizeObserver.js";
|
|
109
211
|
import { vResizableCols } from "../../directives/vResizableCols.js";
|
|
110
212
|
import { twMerge } from "../../utils/twMerge.js";
|
|
111
213
|
defineOptions({
|
|
112
|
-
name: "LibTable"
|
|
214
|
+
name: "LibTable",
|
|
215
|
+
inheritAttrs: false
|
|
113
216
|
});
|
|
114
217
|
const props = defineProps({
|
|
115
218
|
resizable: { type: Object, required: false, default: () => ({}) },
|
|
116
219
|
values: { type: Array, required: false, default: () => [] },
|
|
117
|
-
itemKey: { type: [String, Number, Symbol, Function], required: false, default: "" },
|
|
118
220
|
cols: { type: Array, required: false, default: () => [] },
|
|
119
221
|
rounded: { type: Boolean, required: false, default: true },
|
|
120
222
|
border: { type: Boolean, required: false, default: true },
|
|
121
223
|
cellBorder: { type: Boolean, required: false, default: true },
|
|
122
224
|
header: { type: Boolean, required: false, default: true },
|
|
123
|
-
colConfig: { type: Object, required: false, default: () => ({}) }
|
|
225
|
+
colConfig: { type: Object, required: false, default: () => ({}) },
|
|
226
|
+
virtualizerOptions: { type: Object, required: false, default: () => ({}) },
|
|
227
|
+
stickyHeader: { type: Boolean, required: false },
|
|
228
|
+
itemKey: { type: [String, Number, Symbol, Function], required: false, default: "" }
|
|
124
229
|
});
|
|
125
230
|
const widths = ref([]);
|
|
231
|
+
const isPostSetup = ref(false);
|
|
126
232
|
const resizableOptions = computed(() => ({
|
|
127
233
|
colCount: props.cols.length,
|
|
128
234
|
widths,
|
|
129
235
|
selector: ".cell",
|
|
130
|
-
...props.resizable
|
|
236
|
+
...props.resizable,
|
|
237
|
+
onSetup: (el) => {
|
|
238
|
+
isPostSetup.value = true;
|
|
239
|
+
if (props.resizable.onSetup) {
|
|
240
|
+
props.resizable.onSetup(el);
|
|
241
|
+
}
|
|
242
|
+
},
|
|
243
|
+
onTeardown: (el) => {
|
|
244
|
+
isPostSetup.value = false;
|
|
245
|
+
if (props.resizable.onTeardown) {
|
|
246
|
+
props.resizable.onTeardown(el);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
131
249
|
}));
|
|
250
|
+
const parentRef = ref(null);
|
|
251
|
+
const mergedVirtualizerOpts = computed(() => {
|
|
252
|
+
return {
|
|
253
|
+
// we have to put the defaults here as they can't reference local variables
|
|
254
|
+
count: props.values.length,
|
|
255
|
+
getScrollElement: () => parentRef.value,
|
|
256
|
+
estimateSize: () => {
|
|
257
|
+
return 33;
|
|
258
|
+
},
|
|
259
|
+
overscan: props.virtualizerOptions?.overscan ?? (props.virtualizerOptions?.method === "dynamic" ? 10 : 50),
|
|
260
|
+
method: "fixed",
|
|
261
|
+
enabled: false,
|
|
262
|
+
...props.virtualizerOptions
|
|
263
|
+
};
|
|
264
|
+
});
|
|
265
|
+
const rowVirtualizer = useVirtualizer(mergedVirtualizerOpts);
|
|
266
|
+
const virtualList = computed(() => {
|
|
267
|
+
return mergedVirtualizerOpts.value.enabled ? rowVirtualizer.value.getVirtualItems() : props.values.map((_, i) => ({
|
|
268
|
+
index: i,
|
|
269
|
+
size: void 0,
|
|
270
|
+
start: 0,
|
|
271
|
+
end: 0,
|
|
272
|
+
key: typeof props.itemKey === "function" ? props.itemKey(_) : props.itemKey ? props.values[props.itemKey] : i
|
|
273
|
+
}));
|
|
274
|
+
});
|
|
275
|
+
const totalSize = computed(() => rowVirtualizer.value.getTotalSize());
|
|
276
|
+
function measureElement(el) {
|
|
277
|
+
if (!el || !mergedVirtualizerOpts.value.enabled) return;
|
|
278
|
+
if (mergedVirtualizerOpts.value?.method === "dynamic") {
|
|
279
|
+
rowVirtualizer.value.measureElement(el);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
function forceRecalculateFixedVirtualizer() {
|
|
283
|
+
if (mergedVirtualizerOpts.value?.method === "dynamic" || !mergedVirtualizerOpts.value.enabled) return;
|
|
284
|
+
if (!parentRef.value) {
|
|
285
|
+
throw new Error("forceRecalculateFixedVirtualizer cannot be called before the table is mounted.");
|
|
286
|
+
}
|
|
287
|
+
const height = parentRef.value.querySelector("td")?.getBoundingClientRect().height;
|
|
288
|
+
if (!height) return;
|
|
289
|
+
for (let i = 0; i < props.values.length; i++) {
|
|
290
|
+
rowVirtualizer.value.resizeItem(i, height);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
const tableHeight = ref(0);
|
|
294
|
+
function updateTableHeight() {
|
|
295
|
+
if (!parentRef.value) return;
|
|
296
|
+
const el = parentRef.value.querySelector("tbody");
|
|
297
|
+
if (!el) return;
|
|
298
|
+
if (tableHeight.value === el.getBoundingClientRect().height) return;
|
|
299
|
+
tableHeight.value = el.getBoundingClientRect().height;
|
|
300
|
+
}
|
|
301
|
+
const throttledUpdateTableHeight = throttle(updateTableHeight, 100, { leading: true });
|
|
302
|
+
onMounted(() => {
|
|
303
|
+
throttledUpdateTableHeight();
|
|
304
|
+
forceRecalculateFixedVirtualizer();
|
|
305
|
+
useGlobalResizeObserver(parentRef, onResize);
|
|
306
|
+
});
|
|
307
|
+
const hasScrollbar = ref({ vertical: false, horizontal: false });
|
|
308
|
+
function onResize() {
|
|
309
|
+
const el = parentRef.value;
|
|
310
|
+
if (!el) return;
|
|
311
|
+
hasScrollbar.value = {
|
|
312
|
+
vertical: el.scrollHeight > el.clientHeight,
|
|
313
|
+
horizontal: el.scrollWidth > el.clientWidth
|
|
314
|
+
};
|
|
315
|
+
if (hasScrollbar.value.vertical) {
|
|
316
|
+
throttledUpdateTableHeight();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
132
319
|
const getExtraClasses = (row, col, isHeader) => {
|
|
133
320
|
const res = {
|
|
134
321
|
bl: !isHeader && row === props.values.length - 1 && col === 0,
|
|
@@ -142,12 +329,28 @@ const getExtraClasses = (row, col, isHeader) => {
|
|
|
142
329
|
};
|
|
143
330
|
return keys(res).filter((key) => res[key]);
|
|
144
331
|
};
|
|
145
|
-
const
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
)
|
|
332
|
+
const classes = computed(() => {
|
|
333
|
+
const res = {};
|
|
334
|
+
const headerTdClass = `table--header-cell cell truncate`;
|
|
335
|
+
const bodyTdClass = `table--cell cell truncate`;
|
|
336
|
+
for (let i = -1; i < props.values.length + 1; i++) {
|
|
337
|
+
for (let j = 0; j < props.cols.length; j++) {
|
|
338
|
+
const col = props.cols[j];
|
|
339
|
+
const colConfig = props.colConfig[col];
|
|
340
|
+
const key = `${i}-${j}`;
|
|
341
|
+
res[key] = twMerge(
|
|
342
|
+
getExtraClasses(i, j, i === -1).join(" "),
|
|
343
|
+
i === -1 ? headerTdClass : bodyTdClass,
|
|
344
|
+
i === -1 ? colConfig?.resizable === false && `no-resize` : void 0,
|
|
345
|
+
i !== -1 && mergedVirtualizerOpts.value.enabled && mergedVirtualizerOpts.value.method === "dynamic" && `flex`
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return res;
|
|
350
|
+
});
|
|
351
|
+
defineExpose({
|
|
352
|
+
forceRecalculateFixedVirtualizer
|
|
353
|
+
});
|
|
151
354
|
</script>
|
|
152
355
|
|
|
153
356
|
<script>
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type VirtualizerOptions } from "@tanstack/vue-virtual";
|
|
1
2
|
import { type TableHTMLAttributes } from "vue";
|
|
2
3
|
import type { ResizableOptions, TableColConfig } from "../../types/index.js";
|
|
3
4
|
import type { TailwindClassProp } from "../shared/props.js";
|
|
@@ -5,7 +6,6 @@ type T = any;
|
|
|
5
6
|
type RealProps = {
|
|
6
7
|
resizable?: Partial<ResizableOptions>;
|
|
7
8
|
values?: T[];
|
|
8
|
-
itemKey?: keyof T | ((item: T) => string);
|
|
9
9
|
/** Let's the table know the shape of the data since values might be empty. */
|
|
10
10
|
cols?: (keyof T)[];
|
|
11
11
|
rounded?: boolean;
|
|
@@ -13,6 +13,56 @@ type RealProps = {
|
|
|
13
13
|
cellBorder?: boolean;
|
|
14
14
|
header?: boolean;
|
|
15
15
|
colConfig?: TableColConfig<T>;
|
|
16
|
+
/**
|
|
17
|
+
* See tanstack/vue-virtual {@link https://tanstack.com/virtual/latest/docs/api/virtualizer}
|
|
18
|
+
*
|
|
19
|
+
* The defaults are:
|
|
20
|
+
*
|
|
21
|
+
* - enabled: false
|
|
22
|
+
* - method: "fixed"
|
|
23
|
+
* - overscan: (50 if fixed, 10 if dynamic)
|
|
24
|
+
* - estimateSize: () => { return 33 }
|
|
25
|
+
*
|
|
26
|
+
* This also has an additional option, `method`, which can be set to `fixed` or `dynamic` (experimental).
|
|
27
|
+
*
|
|
28
|
+
* Notes:
|
|
29
|
+
*
|
|
30
|
+
* - Because of how virtualization works, initial layout (before .resizable-cols-setup class is applied) will only have access to the headers and not the rows. This can cause cols to look very small, especially if using resizable.fitWidth false.
|
|
31
|
+
*
|
|
32
|
+
* ### Fixed
|
|
33
|
+
*
|
|
34
|
+
* `fixed` is the default and will set the height of ALL items to the height of the first item onMounted (tanstack does not do this and if your estimateSize if off, the scrolling is weird).
|
|
35
|
+
*
|
|
36
|
+
* Since the table now truncates rows by default, they will always be the same height unless you change the inner styling. In fixed mode, `forceRecalculateFixedVirtualizer` is exposed if you need to force re-calculation.
|
|
37
|
+
*
|
|
38
|
+
* If using slots, be sure to at least pass the `class` slot prop to the td element. `style` with width is also supplied but is not required if you're displaying the table as a table.
|
|
39
|
+
*
|
|
40
|
+
* ### Dynamic (experimental)
|
|
41
|
+
*
|
|
42
|
+
* In `dynamic` mode we use tanstack's measureElement method. This is more expensive, but it will work with any heights.
|
|
43
|
+
*
|
|
44
|
+
* Dynamic mode also requires the table displays itself using grid and flex post setup as otherwise dynamic mode doesn't work.
|
|
45
|
+
*
|
|
46
|
+
* You don't need to do anything unless using slots. If using slots, pass the given `ref` slot prop to ref (internally this is tanstack's measureElement) and the class and style slot props at the very least:
|
|
47
|
+
* ```vue
|
|
48
|
+
* <template #[`${colName}`]="slotProps">
|
|
49
|
+
* <td
|
|
50
|
+
* :ref="slotProps.ref"
|
|
51
|
+
* :class="slotProps.class"
|
|
52
|
+
* :style="slotProps.style"
|
|
53
|
+
* >
|
|
54
|
+
* {{ slotProps.value }}
|
|
55
|
+
* </td>
|
|
56
|
+
* </template>
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
virtualizerOptions?: Partial<VirtualizerOptions<any, any>> & {
|
|
60
|
+
method?: "fixed" | "dynamic";
|
|
61
|
+
};
|
|
62
|
+
/** Whether to enable sticky header styles. This requires `border:false`. This moves the border to the wrapper and styles a straight border between the scroll bar and the rounded border. */
|
|
63
|
+
stickyHeader?: boolean;
|
|
64
|
+
/** Which key to use for the rows (only if not using virtualization). */
|
|
65
|
+
itemKey?: keyof T | ((item: T) => string);
|
|
16
66
|
};
|
|
17
67
|
interface Props extends
|
|
18
68
|
/** @vue-ignore */
|
|
@@ -20,17 +70,25 @@ Partial<Omit<TableHTMLAttributes, "class" | "readonly" | "disabled"> & TailwindC
|
|
|
20
70
|
}
|
|
21
71
|
declare const _default: <T>(__VLS_props: NonNullable<Awaited<typeof __VLS_setup>>["props"], __VLS_ctx?: __VLS_PrettifyLocal<Pick<NonNullable<Awaited<typeof __VLS_setup>>, "attrs" | "emit" | "slots">>, __VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>["expose"], __VLS_setup?: Promise<{
|
|
22
72
|
props: __VLS_PrettifyLocal<Pick<Partial<{}> & Omit<{} & import("vue").VNodeProps & import("vue").AllowedComponentProps & import("vue").ComponentCustomProps, never>, never> & Props & {}> & import("vue").PublicProps;
|
|
23
|
-
expose(exposed: import("vue").ShallowUnwrapRef<{
|
|
73
|
+
expose(exposed: import("vue").ShallowUnwrapRef<{
|
|
74
|
+
forceRecalculateFixedVirtualizer: () => void;
|
|
75
|
+
}>): void;
|
|
24
76
|
attrs: any;
|
|
25
77
|
slots: {
|
|
26
78
|
[x: string]: ((props: {
|
|
27
79
|
colKey: any;
|
|
28
|
-
|
|
29
|
-
|
|
80
|
+
config: any;
|
|
81
|
+
style: {
|
|
82
|
+
width: any;
|
|
83
|
+
};
|
|
84
|
+
class: any;
|
|
30
85
|
}) => any) | undefined;
|
|
31
86
|
} & {
|
|
32
87
|
[x: string]: ((props: {
|
|
33
|
-
class:
|
|
88
|
+
class: any;
|
|
89
|
+
style: {
|
|
90
|
+
width: any;
|
|
91
|
+
};
|
|
34
92
|
item: any;
|
|
35
93
|
value: any;
|
|
36
94
|
}) => any) | undefined;
|
|
@@ -25,8 +25,8 @@ export const vResizableCols = {
|
|
|
25
25
|
},
|
|
26
26
|
updated(el, { value: opts = {} }) {
|
|
27
27
|
const options = override({ ...defaultOpts }, opts);
|
|
28
|
-
const info = el && getElInfo(el);
|
|
29
|
-
const hasGrips = el && elMap.get(el)
|
|
28
|
+
const info = el && options.enabled && getElInfo(el);
|
|
29
|
+
const hasGrips = el && options.enabled && elMap.get(el)?.grips;
|
|
30
30
|
const colsNotEqual = info && info.colCount !== options.colCount;
|
|
31
31
|
if (hasGrips && !options.enabled || colsNotEqual) {
|
|
32
32
|
teardownColumns(el);
|
|
@@ -88,7 +88,7 @@ function createPointerDownHandler(el) {
|
|
|
88
88
|
e.preventDefault();
|
|
89
89
|
document.addEventListener("pointerup", $el.pointerUpHandler);
|
|
90
90
|
const { col, colNext } = getCols(el);
|
|
91
|
-
if (col === null || colNext === null) {
|
|
91
|
+
if (col === null || colNext === null && $el.fitWidth) {
|
|
92
92
|
el.classList.add("resizable-cols-error");
|
|
93
93
|
} else {
|
|
94
94
|
document.addEventListener("pointermove", $el.pointerMoveHandler);
|
|
@@ -181,7 +181,7 @@ function getElInfo(el) {
|
|
|
181
181
|
function getColEls(el) {
|
|
182
182
|
const $el = elMap.get(el);
|
|
183
183
|
if (!$el) unreachable("El went missing.");
|
|
184
|
-
return [...el.querySelectorAll(`:scope ${$el.selector ? $el.selector : "tr > td"}`)];
|
|
184
|
+
return [...el.querySelectorAll(`:scope ${$el.selector ? $el.selector : "tr > th, tr > td"}`)];
|
|
185
185
|
}
|
|
186
186
|
function setupColumns(el, opts) {
|
|
187
187
|
const gripWidth = getTestGripSize(el);
|
|
@@ -196,7 +196,8 @@ function setupColumns(el, opts) {
|
|
|
196
196
|
margin: opts.margin === "dynamic" ? gripWidth : opts.margin,
|
|
197
197
|
colCount: opts.colCount,
|
|
198
198
|
widths: opts.widths,
|
|
199
|
-
selector: opts.selector
|
|
199
|
+
selector: opts.selector,
|
|
200
|
+
onTeardown: opts.onTeardown
|
|
200
201
|
};
|
|
201
202
|
elMap.set(el, $el);
|
|
202
203
|
const els = getColEls(el);
|
|
@@ -213,6 +214,7 @@ function setupColumns(el, opts) {
|
|
|
213
214
|
}
|
|
214
215
|
positionGrips(el);
|
|
215
216
|
el.classList.add("resizable-cols-setup");
|
|
217
|
+
opts.onSetup?.(el);
|
|
216
218
|
}
|
|
217
219
|
function positionGrips(el) {
|
|
218
220
|
let xPos = 0;
|
|
@@ -257,4 +259,5 @@ function teardownColumns(el) {
|
|
|
257
259
|
elMap.delete(el);
|
|
258
260
|
el.classList.remove("resizable-cols-setup");
|
|
259
261
|
removeGrips(el);
|
|
262
|
+
$el.onTeardown?.(el);
|
|
260
263
|
}
|
|
@@ -2,9 +2,7 @@ import type { ErrorW } from "@alanscodelog/utils";
|
|
|
2
2
|
import type { Ref } from "vue";
|
|
3
3
|
export type ResizableOptions = {
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* ### true
|
|
5
|
+
* ### true (default)
|
|
8
6
|
* The directive will shrink/expand the columns when the table is resized and will use percentage widths on the table cells. This disables resizing of the last column (from the right handle).
|
|
9
7
|
*
|
|
10
8
|
* Additionally because of the way `table-layout:fixed` works, a min-width cannot be set on the elements via css, so instead, if the table shrinks past `opts.margin * col #`, `min-width` is set on the table until it's resized larger.
|
|
@@ -16,6 +14,8 @@ export type ResizableOptions = {
|
|
|
16
14
|
* The table can be resized past it's normal width and uses pixel widths on the table cells. You might want to set `overscroll-x: scroll` on a parent wrapping element.
|
|
17
15
|
*
|
|
18
16
|
* This will set the table width to `min-content`, else it doesn't work. Note that it does this after the initial reading/setting of sizes so you can, for example, layout the table with `width: 100%`.
|
|
17
|
+
*
|
|
18
|
+
* @default true
|
|
19
19
|
*/
|
|
20
20
|
fitWidth: boolean;
|
|
21
21
|
/**
|
|
@@ -42,6 +42,10 @@ export type ResizableOptions = {
|
|
|
42
42
|
widths: Ref<string[]>;
|
|
43
43
|
/** The selector to use for the cells. "tr > td" by default. */
|
|
44
44
|
selector: string;
|
|
45
|
+
/** Is called just after the `resizable-cols-setup` class is added. Can be useful for controlling the styling of wrappers or doing additional things post-setup. The default table element uses it to set the class on the wrapper also. */
|
|
46
|
+
onSetup?: (el: Element) => void;
|
|
47
|
+
/** Is called on teardown (after the `resizable-cols-setup` class is removed). */
|
|
48
|
+
onTeardown?: (el: Element) => void;
|
|
45
49
|
};
|
|
46
50
|
export type TableColConfig<T = {}> = Record<keyof T, {
|
|
47
51
|
name?: string;
|
|
@@ -2,7 +2,8 @@ import { extendTailwindMerge } from "tailwind-merge";
|
|
|
2
2
|
const _twMergeExtend = {
|
|
3
3
|
extend: {
|
|
4
4
|
classGroups: {
|
|
5
|
-
"focus-outline": [{ "focus-outline": ["", "no-offset", "none"] }]
|
|
5
|
+
"focus-outline": [{ "focus-outline": ["", "no-offset", "none"] }],
|
|
6
|
+
"no-truncate": ["truncate", "no-truncate"]
|
|
6
7
|
}
|
|
7
8
|
}
|
|
8
9
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@witchcraft/ui",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.15",
|
|
4
4
|
"description": "Vue component library.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/runtime/main.lib.js",
|
|
@@ -89,6 +89,7 @@
|
|
|
89
89
|
"@chromatic-com/storybook": "^3.2.7",
|
|
90
90
|
"@commitlint/cli": "^19.8.1",
|
|
91
91
|
"@internationalized/date": "^3.9.0",
|
|
92
|
+
"@faker-js/faker": "^10.0.0",
|
|
92
93
|
"@nuxt/eslint-config": "^1.9.0",
|
|
93
94
|
"@nuxt/module-builder": "^1.0.2",
|
|
94
95
|
"@nuxtjs/i18n": "^9.5.6",
|
|
@@ -106,6 +107,7 @@
|
|
|
106
107
|
"@storybook/test-runner": "^0.22.1",
|
|
107
108
|
"@storybook/vue3": "^8.6.14",
|
|
108
109
|
"@storybook/vue3-vite": "^8.6.14",
|
|
110
|
+
"@tanstack/vue-virtual": "^3.13.0",
|
|
109
111
|
"@tailwindcss/cli": "^4.1.12",
|
|
110
112
|
"@tailwindcss/postcss": "^4.1.12",
|
|
111
113
|
"@types/node": "^24.3.0",
|