Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Sweeping table improvements (#3490)

Marcus Farrelld7a1630d1530
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-row-menu.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-row-menu.tsx
index 00fadf2248..8256235e59 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-row-menu.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/bounties/[bountyId]/bounty-submission-row-menu.tsx
@@ -98,7 +98,7 @@ export function BountySubmissionRowMenu({
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/campaigns-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/campaigns-table.tsx
index e5c350587a..687d8a905a 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/campaigns-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/campaigns-table.tsx
@@ -304,7 +304,7 @@ function RowMenuButton({
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/fraud/fraud-group-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/fraud/fraud-group-table.tsx
index ec3a3b48e5..773cf1b02c 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/fraud/fraud-group-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/fraud/fraud-group-table.tsx
@@ -479,7 +479,7 @@ function RowMenuButton({ row }: { row: Row<FraudGroupProps> }) {
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/groups-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/groups-table.tsx
index e78661f287..912c447fa8 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/groups-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/groups-table.tsx
@@ -323,7 +323,7 @@ function RowMenuButton({ row }: { row: Row<GroupExtendedProps> }) {
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx
index 74d46c0edf..d10c617275 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/page-client.tsx
@@ -554,7 +554,7 @@ function RowMenuButton({
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="size-4 shrink-0" />}
/>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx
index 6453ac5d75..c01d4004e7 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/applications/rejected/page-client.tsx
@@ -512,7 +512,7 @@ function PartnerRowMenuButton({
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={
isApprovingPartner ? (
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx
index 5939c541d4..279807a6b8 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/partners/partners-table.tsx
@@ -279,8 +279,10 @@ export function PartnersTable() {
{
id: "location",
header: "Location",
- minSize: 150,
+ minSize: 190,
+ size: 190,
meta: {
+ disableTruncate: true,
filterParams: ({ getValue }) =>
getValue()
? {
@@ -291,7 +293,7 @@ export function PartnersTable() {
cell: ({ row }) => {
const country = row.original.country;
return (
- <div className="flex items-center gap-2">
+ <div className="flex items-center gap-2 whitespace-nowrap">
{country && (
<img
alt={`${country} flag`}
@@ -299,7 +301,7 @@ export function PartnersTable() {
className="size-4 shrink-0"
/>
)}
- <span className="min-w-0 truncate">
+ <span className="whitespace-nowrap">
{(country ? COUNTRIES[country] : null) ?? "-"}
</span>
</div>
@@ -911,7 +913,7 @@ function RowMenuButton({
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tokens/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tokens/page.tsx
index 44fa16d3a4..baec8b30c2 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tokens/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/tokens/page.tsx
@@ -254,7 +254,7 @@ function RowMenuButton({
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
disabledTooltip={
diff --git a/apps/web/ui/analytics/events/row-menu-button.tsx b/apps/web/ui/analytics/events/row-menu-button.tsx
index a4b580de2b..60486ffe92 100644
--- a/apps/web/ui/analytics/events/row-menu-button.tsx
+++ b/apps/web/ui/analytics/events/row-menu-button.tsx
@@ -49,7 +49,7 @@ export function RowMenuButton({ row }: { row: Row<EventDatum> }) {
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/apps/web/ui/partners/commission-row-menu.tsx b/apps/web/ui/partners/commission-row-menu.tsx
index 5a71f9c4aa..5fa16c0923 100644
--- a/apps/web/ui/partners/commission-row-menu.tsx
+++ b/apps/web/ui/partners/commission-row-menu.tsx
@@ -109,7 +109,7 @@ export function CommissionRowMenu({ row }: { row: Row<CommissionResponse> }) {
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/apps/web/ui/partners/partner-row-item.tsx b/apps/web/ui/partners/partner-row-item.tsx
index 7cbb078122..9662bb9159 100644
--- a/apps/web/ui/partners/partner-row-item.tsx
+++ b/apps/web/ui/partners/partner-row-item.tsx
@@ -137,28 +137,34 @@ export function PartnerRowItem({
const As = showPermalink ? Link : "div";
return (
- <div className="flex items-center gap-2">
- <DynamicTooltipWrapper
- tooltipProps={{
- content: <PartnerPayoutStatusTooltip statusKey={statusKey} />,
- }}
- >
- <div className="relative shrink-0">
- <img
- src={partner.image || `${OG_AVATAR_URL}${partner.id}`}
- alt={partner.id}
- className="size-5 shrink-0 rounded-full"
- />
- {showPayoutsEnabled && statusKey && (
- <div
- className={cn(
- "absolute -bottom-0.5 -right-0.5 size-2 rounded-full",
- PAYOUT_STATUS_CONFIG[statusKey].indicatorColor,
- )}
+ <div className="flex min-w-0 items-center gap-2">
+ <div className="shrink-0">
+ <DynamicTooltipWrapper
+ tooltipProps={
+ statusKey
+ ? {
+ content: <PartnerPayoutStatusTooltip statusKey={statusKey} />,
+ }
+ : undefined
+ }
+ >
+ <div className="relative shrink-0">
+ <img
+ src={partner.image || `${OG_AVATAR_URL}${partner.id}`}
+ alt={partner.id}
+ className="size-5 shrink-0 rounded-full"
/>
- )}
- </div>
- </DynamicTooltipWrapper>
+ {showPayoutsEnabled && statusKey && (
+ <div
+ className={cn(
+ "absolute -bottom-0.5 -right-0.5 size-2 rounded-full",
+ PAYOUT_STATUS_CONFIG[statusKey].indicatorColor,
+ )}
+ />
+ )}
+ </div>
+ </DynamicTooltipWrapper>
+ </div>
<As
href={`/${slug}/program/partners/${partner.id}`}
diff --git a/apps/web/ui/partners/payout-row-menu.tsx b/apps/web/ui/partners/payout-row-menu.tsx
index 376f269dce..710975c1aa 100644
--- a/apps/web/ui/partners/payout-row-menu.tsx
+++ b/apps/web/ui/partners/payout-row-menu.tsx
@@ -74,7 +74,7 @@ export function PayoutRowMenu({ row }: { row: Row<PartnerPayoutResponse> }) {
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Dots className="h-4 w-4 shrink-0" />}
/>
diff --git a/packages/ui/src/table/edit-columns-button.tsx b/packages/ui/src/table/edit-columns-button.tsx
index 93512bd860..ea3a068293 100644
--- a/packages/ui/src/table/edit-columns-button.tsx
+++ b/packages/ui/src/table/edit-columns-button.tsx
@@ -47,7 +47,7 @@ export function EditColumnsButton({ table }: { table: Table<any> }) {
>
<Button
type="button"
- className="h-8 whitespace-nowrap px-2"
+ className="size-8 shrink-0 whitespace-nowrap rounded-lg p-0"
variant="outline"
icon={<Gear className="h-4 w-4 shrink-0" />}
/>
diff --git a/packages/ui/src/table/selection-toolbar.tsx b/packages/ui/src/table/selection-toolbar.tsx
index 901d1f669a..382d177794 100644
--- a/packages/ui/src/table/selection-toolbar.tsx
+++ b/packages/ui/src/table/selection-toolbar.tsx
@@ -38,36 +38,46 @@ export function SelectionToolbar<T>({
)}
inert={selectedCount === 0}
>
- <div className="flex h-11 items-center gap-2.5 px-2 py-2.5">
- <div className="flex w-6 items-center justify-center">
- <Checkbox
- className="border-border-default size-4 rounded data-[state=checked]:bg-black data-[state=indeterminate]:bg-black"
- checked={
- table.getIsAllRowsSelected()
- ? true
- : table.getIsSomeRowsSelected()
- ? "indeterminate"
- : false
- }
- onCheckedChange={() => table.toggleAllRowsSelected()}
+ <div className="flex h-11 items-center py-2.5 pr-2">
+ <div className="relative flex h-full w-12 shrink-0 items-center justify-center">
+ <button
+ type="button"
+ className="absolute inset-0 flex items-center justify-center"
+ onClick={(e) => {
+ e.stopPropagation();
+ table.toggleAllRowsSelected();
+ }}
title="Select all"
- />
+ >
+ <Checkbox
+ className="pointer-events-none border-border-default size-4 rounded data-[state=checked]:bg-black data-[state=indeterminate]:bg-black"
+ checked={
+ table.getIsAllRowsSelected()
+ ? true
+ : table.getIsSomeRowsSelected()
+ ? "indeterminate"
+ : false
+ }
+ />
+ </button>
</div>
- <span
- className={cn(
- "text-content-emphasis mr-2 text-sm font-medium tabular-nums transition-transform duration-150",
- selectedCount > 0 ? "translate-x-0" : "-translate-x-1",
- )}
- >
- {lastSelectedCount} selected
- </span>
- <div
- className={cn(
- "flex items-center gap-2 transition-transform duration-150",
- selectedCount > 0 ? "translate-x-0" : "-translate-x-1",
- )}
- >
- {controls?.(table)}
+ <div className="flex min-w-0 items-center gap-2.5 pl-1">
+ <span
+ className={cn(
+ "text-content-emphasis mr-2 text-sm font-medium tabular-nums transition-transform duration-150",
+ selectedCount > 0 ? "translate-x-0" : "-translate-x-1",
+ )}
+ >
+ {lastSelectedCount} selected
+ </span>
+ <div
+ className={cn(
+ "flex items-center gap-2 transition-transform duration-150",
+ selectedCount > 0 ? "translate-x-0" : "-translate-x-1",
+ )}
+ >
+ {controls?.(table)}
+ </div>
</div>
</div>
</div>
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index 4e0244553e..5803295066 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -13,6 +13,7 @@ import { AnimatePresence, motion } from "motion/react";
import {
CSSProperties,
HTMLAttributes,
+ MouseEvent,
memo,
useEffect,
useMemo,
@@ -28,12 +29,22 @@ import { Tooltip } from "../tooltip";
import { SelectionToolbar } from "./selection-toolbar";
import { TableProps, UseTableProps } from "./types";
-const tableCellClassName = (columnId: string, clickable?: boolean) =>
+const SELECT_COLUMN_WIDTH = 48;
+const MENU_COLUMN_WIDTH = 40;
+const FIXED_UTILITY_COLUMN_IDS = new Set(["select", "menu"]);
+
+const tableCellClassName = (
+ columnId: string,
+ clickable?: boolean,
+ hasSelectBefore?: boolean,
+) =>
cn([
- "py-2.5 text-left text-sm leading-6 whitespace-nowrap border-border-subtle px-4 relative",
+ "py-2.5 text-left text-sm leading-6 whitespace-nowrap border-border-subtle relative",
"border-l border-b",
- columnId === "select" && "py-0 pr-0 pl-2",
+ columnId === "select" && "w-12 min-w-12 max-w-12 px-0 py-0",
columnId === "menu" && "bg-bg-default border-l-transparent py-0 px-1",
+ !["select", "menu"].includes(columnId) &&
+ (hasSelectBefore ? "pl-1 pr-4" : "px-4"),
clickable && "group-hover/row:bg-bg-muted transition-colors duration-75",
"group-data-[selected=true]/row:bg-blue-50",
]);
@@ -66,6 +77,11 @@ export function useTable<T extends any>(
const selectionEnabled =
!!props.onRowSelectionChange || !!props.selectionControls;
+ const hasUtilityColumns = columns.some((column: any) =>
+ FIXED_UTILITY_COLUMN_IDS.has(column?.id),
+ );
+ const useFixedColumnSizing =
+ enableColumnResizing || selectionEnabled || hasUtilityColumns;
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>(
props.columnVisibility ?? {},
@@ -117,6 +133,21 @@ export function useTable<T extends any>(
props.onColumnVisibilityChange?.(columnVisibility);
}, [columnVisibility]);
+ const normalizedColumns = useMemo(
+ () =>
+ columns.map((column: any) =>
+ column?.id === "menu"
+ ? {
+ ...column,
+ minSize: MENU_COLUMN_WIDTH,
+ size: MENU_COLUMN_WIDTH,
+ maxSize: MENU_COLUMN_WIDTH,
+ }
+ : column,
+ ),
+ [columns],
+ );
+
const tableColumns = useMemo(
() => [
...(selectionEnabled
@@ -124,13 +155,21 @@ export function useTable<T extends any>(
{
id: "select",
enableHiding: false,
- minSize: 30,
- size: 30,
- maxSize: 30,
+ minSize: SELECT_COLUMN_WIDTH,
+ size: SELECT_COLUMN_WIDTH,
+ maxSize: SELECT_COLUMN_WIDTH,
header: ({ table }: { table: TableType<T> }) => (
- <div className="flex size-full items-center justify-center">
+ <button
+ type="button"
+ className="flex size-full items-center justify-center"
+ onClick={(e) => {
+ e.stopPropagation();
+ table.toggleAllRowsSelected();
+ }}
+ title="Select all"
+ >
<Checkbox
- className="border-border-default size-4 rounded data-[state=checked]:bg-black data-[state=indeterminate]:bg-black"
+ className="pointer-events-none border-border-default size-4 rounded data-[state=checked]:bg-black data-[state=indeterminate]:bg-black"
checked={
table.getIsAllRowsSelected()
? true
@@ -138,71 +177,91 @@ export function useTable<T extends any>(
? "indeterminate"
: false
}
- onCheckedChange={() => table.toggleAllRowsSelected()}
- title="Select all"
/>
- </div>
+ </button>
),
- cell: ({ row, table }: { row: Row<T>; table: TableType<T> }) => (
- <div className="flex size-full items-center justify-center">
- <Checkbox
- className="border-border-default size-4 rounded data-[state=checked]:bg-black data-[state=indeterminate]:bg-black"
- checked={row.getIsSelected()}
- onClick={(e) => {
- const currentId = getRowId?.(row.original);
- const rows = table.getRowModel().rows;
- const lastSelectedIndex =
- lastSelectedRowId.current !== null
- ? rows.findIndex(
- (row) =>
- getRowId?.(row.original) ===
- lastSelectedRowId.current,
- )
- : -1;
-
- if (
- e.shiftKey &&
- lastSelectedRowId.current !== null &&
- lastSelectedIndex !== -1
- ) {
- // Multi-select w/ shift key
- const currentIndex = row.index;
-
- const start = Math.min(lastSelectedIndex, currentIndex);
- const end = Math.max(lastSelectedIndex, currentIndex);
- const rangeIds = rows
- .slice(start, end + 1)
- .map((row) => getRowId?.(row.original));
-
- table.setRowSelection((rowSelection) => {
- const alreadySelected =
- currentId !== undefined &&
- (rowSelection?.[currentId] ?? false);
-
- return {
- ...rowSelection,
- ...Object.fromEntries(
- rangeIds.map((id) => [id, !alreadySelected]),
- ),
- };
- });
+ cell: ({ row, table }: { row: Row<T>; table: TableType<T> }) => {
+ const onSelectRow = (e: MouseEvent<HTMLButtonElement>) => {
+ e.stopPropagation();
+ const currentId = getRowId?.(row.original);
+ const rows = table.getRowModel().rows;
+ const lastSelectedIndex =
+ lastSelectedRowId.current !== null
+ ? rows.findIndex(
+ (row) =>
+ getRowId?.(row.original) ===
+ lastSelectedRowId.current,
+ )
+ : -1;
+
+ if (
+ e.shiftKey &&
+ lastSelectedRowId.current !== null &&
+ lastSelectedIndex !== -1
+ ) {
+ // Multi-select w/ shift key
+ const currentIndex =
+ currentId !== undefined
+ ? rows.findIndex(
+ (row) => getRowId?.(row.original) === currentId,
+ )
+ : -1;
+ if (currentIndex === -1) {
+ row.toggleSelected();
+ lastSelectedRowId.current = currentId ?? null;
+ return;
+ }
- lastSelectedRowId.current = currentId ?? null;
- } else {
- row.toggleSelected();
- lastSelectedRowId.current = currentId ?? null;
- }
- }}
+ const start = Math.min(lastSelectedIndex, currentIndex);
+ const end = Math.max(lastSelectedIndex, currentIndex);
+ const rangeIds = rows
+ .slice(start, end + 1)
+ .map((row) => getRowId?.(row.original))
+ .filter((id): id is string => id !== undefined);
+
+ table.setRowSelection((rowSelection) => {
+ const validRangeIds = rangeIds.filter(
+ (id): id is string => id !== undefined,
+ );
+ const alreadySelected =
+ currentId !== undefined &&
+ (rowSelection?.[currentId] ?? false);
+
+ return {
+ ...rowSelection,
+ ...Object.fromEntries(
+ validRangeIds.map((id) => [id, !alreadySelected]),
+ ),
+ };
+ });
+
+ lastSelectedRowId.current = currentId ?? null;
+ } else {
+ row.toggleSelected();
+ lastSelectedRowId.current = currentId ?? null;
+ }
+ };
+
+ return (
+ <button
+ type="button"
+ className="flex size-full items-center justify-center"
+ onClick={onSelectRow}
title="Select"
- />
- </div>
- ),
+ >
+ <Checkbox
+ className="pointer-events-none border-border-default size-4 rounded data-[state=checked]:bg-black data-[state=indeterminate]:bg-black"
+ checked={row.getIsSelected()}
+ />
+ </button>
+ );
+ },
},
]
: []),
- ...columns,
+ ...normalizedColumns,
],
- [selectionEnabled, columns],
+ [selectionEnabled, normalizedColumns],
);
const table = useReactTable({
@@ -211,7 +270,7 @@ export function useTable<T extends any>(
columns: tableColumns,
defaultColumn: {
minSize: 120,
- size: 0,
+ size: useFixedColumnSizing ? 120 : 0,
maxSize: 300,
enableResizing: enableColumnResizing,
...defaultColumn,
@@ -297,33 +356,66 @@ const ResizableTableRow = memo(
data-selected={row.getIsSelected()}
{...rest}
>
- {row.getVisibleCells().map((cell) => (
- <td
- key={cell.id}
- className={cn(
- tableCellClassName(cell.column.id, !!onRowClick),
- "text-content-default group",
- getCommonPinningClassNames(
- cell.column,
- row.index === table.getRowModel().rows.length - 1,
- ),
- typeof tdClassName === "function"
- ? tdClassName(cell.column.id, row)
- : tdClassName,
- )}
- style={{
- width: cell.column.getSize(),
- ...getCommonPinningStyles(cell.column),
- }}
- >
- <div className="flex w-full items-center justify-between overflow-hidden truncate">
- <div className="min-w-0 shrink grow truncate">
- {flexRender(cell.column.columnDef.cell, cell.getContext())}
- </div>
- {cellRight?.(cell)}
- </div>
- </td>
- ))}
+ {row.getVisibleCells().map((cell, index, cells) => {
+ const isUtilityColumn = ["select", "menu"].includes(cell.column.id);
+ const isSelectColumn = cell.column.id === "select";
+ const isColumnAfterSelect = cells[index - 1]?.column.id === "select";
+ const disableTruncate = !!(cell.column.columnDef.meta as any)
+ ?.disableTruncate;
+
+ return (
+ <td
+ key={cell.id}
+ className={cn(
+ tableCellClassName(
+ cell.column.id,
+ !!onRowClick,
+ isColumnAfterSelect,
+ ),
+ "text-content-default group",
+ getCommonPinningClassNames(
+ cell.column,
+ row.index === table.getRowModel().rows.length - 1,
+ ),
+ typeof tdClassName === "function"
+ ? tdClassName(cell.column.id, row)
+ : tdClassName,
+ )}
+ style={{
+ width: cell.column.getSize(),
+ ...getCommonPinningStyles(cell.column),
+ }}
+ >
+ {isSelectColumn ? (
+ <div className="absolute inset-0 flex items-center justify-center">
+ {flexRender(cell.column.columnDef.cell, cell.getContext())}
+ </div>
+ ) : (
+ <div
+ className={cn(
+ "flex items-center",
+ isUtilityColumn ? "justify-center" : "w-full justify-between",
+ !isUtilityColumn &&
+ (disableTruncate
+ ? "overflow-visible"
+ : "overflow-hidden truncate"),
+ )}
+ >
+ <div
+ className={cn(
+ disableTruncate ? "whitespace-nowrap" : "truncate",
+ isUtilityColumn ? "shrink-0" : "min-w-0 shrink grow",
+ disableTruncate && !isUtilityColumn && "min-w-max shrink-0",
+ )}
+ >
+ {flexRender(cell.column.columnDef.cell, cell.getContext())}
+ </div>
+ {!isUtilityColumn && cellRight?.(cell)}
+ </div>
+ )}
+ </td>
+ );
+ })}
</tr>
);
},
@@ -367,14 +459,80 @@ export function Table<T>({
enableColumnResizing = false,
}: TableProps<T>) {
const selectionEnabled = !!onRowSelectionChange || !!selectionControls;
+ const visibleColumns = table.getVisibleLeafColumns();
+ const columnsAfterSelect = new Set<string>();
+ for (let i = 1; i < visibleColumns.length; i++) {
+ if (visibleColumns[i - 1].id === "select") {
+ columnsAfterSelect.add(visibleColumns[i].id);
+ }
+ }
+ const utilityFixedSizingMode =
+ !enableColumnResizing &&
+ visibleColumns.some((column) => FIXED_UTILITY_COLUMN_IDS.has(column.id));
+
+ const scrollWrapperRef = useRef<HTMLDivElement>(null);
+ const [scrollWrapperWidth, setScrollWrapperWidth] = useState<number | null>(
+ null,
+ );
+
+ useEffect(() => {
+ if (!utilityFixedSizingMode) return;
+ const el = scrollWrapperRef.current;
+ if (!el) return;
+
+ const updateWidth = () => setScrollWrapperWidth(el.clientWidth);
+ updateWidth();
- // Memoize table width calculation
- const tableWidth = useMemo(() => {
- if (!enableColumnResizing) return "100%";
- return table
- .getVisibleLeafColumns()
- .reduce((acc, column) => acc + column.getSize(), 0);
- }, [enableColumnResizing, table.getVisibleLeafColumns()]);
+ const observer = new ResizeObserver(updateWidth);
+ observer.observe(el);
+
+ return () => observer.disconnect();
+ }, [utilityFixedSizingMode]);
+
+ const baseColumnWidths = visibleColumns.map((column) => ({
+ id: column.id,
+ width: column.getSize(),
+ }));
+
+ const utilityColumnWidths = new Map(
+ baseColumnWidths.map(({ id, width }) => [id, width]),
+ );
+
+ let utilityTableWidth = baseColumnWidths.reduce(
+ (acc, { width }) => acc + width,
+ 0,
+ );
+
+ if (
+ utilityFixedSizingMode &&
+ scrollWrapperWidth !== null &&
+ utilityTableWidth < scrollWrapperWidth
+ ) {
+ const fixedWidth = baseColumnWidths
+ .filter(({ id }) => FIXED_UTILITY_COLUMN_IDS.has(id))
+ .reduce((acc, { width }) => acc + width, 0);
+
+ const flexibleColumns = baseColumnWidths.filter(
+ ({ id }) => !FIXED_UTILITY_COLUMN_IDS.has(id),
+ );
+ const flexibleBaseWidth = flexibleColumns.reduce(
+ (acc, { width }) => acc + width,
+ 0,
+ );
+
+ const flexibleTargetWidth = Math.max(scrollWrapperWidth - fixedWidth, 0);
+ const scale =
+ flexibleBaseWidth > 0 ? flexibleTargetWidth / flexibleBaseWidth : 1;
+
+ flexibleColumns.forEach(({ id, width }) => {
+ utilityColumnWidths.set(id, width * scale);
+ });
+
+ utilityTableWidth = scrollWrapperWidth;
+ }
+
+ const getUtilityColumnWidth = (columnId: string, fallback: number) =>
+ utilityColumnWidths.get(columnId) ?? fallback;
return (
<div
@@ -394,6 +552,7 @@ export function Table<T>({
/>
)}
<div
+ ref={scrollWrapperRef}
className={cn(
"relative min-h-[400px] overflow-x-auto rounded-[inherit]",
scrollWrapperClassName,
@@ -412,11 +571,37 @@ export function Table<T>({
className,
)}
style={{
- width: "100%",
- tableLayout: enableColumnResizing ? "fixed" : "auto",
- minWidth: tableWidth,
+ width: utilityFixedSizingMode
+ ? `${utilityTableWidth}px`
+ : "100%",
+ tableLayout:
+ enableColumnResizing || utilityFixedSizingMode
+ ? "fixed"
+ : "auto",
+ minWidth: utilityFixedSizingMode
+ ? `${utilityTableWidth}px`
+ : enableColumnResizing
+ ? table
+ .getVisibleLeafColumns()
+ .reduce((acc, column) => acc + column.getSize(), 0)
+ : "100%",
}}
>
+ {utilityFixedSizingMode && (
+ <colgroup>
+ {visibleColumns.map((column) => (
+ <col
+ key={column.id}
+ style={{
+ width: getUtilityColumnWidth(
+ column.id,
+ column.getSize(),
+ ),
+ }}
+ />
+ ))}
+ </colgroup>
+ )}
<thead className="relative">
{table.getHeaderGroups().map((headerGroup) => (
<tr key={headerGroup.id}>
@@ -425,13 +610,20 @@ export function Table<T>({
header.column.id,
);
const ButtonOrDiv = isSortableColumn ? "button" : "div";
+ const isColumnAfterSelect = columnsAfterSelect.has(
+ header.column.id,
+ );
return (
<th
key={header.id}
colSpan={header.colSpan}
className={cn(
- tableCellClassName(header.id),
+ tableCellClassName(
+ header.column.id,
+ false,
+ isColumnAfterSelect,
+ ),
"text-content-emphasis select-none font-medium",
getCommonPinningClassNames(
header.column,
@@ -443,15 +635,27 @@ export function Table<T>({
enableColumnResizing && "relative",
)}
style={{
- width: header.getSize(),
+ width: utilityFixedSizingMode
+ ? getUtilityColumnWidth(
+ header.column.id,
+ header.getSize(),
+ )
+ : header.getSize(),
...getCommonPinningStyles(header.column),
}}
>
- <div className="flex items-center justify-between gap-6 !pr-0">
+ <div
+ className={cn(
+ header.column.id === "select"
+ ? "absolute inset-0 flex items-center justify-center"
+ : "flex items-center justify-between gap-6 !pr-0",
+ )}
+ >
<ButtonOrDiv
className={cn(
- "flex items-center gap-2",
- header.column.id === "select" && "size-full",
+ header.column.id === "select"
+ ? "flex size-full items-center justify-center"
+ : "flex items-center gap-2",
)}
{...(isSortableColumn && {
type: "button",
@@ -563,38 +767,90 @@ export function Table<T>({
data-selected={row.getIsSelected()}
{...rest}
>
- {row.getVisibleCells().map((cell) => (
- <td
- key={cell.id}
- className={cn(
- tableCellClassName(cell.column.id, !!onRowClick),
- "text-content-default group",
- getCommonPinningClassNames(
- cell.column,
- row.index === table.getRowModel().rows.length - 1,
- ),
- typeof tdClassName === "function"
- ? tdClassName(cell.column.id, row)
- : tdClassName,
- )}
- style={{
- minWidth: cell.column.columnDef.minSize,
- maxWidth: cell.column.columnDef.maxSize,
- width: cell.column.columnDef.size || "auto",
- ...getCommonPinningStyles(cell.column),
- }}
- >
- <div className="flex w-full items-center justify-between overflow-hidden truncate">
- <div className="min-w-0 shrink grow truncate">
- {flexRender(
- cell.column.columnDef.cell,
- cell.getContext(),
- )}
- </div>
- {cellRight?.(cell)}
- </div>
- </td>
- ))}
+ {row.getVisibleCells().map((cell) => {
+ const isUtilityColumn = ["select", "menu"].includes(
+ cell.column.id,
+ );
+ const isSelectColumn = cell.column.id === "select";
+ const isColumnAfterSelect = columnsAfterSelect.has(
+ cell.column.id,
+ );
+ const disableTruncate = !!(cell.column.columnDef
+ .meta as any)?.disableTruncate;
+
+ return (
+ <td
+ key={cell.id}
+ className={cn(
+ tableCellClassName(
+ cell.column.id,
+ !!onRowClick,
+ isColumnAfterSelect,
+ ),
+ "text-content-default group",
+ getCommonPinningClassNames(
+ cell.column,
+ row.index === table.getRowModel().rows.length -
+ 1,
+ ),
+ typeof tdClassName === "function"
+ ? tdClassName(cell.column.id, row)
+ : tdClassName,
+ )}
+ style={{
+ minWidth: cell.column.columnDef.minSize,
+ maxWidth: cell.column.columnDef.maxSize,
+ width: utilityFixedSizingMode
+ ? getUtilityColumnWidth(
+ cell.column.id,
+ cell.column.getSize(),
+ )
+ : cell.column.columnDef.size || "auto",
+ ...getCommonPinningStyles(cell.column),
+ }}
+ >
+ {isSelectColumn ? (
+ <div className="absolute inset-0 flex items-center justify-center">
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext(),
+ )}
+ </div>
+ ) : (
+ <div
+ className={cn(
+ "flex items-center",
+ isUtilityColumn
+ ? "justify-center"
+ : "w-full justify-between",
+ !isUtilityColumn &&
+ (disableTruncate
+ ? "overflow-visible"
+ : "overflow-hidden truncate"),
+ )}
+ >
+ <div
+ className={cn(
+ disableTruncate ? "whitespace-nowrap" : "truncate",
+ isUtilityColumn
+ ? "shrink-0"
+ : "min-w-0 shrink grow",
+ disableTruncate &&
+ !isUtilityColumn &&
+ "min-w-max shrink-0",
+ )}
+ >
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext(),
+ )}
+ </div>
+ {!isUtilityColumn && cellRight?.(cell)}
+ </div>
+ )}
+ </td>
+ );
+ })}
</tr>
);
})}