|
| 1 | +import {useState} from "preact/hooks"; |
| 2 | +import {Icon} from "../../shared/components/Icon.jsx"; |
| 3 | + |
| 4 | +const CHECK_SVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path fill-rule="evenodd" d="M16.704 4.153a.75.75 0 01.143 1.052l-8 10.5a.75.75 0 01-1.127.075l-4.5-4.5a.75.75 0 011.06-1.06l3.894 3.893 7.48-9.817a.75.75 0 011.05-.143z" clip-rule="evenodd"/></svg>`; |
| 5 | +const X_SVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true"><path d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"/></svg>`; |
| 6 | + |
| 7 | +function renderText(text) { |
| 8 | + if (!text) return ""; |
| 9 | + return String(text) |
| 10 | + .replace(/\*\*(.*?)\*\*/g, "<strong>$1</strong>") |
| 11 | + .replace(/\*(.*?)\*/g, "<em>$1</em>") |
| 12 | + .replace(/`(.*?)`/g, "<code>$1</code>"); |
| 13 | +} |
| 14 | + |
| 15 | +function BillingToggle({billing, setBilling, toggle}) { |
| 16 | + const isYearly = billing === "yearly"; |
| 17 | + return ( |
| 18 | + <div class="flex items-center justify-center gap-4 mb-10"> |
| 19 | + <span class={`text-sm transition-colors ${isYearly ? "font-normal text-gray-400 dark:text-gray-500" : "font-semibold text-gray-900 dark:text-white"}`}> |
| 20 | + {toggle.monthly_label || "Monthly"} |
| 21 | + </span> |
| 22 | + <button |
| 23 | + role="switch" |
| 24 | + aria-checked={isYearly} |
| 25 | + onClick={() => setBilling(isYearly ? "monthly" : "yearly")} |
| 26 | + class={`relative inline-flex h-7 w-12 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 dark:focus:ring-offset-gray-900 ${isYearly ? "bg-primary-600" : "bg-gray-300 dark:bg-gray-600"}`} |
| 27 | + > |
| 28 | + <span class="sr-only">Toggle billing period</span> |
| 29 | + <span class={`pointer-events-none inline-block h-6 w-6 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out ${isYearly ? "translate-x-5" : "translate-x-0"}`} /> |
| 30 | + </button> |
| 31 | + <span class={`flex items-center gap-2 text-sm transition-colors ${isYearly ? "font-semibold text-gray-900 dark:text-white" : "font-normal text-gray-400 dark:text-gray-500"}`}> |
| 32 | + {toggle.yearly_label || "Yearly"} |
| 33 | + {toggle.yearly_discount && ( |
| 34 | + <span class="inline-flex items-center rounded-full bg-green-50 dark:bg-green-900/20 px-2.5 py-0.5 text-xs font-semibold text-green-700 dark:text-green-400"> |
| 35 | + {toggle.yearly_discount} |
| 36 | + </span> |
| 37 | + )} |
| 38 | + </span> |
| 39 | + </div> |
| 40 | + ); |
| 41 | +} |
| 42 | + |
| 43 | +function PriceDisplay({price = {}, price_suffix, price_note, price_note_monthly, price_note_yearly, billing}) { |
| 44 | + const raw = billing === "yearly" && price.yearly != null ? price.yearly : price.monthly; |
| 45 | + const currency = price.currency ?? "$"; |
| 46 | + const isContact = raw === "" || raw == null; |
| 47 | + const isFree = String(raw) === "0"; |
| 48 | + |
| 49 | + // billing-aware note: per-billing field → static fallback |
| 50 | + const note = billing === "yearly" |
| 51 | + ? (price_note_yearly ?? price_note) |
| 52 | + : (price_note_monthly ?? price_note); |
| 53 | + |
| 54 | + if (isContact) { |
| 55 | + return ( |
| 56 | + <div class="mb-6"> |
| 57 | + <p class="text-3xl font-bold text-gray-900 dark:text-white"> |
| 58 | + {price_note || "Contact us"} |
| 59 | + </p> |
| 60 | + </div> |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + if (isFree) { |
| 65 | + return ( |
| 66 | + <div class="mb-6"> |
| 67 | + <p class="text-5xl font-bold tracking-tight text-gray-900 dark:text-white">Free</p> |
| 68 | + {note && <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">{note}</p>} |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return ( |
| 74 | + <div class="mb-6"> |
| 75 | + <div class="flex items-baseline gap-x-1"> |
| 76 | + {currency && <span class="text-2xl font-semibold text-gray-900 dark:text-white">{currency}</span>} |
| 77 | + <span class="text-5xl font-bold tracking-tight text-gray-900 dark:text-white">{raw}</span> |
| 78 | + {price_suffix && <span class="text-base text-gray-500 dark:text-gray-400 ml-1">{price_suffix}</span>} |
| 79 | + </div> |
| 80 | + {note && <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">{note}</p>} |
| 81 | + </div> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | +function FeatureRow({feature}) { |
| 86 | + const text = typeof feature === "string" ? feature : (feature.text ?? ""); |
| 87 | + const included = typeof feature === "string" ? true : (feature.included !== false); |
| 88 | + const note = typeof feature === "object" ? feature.note : null; |
| 89 | + |
| 90 | + return ( |
| 91 | + <li class="flex items-start gap-3"> |
| 92 | + <span |
| 93 | + class={`mt-0.5 h-5 w-5 flex-shrink-0 ${included ? "text-green-500 dark:text-green-400" : "text-gray-300 dark:text-gray-600"}`} |
| 94 | + dangerouslySetInnerHTML={{__html: included ? CHECK_SVG : X_SVG}} |
| 95 | + /> |
| 96 | + <span class={`text-sm leading-6 ${included ? "text-gray-700 dark:text-gray-300" : "text-gray-400 dark:text-gray-500"}`}> |
| 97 | + {text} |
| 98 | + {note && <span class="ml-1 text-xs text-gray-400 dark:text-gray-500">({note})</span>} |
| 99 | + </span> |
| 100 | + </li> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +function PricingTier({tier, billing, icon_svgs}) { |
| 105 | + const { |
| 106 | + name = "", |
| 107 | + badge = "", |
| 108 | + description = "", |
| 109 | + price = {}, |
| 110 | + price_suffix = "", |
| 111 | + price_note = "", |
| 112 | + price_note_monthly, |
| 113 | + price_note_yearly, |
| 114 | + highlight = false, |
| 115 | + cta = {}, |
| 116 | + features = [], |
| 117 | + } = tier; |
| 118 | + |
| 119 | + const ctaStyle = cta.style || (highlight ? "primary" : "outline"); |
| 120 | + const ctaIconSvg = cta.icon ? (icon_svgs?.[cta.icon] ?? null) : null; |
| 121 | + const isExternalCta = cta.url && (cta.url.startsWith("http://") || cta.url.startsWith("https://")); |
| 122 | + |
| 123 | + return ( |
| 124 | + <div |
| 125 | + class={`relative flex flex-col rounded-3xl p-8 transition-shadow duration-200 ${ |
| 126 | + highlight |
| 127 | + ? "ring-2 ring-primary-500 shadow-2xl bg-primary-50 dark:bg-primary-900/10 scale-[1.03]" |
| 128 | + : "ring-1 ring-gray-200 dark:ring-gray-700 bg-white dark:bg-gray-800 hover:shadow-xl" |
| 129 | + }`} |
| 130 | + > |
| 131 | + {badge && ( |
| 132 | + <div class="absolute -top-4 inset-x-0 flex justify-center"> |
| 133 | + <span class="inline-flex items-center rounded-full bg-primary-600 px-4 py-1 text-xs font-semibold text-white shadow-sm"> |
| 134 | + {badge} |
| 135 | + </span> |
| 136 | + </div> |
| 137 | + )} |
| 138 | + |
| 139 | + <div class="mb-5"> |
| 140 | + <h3 class="text-lg font-bold text-gray-900 dark:text-white">{name}</h3> |
| 141 | + {description && ( |
| 142 | + <p class="mt-1.5 text-sm text-gray-500 dark:text-gray-400">{description}</p> |
| 143 | + )} |
| 144 | + </div> |
| 145 | + |
| 146 | + <PriceDisplay |
| 147 | + price={price} |
| 148 | + price_suffix={price_suffix} |
| 149 | + price_note={price_note} |
| 150 | + price_note_monthly={price_note_monthly} |
| 151 | + price_note_yearly={price_note_yearly} |
| 152 | + billing={billing} |
| 153 | + /> |
| 154 | + |
| 155 | + {cta.text && cta.url && ( |
| 156 | + <a |
| 157 | + href={cta.url} |
| 158 | + {...(isExternalCta ? {target: "_blank", rel: "noopener noreferrer"} : {})} |
| 159 | + class={`mb-8 flex w-full items-center justify-center gap-2 rounded-xl px-4 py-3 text-sm font-semibold transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-primary-500 focus:ring-offset-2 dark:focus:ring-offset-gray-900 ${ |
| 160 | + ctaStyle === "primary" |
| 161 | + ? "bg-primary-600 text-white hover:bg-primary-700 shadow-sm hover:shadow-md" |
| 162 | + : "ring-1 ring-inset ring-primary-500 text-primary-600 dark:text-primary-400 hover:bg-primary-50 dark:hover:bg-primary-900/20" |
| 163 | + }`} |
| 164 | + > |
| 165 | + {cta.text} |
| 166 | + {ctaIconSvg && <Icon svg={ctaIconSvg} attributes={{class: "w-4 h-4 flex-shrink-0"}} />} |
| 167 | + </a> |
| 168 | + )} |
| 169 | + |
| 170 | + {features.length > 0 && ( |
| 171 | + <ul class="mt-auto space-y-3 border-t border-gray-100 dark:border-gray-700 pt-6"> |
| 172 | + {features.map((f, i) => <FeatureRow key={i} feature={f} />)} |
| 173 | + </ul> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
| 178 | + |
| 179 | +export const PricingBlock = ({content = {}, design = {}, icon_svgs = {}}) => { |
| 180 | + const {title, subtitle, billing_toggle = {}, tiers = []} = content; |
| 181 | + |
| 182 | + const hasYearlyPrices = tiers.some( |
| 183 | + (t) => t.price?.yearly != null && t.price.yearly !== t.price.monthly |
| 184 | + ); |
| 185 | + const showToggle = billing_toggle.enabled && hasYearlyPrices; |
| 186 | + |
| 187 | + const [billing, setBilling] = useState("monthly"); |
| 188 | + |
| 189 | + const colsClass = |
| 190 | + tiers.length === 1 |
| 191 | + ? "max-w-sm mx-auto" |
| 192 | + : tiers.length === 2 |
| 193 | + ? "max-w-4xl mx-auto grid-cols-1 sm:grid-cols-2" |
| 194 | + : "grid-cols-1 md:grid-cols-3"; |
| 195 | + |
| 196 | + return ( |
| 197 | + <div class="py-16 sm:py-24 px-4 sm:px-6 lg:px-8"> |
| 198 | + <div class="max-w-7xl mx-auto"> |
| 199 | + {(title || subtitle) && ( |
| 200 | + <div class="text-center max-w-3xl mx-auto mb-10 lg:mb-14"> |
| 201 | + {title && ( |
| 202 | + <h2 |
| 203 | + class="text-3xl sm:text-4xl lg:text-5xl font-bold text-gray-900 dark:text-white tracking-tight mb-4" |
| 204 | + dangerouslySetInnerHTML={{__html: renderText(title)}} |
| 205 | + /> |
| 206 | + )} |
| 207 | + {subtitle && ( |
| 208 | + <p |
| 209 | + class="text-lg text-gray-600 dark:text-gray-400" |
| 210 | + dangerouslySetInnerHTML={{__html: renderText(subtitle)}} |
| 211 | + /> |
| 212 | + )} |
| 213 | + </div> |
| 214 | + )} |
| 215 | + |
| 216 | + {showToggle && ( |
| 217 | + <BillingToggle billing={billing} setBilling={setBilling} toggle={billing_toggle} /> |
| 218 | + )} |
| 219 | + |
| 220 | + {/* pt-6 clears space for the absolutely-positioned badge on the highlighted tier */} |
| 221 | + <div class={`grid gap-8 pt-6 ${colsClass}`}> |
| 222 | + {tiers.map((tier, i) => ( |
| 223 | + <PricingTier key={i} tier={tier} billing={billing} icon_svgs={icon_svgs} /> |
| 224 | + ))} |
| 225 | + </div> |
| 226 | + </div> |
| 227 | + </div> |
| 228 | + ); |
| 229 | +}; |
0 commit comments