import type { ColumnSizingState, Table } from '@tanstack/react-table' interface DataGridSizing { gridTemplateColumns: string width: string | number } export function resolveDataGridSizing( table: Table, columnSizing: ColumnSizingState ): DataGridSizing { const leafColumns = table.getVisibleLeafColumns() if (Object.keys(columnSizing).length > 0) { return { gridTemplateColumns: leafColumns.map((column) => `${column.getSize()}px`).join(' '), width: table.getTotalSize() } } // `useTableUtils` measures the container in a layout effect, so server-rendered markup and the // first client render have no measurement yet. `auto` max tracks absorb the leftover width equally // under grid's default `justify-content: stretch` — the same distribution that measurement lands // on — so the grid fills its container before hydration instead of sitting at its base widths. const hasResizableColumn = leafColumns.some((column) => column.getCanResize()) return { gridTemplateColumns: leafColumns .map((column) => hasResizableColumn && !column.getCanResize() ? `${column.getSize()}px` : `minmax(${column.getSize()}px, auto)` ) .join(' '), width: '100%' } }