Adding sticky column for data table. #4202
atiqueMorshed
started this conversation in
Ideas
Replies: 1 comment
-
This is how I managed to create a sticky column (with decent visual cues):
const TableRow = React.forwardRef<
HTMLTableRowElement,
React.HTMLAttributes<HTMLTableRowElement>
>(({ className, ...props }, ref) => (
<tr
ref={ref}
className={cn(
"bg-background border-b transition-colors hover:bg-[#D6D3CD] dark:hover:bg-muted data-[state=selected]:bg-[#D6D3CD] dark:data-[state=selected]:bg-muted",
className
)}
{...props}
/>
));
TableRow.displayName = "TableRow"; const TableHead = React.forwardRef<
HTMLTableCellElement,
React.ThHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => (
<th
ref={ref}
className={cn(
"bg-inherit h-10 px-2 text-left align-middle font-medium text-muted-foreground [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",
className
)}
{...props}
/>
));
TableHead.displayName = "TableHead"; const TableCell = React.forwardRef<
HTMLTableCellElement,
React.TdHTMLAttributes<HTMLTableCellElement>
>(({ className, ...props }, ref) => (
<td
ref={ref}
className={cn(
"bg-inherit p-2 align-middle [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",
className
)}
{...props}
/>
));
TableCell.displayName = "TableCell"; Note:
<TableHead
key={header.id}
className={`border ${
stickyColumnId?.includes(header.column.id)
? "sticky left-0 z-10 shadow-[inset_-2px_0_0_0_rgba(255,255,255,0.8)] dark:shadow-[inset_-2px_0_0_0_rgba(108,117,125,0.5)]"
: ""
}`}
> <TableCell
key={cell.id}
className={`border ${
stickyColumnId?.includes(cell.column.id)
? "sticky left-0 z-10 shadow-[inset_-2px_0_0_0_rgba(255,255,255,0.8)] dark:shadow-[inset_-2px_0_0_0_rgba(108,117,125,0.5)]"
: ""
}`}
> Note:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It's typical for a table to have the identifier column or the action column to retain its position on horizontal scroll. Would love to see that being implemented with the data table.
Beta Was this translation helpful? Give feedback.
All reactions