Heatmap Table
shadcn-table-library/heatmap-tableFreeBlock
Matrix data with cell background intensity mapped to value, normalized globally across the whole table.
Install it
npx shadcn@latest add https://shad-table.dev/r/heatmap-table.jsonSource
1'use client'23import { useMemo } from 'react'4import {5 flexRender,6 getCoreRowModel,7 useReactTable,8} from '@tanstack/react-table'9import type { ColumnDef } from '@tanstack/react-table'1011import {12 Table,13 TableBody,14 TableCell,15 TableHead,16 TableHeader,17 TableRow,18} from '#/components/ui/table'19import { cn } from '#/lib/utils.ts'20import type { HeatmapRow } from './heatmap'21import { getHeatColor, getHeatmapRange } from './heatmap'2223interface HeatmapTableProps {24 columns: string[]25 data: HeatmapRow[]26 formatValue?: (value: number) => string27}2829export function HeatmapTable({30 columns,31 data,32 formatValue = (value) => String(value),33}: HeatmapTableProps) {34 const { min, max } = useMemo(35 () => getHeatmapRange(data, columns),36 [data, columns],37 )3839 const tableColumns = useMemo<ColumnDef<HeatmapRow>[]>(40 () => [41 {42 id: 'label',43 header: '',44 accessorKey: 'label',45 cell: ({ getValue }) => (46 <span className="font-medium">{getValue() as string}</span>47 ),48 },49 ...columns.map(50 (col): ColumnDef<HeatmapRow> => ({51 id: col,52 header: col,53 accessorFn: (row) => row.values[col],54 cell: ({ getValue }) => {55 const value = getValue() as number56 const { backgroundColor, isDark } = getHeatColor(value, min, max)57 return (58 <div59 style={{ backgroundColor }}60 className={cn(61 'flex h-full w-full items-center justify-center px-3 py-2.5 text-sm tabular-nums',62 isDark ? 'text-white' : 'text-foreground',63 )}64 >65 {formatValue(value)}66 </div>67 )68 },69 }),70 ),71 ],72 [columns, min, max, formatValue],73 )7475 const table = useReactTable({76 data,77 columns: tableColumns,78 getCoreRowModel: getCoreRowModel(),79 })8081 const legendLow = getHeatColor(min, min, max).backgroundColor82 const legendHigh = getHeatColor(max, min, max).backgroundColor8384 return (85 <div>86 <div className="overflow-hidden rounded-md border">87 <Table>88 <TableHeader>89 {table.getHeaderGroups().map((headerGroup) => (90 <TableRow key={headerGroup.id}>91 {headerGroup.headers.map((header) => (92 <TableHead key={header.id} className="text-center">93 {header.isPlaceholder94 ? null95// … truncated
What it pulls in
npm packages
Other registry items
Files it writes
More from shadcn-table-library
All 18 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Animated Icons Tableanimated-icons-table | shadcn-table-library | Blocks | Free | 2 deps · 9 files | |
| Comparison Tablecomparison-table | shadcn-table-library | Blocks | Free | 1 dep · 4 files | |
| Data Tabledata-table | shadcn-table-library | Blocks | Free | 1 dep · 4 files | |
| Density & Exportdensity-export-table | shadcn-table-library | Blocks | Free | 1 dep · 4 files | |
| Editable Tableeditable-table | shadcn-table-library | Blocks | Free | 1 dep · 3 files | |
| Grouped Tablegrouped-table | shadcn-table-library | Blocks | Free | 1 dep · 3 files | |
| Summary / KPI Tablekpi-table | shadcn-table-library | Blocks | Free | 1 dep · 5 files | |
| Live Status Indicatorslive-status-table | shadcn-table-library | Blocks | Free | 1 dep · 4 files |
