Plan upsell
A locked page and a plan picker for a feature that needs a paid plan.
284 lines · React · Tailwind 4
Install
$ bunx shadcn@latest add @blocco/plan-upsellCode
import { useRef, useState, type ReactNode } from "react";
import { Button } from "@/registry/blocco/ui/button";
import { buttonClass } from "@/registry/blocco/ui/button-class";
import { Dialog } from "@/registry/blocco/ui/dialog";
import { Input } from "@/registry/blocco/ui/field";
import { Check, Lock } from "@/registry/blocco/ui/icons";
import { Segmented } from "@/registry/blocco/ui/segmented";
import { BusyContent } from "@/registry/blocco/ui/spinner";
import { useToast } from "@/registry/blocco/ui/toast";
import { cn } from "@/registry/blocco/ui/cn";
type PaidPlan = "hobby" | "pro";
type Reason = "styles" | "domains";
const PLANS = {
hobby: { name: "Hobby", price: "$5", domains: 1, links: 1000, members: 3 },
pro: { name: "Pro", price: "$15", domains: 5, links: 10000, members: 10 },
} as const;
/** Four facts per plan, the one that brought them here first. Each fits one line of a card. */
function planFacts(plan: PaidPlan, reason: Reason): string[] {
const p = PLANS[plan];
const domains = `${p.domains} custom domain${p.domains === 1 ? "" : "s"}`;
const styles = "Your colors on QR codes";
const [lead, next] = reason === "styles" ? [styles, domains] : [domains, styles];
return [lead, next, `${p.links.toLocaleString("en-US")} links`, `${p.members} members`];
}
/**
* A page the plan does not include: a lock, what the feature is on, one line on what it does,
* and "See plans" for the owner (who pays), or who can upgrade for anyone else.
*/
function FeatureLocked({
title,
line,
isOwner,
ownerNote,
onSeePlans,
}: {
title: string;
line: ReactNode;
isOwner: boolean;
/** What anyone but the owner reads instead of the button. */
ownerNote: string;
onSeePlans: () => void;
}) {
return (
<div className="flex flex-col items-start gap-4 rounded-xl bg-surface px-5 py-6 smooth-shadow-ring-xs sm:flex-row sm:items-center">
<span
aria-hidden
className="grid size-9 shrink-0 place-items-center rounded-lg bg-surface-2 text-muted"
>
<Lock size={17} />
</span>
<div className="min-w-0 flex-1">
<p className="text-sm font-medium text-balance">{title}</p>
<p className="mt-1 max-w-[60ch] text-sm text-pretty text-muted">{line}</p>
{!isOwner && <p className="mt-2 text-sm text-muted">{ownerNote}</p>}
</div>
{isOwner && (
<button
type="button"
onClick={onSeePlans}
className={buttonClass({ variant: "primary", className: "shrink-0 max-sm:w-full" })}
>
See plans
</button>
)}
</div>
);
}
/** Enterprise is handled by hand: no checkout, so the only call to action is an email. */
function EnterpriseNote({ className }: { className?: string }) {
return (
<p className={cn("text-sm text-muted", className)}>
Need several organizations or higher limits?{" "}
<a href="mailto:sales@acme.test?subject=Enterprise" className="text-accent hover:underline">
Email us about Enterprise
</a>
.
</p>
);
}
/**
* Hobby and Pro as two cards to pick from, each with its price and four facts, Hobby picked;
* the button names the plan and goes to its checkout.
*/
function PlanPickerDialog({
open,
onClose,
title,
why,
reason,
}: {
open: boolean;
onClose: () => void;
title: string;
why: string;
reason: Reason;
}) {
const [plan, setPlan] = useState<PaidPlan>("hobby");
const [leaving, setLeaving] = useState(false);
const toast = useToast();
const cta = useRef<HTMLButtonElement>(null);
const go = async () => {
setLeaving(true);
await new Promise((r) => setTimeout(r, 900));
setLeaving(false);
onClose();
toast(`Checkout for ${PLANS[plan].name} opens here`);
};
return (
<Dialog
open={open}
onOpenChange={(next) => !next && onClose()}
title={title}
size="lg"
initialFocus={cta}
>
<div className="flex flex-col gap-5">
<p className="-mt-2 text-sm text-pretty text-muted">{why}</p>
<fieldset className="grid gap-3 sm:grid-cols-2">
<legend className="sr-only">Plan</legend>
{(["hobby", "pro"] as const).map((p) => {
const on = plan === p;
return (
// A native radio in each card: one Tab stop, arrows move between them.
<label
key={p}
className={cn(
"relative flex cursor-pointer flex-col gap-3 rounded-xl p-4 text-start transition-[box-shadow,background-color] duration-150 ease-out has-[:focus-visible]:outline-2 has-[:focus-visible]:outline-offset-2 has-[:focus-visible]:outline-accent",
on
? "bg-accent/6 shadow-[0_0_0_1.5px_var(--accent)]"
: "smooth-shadow-ring-xs [@media(hover:hover)]:hover:bg-surface-2/60",
)}
>
<input
type="radio"
name="plan"
value={p}
checked={on}
onChange={() => setPlan(p)}
aria-label={`${PLANS[p].name}, ${PLANS[p].price} a month`}
// Invisible, over the whole card: the card is what you press.
className="absolute inset-0 cursor-pointer appearance-none rounded-xl opacity-0"
/>
<span
aria-hidden
className={cn(
"absolute top-3.5 right-3.5 grid size-5 place-items-center rounded-full transition-[opacity,scale] duration-150 ease-out",
on ? "scale-100 bg-accent text-bg opacity-100" : "scale-75 opacity-0",
)}
>
<Check size={13} stroke={2.5} />
</span>
<span className="text-sm font-medium text-muted">{PLANS[p].name}</span>
<span className="tnum -mt-1.5 flex items-baseline gap-1">
<span className="text-2xl font-semibold tracking-tight">{PLANS[p].price}</span>
<span className="text-xs text-muted">a month</span>
</span>
<ul className="flex flex-col gap-1.5 text-[13px]">
{planFacts(p, reason).map((fact, i) => (
<li key={fact} className="flex gap-2">
<Check
aria-hidden
size={14}
stroke={2.25}
className={cn("mt-0.5 shrink-0", i === 0 ? "text-accent" : "text-muted")}
/>
<span className={i === 0 ? "text-text" : "text-muted"}>{fact}</span>
</li>
))}
</ul>
</label>
);
})}
</fieldset>
<div className="flex items-center gap-3 max-sm:flex-col-reverse max-sm:items-stretch">
<EnterpriseNote className="mr-auto text-xs max-sm:mx-auto max-sm:text-center" />
<Button
ref={cta}
variant="primary"
onClick={() => void go()}
disabled={leaving}
className="shrink-0"
>
<BusyContent busy={leaving}>Continue with {PLANS[plan].name}</BusyContent>
</Button>
</div>
</div>
</Dialog>
);
}
/**
* How a feature the plan lacks offers itself. A whole page the plan lacks says so once, in a
* locked state with one way to the plans. A setting the plan cannot save stays usable to try,
* and saving it opens the plan picker.
*/
export default function PlanUpsell() {
const [asOwner, setAsOwner] = useState<"owner" | "member">("owner");
const [picker, setPicker] = useState<Reason | null>(null);
// Kept so the dialog's text does not change while it closes.
const [last, setLast] = useState<Reason>("domains");
const [accent, setAccent] = useState("#6d5efc");
const open = (reason: Reason) => {
setLast(reason);
setPicker(reason);
};
return (
<section className="mx-auto flex max-w-3xl flex-col gap-8 px-4 py-10 sm:px-6 sm:py-12">
<div className="flex flex-wrap items-end justify-between gap-3">
<div>
<h1 className="text-xl font-semibold tracking-tight text-balance">Organization</h1>
<p className="mt-1 text-sm text-muted">You are on the Free plan.</p>
</div>
<Segmented
label="Viewing as"
items={[
{ id: "owner", label: "Owner" },
{ id: "member", label: "Member" },
]}
value={asOwner}
onChange={setAsOwner}
/>
</div>
<FeatureLocked
title="Custom domains come with a paid plan"
line="Put links on your own domain, like go.acme.test, and choose the slug yourself."
isOwner={asOwner === "owner"}
ownerNote="Ask Ada Lovelace, the owner, to upgrade."
onSeePlans={() => open("domains")}
/>
<div className="flex flex-col gap-3">
<h2 className="text-sm font-semibold">QR colors</h2>
<div className="flex flex-wrap items-end gap-3 rounded-xl bg-surface p-4 smooth-shadow-ring-xs">
<label className="min-w-0 flex-1 basis-48">
<span className="mb-1.5 block text-xs text-muted">Color</span>
<Input
value={accent}
onChange={(e) => setAccent(e.target.value)}
spellCheck={false}
autoComplete="off"
className="font-mono"
/>
</label>
<span
aria-hidden
className="size-9 shrink-0 rounded-lg smooth-shadow-ring-xs"
style={{ background: /^#[0-9a-f]{3,8}$/i.test(accent) ? accent : "transparent" }}
/>
<Button variant="primary" onClick={() => open("styles")} className="max-sm:w-full">
Save colors
</Button>
</div>
<p className="text-xs text-muted">
Free plans can try any color. Saving one needs a paid plan.
</p>
</div>
<EnterpriseNote />
<PlanPickerDialog
open={picker !== null}
onClose={() => setPicker(null)}
reason={last}
title={last === "domains" ? "Use your own domain" : "Save your QR colors"}
why={
last === "domains"
? "Custom domains come with Hobby and Pro."
: "Colors on QR codes come with Hobby and Pro."
}
/>
</section>
);
}