|
| 1 | +import { Check, Minus } from 'lucide-react' |
| 2 | +import React, { type FC, type ReactNode } from 'react' |
| 3 | +import { cn } from 'ui' |
| 4 | +import { TextLink } from 'ui-patterns/TextLink' |
| 5 | + |
| 6 | +import SectionContainer from '@/components/Layouts/SectionContainer' |
| 7 | + |
| 8 | +export interface PricingComparisonPlan { |
| 9 | + name: string |
| 10 | + /** Highlights the column visually (eg. the recommended plan). */ |
| 11 | + highlight?: boolean |
| 12 | +} |
| 13 | + |
| 14 | +export interface PricingComparisonRow { |
| 15 | + feature: ReactNode |
| 16 | + /** One value per plan, in the same order as `plans`. Booleans render as a check or dash. */ |
| 17 | + values: (ReactNode | boolean)[] |
| 18 | +} |
| 19 | + |
| 20 | +export interface PricingComparisonSectionProps { |
| 21 | + id?: string |
| 22 | + heading: ReactNode |
| 23 | + subheading?: ReactNode |
| 24 | + plans: PricingComparisonPlan[] |
| 25 | + rows: PricingComparisonRow[] |
| 26 | + cta?: { |
| 27 | + label: string |
| 28 | + url: string |
| 29 | + } |
| 30 | + className?: string |
| 31 | +} |
| 32 | + |
| 33 | +const PlanValue = ({ value }: { value: ReactNode | boolean }) => { |
| 34 | + if (value === true) { |
| 35 | + return <Check className="w-4 h-4 text-brand" strokeWidth={2} aria-label="Included" /> |
| 36 | + } |
| 37 | + if (value === false) { |
| 38 | + return ( |
| 39 | + <Minus className="w-4 h-4 text-foreground-muted" strokeWidth={2} aria-label="Not included" /> |
| 40 | + ) |
| 41 | + } |
| 42 | + return <span className="text-sm text-foreground">{value}</span> |
| 43 | +} |
| 44 | + |
| 45 | +const PricingComparisonSection: FC<PricingComparisonSectionProps> = ({ |
| 46 | + id, |
| 47 | + heading, |
| 48 | + subheading, |
| 49 | + plans, |
| 50 | + rows, |
| 51 | + cta, |
| 52 | + className, |
| 53 | +}) => { |
| 54 | + return ( |
| 55 | + <SectionContainer id={id} className={cn('flex flex-col gap-8 py-16 md:py-24', className)}> |
| 56 | + <div className="flex flex-col gap-2 max-w-xl"> |
| 57 | + <h2 className="h2 text-foreground-lighter m-0!">{heading}</h2> |
| 58 | + {subheading && <p className="text-foreground-lighter">{subheading}</p>} |
| 59 | + </div> |
| 60 | + |
| 61 | + <div className="w-full overflow-x-auto border border-default rounded-lg bg-surface-75"> |
| 62 | + <table className="w-full min-w-[480px] border-collapse text-left"> |
| 63 | + <thead> |
| 64 | + <tr className="border-b border-default"> |
| 65 | + <th |
| 66 | + scope="col" |
| 67 | + className="py-4 px-4 md:px-6 text-sm font-normal text-foreground-light" |
| 68 | + > |
| 69 | + What you get |
| 70 | + </th> |
| 71 | + {plans.map((plan) => ( |
| 72 | + <th |
| 73 | + key={plan.name} |
| 74 | + scope="col" |
| 75 | + className={cn( |
| 76 | + 'py-4 px-4 md:px-6 text-sm font-medium text-center', |
| 77 | + plan.highlight ? 'text-foreground bg-surface-100' : 'text-foreground' |
| 78 | + )} |
| 79 | + > |
| 80 | + {plan.name} |
| 81 | + </th> |
| 82 | + ))} |
| 83 | + </tr> |
| 84 | + </thead> |
| 85 | + <tbody> |
| 86 | + {rows.map((row, rowIndex) => ( |
| 87 | + <tr |
| 88 | + key={rowIndex} |
| 89 | + className="border-b border-default last:border-b-0 hover:bg-surface-100/50 transition-colors" |
| 90 | + > |
| 91 | + <th |
| 92 | + scope="row" |
| 93 | + className="py-3 px-4 md:px-6 text-sm font-normal text-foreground-light" |
| 94 | + > |
| 95 | + {row.feature} |
| 96 | + </th> |
| 97 | + {row.values.map((value, valueIndex) => ( |
| 98 | + <td |
| 99 | + key={valueIndex} |
| 100 | + className={cn( |
| 101 | + 'py-3 px-4 md:px-6 text-center', |
| 102 | + plans[valueIndex]?.highlight && 'bg-surface-100' |
| 103 | + )} |
| 104 | + > |
| 105 | + <span className="flex items-center justify-center"> |
| 106 | + <PlanValue value={value} /> |
| 107 | + </span> |
| 108 | + </td> |
| 109 | + ))} |
| 110 | + </tr> |
| 111 | + ))} |
| 112 | + </tbody> |
| 113 | + </table> |
| 114 | + </div> |
| 115 | + |
| 116 | + {cta && <TextLink hasChevron label={cta.label} url={cta.url} className="mt-2" />} |
| 117 | + </SectionContainer> |
| 118 | + ) |
| 119 | +} |
| 120 | + |
| 121 | +export default PricingComparisonSection |
0 commit comments