{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"pricing-toggle","type":"registry:block","title":"Pricing toggle","description":"Three pricing plans with a monthly/yearly switch: prices morph digit by digit, the yearly saving badge slides in, and the recommended plan carries a slow border light.","dependencies":["motion","lucide-react"],"registryDependencies":["utils"],"files":[{"path":"registry/prequel/pricing-toggle/pricing-toggle.tsx","type":"registry:component","content":"\"use client\";\n\nimport { useRef, useState } from \"react\";\nimport {\n  AnimatePresence,\n  MotionConfig,\n  motion,\n  useInView,\n  useReducedMotion,\n} from \"motion/react\";\nimport { Check } from \"lucide-react\";\nimport { cn } from \"@/lib/utils\";\n\nconst EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\n\n/* ------------------------------------------------------------------ */\n/* types                                                               */\n/* ------------------------------------------------------------------ */\n\nexport type BillingPeriod = \"monthly\" | \"yearly\";\n\nexport type PricingPlan = {\n  id: string;\n  name: string;\n  description: string;\n  /** Price per month when billed monthly. */\n  monthlyPrice: number;\n  /** Price per month when billed yearly. */\n  yearlyPrice: number;\n  features: string[];\n  cta?: string;\n  /** Where the CTA leads when no `onSelectPlan` handler is provided. */\n  href?: string;\n  recommended?: boolean;\n};\n\nexport type PricingToggleProps = {\n  className?: string;\n  title?: string;\n  subtitle?: string;\n  plans?: PricingPlan[];\n  defaultPeriod?: BillingPeriod;\n  /** Percentage shown on the yearly saving badge. Defaults to 20. */\n  yearlyDiscount?: number;\n  /** Currency symbol placed before every price. Defaults to \"$\". */\n  currency?: string;\n  onSelectPlan?: (plan: PricingPlan, period: BillingPeriod) => void;\n};\n\n/* ------------------------------------------------------------------ */\n/* deterministic demo data                                             */\n/* ------------------------------------------------------------------ */\n\nconst DEFAULT_TITLE = \"Simple pricing for every stage\";\nconst DEFAULT_SUBTITLE =\n  \"Start free, then pick the plan that fits your team. No hidden fees, cancel whenever you like.\";\n\nconst DEFAULT_PLANS: PricingPlan[] = [\n  {\n    id: \"starter\",\n    name: \"Starter\",\n    description: \"For small projects and side ideas finding their feet.\",\n    monthlyPrice: 19,\n    yearlyPrice: 15,\n    features: [\n      \"Up to 3 projects\",\n      \"10 GB storage\",\n      \"Basic analytics\",\n      \"Email support\",\n    ],\n    cta: \"Start free\",\n    href: \"/pricing\",\n  },\n  {\n    id: \"studio\",\n    name: \"Studio\",\n    description: \"For growing teams that ship every week.\",\n    monthlyPrice: 49,\n    yearlyPrice: 39,\n    features: [\n      \"Unlimited projects\",\n      \"100 GB storage\",\n      \"Advanced analytics\",\n      \"Custom domains\",\n      \"Priority support\",\n    ],\n    cta: \"Start free trial\",\n    href: \"/pricing\",\n    recommended: true,\n  },\n  {\n    id: \"scale\",\n    name: \"Scale\",\n    description: \"For organisations that need control and room to grow.\",\n    monthlyPrice: 99,\n    yearlyPrice: 79,\n    features: [\n      \"Everything in Studio\",\n      \"1 TB storage\",\n      \"SSO and audit logs\",\n      \"Dedicated support\",\n      \"99.9% uptime SLA\",\n    ],\n    cta: \"Contact sales\",\n    href: \"/pricing\",\n  },\n];\n\n/* ------------------------------------------------------------------ */\n/* helpers                                                             */\n/* ------------------------------------------------------------------ */\n\nfunction formatAmount(value: number, currency: string) {\n  return `${currency}${Math.round(value).toLocaleString(\"en-US\")}`;\n}\n\n/* ------------------------------------------------------------------ */\n/* digit-by-digit price morph                                          */\n/* ------------------------------------------------------------------ */\n\nfunction AnimatedDigit({\n  char,\n  index,\n  reduce,\n}: {\n  char: string;\n  index: number;\n  reduce: boolean;\n}) {\n  return (\n    <span className=\"relative inline-block overflow-hidden align-baseline\">\n      {/* invisible glyph keeps the slot at its final width */}\n      <span className=\"invisible\">{char}</span>\n      <AnimatePresence initial={false}>\n        <motion.span\n          key={char}\n          className=\"absolute inset-0 flex items-center justify-center\"\n          initial={reduce ? false : { y: \"80%\", opacity: 0 }}\n          animate={{ y: \"0%\", opacity: 1 }}\n          exit={reduce ? { opacity: 0 } : { y: \"-80%\", opacity: 0 }}\n          transition={{\n            duration: reduce ? 0 : 0.34,\n            ease: EASE,\n            delay: reduce ? 0 : index * 0.025,\n          }}\n        >\n          {char}\n        </motion.span>\n      </AnimatePresence>\n    </span>\n  );\n}\n\nfunction AnimatedPrice({\n  value,\n  reduce,\n}: {\n  value: number;\n  reduce: boolean;\n}) {\n  const chars = Math.round(value).toLocaleString(\"en-US\").split(\"\");\n\n  return (\n    <span aria-hidden=\"true\" className=\"inline-flex items-baseline tabular-nums\">\n      {chars.map((char, index) =>\n        /\\d/.test(char) ? (\n          <AnimatedDigit\n            key={index}\n            char={char}\n            index={index}\n            reduce={reduce}\n          />\n        ) : (\n          <span key={index} className=\"inline-block\">\n            {char}\n          </span>\n        )\n      )}\n    </span>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* recommended plan: a slow light travelling around the border         */\n/* ------------------------------------------------------------------ */\n\nfunction BorderLight({ reduce }: { reduce: boolean }) {\n  return (\n    <span\n      aria-hidden=\"true\"\n      className=\"pointer-events-none absolute inset-0 z-0 overflow-hidden rounded-xl p-px\"\n      style={{\n        WebkitMask:\n          \"linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)\",\n        WebkitMaskComposite: \"xor\",\n        mask: \"linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0)\",\n        maskComposite: \"exclude\",\n      }}\n    >\n      <motion.span\n        className=\"absolute -inset-[110%] block opacity-60\"\n        style={{\n          background:\n            \"conic-gradient(from 0deg, transparent 0deg, transparent 18deg, var(--foreground) 46deg, transparent 74deg)\",\n        }}\n        animate={reduce ? undefined : { rotate: 360 }}\n        transition={{ duration: 16, repeat: Infinity, ease: \"linear\" }}\n      />\n    </span>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* one plan card                                                       */\n/* ------------------------------------------------------------------ */\n\ntype PricingCardProps = {\n  plan: PricingPlan;\n  index: number;\n  yearly: boolean;\n  show: boolean;\n  reduce: boolean;\n  currency: string;\n  onSelect?: (plan: PricingPlan, period: BillingPeriod) => void;\n};\n\nfunction PricingCard({\n  plan,\n  index,\n  yearly,\n  show,\n  reduce,\n  currency,\n  onSelect,\n}: PricingCardProps) {\n  const price = yearly ? plan.yearlyPrice : plan.monthlyPrice;\n  const annual = Math.round(plan.yearlyPrice * 12);\n  const period: BillingPeriod = yearly ? \"yearly\" : \"monthly\";\n\n  return (\n    <motion.li\n      initial={{ opacity: 0, y: reduce ? 0 : 18 }}\n      animate={{ opacity: show ? 1 : 0, y: 0 }}\n      transition={{\n        duration: reduce ? 0 : 0.55,\n        ease: EASE,\n        delay: reduce ? 0 : index * 0.08,\n      }}\n      className={cn(\n        \"relative flex h-full min-w-0 flex-col rounded-xl border bg-card p-5 text-card-foreground shadow-sm sm:p-6\",\n        plan.recommended ? \"border-foreground/20\" : \"border-border\"\n      )}\n    >\n      {plan.recommended ? <BorderLight reduce={reduce} /> : null}\n\n      <div className=\"relative z-10 flex flex-1 flex-col\">\n        <div className=\"flex flex-wrap items-center justify-between gap-2\">\n          <h3 className=\"text-[15px] font-medium tracking-tight\">\n            {plan.name}\n          </h3>\n          {plan.recommended ? (\n            <span className=\"inline-flex shrink-0 items-center gap-1.5 rounded-full border border-foreground/15 bg-background/70 px-2 py-0.5 text-[10.5px] font-medium tracking-wide text-foreground/80\">\n              <span\n                aria-hidden=\"true\"\n                className=\"relative flex h-2.5 w-2.5 items-center justify-center rounded-full border border-current\"\n              >\n                <span className=\"absolute right-[1px] top-[1px] h-[2px] w-[2px] rounded-full bg-current\" />\n              </span>\n              Recommended\n            </span>\n          ) : null}\n        </div>\n\n        <p className=\"mt-1.5 min-h-10 text-[13px] leading-5 text-muted-foreground\">\n          {plan.description}\n        </p>\n\n        <p className=\"sr-only\">\n          {plan.name} plan, {formatAmount(price, currency)} per month,{\" \"}\n          {yearly\n            ? `billed ${formatAmount(annual, currency)} yearly`\n            : \"billed monthly\"}\n          .\n        </p>\n\n        <div\n          aria-hidden=\"true\"\n          className=\"mt-4 flex items-baseline gap-1\"\n        >\n          <span className=\"text-[22px] font-medium text-muted-foreground\">\n            {currency}\n          </span>\n          <span className=\"text-[38px] font-semibold leading-none tracking-tight text-card-foreground sm:text-[42px]\">\n            <AnimatedPrice value={price} reduce={reduce} />\n          </span>\n          <span className=\"text-[12.5px] text-muted-foreground\">/ mo</span>\n        </div>\n\n        <div\n          aria-hidden=\"true\"\n          className=\"mt-1.5 h-4 text-[11.5px] text-muted-foreground\"\n        >\n          <AnimatePresence initial={false} mode=\"wait\">\n            <motion.span\n              key={period}\n              className=\"block\"\n              initial={reduce ? false : { opacity: 0 }}\n              animate={{ opacity: 1 }}\n              exit={{ opacity: 0 }}\n              transition={{ duration: reduce ? 0 : 0.2, ease: EASE }}\n            >\n              {yearly\n                ? `Billed ${formatAmount(annual, currency)} yearly`\n                : \"Billed monthly\"}\n            </motion.span>\n          </AnimatePresence>\n        </div>\n\n        {plan.href && !onSelect ? (\n          <a\n            href={plan.href}\n            className={cn(\n              \"mt-4 inline-flex h-10 w-full items-center justify-center rounded-lg px-4 text-[13px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n              plan.recommended\n                ? \"bg-foreground text-background hover:bg-foreground/90\"\n                : \"border border-border bg-background text-foreground hover:bg-muted\"\n            )}\n          >\n            {plan.cta ?? \"Get started\"}\n          </a>\n        ) : (\n          <button\n            type=\"button\"\n            onClick={() => onSelect?.(plan, period)}\n            className={cn(\n              \"mt-4 inline-flex h-10 w-full items-center justify-center rounded-lg px-4 text-[13px] font-medium transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\",\n              plan.recommended\n                ? \"bg-foreground text-background hover:bg-foreground/90\"\n                : \"border border-border bg-background text-foreground hover:bg-muted\"\n            )}\n          >\n            {plan.cta ?? \"Get started\"}\n          </button>\n        )}\n\n        <div className=\"my-4 h-px w-full bg-border\" />\n\n        <ul className=\"flex flex-col gap-2.5\">\n          {plan.features.map((feature, featureIndex) => (\n            <motion.li\n              key={feature}\n              initial={reduce ? false : { opacity: 0, x: -4 }}\n              animate={show ? { opacity: 1, x: 0 } : { opacity: 0, x: -4 }}\n              transition={{\n                duration: reduce ? 0 : 0.4,\n                ease: EASE,\n                delay: reduce ? 0 : index * 0.08 + featureIndex * 0.035 + 0.1,\n              }}\n              className=\"flex items-start gap-2 text-[13px] leading-5 text-muted-foreground\"\n            >\n              <span className=\"mt-0.5 flex h-4 w-4 shrink-0 items-center justify-center rounded-full border border-border bg-muted/60 text-foreground\">\n                <Check aria-hidden=\"true\" className=\"h-3 w-3\" strokeWidth={2.5} />\n              </span>\n              <span className=\"min-w-0\">{feature}</span>\n            </motion.li>\n          ))}\n        </ul>\n      </div>\n    </motion.li>\n  );\n}\n\n/* ------------------------------------------------------------------ */\n/* block                                                               */\n/* ------------------------------------------------------------------ */\n\nexport function PricingToggle({\n  className,\n  title = DEFAULT_TITLE,\n  subtitle = DEFAULT_SUBTITLE,\n  plans = DEFAULT_PLANS,\n  defaultPeriod = \"monthly\",\n  yearlyDiscount = 20,\n  currency = \"$\",\n  onSelectPlan,\n}: PricingToggleProps) {\n  const ref = useRef<HTMLDivElement>(null);\n  const inView = useInView(ref, { once: true, margin: \"-10% 0px\" });\n  const reduce = Boolean(useReducedMotion());\n  const [period, setPeriod] = useState<BillingPeriod>(defaultPeriod);\n  const yearly = period === \"yearly\";\n\n  return (\n    <MotionConfig reducedMotion=\"user\">\n      <div ref={ref} className={cn(\"w-full\", className)}>\n        <motion.div\n          initial={{ opacity: 0, y: reduce ? 0 : 12 }}\n          animate={{ opacity: inView ? 1 : 0, y: 0 }}\n          transition={{ duration: reduce ? 0 : 0.55, ease: EASE }}\n          className=\"mx-auto max-w-2xl text-center\"\n        >\n          <h2 className=\"text-balance text-2xl font-semibold tracking-tight sm:text-3xl\">\n            {title}\n          </h2>\n          <p className=\"mt-2 text-pretty text-sm leading-6 text-muted-foreground\">\n            {subtitle}\n          </p>\n        </motion.div>\n\n        <motion.div\n          initial={{ opacity: 0, y: reduce ? 0 : 10 }}\n          animate={{ opacity: inView ? 1 : 0, y: 0 }}\n          transition={{ duration: reduce ? 0 : 0.5, ease: EASE, delay: 0.1 }}\n          className=\"mt-6 flex flex-wrap items-center justify-center gap-2.5\"\n        >\n          <button\n            type=\"button\"\n            role=\"switch\"\n            aria-checked={yearly}\n            aria-label=\"Bill yearly instead of monthly\"\n            onClick={() => setPeriod(yearly ? \"monthly\" : \"yearly\")}\n            className=\"relative inline-grid w-[184px] grid-cols-2 items-center rounded-full border border-border bg-muted p-1 text-[12.5px] font-medium focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background\"\n          >\n            <motion.span\n              aria-hidden=\"true\"\n              className=\"absolute inset-y-1 left-1 w-[calc(50%-0.25rem)] rounded-full bg-background shadow-sm\"\n              initial={false}\n              animate={{ x: yearly ? \"100%\" : \"0%\" }}\n              transition={\n                reduce ? { duration: 0 } : { duration: 0.32, ease: EASE }\n              }\n            />\n            <span\n              className={cn(\n                \"relative z-10 py-1.5 text-center transition-colors\",\n                yearly ? \"text-muted-foreground\" : \"text-foreground\"\n              )}\n            >\n              Monthly\n            </span>\n            <span\n              className={cn(\n                \"relative z-10 py-1.5 text-center transition-colors\",\n                yearly ? \"text-foreground\" : \"text-muted-foreground\"\n              )}\n            >\n              Yearly\n            </span>\n          </button>\n\n          <AnimatePresence initial={false}>\n            {yearly ? (\n              <motion.span\n                key=\"saving\"\n                initial={reduce ? false : { opacity: 0, y: -5 }}\n                animate={{ opacity: 1, y: 0 }}\n                exit={reduce ? { opacity: 0 } : { opacity: 0, y: -5 }}\n                transition={{ duration: reduce ? 0 : 0.28, ease: EASE }}\n                className=\"inline-flex items-center rounded-full border border-success/25 bg-success/10 px-2.5 py-1 text-[11px] font-medium text-success\"\n              >\n                Save {yearlyDiscount}%\n              </motion.span>\n            ) : null}\n          </AnimatePresence>\n        </motion.div>\n\n        <ul className=\"mt-8 grid grid-cols-1 gap-4 md:grid-cols-3\">\n          {plans.map((plan, index) => (\n            <PricingCard\n              key={plan.id}\n              plan={plan}\n              index={index}\n              yearly={yearly}\n              show={inView}\n              reduce={reduce}\n              currency={currency}\n              onSelect={onSelectPlan}\n            />\n          ))}\n        </ul>\n      </div>\n    </MotionConfig>\n  );\n}\n\nexport default PricingToggle;\n"}]}