Comparison Table
Products, laptops, plans or ranks compared feature by feature, in groups, with a "differences only" switch. Checks and dashes carry text for screen readers. Wide, every item is a column, the names stay pinned at the top and the features at the left. Narrow, two pickers choose the items side by side and differences follow the two in view.
import { ComparisonTable } from "@/components/blocks/comparison-table";import { buttonVariants } from "@/components/ui/button-variants";export function Laptops() { return ( <ComparisonTable label="Laptops compared" stickyTop="3.5rem" items={[ { id: "lumen", name: "Lumen 13", details: "$899" }, { id: "harbor", name: "Harbor 15", details: "$1,399", badge: "Popular", featured: true, action: ( <a className={buttonVariants({ size: "sm" })} href="/buy/harbor"> Buy </a> ), }, ]} groups={[ { label: "Display", rows: [ { label: "Size", values: { lumen: "13.3 in", harbor: "15.6 in" } }, { label: "Touch screen", values: { lumen: false, harbor: true } }, ], }, { label: "Ports", rows: [{ label: "USB-C", values: { lumen: 2, harbor: 2 } }], }, ]} /> );}import { ComparisonTable } from "@/components/blocks/comparison-table";import { Badge } from "@/components/ui/badge";// Your own nodes can't be compared, so say whether the row differs.export function Support() { return ( <ComparisonTable items={[ { id: "iron", name: "Iron" }, { id: "gold", name: "Gold" }, ]} groups={[ { label: "Support", rows: [ { label: "Reply within", differs: true, values: { iron: <Badge variant="secondary">48 h</Badge>, gold: <Badge>4 h</Badge>, }, }, ], }, ]} /> );}<ComparisonTable>
A feature-by-feature comparison, grouped, that can show only the differences. Wide, every item is a column under a pinned header and beside a pinned feature column. Narrow, two pickers choose the items side by side, each feature's name above its values, and differences follow the two in view.
groups requiredComparisonGroup[]Grouped rows in display order.items requiredComparisonItem[]Columns in display order.columnLabelDefault"Column"stringNames the column pickers on a narrow screen, before the column's number.countLabelDefault(shown, total) => `${shown} of ${total} rows`(shown: number, total: number) => stringText beside the switch while it is on. Call it from a client parent to translate.defaultDifferencesOnlyDefaultfalseboolean"Differences only" at first render (uncontrolled).differencesLabelDefault"Differences only"stringText on the switch.differencesOnlybooleanControlled "differences only"; pair withonDifferencesOnlyChange.differencesToggleDefaulttruebooleanShows the switch. Turn it off for a short table, or when your own control setsdifferencesOnly.emptyLabelDefault"Every row is the same."ReactNodeShown when every row is the same.featureLabelDefault"Feature"stringRead by screen readers as the heading of the feature column.labelDefault"Comparison"stringNames the table for screen readers.noLabelDefault"No"stringRead by screen readers on a dash.onDifferencesOnlyChange(differencesOnly: boolean) => voidCalled with the new "differences only" state.stickyTopDefault"0px"stringHow far below the top of the page the header pins, as a CSS length. Pass your sticky site header's height.yesLabelDefault"Yes"stringRead by screen readers on a check.
| Prop | Type | Default |
|---|---|---|
groups requiredGrouped rows in display order. | ComparisonGroup[] | — |
items requiredColumns in display order. | ComparisonItem[] | — |
columnLabelNames the column pickers on a narrow screen, before the column's number. | string | "Column" |
countLabelText beside the switch while it is on. Call it from a client parent to translate. | (shown: number, total: number) => string | (shown, total) => `${shown} of ${total} rows` |
defaultDifferencesOnly"Differences only" at first render (uncontrolled). | boolean | false |
differencesLabelText on the switch. | string | "Differences only" |
differencesOnlyControlled "differences only"; pair with onDifferencesOnlyChange. | boolean | — |
differencesToggleShows the switch. Turn it off for a short table, or when your own control sets differencesOnly. | boolean | true |
emptyLabelShown when every row is the same. | ReactNode | "Every row is the same." |
featureLabelRead by screen readers as the heading of the feature column. | string | "Feature" |
labelNames the table for screen readers. | string | "Comparison" |
noLabelRead by screen readers on a dash. | string | "No" |
onDifferencesOnlyChangeCalled with the new "differences only" state. | (differencesOnly: boolean) => void | — |
stickyTopHow far below the top of the page the header pins, as a CSS length. Pass your sticky site header's height. | string | "0px" |
yesLabelRead by screen readers on a check. | string | "Yes" |
required
Also accepts every <div> prop.
- type ComparisonItem
- { id: string; name: string; href?: string; media?: ReactNode; details?: ReactNode; action?: ReactNode; badge?: string; featured?: boolean; }
- type ComparisonGroup
- { label: string; rows: ComparisonRow[]; }
Every component also takes className.