About
Options side by side that stream in cell by cell, with the best value in each row marked and a recommended column.
Built with React and Tailwind. Copy the prompt and paste it into Claude Code, Cursor or Lovable: your agent adds Comparison Table to your project and fits it to your theme.
Code
How Comparison Table is used: the demo's code. The component's own source comes with a free account.
Usage.tsx
"use client"
import * as React from "react"
import { ComparisonAttribute, ComparisonOption, ComparisonTable, ComparisonValue } from "@/components/ui/comparison-table"
/** Ticks: a beat of skeleton, the columns arriving, every cell, then the verdict and the pick. */
const HEAD = 3
/** Three made up Postgres hosts, so no real vendor or price is quoted. */
const OPTIONS: ComparisonOption[] = [
{
id: "tern",
name: "Tern",
description: "Launch plan",
values: { price: 19, storage: 10, coldStart: 500, regions: 11, branching: true, pitr: "7 days" },
},
{
id: "basalt",
name: "Basalt",
description: "Pro plan",
values: { price: 25, storage: 8, coldStart: 0, regions: 17, branching: true, pitr: "7 days" },
},
{
id: "quarry",
name: "Quarry Cloud",
description: "Small instance",
values: { price: 31, storage: 20, coldStart: 0, regions: 24, branching: false, pitr: "35 days" },
},
]
const ATTRIBUTES: ComparisonAttribute[] = [
{ key: "price", label: "Monthly price", type: "currency", better: "lower" },
{ key: "storage", label: "Included storage", unit: "GB", better: "higher" },
{ key: "coldStart", label: "Cold start", unit: "ms", better: "lower", hint: "From idle" },
{ key: "regions", label: "Regions", better: "higher" },
{ key: "branching", label: "Database branching" },
{ key: "pitr", label: "Point in time restore" },
]
const CELLS = ATTRIBUTES.length * OPTIONS.length
const VERDICT = HEAD + OPTIONS.length + CELLS
const PICK = VERDICT + 10
const END = PICK + 14
const reducedMotion = () => window.matchMedia("(prefers-reduced-motion: reduce)").matches
const CELL_MS = 170
const FIT_CSS = "html,body{overflow:hidden}[data-fit-small] [data-fit-hide]{display:none!important}"
/**
* 21st shows a demo in a small frame: about 674 by 421 on a component page, 358 by 224 on a
* phone and 245 by 139 on a profile card. A demo taller than its frame gets scrollbars, which
* render white on dark pages. FitFrame fills the frame exactly instead.
*
* - "scale": the content shrinks until it fits, centered. For visuals such as orbs.
* - "crop": the content stays at full size from the top and fades out at the bottom. For text
* that streams in, where a changing scale would make the page breathe.
*
* `minWidth` lays out a wide demo (a table, a two column panel) at that width and scales it down
* to the frame, instead of letting it collapse to a narrow layout. The frame centers it, so it
* shrinks evenly on both sides.
*
* Anything marked `data-fit-hide` (state buttons, the chatcn.dev line) drops out in frames
* shorter than `hideBelow`, so a profile card shows the visual alone.
*/
function FitFrame({
children,
mode = "scale",
hideBelow = 360,
minWidth,
}: {
children: React.ReactNode
mode?: "scale" | "crop"
hideBelow?: number
minWidth?: string
}) {
const content = React.useRef<HTMLDivElement>(null)
const [cropped, setCropped] = React.useState(false)
React.useLayoutEffect(() => {
const el = content.current
if (!el) return
const measure = () => {
const vw = window.innerWidth
const vh = window.innerHeight
el.toggleAttribute("data-fit-small", vh < hideBelow)
// Layout sizes ignore the transform, so measuring never feeds back.
const w = Math.max(el.offsetWidth, el.scrollWidth)
const h = Math.max(el.offsetHeight, el.scrollHeight)
const scale = mode === "scale" ? Math.min(1, vw / w, vh / h) : 1
el.style.transform = scale < 1 ? `scale(${scale})` : ""
setCropped(mode === "crop" && h > vh + 1)
}
measure()
const observer = new ResizeObserver(measure)
observer.observe(el)
// Content that changes inside a fixed width box (a headline that cycles words) never resizes
// the box itself, so changes below it measure again on the next frame.
let frame = 0
const mutations = new MutationObserver(() => {
cancelAnimationFrame(frame)
frame = requestAnimationFrame(measure)
})
mutations.observe(el, { subtree: true, childList: true, characterData: true })
window.addEventListener("resize", measure)
return () => {
observer.disconnect()
mutations.disconnect()
cancelAnimationFrame(frame)
window.removeEventListener("resize", measure)
}
}, [mode, hideBelow])
const fade = "linear-gradient(to bottom, #000 calc(100% - 40px), transparent)"
return (
<div
className="flex w-full justify-center overflow-hidden"
style={{
height: "100dvh",
alignItems: mode === "crop" ? "flex-start" : "center",
maskImage: cropped ? fade : undefined,
WebkitMaskImage: cropped ? fade : undefined,
}}
>
<style>{FIT_CSS}</style>
<div
ref={content}
className="w-full shrink-0"
style={{ transformOrigin: mode === "crop" ? "top center" : "center", minWidth }}
>
{children}
</div>
</div>
)
}
/** The line under each 21st demo that points back to the kit. */
function MoreOn({ slug, children }: { slug: string; children: React.ReactNode }) {
return (
<p data-fit-hide className="text-[0.75rem]/[1rem] text-(color:--chatcn-fg-faint) text-center text-balance">
{children}{" "}
<a
href={`https://chatcn.dev/docs/components/${slug}`}
target="_blank"
rel="noopener"
className="text-(color:--chatcn-fg-subtle) hover:text-(color:--chatcn-fg) underline underline-offset-2"
>
chatcn.dev
</a>
</p>
)
}
/**
* An assistant compares Postgres hosts for a staging database: the columns arrive, the values
* stream in row by row, the best value in each row gets its check, then the recommendation lands
* and the user picks it. Loops. Choose a column yourself and the tour stops there.
*/
export default function ComparisonTablePreview() {
const [tick, setTick] = React.useState(END - 1)
const [held, setHeld] = React.useState<string>()
React.useEffect(() => {
if (reducedMotion() || held) return
const id = window.setInterval(() => setTick((t) => (t + 1) % END), CELL_MS)
return () => window.clearInterval(id)
}, [held])
const t = held ? END - 1 : tick
const columns = Math.min(OPTIONS.length, Math.max(0, t - HEAD + 1))
const filled = Math.max(0, t - HEAD - OPTIONS.length + 1)
const streaming = t < VERDICT
const options = OPTIONS.slice(0, columns).map((o, col) => {
const values: Record<string, ComparisonValue> = {}
ATTRIBUTES.forEach((a, row) => {
if (row * OPTIONS.length + col < filled) values[a.key] = o.values?.[a.key]
})
return { ...o, values, recommended: !streaming && o.id === "basalt" }
})
return (
<FitFrame minWidth="42rem">
<div className="flex w-full flex-col items-center justify-center gap-4 p-6">
{/* The finished table sits invisible in the same cell, so the frame keeps its final size and
never rescales while the columns, the recommendation and the footer arrive. */}
<div className="grid w-full max-w-2xl">
<div aria-hidden inert className="invisible col-start-1 row-start-1">
<ComparisonTable
title="Postgres for the staging environment"
description="One small instance each, billed monthly."
options={OPTIONS.map((o) => ({ ...o, recommended: o.id === "basalt" }))}
attributes={ATTRIBUTES}
onSelect={() => {}}
footer="Recommended: Basalt. No cold starts, and branching covers preview deployments."
/>
</div>
<div className="col-start-1 row-start-1 self-start">
<ComparisonTable
title="Postgres for the staging environment"
description="One small instance each, billed monthly."
options={options}
attributes={ATTRIBUTES}
expectedOptions={OPTIONS.length}
streaming={streaming}
onSelect={(o) => setHeld(o.id)}
selectedId={held ?? (t >= PICK ? "basalt" : undefined)}
footer={
streaming
? undefined
: "Recommended: Basalt. No cold starts, and branching covers preview deployments."
}
/>
</div>
</div>
<MoreOn slug="comparison-table">Side by side comparisons with the best value marked per row. On</MoreOn>
</div>
</FitFrame>
)
}
















