|
1 | 1 | <script setup lang="ts"> |
2 | 2 | import type { ColumnFilter, DataGridEmits } from '@/types/grid' |
3 | 3 | import { useVirtualizer } from '@tanstack/vue-virtual' |
| 4 | +import { useResizeObserver } from '@vueuse/core' |
4 | 5 | import { computed, ref, watch } from 'vue' |
5 | 6 | import { useI18n } from 'vue-i18n' |
6 | 7 | import { |
@@ -119,6 +120,11 @@ function handleClearSort() { |
119 | 120 | // ── Virtual Scroller ── |
120 | 121 | const scrollContainer = ref<HTMLDivElement | null>(null) |
121 | 122 | const headerViewport = ref<HTMLDivElement | null>(null) |
| 123 | +const viewportWidth = ref(0) |
| 124 | +
|
| 125 | +useResizeObserver(scrollContainer, () => { |
| 126 | + viewportWidth.value = scrollContainer.value?.clientWidth ?? 0 |
| 127 | +}) |
122 | 128 |
|
123 | 129 | // The header lives outside the rows scroll container, so mirror the rows' |
124 | 130 | // scrollLeft onto the header viewport. The overflow-hidden viewport is still a |
@@ -148,6 +154,16 @@ function getColumnWidth(col: string): number { |
148 | 154 | return columnWidths.value.get(col) ?? defaultColumnWidth |
149 | 155 | } |
150 | 156 |
|
| 157 | +// Absolute virtual rows must not size themselves from their cell contents. |
| 158 | +// Share one explicit width with the header, including scrollbar-free viewport space. |
| 159 | +const gridWidth = computed(() => Math.max( |
| 160 | + viewportWidth.value, |
| 161 | + 40 + props.columns.reduce((width, col) => width + getColumnWidth(col), 0) |
| 162 | + + (props.connectionId ? 80 : 0), |
| 163 | +)) |
| 164 | +
|
| 165 | +watch([gridWidth, viewportWidth], syncHeaderScroll, { flush: 'post' }) |
| 166 | +
|
151 | 167 | let resizeStartX = 0 |
152 | 168 | let resizeStartWidth = 0 |
153 | 169 | let resizeColumn = '' |
@@ -590,9 +606,11 @@ watch(sortableColumns, (columns) => { |
590 | 606 | <div |
591 | 607 | ref="headerViewport" |
592 | 608 | class="border-b bg-muted flex flex-shrink-0 overflow-hidden" |
| 609 | + :style="{ width: `${viewportWidth}px` }" |
593 | 610 | > |
594 | 611 | <div |
595 | | - class="flex flex-shrink-0 min-w-full" |
| 612 | + class="flex flex-shrink-0" |
| 613 | + :style="{ width: `${gridWidth}px` }" |
596 | 614 | > |
597 | 615 | <!-- Select-All Checkbox --> |
598 | 616 | <div class="flex flex-shrink-0 h-8 w-10 items-center justify-center"> |
@@ -690,20 +708,21 @@ watch(sortableColumns, (columns) => { |
690 | 708 | @scroll="syncHeaderScroll" |
691 | 709 | > |
692 | 710 | <div |
693 | | - :style="{ height: `${rowVirtualizer.getTotalSize()}px` }" |
| 711 | + :style="{ width: `${gridWidth}px`, height: `${rowVirtualizer.getTotalSize()}px` }" |
694 | 712 | class="relative" |
695 | 713 | > |
696 | 714 | <!-- Virtual Rows --> |
697 | 715 | <div |
698 | 716 | v-for="virtualRow in rowVirtualizer.getVirtualItems()" |
699 | 717 | :key="`r-${virtualRow.index}`" |
700 | 718 | :data-index="virtualRow.index" |
701 | | - class="group border-b border-border/30 flex min-w-full transition-colors duration-75 left-0 top-0 absolute hover:bg-muted/[0.08]" |
| 719 | + class="group border-b border-border/30 flex transition-colors duration-75 left-0 top-0 absolute hover:bg-muted/[0.08]" |
702 | 720 | :class="[ |
703 | 721 | virtualRow.index % 2 === 0 ? 'bg-muted/[0.03]' : '', |
704 | 722 | selection.isSelected(virtualRow.index) ? 'bg-primary/[0.08]' : '', |
705 | 723 | ]" |
706 | 724 | :style="{ |
| 725 | + width: `${gridWidth}px`, |
707 | 726 | height: `${virtualRow.size}px`, |
708 | 727 | transform: `translateY(${virtualRow.start}px)`, |
709 | 728 | }" |
@@ -787,7 +806,7 @@ watch(sortableColumns, (columns) => { |
787 | 806 | <!-- Row Actions --> |
788 | 807 | <div |
789 | 808 | v-if="connectionId" |
790 | | - class="bg-background/80 flex flex-shrink-0 w-20 items-center right-0 justify-center sticky z-10" |
| 809 | + class="bg-background flex flex-shrink-0 w-20 items-center right-0 justify-center sticky z-10" |
791 | 810 | @click.stop |
792 | 811 | > |
793 | 812 | <DropdownMenu> |
|
0 commit comments