Pricing tiers with billing toggle

Three plans side by side with a monthly or yearly switch built from native radio buttons. The yearly option shows the discount it works out from your prices, each card shows the yearly total, and the highlighted plan is set in your primary colour with its own badge. Feature lists with check icons, an optional intro line, Intl currency formatting, and a period change callback.

PricingBlockTailwind v4

Source

"use client";

import { useId, useMemo, useState } from "react";
import { Check } from "lucide-react";
import { cn } from "@/lib/utils";

export type BillingPeriod = "monthly" | "yearly";

export type PricingTier = {
  id: string;
  name: string;
  description: string;
  /** Price per seat per month on each period. Use 0 for a free plan. */
  price: { monthly: number; yearly: number };
  cta: { label: string; href: string };
  /** Line above the feature list, such as "Everything in Team, plus". */
  featuresIntro?: string;
  features: string[];
  highlighted?: boolean;
  /** Short label on the highlighted plan. */
  badge?: string;
};

export type PricingTiersProps = {
  title?: string;
  description?: string;
  tiers?: PricingTier[];
  currency?: string;
  locale?: string;
  defaultPeriod?: BillingPeriod;
  /** Unit after the price. */
  unit?: string;
  footnote?: string;
  onPeriodChange?: (period: BillingPeriod) => void;
  className?: string;
};

const DEFAULT_TIERS: PricingTier[] = [
  {
    id: "starter",
    name: "Starter",
    description: "For small teams writing their first shared docs.",
    price: { monthly: 0, yearly: 0 },
    cta: { label: "Start for free", href: "#starter" },
    features: ["Up to 5 members", "Unlimited pages", "7 days of page history", "Import from Markdown and HTML"],
  },
  {
    id: "team",
    name: "Team",
    description: "For teams that run their week on shared pages.",
    price: { monthly: 15, yearly: 12 },
    cta: { label: "Start 14-day trial", href: "#team" },
    featuresIntro: "Everything in Starter, plus",
    features: ["Unlimited members and guests", "90 days of page history", "Page-level permissions", "Offline desktop and mobile apps", "Email support within a day"],
    highlighted: true,
    badge: "Most teams pick this",
  },
  {
    id: "business",
    name: "Business",
    description: "For companies that pass security reviews.",
    price: { monthly: 25, yearly: 20 },
    cta: { label: "Talk to sales", href: "#sales" },
    featuresIntro: "Everything in Team, plus",
    features: ["SAML single sign-on and SCIM", "Unlimited page history", "Audit log export", "Data kept in the EU or US", "99.9% uptime SLA"],
  },
];

const KEYFRAMES = "@keyframes chm-price-in{from{opacity:0;transform:translateY(6px)}to{opacity:1;transform:none}}";
const focusRing = "outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background";

