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.

Misc. cleanup/fixes

Tim Wilson1e0d0fa5566e
diff --git a/packages/ui/src/table/selection-toolbar.tsx b/packages/ui/src/table/selection-toolbar.tsx
index 2fa1b3d636..5abd83cdc0 100644
--- a/packages/ui/src/table/selection-toolbar.tsx
+++ b/packages/ui/src/table/selection-toolbar.tsx
@@ -1,19 +1,17 @@
import { cn } from "@dub/utils";
import { Table } from "@tanstack/react-table";
import { ReactNode, useEffect, useState } from "react";
-import { useKeyboardShortcut } from "../hooks";
import { Checkbox } from "../checkbox";
+import { useKeyboardShortcut } from "../hooks";
export function SelectionToolbar<T>({
table,
controls,
className,
- overlayWidth,
}: {
table: Table<T>;
controls?: (table: Table<T>) => ReactNode;
className?: string;
- overlayWidth?: string | number;
}) {
const selectedCount = table.getSelectedRowModel().rows.length;
const [lastSelectedCount, setLastSelectedCount] = useState(0);
@@ -31,47 +29,47 @@ export function SelectionToolbar<T>({
return (
<div
className={cn(
- "pointer-events-auto w-full border-b border-border-subtle bg-white",
- "transition-transform duration-200 ease-out transition-opacity duration-200 ease-out",
+ "border-border-subtle w-full border-b bg-white",
+ "transition-opacity duration-100",
selectedCount > 0
- ? "opacity-100 translate-x-0 pointer-events-auto"
- : "opacity-0 -translate-x-full pointer-events-none",
- className
+ ? "pointer-events-auto opacity-100"
+ : "pointer-events-none opacity-0",
+ className,
)}
- style={overlayWidth ? { width: overlayWidth } : undefined}
+ {...{ inert: selectedCount > 0 ? undefined : "" }}
>
- <div className="flex items-center gap-2.5 px-2 py-2.5 h-11">
- <div className="flex items-center justify-center w-6">
- <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()}
- title="Select all"
- />
- </div>
- <span className={cn(
- "text-content-emphasis text-sm font-medium tabular-nums mr-2 transition-all duration-200 ease-out",
- selectedCount > 0
- ? "translate-x-0 opacity-100"
- : "-translate-x-1 opacity-0"
- )}>
- {lastSelectedCount} selected
- </span>
- <div className={cn(
- "flex items-center gap-2 transition-all duration-200 ease-out",
- selectedCount > 0
- ? "translate-x-0 opacity-100"
- : "-translate-x-1 opacity-0"
- )}>
- {controls?.(table)}
- </div>
+ <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()}
+ title="Select all"
+ />
+ </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>
+ </div>
</div>
);
}
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index 848bbb99ba..e8d7a2fa20 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -17,8 +17,6 @@ import {
useEffect,
useMemo,
useState,
- useRef,
- useLayoutEffect,
} from "react";
import { Button } from "../button";
import { Checkbox } from "../checkbox";
@@ -313,221 +311,211 @@ export function Table<T>({
.reduce((acc, column) => acc + column.getSize(), 0);
}, [enableColumnResizing, table.getVisibleLeafColumns()]);
- // --- NEW: Table ref and measured width for overlay toolbar ---
- const tableRef = useRef<HTMLTableElement>(null);
- const [measuredWidth, setMeasuredWidth] = useState<string | number>("100%");
- useLayoutEffect(() => {
- if (tableRef.current) {
- setMeasuredWidth(tableRef.current.scrollWidth);
- }
- }, [data, table.getVisibleLeafColumns(), enableColumnResizing, tableWidth]);
- // -----------------------------------------------------------
-
return (
<div
className={cn(
- "border-border-subtle bg-bg-default relative rounded-xl border",
+ "border-border-subtle bg-bg-default relative z-0 rounded-xl border",
containerClassName,
)}
>
{(!error && !!data?.length) || loading ? (
- <div
- className={cn(
- "relative min-h-[400px] overflow-x-auto rounded-[inherit]",
- scrollWrapperClassName,
- )}
- >
+ <>
{/* Selection Toolbar Overlay */}
{selectionEnabled && (
<SelectionToolbar
table={table}
controls={selectionControls}
- overlayWidth={measuredWidth}
- className="absolute left-0 top-0 z-10"
+ className="absolute left-0 top-0 z-10 rounded-t-[inherit]"
/>
)}
- <table
- ref={tableRef}
+ <div
className={cn(
- [
- "group/table w-full border-separate border-spacing-0 transition-[border-spacing,margin-top]",
- "[&_tr>*:first-child]:border-l-transparent",
- "[&_tr>*:last-child]:border-r-transparent",
- "[&_tr>*:last-child]:border-r-transparent",
- "[&_th]:relative [&_th]:select-none",
- enableColumnResizing && "[&_th]:group/resize",
- ],
- className,
+ "relative min-h-[400px] overflow-x-auto rounded-[inherit]",
+ scrollWrapperClassName,
)}
- style={{
- width: "100%",
- tableLayout: enableColumnResizing ? "fixed" : "auto",
- minWidth: tableWidth,
- }}
>
- <thead className="relative">
- {table.getHeaderGroups().map((headerGroup) => (
- <tr key={headerGroup.id}>
- {headerGroup.headers.map((header) => {
- const isSortableColumn = sortableColumns.includes(
- header.column.id,
- );
- const ButtonOrDiv = isSortableColumn ? "button" : "div";
+ <table
+ className={cn(
+ [
+ "group/table w-full border-separate border-spacing-0 transition-[border-spacing,margin-top]",
+ "[&_tr>*:first-child]:border-l-transparent",
+ "[&_tr>*:last-child]:border-r-transparent",
+ "[&_tr>*:last-child]:border-r-transparent",
+ "[&_th]:relative [&_th]:select-none",
+ enableColumnResizing && "[&_th]:group/resize",
+ ],
+ className,
+ )}
+ style={{
+ width: "100%",
+ tableLayout: enableColumnResizing ? "fixed" : "auto",
+ minWidth: tableWidth,
+ }}
+ >
+ <thead className="relative">
+ {table.getHeaderGroups().map((headerGroup) => (
+ <tr key={headerGroup.id}>
+ {headerGroup.headers.map((header) => {
+ const isSortableColumn = sortableColumns.includes(
+ header.column.id,
+ );
+ const ButtonOrDiv = isSortableColumn ? "button" : "div";
- return (
- <th
- key={header.id}
- colSpan={header.colSpan}
- className={cn(
- tableCellClassName(header.id),
- "text-content-emphasis select-none font-medium",
- getCommonPinningClassNames(
- header.column,
- !table.getRowModel().rows.length,
- ),
- typeof thClassName === "function"
- ? thClassName(header.column.id)
- : thClassName,
- enableColumnResizing && "relative",
- )}
- style={{
- width: header.getSize(),
- ...getCommonPinningStyles(header.column),
- }}
- >
- <div className="flex items-center justify-between gap-6 !pr-0">
- <ButtonOrDiv
- className={cn(
- "flex items-center gap-2",
- header.column.id === "select" && "size-full",
- )}
- {...(isSortableColumn && {
- type: "button",
- disabled: !isSortableColumn,
- "aria-label": "Sort by column",
- onClick: () =>
- onSortChange?.({
- sortBy: header.column.id,
- sortOrder:
- sortBy !== header.column.id
- ? "desc"
- : sortOrder === "asc"
+ return (
+ <th
+ key={header.id}
+ colSpan={header.colSpan}
+ className={cn(
+ tableCellClassName(header.id),
+ "text-content-emphasis select-none font-medium",
+ getCommonPinningClassNames(
+ header.column,
+ !table.getRowModel().rows.length,
+ ),
+ typeof thClassName === "function"
+ ? thClassName(header.column.id)
+ : thClassName,
+ enableColumnResizing && "relative",
+ )}
+ style={{
+ width: header.getSize(),
+ ...getCommonPinningStyles(header.column),
+ }}
+ >
+ <div className="flex items-center justify-between gap-6 !pr-0">
+ <ButtonOrDiv
+ className={cn(
+ "flex items-center gap-2",
+ header.column.id === "select" && "size-full",
+ )}
+ {...(isSortableColumn && {
+ type: "button",
+ disabled: !isSortableColumn,
+ "aria-label": "Sort by column",
+ onClick: () =>
+ onSortChange?.({
+ sortBy: header.column.id,
+ sortOrder:
+ sortBy !== header.column.id
? "desc"
- : "asc",
- }),
- })}
- >
- {header.isPlaceholder
- ? null
- : flexRender(
- header.column.columnDef.header,
- header.getContext(),
+ : sortOrder === "asc"
+ ? "desc"
+ : "asc",
+ }),
+ })}
+ >
+ {header.isPlaceholder
+ ? null
+ : flexRender(
+ header.column.columnDef.header,
+ header.getContext(),
+ )}
+ {isSortableColumn &&
+ sortBy === header.column.id && (
+ <SortOrder
+ className="h-3 w-3 shrink-0"
+ order={sortOrder || "desc"}
+ />
)}
- {isSortableColumn &&
- sortBy === header.column.id && (
- <SortOrder
- className="h-3 w-3 shrink-0"
- order={sortOrder || "desc"}
- />
- )}
- </ButtonOrDiv>
- </div>
- {enableColumnResizing &&
- header.column.getCanResize() &&
- !["select", "menu"].includes(header.column.id) && (
- <div
- onMouseDown={header.getResizeHandler()}
- onTouchStart={header.getResizeHandler()}
- onClick={(e) => e.stopPropagation()}
- className={resizingClassName}
- />
- )}
- </th>
- );
- })}
- </tr>
- ))}
- </thead>
- <tbody>
- {table.getRowModel().rows.map((row) => {
- const props =
- typeof rowProps === "function" ? rowProps(row) : rowProps;
- const { className, ...rest } = props || {};
-
- return enableColumnResizing ? (
- <ResizableTableRow
- key={`${row.id}-${table
- .getVisibleLeafColumns()
- .map((col) => col.id)
- .join(",")}`}
- row={row}
- onRowClick={onRowClick}
- rowProps={props}
- cellRight={cellRight}
- tdClassName={tdClassName}
- table={table}
- />
- ) : (
- <tr
- key={row.id}
- className={cn(
- "group/row",
- onRowClick && "cursor-pointer select-none",
- table.getRowModel().rows.length > 8 &&
- row.index === table.getRowModel().rows.length - 1 &&
- "[&_td]:border-b-0",
- className,
- )}
- onClick={
- onRowClick
- ? (e) => {
- if (isClickOnInteractiveChild(e)) return;
- onRowClick(row, e);
- }
- : undefined
- }
- 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)
- : 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(),
- )}
+ </ButtonOrDiv>
</div>
- {cellRight?.(cell)}
- </div>
- </td>
- ))}
+ {enableColumnResizing &&
+ header.column.getCanResize() &&
+ !["select", "menu"].includes(header.column.id) && (
+ <div
+ onMouseDown={header.getResizeHandler()}
+ onTouchStart={header.getResizeHandler()}
+ onClick={(e) => e.stopPropagation()}
+ className={resizingClassName}
+ />
+ )}
+ </th>
+ );
+ })}
</tr>
- );
- })}
- </tbody>
- </table>
- {children}
- </div>
+ ))}
+ </thead>
+ <tbody>
+ {table.getRowModel().rows.map((row) => {
+ const props =
+ typeof rowProps === "function" ? rowProps(row) : rowProps;
+ const { className, ...rest } = props || {};
+
+ return enableColumnResizing ? (
+ <ResizableTableRow
+ key={`${row.id}-${table
+ .getVisibleLeafColumns()
+ .map((col) => col.id)
+ .join(",")}`}
+ row={row}
+ onRowClick={onRowClick}
+ rowProps={props}
+ cellRight={cellRight}
+ tdClassName={tdClassName}
+ table={table}
+ />
+ ) : (
+ <tr
+ key={row.id}
+ className={cn(
+ "group/row",
+ onRowClick && "cursor-pointer select-none",
+ table.getRowModel().rows.length > 8 &&
+ row.index === table.getRowModel().rows.length - 1 &&
+ "[&_td]:border-b-0",
+ className,
+ )}
+ onClick={
+ onRowClick
+ ? (e) => {
+ if (isClickOnInteractiveChild(e)) return;
+ onRowClick(row, e);
+ }
+ : undefined
+ }
+ 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)
+ : 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>
+ ))}
+ </tr>
+ );
+ })}
+ </tbody>
+ </table>
+ {children}
+ </div>
+ </>
) : (
<div className="text-content-subtle flex h-96 w-full items-center justify-center text-sm">
{error ||