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.

Move links list stuff from Table to a new CardList component

Tim Wilsonec1566439c31
diff --git a/apps/web/ui/links/link-card-placeholder.tsx b/apps/web/ui/links/link-card-placeholder.tsx
index 004206a9cd..dd8fe21520 100644
--- a/apps/web/ui/links/link-card-placeholder.tsx
+++ b/apps/web/ui/links/link-card-placeholder.tsx
@@ -1,16 +1,36 @@
+import { CardList } from "@dub/ui";
+import { cn } from "@dub/utils";
+import { useContext } from "react";
+
export default function LinkCardPlaceholder() {
+ const { variant } = useContext(CardList.Context);
+
return (
- <li className="flex items-center rounded-lg border border-gray-200 bg-white p-3 ring-2 ring-gray-50 transition-all sm:p-4">
- <div className="mr-2 h-10 w-10 animate-pulse rounded-full bg-gray-200" />
- <div>
- <div className="mb-3 flex items-center space-x-2">
- <div className="h-5 w-28 animate-pulse rounded-md bg-gray-200" />
- <div className="h-5 w-5 animate-pulse rounded-full bg-gray-200" />
- <div className="h-5 w-5 animate-pulse rounded-full bg-gray-200" />
- <div className="h-5 w-20 animate-pulse rounded-md bg-gray-200" />
+ <>
+ <div className="flex grow items-center gap-3">
+ <div className="hidden h-8 w-8 animate-pulse rounded-full bg-gray-200 sm:block" />
+ <div
+ className={cn(
+ "flex h-[60px] gap-x-2 gap-y-1 transition-[height]",
+ variant === "loose"
+ ? "h-[60px] flex-col justify-center"
+ : "h-[32px] flex-row items-center",
+ )}
+ >
+ <div className="h-5 w-32 animate-pulse rounded-md bg-gray-200 sm:w-44" />
+ <div
+ className={cn(
+ "h-4 w-28 animate-pulse rounded-md bg-gray-200",
+ variant === "compact" && "hidden sm:block",
+ )}
+ />
</div>
- <div className="h-4 w-full animate-pulse rounded-md bg-gray-200" />
</div>
- </li>
+ <div className="flex items-center gap-5">
+ <div className="h-6 w-16 animate-pulse rounded-md bg-gray-200" />
+ <div className="hidden h-6 w-11 animate-pulse rounded-md bg-gray-200 sm:block" />
+ <div className="hidden h-6 w-10 sm:block" />
+ </div>
+ </>
);
}
diff --git a/apps/web/ui/links/link-controls.tsx b/apps/web/ui/links/link-controls.tsx
index c59b4c9341..4b42c47fa3 100644
--- a/apps/web/ui/links/link-controls.tsx
+++ b/apps/web/ui/links/link-controls.tsx
@@ -117,8 +117,8 @@ export function LinkControls({ link }: { link: ResponseLink }) {
<div
className={cn(
"flex items-center justify-end divide-x divide-gray-200 overflow-hidden rounded-md border border-gray-200",
- "divide-transparent border-transparent sm:group-hover/row:divide-gray-200 sm:group-hover/row:border-gray-200", // Only show divider on mobile or when hovered
- "w-[30px] transition-[width] duration-300 sm:w-[43px] sm:group-hover/row:w-[124px]",
+ "divide-transparent border-transparent sm:group-hover/card:divide-gray-200 sm:group-hover/card:border-gray-200", // Only show divider on mobile or when hovered
+ "w-[30px] transition-[width] duration-300 sm:w-[43px] sm:group-hover/card:w-[124px]",
)}
>
<LinkQRModal />
diff --git a/apps/web/ui/links/link-title-column.tsx b/apps/web/ui/links/link-title-column.tsx
index 3db32d766e..723389b67b 100644
--- a/apps/web/ui/links/link-title-column.tsx
+++ b/apps/web/ui/links/link-title-column.tsx
@@ -38,33 +38,33 @@ export function LinkTitleColumn({ link }: { link: ResponseLink }) {
return (
<div
ref={ref}
- className="flex h-[32px] items-center gap-3 transition-[height] group-data-[variant=loose-list]/table:h-[60px]"
+ className="flex h-[32px] items-center gap-3 transition-[height] group-data-[variant=loose]/card-list:h-[60px]"
>
{isVisible && (
<>
<div className="relative hidden shrink-0 items-center justify-center sm:flex">
{/* Link logo background circle */}
- <div className="absolute inset-0 shrink-0 rounded-full border border-gray-200 opacity-0 transition-opacity group-data-[variant=loose-list]/table:sm:opacity-100">
+ <div className="absolute inset-0 shrink-0 rounded-full border border-gray-200 opacity-0 transition-opacity group-data-[variant=loose]/card-list:sm:opacity-100">
<div className="h-full w-full rounded-full border border-white bg-gradient-to-t from-gray-100" />
</div>
- <div className="relative pr-0.5 transition-[padding] group-data-[variant=loose-list]/table:sm:p-2">
+ <div className="relative pr-0.5 transition-[padding] group-data-[variant=loose]/card-list:sm:p-2">
<LinkLogo
apexDomain={getApexDomain(url)}
- className="h-4 w-4 shrink-0 transition-[width,height] sm:h-6 sm:w-6 group-data-[variant=loose-list]/table:sm:h-5 group-data-[variant=loose-list]/table:sm:w-5"
+ className="h-4 w-4 shrink-0 transition-[width,height] sm:h-6 sm:w-6 group-data-[variant=loose]/card-list:sm:h-5 group-data-[variant=loose]/card-list:sm:w-5"
/>
</div>
</div>
- <div className="h-[24px] min-w-0 overflow-hidden transition-[height] group-data-[variant=loose-list]/table:h-[44px]">
+ <div className="h-[24px] min-w-0 overflow-hidden transition-[height] group-data-[variant=loose]/card-list:h-[44px]">
<div className="flex items-center gap-2">
<div className="min-w-0 text-gray-950">
<UnverifiedTooltip link={link}>
<span
- className="flex items-center truncate font-medium"
+ className="flex items-center font-medium"
title={`${domain}${path}`}
>
- {domain + path}
+ <span className="truncate">{domain + path}</span>
- <div className="transition-all group-hover/row:translate-x-0 group-hover:opacity-100 sm:-translate-x-1 sm:opacity-0">
+ <div className="transition-all group-hover/card:w-[22px] group-hover/card:translate-x-0 group-hover/card:opacity-100 sm:w-0 sm:-translate-x-1 sm:opacity-0">
<CopyButton
value={domain + path}
variant="neutral"
@@ -122,10 +122,10 @@ function Details({ link, compact }: { link: ResponseLink; compact?: boolean }) {
return (
<div
className={cn(
- "flex min-w-0 items-center gap-1.5 text-xs transition-[opacity,width] delay-[0s,150ms] duration-[150ms,0s] md:gap-3",
+ "min-w-0 items-center gap-1.5 text-xs transition-[opacity,display] delay-[0s,150ms] duration-[150ms,0s] md:gap-3",
compact
- ? "w-0 opacity-0 group-data-[variant=compact-list]/table:w-auto group-data-[variant=compact-list]/table:opacity-100"
- : "opacity-0 group-data-[variant=loose-list]/table:opacity-100",
+ ? "hidden opacity-0 group-data-[variant=compact]/card-list:flex group-data-[variant=compact]/card-list:opacity-100"
+ : "hidden opacity-0 group-data-[variant=loose]/card-list:flex group-data-[variant=loose]/card-list:opacity-100",
)}
>
<div className="flex min-w-0 items-center gap-1">
diff --git a/apps/web/ui/links/links-container.tsx b/apps/web/ui/links/links-container.tsx
index 4fc01f14b5..4510997a63 100644
--- a/apps/web/ui/links/links-container.tsx
+++ b/apps/web/ui/links/links-container.tsx
@@ -3,17 +3,9 @@
import useLinks from "@/lib/swr/use-links";
import useLinksCount from "@/lib/swr/use-links-count";
import { LinkWithTagsProps, UserProps } from "@/lib/types";
-import {
- MaxWidthWrapper,
- Table,
- useMediaQuery,
- useRouterStuff,
- useTable,
-} from "@dub/ui";
-import { PAGINATION_LIMIT } from "@dub/utils";
-import { ColumnDef } from "@tanstack/react-table";
+import { CardList, MaxWidthWrapper, useRouterStuff } from "@dub/ui";
import { useSearchParams } from "next/navigation";
-import { useEffect, useMemo, useState } from "react";
+import LinkCardPlaceholder from "./link-card-placeholder";
import { LinkDetailsColumn } from "./link-details-column";
import LinkNotFound from "./link-not-found";
import { LinkTitleColumn } from "./link-title-column";
@@ -64,7 +56,7 @@ function LinksList({
}) {
const { queryParams } = useRouterStuff();
const searchParams = useSearchParams();
- const currentPage = parseInt(searchParams?.get("page") || "1") || 1;
+ const page = (parseInt(searchParams?.get("page") || "1") || 1) - 1;
const isFiltered = [
"domain",
@@ -74,62 +66,51 @@ function LinksList({
"showArchived",
].some((param) => searchParams.has(param));
- const { isMobile } = useMediaQuery();
+ return loading || links?.length ? (
+ <div>
+ {/* Cards */}
+ <CardList variant={compact ? "compact" : "loose"} loading={loading}>
+ {links?.length
+ ? // Link cards
+ links.map((link) => (
+ <CardList.Card
+ key={link.id}
+ className="flex items-center gap-4 text-sm"
+ >
+ <div className="min-w-0 grow">
+ <LinkTitleColumn link={link} />
+ </div>
+ <LinkDetailsColumn link={link} />
+ </CardList.Card>
+ ))
+ : // Loading placeholder cards
+ Array.from({ length: 6 }).map((_, idx) => (
+ <CardList.Card key={idx} className="flex items-center gap-4">
+ <LinkCardPlaceholder />
+ </CardList.Card>
+ ))}
+ </CardList>
- const columns: ColumnDef<ResponseLink, any>[] = useMemo(
- () => [
- {
- id: "link",
- accessorFn: (d) => d,
- cell: ({ getValue }) => (
- <LinkTitleColumn link={getValue() as ResponseLink} />
- ),
- enableHiding: false,
- size: 10000,
- },
- {
- id: "details",
- accessorFn: (d) => d,
- cell: ({ getValue }) => (
- <LinkDetailsColumn link={getValue() as ResponseLink} />
- ),
- size: isMobile ? 80 : 400,
- },
- ],
- [isMobile],
- );
-
- const [pagination, setPagination] = useState({
- pageIndex: currentPage - 1,
- pageSize: PAGINATION_LIMIT,
- });
-
- useEffect(() => {
- queryParams(
- pagination.pageIndex === 0
- ? { del: "page" }
- : {
- set: {
- page: (pagination.pageIndex + 1).toString(),
- },
- },
- );
- }, [pagination]);
-
- const { table, ...tableProps } = useTable({
- variant: compact ? "compact-list" : "loose-list",
- showColumnHeadings: false,
- data: links ?? [],
- loading,
- columns,
- pagination: pagination,
- rowCount: count ?? 0,
- onPaginationChange: setPagination,
- resourceName: (plural) => `link${plural ? "s" : ""}`,
- });
-
- return links === undefined || links.length ? (
- <Table table={table} {...tableProps} />
+ {/* Pagination */}
+ {!!links?.length && (
+ <CardList.Pagination
+ page={page}
+ onPageChange={(p) => {
+ const newPage = p(page);
+ queryParams(
+ newPage === 0
+ ? { del: "page" }
+ : {
+ set: {
+ page: (newPage + 1).toString(),
+ },
+ },
+ );
+ }}
+ totalCount={count ?? links.length}
+ />
+ )}
+ </div>
) : isFiltered ? (
<LinkNotFound />
) : (
diff --git a/packages/ui/src/card-list/card-list-card.tsx b/packages/ui/src/card-list/card-list-card.tsx
new file mode 100644
index 0000000000..4c0b3c04c3
--- /dev/null
+++ b/packages/ui/src/card-list/card-list-card.tsx
@@ -0,0 +1,31 @@
+import { cn } from "@dub/utils";
+import { cva } from "class-variance-authority";
+import { PropsWithChildren, useContext } from "react";
+import { CardListContext } from "./card-list";
+
+const cardListCardVariants = cva(
+ "w-full py-2.5 px-4 group/card border-gray-200 bg-white",
+ {
+ variants: {
+ variant: {
+ compact:
+ "first-of-type:rounded-t-xl last-of-type:rounded-b-xl first-of-type:border-t border-b border-x",
+ loose:
+ "border rounded-xl transition-[filter] hover:[filter:drop-shadow(0_8px_12px_#222A350d)_drop-shadow(0_32px_80px_#2f30370f)]",
+ },
+ },
+ },
+);
+
+export function CardListCard({
+ className,
+ children,
+}: PropsWithChildren<{ className?: string }>) {
+ const { variant } = useContext(CardListContext);
+
+ return (
+ <li className={cn(cardListCardVariants({ variant }), className)}>
+ {children}
+ </li>
+ );
+}
diff --git a/packages/ui/src/card-list/card-list-pagination.tsx b/packages/ui/src/card-list/card-list-pagination.tsx
new file mode 100644
index 0000000000..dc240dede6
--- /dev/null
+++ b/packages/ui/src/card-list/card-list-pagination.tsx
@@ -0,0 +1,50 @@
+import { PAGINATION_LIMIT } from "@dub/utils";
+import { Button } from "../button";
+
+export function CardListPagination({
+ page,
+ onPageChange,
+ pageSize = PAGINATION_LIMIT,
+ totalCount,
+ resourceName = (plural) => `item${plural ? "s" : ""}`,
+}: {
+ page: number;
+ onPageChange: (fn: (prev: number) => number) => void;
+ pageSize?: number;
+ totalCount: number;
+ resourceName?: (plural: boolean) => string;
+}) {
+ return (
+ <>
+ {/* Placeholder to make room for the floating pagination at the bottom of the list */}
+ <div className="h-[90px]" />
+ <nav className="fixed bottom-4 left-1/2 flex w-full max-w-[768px] -translate-x-1/2 items-center justify-between rounded-xl border border-gray-200 bg-white px-4 py-3.5 text-sm leading-6 text-gray-600 [filter:drop-shadow(0_5px_8px_#222A351d)]">
+ <div>
+ <span className="hidden sm:inline-block">Viewing</span>{" "}
+ <span className="font-medium">
+ {page * pageSize + 1}-
+ {Math.min(page * pageSize + pageSize, totalCount)}
+ </span>{" "}
+ of <span className="font-medium">{totalCount.toLocaleString()}</span>{" "}
+ {resourceName?.(totalCount !== 1) || "items"}
+ </div>
+ <div className="flex items-center gap-2">
+ <Button
+ variant="secondary"
+ text="Previous"
+ className="h-7 px-2"
+ onClick={() => onPageChange((prev) => prev - 1)}
+ disabled={page < 1}
+ />
+ <Button
+ variant="secondary"
+ text="Next"
+ className="h-7 px-2"
+ onClick={() => onPageChange((prev) => prev + 1)}
+ disabled={(page + 1) * pageSize >= totalCount}
+ />
+ </div>
+ </nav>
+ </>
+ );
+}
diff --git a/packages/ui/src/card-list/card-list.tsx b/packages/ui/src/card-list/card-list.tsx
new file mode 100644
index 0000000000..ee43934036
--- /dev/null
+++ b/packages/ui/src/card-list/card-list.tsx
@@ -0,0 +1,46 @@
+import { cn } from "@dub/utils";
+import { VariantProps, cva } from "class-variance-authority";
+import { PropsWithChildren, createContext } from "react";
+
+const cardListVariants = cva(
+ "group/card-list w-full flex flex-col transition-[gap,opacity] min-w-0",
+ {
+ variants: {
+ variant: {
+ compact: "gap-0",
+ loose: "gap-4",
+ },
+ loading: {
+ true: "opacity-50",
+ },
+ },
+ },
+);
+
+type CardListProps = PropsWithChildren<{
+ loading?: boolean;
+ className?: string;
+}> &
+ VariantProps<typeof cardListVariants>;
+
+export const CardListContext = createContext<Pick<CardListProps, "variant">>({
+ variant: "loose",
+});
+
+export function CardList({
+ variant = "loose",
+ loading = false,
+ className,
+ children,
+}: CardListProps) {
+ return (
+ <ul
+ className={cn(cardListVariants({ variant, loading }), className)}
+ data-variant={variant}
+ >
+ <CardListContext.Provider value={{ variant }}>
+ {children}
+ </CardListContext.Provider>
+ </ul>
+ );
+}
diff --git a/packages/ui/src/card-list/index.ts b/packages/ui/src/card-list/index.ts
new file mode 100644
index 0000000000..050ba7d3ed
--- /dev/null
+++ b/packages/ui/src/card-list/index.ts
@@ -0,0 +1,11 @@
+import { CardList as CardListComponent, CardListContext } from "./card-list";
+import { CardListCard } from "./card-list-card";
+import { CardListPagination } from "./card-list-pagination";
+
+const CardList = Object.assign(CardListComponent, {
+ Card: CardListCard,
+ Context: CardListContext,
+ Pagination: CardListPagination,
+});
+
+export { CardList };
diff --git a/packages/ui/src/index.tsx b/packages/ui/src/index.tsx
index d1834792f0..0ba31731a2 100644
--- a/packages/ui/src/index.tsx
+++ b/packages/ui/src/index.tsx
@@ -7,6 +7,7 @@ export * from "./alert";
export * from "./avatar";
export * from "./badge";
export * from "./button";
+export * from "./card-list";
export * from "./checkbox";
export * from "./date-picker";
export * from "./file-upload";
diff --git a/packages/ui/src/table/table.tsx b/packages/ui/src/table/table.tsx
index 10204ccb1e..b4ccccdbff 100644
--- a/packages/ui/src/table/table.tsx
+++ b/packages/ui/src/table/table.tsx
@@ -9,13 +9,11 @@ import {
getCoreRowModel,
useReactTable,
} from "@tanstack/react-table";
-import { VariantProps, cva } from "class-variance-authority";
import { AnimatePresence, motion } from "framer-motion";
import {
Dispatch,
ReactNode,
SetStateAction,
- createContext,
useEffect,
useMemo,
useState,
@@ -23,117 +21,16 @@ import {
import { Button } from "../button";
import { LoadingSpinner, SortOrder } from "../icons";
-const variantDefaults = { default: "", "compact-list": "", "loose-list": "" };
+const tableCellClassName = cn([
+ "py-2.5 text-left text-sm leading-6 whitespace-nowrap border-gray-200 px-4 relative",
+ "border-r border-b", // Right and left borders, removed from outer columns above
+]);
-const tableVariants = cva(
- [
- "group/table w-full table-fixed border-separate transition-[border-spacing,margin-top] border-spacing-0",
- ],
- {
- variants: {
- variant: {
- ...variantDefaults,
- "loose-list": [
- "border-spacing-y-4 -mt-4",
- // Round left and right sides of each row
- "[&_tr>*:first-child]:rounded-l-xl",
- "[&_tr>*:last-child]:rounded-r-xl",
- ],
- },
- },
- compoundVariants: [
- {
- variant: ["loose-list", "compact-list"],
- className: [
- "[&_tr>*:first-child]:border-l [&_tr>*:last-child]:border-r",
- ],
- },
- {
- variant: ["default", "compact-list"],
- className: [
- // Remove side borders from table to avoid interfering with outer border
- "[&_tr>*:first-child]:border-l-transparent", // Left column
- "[&_tr>*:last-child]:border-r-transparent", // Right column
- ],
- },
- ],
- },
-);
-
-const tableWrapperVariants = cva("relative sm:rounded-xl", {
- variants: {
- variant: {
- ...variantDefaults,
- default: "border border-gray-200 bg-white",
- "compact-list": "border border-gray-200 bg-white",
- },
- },
-});
-
-const tableCellVariants = cva(
- "py-2.5 text-left text-sm leading-6 whitespace-nowrap border-gray-200 transition-[background-color]",
- {
- variants: {
- variant: {
- default: "border-r border-b", // Right and left borders, removed from outer columns above
- "compact-list": "border-b", // Bottom border, with other sides added above
- "loose-list": [
- "border-y bg-white px-2", // Top and bottom borders, with other sides added above
-
- // Outer cells have extra padding on the outside
- "[&:first-child]:pl-4",
- "[&:last-child]:pr-4",
- ],
- },
- },
- compoundVariants: [
- {
- variant: ["default", "compact-list"],
- className: "px-4",
- },
- ],
- },
-);
-
-const tableRowVariants = cva("group/row", {
- variants: {
- variant: {
- ...variantDefaults,
- "loose-list":
- "transition-[filter] hover:[filter:drop-shadow(0_8px_12px_#222A350d)_drop-shadow(0_32px_80px_#2f30370f)]",
- },
- },
-});
-
-const tablePaginationVariants = cva(
- "-mt-px sticky flex mx-auto items-center border-gray-200 justify-between bg-white px-4 py-3.5 text-sm leading-6 text-gray-600 transition-[max-width,width,bottom,filter]",
- {
- variants: {
- variant: {
- ...variantDefaults,
- "loose-list":
- "border rounded-xl [filter:drop-shadow(0_5px_8px_#222A351d)] bottom-4 max-w-[768px] w-full",
- },
- },
- compoundVariants: [
- {
- variant: ["default", "compact-list"],
- className: "rounded-b-[inherit] bottom-0 w-full border-t max-w-full",
- },
- ],
- },
-);
-
-export const TableContext = createContext<{
- variant: "default" | "compact-list" | "loose-list";
-}>({ variant: "default" });
-
-type UseTableProps<T> = VariantProps<typeof tableVariants> & {
+type UseTableProps<T> = {
columns: ColumnDef<T, any>[];
data: T[];
loading?: boolean;
error?: string;
- showColumnHeadings?: boolean;
emptyState?: ReactNode;
cellRight?: (cell: Cell<T, any>) => ReactNode;
defaultColumn?: Partial<ColumnDef<T, any>>;
@@ -152,13 +49,13 @@ type UseTableProps<T> = VariantProps<typeof tableVariants> & {
thClassName?: string;
tdClassName?: string;
} & (
- | {
- pagination: PaginationState;
- onPaginationChange?: Dispatch<SetStateAction<PaginationState>>;
- rowCount: number;
- }
- | { pagination?: never; onPaginationChange?: never; rowCount?: never }
- );
+ | {
+ pagination: PaginationState;
+ onPaginationChange?: Dispatch<SetStateAction<PaginationState>>;
+ rowCount: number;
+ }
+ | { pagination?: never; onPaginationChange?: never; rowCount?: never }
+);
type TableProps<T> = UseTableProps<T> & {
table: TableType<T>;
@@ -218,12 +115,10 @@ export function useTable<T extends any>(
}
export function Table<T>({
- variant = "default",
columns,
data,
loading,
error,
- showColumnHeadings = true,
emptyState,
cellRight,
sortBy,
@@ -257,178 +152,181 @@ export function Table<T>({
]);
return (
- <TableContext.Provider value={{ variant: variant ?? "default" }}>
- <div className={tableWrapperVariants({ variant })}>
- {(!error && !!data?.length) || loading ? (
- <div className="min-h-[400px] rounded-[inherit]">
- <table
- className={cn(tableVariants({ variant }), className)}
- style={columnSizeVars}
- data-variant={variant}
- >
- {showColumnHeadings && (
- <thead>
- {table.getHeaderGroups().map((headerGroup) => (
- <tr key={headerGroup.id}>
- {headerGroup.headers.map((header) => {
- const isSortableColumn = sortableColumns.includes(
- header.column.id,
- );
- return (
- <th
- key={header.id}
- className={cn(
- tableCellVariants({ variant }),
- "relative select-none font-medium",
- thClassName,
- )}
- style={{
- width: `calc(var(--header-${header?.id}-size) * 1px)`,
- }}
- >
- <div className="flex items-center justify-between gap-6 !pr-0">
- <button
- type="button"
- className="flex items-center gap-2"
- disabled={!isSortableColumn}
- onClick={() =>
- onSortChange?.({
- sortBy: header.column.id,
- sortOrder:
- sortBy !== header.column.id
- ? "desc"
- : sortOrder === "asc"
- ? "desc"
- : "asc",
- })
- }
- aria-label="Sort by column"
- >
- <span>
- {header.isPlaceholder
- ? null
- : flexRender(
- header.column.columnDef.header,
- header.getContext(),
- )}
- </span>
- {isSortableColumn && (
- <SortOrder
- order={
- sortBy === header.column.id
- ? sortOrder || "desc"
- : null
- }
- />
- )}
- </button>
- </div>
- {resizeColumns && (
- <div
- className="absolute -right-[4px] top-0 z-[1] h-full w-[7px] cursor-col-resize"
- {...{
- onDoubleClick: () =>
- header.column.resetSize(),
- onMouseDown: header.getResizeHandler(),
- onTouchStart: header.getResizeHandler(),
- }}
- />
- )}
- </th>
- );
- })}
- </tr>
- ))}
- </thead>
- )}
- <tbody>
- {table.getRowModel().rows.map((row) => (
- <tr key={row.id} className={tableRowVariants({ variant })}>
- {row.getVisibleCells().map((cell) => (
- <td
- key={cell.id}
+ <div className="relative border border-gray-200 bg-white sm:rounded-xl">
+ {(!error && !!data?.length) || loading ? (
+ <div className="min-h-[400px] overflow-x-auto rounded-[inherit]">
+ <table
+ className={cn(
+ [
+ "group/table w-full table-fixed border-separate border-spacing-0 transition-[border-spacing,margin-top]",
+ // Remove side borders from table to avoid interfering with outer border
+ "[&_tr>*:first-child]:border-l-transparent", // Left column
+ "[&_tr>*:last-child]:border-r-transparent", // Right column
+ ],
+ className,
+ )}
+ style={columnSizeVars}
+ >
+ <thead>
+ {table.getHeaderGroups().map((headerGroup) => (
+ <tr key={headerGroup.id}>
+ {headerGroup.headers.map((header) => {
+ const isSortableColumn = sortableColumns.includes(
+ header.column.id,
+ );
+ return (
+ <th
+ key={header.id}
className={cn(
- tableCellVariants({ variant }),
- "group relative text-gray-600",
- tdClassName,
+ tableCellClassName,
+ "select-none font-medium",
+ thClassName,
)}
style={{
- width: `calc(var(--col-${cell.column.id}-size) * 1px)`,
+ width: `calc(var(--header-${header?.id}-size) * 1px)`,
}}
>
- <div className="flex w-full items-center justify-between overflow-hidden truncate">
- <div className="min-w-0 shrink grow">
- {flexRender(
- cell.column.columnDef.cell,
- cell.getContext(),
+ <div className="flex items-center justify-between gap-6 !pr-0">
+ <button
+ type="button"
+ className="flex items-center gap-2"
+ disabled={!isSortableColumn}
+ onClick={() =>
+ onSortChange?.({
+ sortBy: header.column.id,
+ sortOrder:
+ sortBy !== header.column.id
+ ? "desc"
+ : sortOrder === "asc"
+ ? "desc"
+ : "asc",
+ })
+ }
+ aria-label="Sort by column"
+ >
+ <span>
+ {header.isPlaceholder
+ ? null
+ : flexRender(
+ header.column.columnDef.header,
+ header.getContext(),
+ )}
+ </span>
+ {isSortableColumn && (
+ <SortOrder
+ order={
+ sortBy === header.column.id
+ ? sortOrder || "desc"
+ : null
+ }
+ />
)}
- </div>
- {cellRight?.(cell)}
+ </button>
</div>
- </td>
- ))}
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- ) : (
- <div className="flex h-96 w-full items-center justify-center text-sm text-gray-500">
- {error ||
- emptyState ||
- `No ${resourceName?.(true) || "items"} found.`}
+ {resizeColumns &&
+ header.index < headerGroup.headers.length - 1 && (
+ <div
+ className="absolute -right-[4px] top-0 z-[1] h-full w-[7px] cursor-col-resize"
+ {...{
+ onDoubleClick: () => header.column.resetSize(),
+ onMouseDown: header.getResizeHandler(),
+ onTouchStart: header.getResizeHandler(),
+ }}
+ />
+ )}
+ </th>
+ );
+ })}
+ </tr>
+ ))}
+ </thead>
+ <tbody>
+ {table.getRowModel().rows.map((row) => (
+ <tr key={row.id}>
+ {row.getVisibleCells().map((cell) => (
+ <td
+ key={cell.id}
+ className={cn(
+ tableCellClassName,
+ "group text-gray-600",
+ tdClassName,
+ )}
+ style={{
+ width: `calc(var(--col-${cell.column.id}-size) * 1px)`,
+ }}
+ >
+ <div className="flex w-full items-center justify-between overflow-hidden truncate">
+ <div className="min-w-0 shrink grow">
+ {flexRender(
+ cell.column.columnDef.cell,
+ cell.getContext(),
+ )}
+ </div>
+ {cellRight?.(cell)}
+ </div>
+ </td>
+ ))}
+ </tr>
+ ))}
+ </tbody>
+ </table>
+ </div>
+ ) : (
+ <div className="flex h-96 w-full items-center justify-center text-sm text-gray-500">
+ {error ||
+ emptyState ||
+ `No ${resourceName?.(true) || "items"} found.`}
+ </div>
+ )}
+ {pagination && !error && !!data?.length && (
+ <div className="sticky bottom-0 mx-auto -mt-px flex w-full max-w-full items-center justify-between rounded-b-[inherit] border-t border-gray-200 bg-white px-4 py-3.5 text-sm leading-6 text-gray-600">
+ <div>
+ <span className="hidden sm:inline-block">Viewing</span>{" "}
+ <span className="font-medium">
+ {pagination.pageIndex * pagination.pageSize + 1}-
+ {Math.min(
+ pagination.pageIndex * pagination.pageSize +
+ pagination.pageSize,
+ table.getRowCount(),
+ )}
+ </span>{" "}
+ of{" "}
+ <span className="font-medium">
+ {table.getRowCount().toLocaleString()}
+ </span>{" "}
+ {resourceName?.(table.getRowCount() !== 1) || "items"}
</div>
- )}
- {pagination && !error && !!data?.length && (
- <div className={tablePaginationVariants({ variant })}>
- <div>
- <span className="hidden sm:inline-block">Viewing</span>{" "}
- <span className="font-medium">
- {pagination.pageIndex * pagination.pageSize + 1}-
- {Math.min(
- pagination.pageIndex * pagination.pageSize +
- pagination.pageSize,
- table.getRowCount(),
- )}
- </span>{" "}
- of{" "}
- <span className="font-medium">
- {table.getRowCount().toLocaleString()}
- </span>{" "}
- {resourceName?.(table.getRowCount() !== 1) || "items"}
- </div>
- <div className="flex items-center gap-2">
- <Button
- variant="secondary"
- text="Previous"
- className="h-7 px-2"
- onClick={() => table.previousPage()}
- disabled={!table.getCanPreviousPage()}
- />
- <Button
- variant="secondary"
- text="Next"
- className="h-7 px-2"
- onClick={() => table.nextPage()}
- disabled={!table.getCanNextPage()}
- />
- </div>
+ <div className="flex items-center gap-2">
+ <Button
+ variant="secondary"
+ text="Previous"
+ className="h-7 px-2"
+ onClick={() => table.previousPage()}
+ disabled={!table.getCanPreviousPage()}
+ />
+ <Button
+ variant="secondary"
+ text="Next"
+ className="h-7 px-2"
+ onClick={() => table.nextPage()}
+ disabled={!table.getCanNextPage()}
+ />
</div>
- )}
+ </div>
+ )}
- {/* Loading overlay */}
- <AnimatePresence>
- {loading && (
- <motion.div
- initial={{ opacity: 0 }}
- animate={{ opacity: 1 }}
- className="absolute inset-0 flex items-center justify-center rounded-xl bg-white/50"
- >
- <LoadingSpinner />
- </motion.div>
- )}
- </AnimatePresence>
- </div>
- </TableContext.Provider>
+ {/* Loading overlay */}
+ <AnimatePresence>
+ {loading && (
+ <motion.div
+ initial={{ opacity: 0 }}
+ animate={{ opacity: 1 }}
+ className="absolute inset-0 flex items-center justify-center rounded-xl bg-white/50"
+ >
+ <LoadingSpinner />
+ </motion.div>
+ )}
+ </AnimatePresence>
+ </div>
);
}