pricing-card.tsx185 lines, 5.8 KB
| 1 | "use client"; |
| 2 | |
| 3 | import { Button } from "@/components/ui/button"; |
| 4 | import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; |
| 5 | import { cn } from "@/lib/utils"; |
| 6 | import { BadgeCheck, Check } from "lucide-react"; |
| 7 | import { ComponentProps } from "react"; |
| 8 | |
| 9 | export interface PricingFeature { |
| 10 | text: string; |
| 11 | included: boolean; |
| 12 | highlight?: boolean; |
| 13 | } |
| 14 | |
| 15 | export interface PricingCardProps extends ComponentProps<typeof Card> { |
| 16 | name: string; |
| 17 | price: number | string; |
| 18 | priceUnit?: string; |
| 19 | billingPeriod?: string; |
| 20 | features: PricingFeature[]; |
| 21 | buttonText?: string; |
| 22 | onSubscribe?: () => void; |
| 23 | popular?: boolean; |
| 24 | highlighted?: boolean; |
| 25 | disabled?: boolean; |
| 26 | currentPlan?: boolean; |
| 27 | isLoading?: boolean; |
| 28 | } |
| 29 | |
| 30 | export function PricingCard({ |
| 31 | name, |
| 32 | price, |
| 33 | priceUnit = "$", |
| 34 | billingPeriod = "month", |
| 35 | features, |
| 36 | buttonText = "Subscribe", |
| 37 | onSubscribe, |
| 38 | popular = false, |
| 39 | highlighted = false, |
| 40 | disabled = false, |
| 41 | currentPlan = false, |
| 42 | isLoading = false, |
| 43 | className, |
| 44 | ...props |
| 45 | }: PricingCardProps) { |
| 46 | return ( |
| 47 | <Card |
| 48 | className={cn( |
| 49 | "relative overflow-hidden flex flex-col h-full rounded-2xl border border-muted bg-background", |
| 50 | popular && "border-primary/20", |
| 51 | currentPlan && "border-primary/20", |
| 52 | "transition-all duration-300", |
| 53 | className |
| 54 | )} |
| 55 | {...props} |
| 56 | > |
| 57 | {popular && ( |
| 58 | <div className="absolute top-3 right-3"> |
| 59 | <div className="bg-[#18181b] text-white text-[10px] font-medium py-[2px] px-2 rounded-full uppercase tracking-wide"> |
| 60 | Most popular |
| 61 | </div> |
| 62 | </div> |
| 63 | )} |
| 64 | |
| 65 | <CardHeader className={cn("px-6 pt-5")}> |
| 66 | <div className="mb-2"> |
| 67 | <CardTitle className="text-base font-medium">{name}</CardTitle> |
| 68 | </div> |
| 69 | |
| 70 | <div className="mt-1"> |
| 71 | <div className="flex items-baseline"> |
| 72 | {typeof price === "number" ? ( |
| 73 | <> |
| 74 | <span className="text-lg font-semibold text-foreground"> |
| 75 | {priceUnit} |
| 76 | </span> |
| 77 | <span className="text-4xl font-bold">{price}</span> |
| 78 | </> |
| 79 | ) : ( |
| 80 | <span className="text-4xl font-bold">{price}</span> |
| 81 | )} |
| 82 | {billingPeriod && ( |
| 83 | <span className="ml-1 text-sm text-muted-foreground"> |
| 84 | per {billingPeriod} |
| 85 | </span> |
| 86 | )} |
| 87 | </div> |
| 88 | </div> |
| 89 | |
| 90 | <div className="w-full mt-4"> |
| 91 | <Button |
| 92 | onClick={onSubscribe} |
| 93 | disabled={disabled || isLoading || currentPlan} |
| 94 | className={cn( |
| 95 | "w-full rounded-md font-medium text-sm", |
| 96 | highlighted || popular |
| 97 | ? "bg-black text-white hover:bg-black/90" |
| 98 | : "bg-[#f5f5f4] text-foreground hover:bg-[#e7e5e4]", |
| 99 | currentPlan && "bg-primary/15 text-primary" |
| 100 | )} |
| 101 | variant={highlighted ? "default" : "outline"} |
| 102 | size="default" |
| 103 | > |
| 104 | {isLoading ? ( |
| 105 | <span className="flex items-center"> |
| 106 | <svg |
| 107 | className="animate-spin -ml-1 mr-2 h-4 w-4" |
| 108 | xmlns="http://www.w3.org/2000/svg" |
| 109 | fill="none" |
| 110 | viewBox="0 0 24 24" |
| 111 | > |
| 112 | <circle |
| 113 | className="opacity-25" |
| 114 | cx="12" |
| 115 | cy="12" |
| 116 | r="10" |
| 117 | stroke="currentColor" |
| 118 | strokeWidth="4" |
| 119 | ></circle> |
| 120 | <path |
| 121 | className="opacity-75" |
| 122 | fill="currentColor" |
| 123 | d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" |
| 124 | ></path> |
| 125 | </svg> |
| 126 | Loading... |
| 127 | </span> |
| 128 | ) : currentPlan ? ( |
| 129 | <span className="flex items-center justify-center"> |
| 130 | <BadgeCheck className="mr-2 h-4 w-4" /> |
| 131 | Current Plan |
| 132 | </span> |
| 133 | ) : ( |
| 134 | <span>{buttonText}</span> |
| 135 | )} |
| 136 | </Button> |
| 137 | </div> |
| 138 | </CardHeader> |
| 139 | |
| 140 | <CardContent className="flex-grow px-6 pt-3 pb-3"> |
| 141 | <div className="w-full border-t border-muted my-1"></div> |
| 142 | |
| 143 | <div className="my-3"> |
| 144 | <h4 className="text-[11px] uppercase font-semibold text-foreground tracking-wider"> |
| 145 | FEATURES |
| 146 | </h4> |
| 147 | <p className="text-[11px] text-muted-foreground mt-1 mb-3"> |
| 148 | Everything in{" "} |
| 149 | {name === "Basic plan" |
| 150 | ? "our free plan plus..." |
| 151 | : name === "Business plan" |
| 152 | ? "Basic plus..." |
| 153 | : "Business plus..."} |
| 154 | </p> |
| 155 | |
| 156 | <ul className="space-y-2 text-sm"> |
| 157 | {features.map((feature, i) => ( |
| 158 | <li key={i} className="flex items-center gap-2.5"> |
| 159 | {feature.included ? ( |
| 160 | <div className="flex-shrink-0 h-4 w-4 rounded-full flex items-center justify-center"> |
| 161 | <Check className="h-3.5 w-3.5 text-foreground" /> |
| 162 | </div> |
| 163 | ) : ( |
| 164 | <div className="flex-shrink-0 h-4 w-4 flex items-center justify-center"> |
| 165 | <div className="h-[4px] w-[4px] bg-muted-foreground/50 rounded-full" /> |
| 166 | </div> |
| 167 | )} |
| 168 | <span |
| 169 | className={cn( |
| 170 | "text-[13px]", |
| 171 | feature.included |
| 172 | ? "text-foreground" |
| 173 | : "text-muted-foreground line-through" |
| 174 | )} |
| 175 | > |
| 176 | {feature.text} |
| 177 | </span> |
| 178 | </li> |
| 179 | ))} |
| 180 | </ul> |
| 181 | </div> |
| 182 | </CardContent> |
| 183 | </Card> |
| 184 | ); |
| 185 | } |