export function PricingTiers({
  title = "Pay for the people who write",
  description = "Viewers and guests are always free. Change plans or cancel from settings, any month.",
  tiers = DEFAULT_TIERS,
  currency = "USD",
  locale = "en-US",
  defaultPeriod = "yearly",
  unit = "per seat / month",
  footnote = "Prices in US dollars, before tax. Non-profits and schools get 50% off.",
  onPeriodChange,
  className,
}: PricingTiersProps) {
  const groupName = useId();
  const [period, setPeriod] = useState<BillingPeriod>(defaultPeriod);
  const money = useMemo(() => new Intl.NumberFormat(locale, { style: "currency", currency, maximumFractionDigits: 0 }), [locale, currency]);

  // The biggest yearly discount across paid plans, shown on the toggle.
  const savings = useMemo(() => {
    const rates = tiers.filter((tier) => tier.price.monthly > 0).map((tier) => Math.round((1 - tier.price.yearly / tier.price.monthly) * 100));
    const best = Math.max(0, ...rates);
    return { best, uniform: rates.every((rate) => rate === best) };
  }, [tiers]);

  const choose = (next: BillingPeriod) => {
    setPeriod(next);
    onPeriodChange?.(next);
  };

  return (
    <section aria-labelledby="pricing-tiers-title" className={cn("bg-background px-4 py-20 sm:px-6 lg:px-8 lg:py-28", className)}>
      <style href="chm-pricing-tiers" precedence="default">{KEYFRAMES}</style>
      <div className="mx-auto max-w-6xl">
        <div className="flex flex-col gap-8 lg:flex-row lg:items-end lg:justify-between">
          <div className="max-w-xl">
            <h2 id="pricing-tiers-title" className="text-balance text-4xl font-semibold tracking-[-0.035em] text-foreground sm:text-5xl">
              {title}
            </h2>
            <p className="mt-4 text-lg leading-relaxed text-muted-foreground">{description}</p>
          </div>

          <fieldset className="inline-flex shrink-0 self-start rounded-lg bg-muted p-1 lg:self-auto">
            <legend className="sr-only">Billing period</legend>
            {(["monthly", "yearly"] as const).map((option) => (
              <label
                key={option}
                className={cn(
                  "flex h-9 cursor-pointer items-center gap-2 rounded-md px-4 text-sm font-medium transition-colors",
                  "has-[:focus-visible]:ring-2 has-[:focus-visible]:ring-ring",
                  period === option ? "bg-background text-foreground shadow-sm" : "text-muted-foreground hover:text-foreground",
                )}
              >
                <input
                  type="radio"
                  name={groupName}
                  value={option}
                  checked={period === option}
                  onChange={() => choose(option)}
                  className="sr-only"
                />
                {option === "monthly" ? "Monthly" : "Yearly"}
                {option === "yearly" && savings.best > 0 && (
                  <span className="rounded-full bg-emerald-500/15 px-2 py-0.5 text-xs font-medium text-emerald-700 dark:text-emerald-300">
                    {savings.uniform ? "Save" : "Save up to"} {savings.best}%
                  </span>
                )}
              </label>
            ))}
          </fieldset>
        </div>

        <div className="mt-12 grid gap-4 lg:mt-16 lg:grid-cols-3">
          {tiers.map((tier) => {
            const amount = tier.price[period];
            const free = tier.price.monthly === 0;
            const billing = free
              ? "Free for good, no card needed"
              : period === "yearly"
                ? `${money.format(amount * 12)} per seat, billed yearly`
                : "Billed monthly, cancel any time";
            const muted = tier.highlighted ? "text-primary-foreground/70" : "text-muted-foreground";
            return (
              <article
                key={tier.id}
                aria-labelledby={`${groupName}-${tier.id}`}
                className={cn(
                  "flex flex-col rounded-xl p-6 sm:p-8",
                  tier.highlighted ? "bg-primary text-primary-foreground" : "bg-muted/60 text-foreground dark:bg-muted/30",
                )}
              >
                <div className="flex flex-wrap items-center justify-between gap-3">
                  <h3 id={`${groupName}-${tier.id}`} className="text-lg font-semibold">{tier.name}</h3>
                  {tier.highlighted && tier.badge && (
                    <span className="rounded-full bg-primary-foreground/15 px-2.5 py-1 text-xs font-medium">{tier.badge}</span>
                  )}
                </div>
                <p className={cn("mt-2 text-sm leading-relaxed", muted)}>{tier.description}</p>

                <p className="mt-8 flex items-baseline gap-2">
                  <span key={`${tier.id}-${period}`} className="text-5xl font-semibold tracking-tight tabular-nums motion-safe:animate-[chm-price-in_.35s_ease-out]">
                    {money.format(amount)}
                  </span>
                  <span className={cn("text-sm", muted)}>{free ? "for 5 members" : unit}</span>
                </p>
                <p className={cn("mt-2 min-h-5 text-sm", muted)}>{billing}</p>

                <a
                  href={tier.cta.href}
                  className={cn(
                    "mt-8 inline-flex h-11 items-center justify-center rounded-md px-5 text-sm font-medium transition-colors",
                    tier.highlighted
                      ? "bg-primary-foreground text-primary outline-none hover:bg-primary-foreground/90 focus-visible:ring-2 focus-visible:ring-primary-foreground focus-visible:ring-offset-2 focus-visible:ring-offset-primary"
                      : cn("bg-background text-foreground ring-1 ring-border hover:bg-accent", focusRing),
                  )}
                >
                  {tier.cta.label}
                </a>

                <div className="mt-8">
                  {tier.featuresIntro && <p className="text-sm font-medium">{tier.featuresIntro}</p>}
                  <ul className={cn("space-y-3 text-sm", tier.featuresIntro && "mt-4")}>
                    {tier.features.map((feature) => (
                      <li key={feature} className="flex gap-3">
                        <Check className={cn("mt-0.5 size-4 shrink-0", tier.highlighted ? "text-primary-foreground" : "text-foreground")} aria-hidden="true" />
                        <span className={tier.highlighted ? "text-primary-foreground/90" : "text-foreground/85"}>{feature}</span>
                      </li>
                    ))}
                  </ul>
                </div>
              </article>
            );
          })}
        </div>

        {footnote && <p className="mt-8 text-sm text-muted-foreground">{footnote}</p>}
      </div>
    </section>
  );
}