{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"price-toggle","type":"registry:ui","title":"Price toggle","description":"Monthly or yearly segmented switch with a sliding thumb, plus an animated price that rolls its digits.","dependencies":["motion"],"registryDependencies":["utils"],"files":[{"path":"registry/items/price-toggle/price-toggle.tsx","type":"registry:ui","target":"components/facade/price-toggle.tsx","content":"\"use client\";\n\nimport * as React from \"react\";\nimport { AnimatePresence, motion, useReducedMotion } from \"motion/react\";\nimport { cn } from \"@/lib/utils\";\n\n/* ------------------------------------------------------------------\n   PriceToggle\n------------------------------------------------------------------- */\n\nexport type BillingPeriod = \"monthly\" | \"yearly\";\n\nexport interface PriceToggleProps {\n  value?: BillingPeriod;\n  defaultValue?: BillingPeriod;\n  onChange?: (value: BillingPeriod) => void;\n  /** Badge next to the yearly option. Set to null to hide. */\n  badge?: string | null;\n  labels?: { monthly: string; yearly: string };\n  className?: string;\n}\n\nexport function PriceToggle({\n  value: valueProp,\n  defaultValue = \"monthly\",\n  onChange,\n  badge = \"Save 20%\",\n  labels = { monthly: \"Monthly\", yearly: \"Yearly\" },\n  className,\n}: PriceToggleProps) {\n  const reduce = useReducedMotion();\n  const [inner, setInner] = React.useState<BillingPeriod>(defaultValue);\n  const value = valueProp ?? inner;\n  const uid = React.useId();\n  const refs = React.useRef<Record<BillingPeriod, HTMLButtonElement | null>>({ monthly: null, yearly: null });\n\n  const select = (v: BillingPeriod) => {\n    if (valueProp === undefined) setInner(v);\n    onChange?.(v);\n  };\n\n  const onKeyDown = (e: React.KeyboardEvent) => {\n    if ([\"ArrowLeft\", \"ArrowRight\", \"ArrowUp\", \"ArrowDown\"].includes(e.key)) {\n      e.preventDefault();\n      const next: BillingPeriod = value === \"monthly\" ? \"yearly\" : \"monthly\";\n      select(next);\n      refs.current[next]?.focus();\n    }\n  };\n\n  const options: BillingPeriod[] = [\"monthly\", \"yearly\"];\n\n  return (\n    <div className={cn(\"inline-flex items-center gap-3\", className)}>\n      <div\n        role=\"radiogroup\"\n        aria-label=\"Billing period\"\n        className=\"relative inline-grid grid-cols-2 rounded-full bg-muted p-1\"\n      >\n        {options.map((o) => {\n          const active = value === o;\n          return (\n            <button\n              key={o}\n              ref={(el) => {\n                refs.current[o] = el;\n              }}\n              type=\"button\"\n              role=\"radio\"\n              aria-checked={active}\n              tabIndex={active ? 0 : -1}\n              onClick={() => select(o)}\n              onKeyDown={onKeyDown}\n              className={cn(\n                \"relative h-9 min-w-24 rounded-full px-4 text-sm font-medium transition-colors duration-200\",\n                \"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring\",\n                active ? \"text-foreground\" : \"text-muted-foreground hover:text-foreground\",\n              )}\n            >\n              {active && (\n                <motion.span\n                  layoutId={`${uid}-thumb`}\n                  className=\"absolute inset-0 rounded-full bg-background shadow-sm shadow-foreground/[0.08] dark:bg-foreground/[0.12] dark:shadow-none\"\n                  transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 400, damping: 36 }}\n                  aria-hidden\n                />\n              )}\n              <span className=\"relative\">{labels[o]}</span>\n            </button>\n          );\n        })}\n      </div>\n      {badge && (\n        <motion.span\n          animate={\n            value === \"yearly\"\n              ? { opacity: 1, scale: 1 }\n              : { opacity: 0.75, scale: reduce ? 1 : 0.96 }\n          }\n          transition={{ type: \"spring\", stiffness: 400, damping: 34 }}\n          className={cn(\n            \"inline-flex h-6 items-center rounded-full px-2.5 text-xs font-medium transition-colors\",\n            value === \"yearly\" ? \"bg-success/12 text-success\" : \"bg-muted text-muted-foreground\",\n          )}\n        >\n          {badge}\n        </motion.span>\n      )}\n    </div>\n  );\n}\n\n/* ------------------------------------------------------------------\n   AnimatedPrice: rolls each digit when the value changes\n------------------------------------------------------------------- */\n\nexport interface AnimatedPriceProps {\n  value: number;\n  currency?: string;\n  /** Decimal places. Defaults to 0 for whole numbers, 2 otherwise. */\n  fractionDigits?: number;\n  locale?: string;\n  className?: string;\n}\n\nconst DIGITS = [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"];\n\nfunction Digit({ digit, reduce }: { digit: number; reduce: boolean | null }) {\n  return (\n    <span className=\"relative inline-block h-[1em] overflow-hidden leading-none tabular-nums\" aria-hidden>\n      <span className=\"invisible\">0</span>\n      <motion.span\n        className=\"absolute inset-x-0 top-0 flex flex-col\"\n        initial={false}\n        animate={{ y: `${-digit * 10}%` }}\n        transition={reduce ? { duration: 0 } : { type: \"spring\", stiffness: 300, damping: 34, mass: 0.9 }}\n      >\n        {DIGITS.map((d) => (\n          <span key={d} className=\"block h-[1em] leading-none\">\n            {d}\n          </span>\n        ))}\n      </motion.span>\n    </span>\n  );\n}\n\nexport function AnimatedPrice({\n  value,\n  currency = \"USD\",\n  fractionDigits,\n  locale = \"en-US\",\n  className,\n}: AnimatedPriceProps) {\n  const reduce = useReducedMotion();\n  const digits = fractionDigits ?? (Number.isInteger(value) ? 0 : 2);\n  const formatter = React.useMemo(\n    () =>\n      new Intl.NumberFormat(locale, {\n        style: \"currency\",\n        currency,\n        minimumFractionDigits: digits,\n        maximumFractionDigits: digits,\n      }),\n    [locale, currency, digits],\n  );\n  const parts = formatter.formatToParts(value);\n  const full = formatter.format(value);\n\n  // Key digits from the right so the ones place stays the same element as the number grows.\n  const chars: Array<{ ch: string; isDigit: boolean }> = [];\n  for (const p of parts) {\n    for (const ch of p.value) chars.push({ ch, isDigit: /\\d/.test(ch) });\n  }\n  let digitIndexFromRight = chars.filter((c) => c.isDigit).length;\n\n  return (\n    <span className={cn(\"inline-flex items-baseline\", className)}>\n      <span className=\"sr-only\">{full}</span>\n      <span className=\"inline-flex items-baseline\" aria-hidden>\n        <AnimatePresence initial={false} mode=\"popLayout\">\n          {chars.map((c, i) => {\n            if (c.isDigit) {\n              digitIndexFromRight -= 1;\n              return (\n                <motion.span\n                  key={`d-${digitIndexFromRight}`}\n                  layout={!reduce}\n                  initial={reduce ? false : { opacity: 0, y: \"-0.3em\" }}\n                  animate={{ opacity: 1, y: 0 }}\n                  exit={reduce ? { opacity: 0 } : { opacity: 0, y: \"0.3em\" }}\n                  transition={{ type: \"spring\", stiffness: 300, damping: 32 }}\n                  className=\"inline-block\"\n                >\n                  <Digit digit={Number(c.ch)} reduce={reduce} />\n                </motion.span>\n              );\n            }\n            return (\n              <motion.span\n                key={`s-${c.ch}-${i - chars.length}`}\n                layout={!reduce}\n                transition={{ type: \"spring\", stiffness: 300, damping: 32 }}\n                className=\"inline-block\"\n              >\n                {c.ch}\n              </motion.span>\n            );\n          })}\n        </AnimatePresence>\n      </span>\n    </span>\n  );\n}\n\nexport default PriceToggle;\n"}],"categories":["forms"],"meta":{"tier":"free","demo":"\"use client\";\n\nimport * as React from \"react\";\nimport { ArrowRight, Check } from \"lucide-react\";\nimport { AnimatedPrice, PriceToggle, type BillingPeriod } from \"./price-toggle\";\n\nconst features = [\n  \"Two professional cleanings per year\",\n  \"Annual exam and digital X-rays\",\n  \"20% off fillings, crowns and whitening\",\n  \"Same week emergency visits\",\n  \"No insurance, no deductibles, no waiting periods\",\n];\n\nexport default function Demo() {\n  const [period, setPeriod] = React.useState<BillingPeriod>(\"monthly\");\n  const monthly = 39;\n  const yearlyPerMonth = 31;\n  const price = period === \"monthly\" ? monthly : yearlyPerMonth;\n\n  return (\n    <div className=\"mx-auto flex w-full max-w-md flex-col items-center gap-6 p-4\">\n      <PriceToggle value={period} onChange={setPeriod} />\n\n      <article className=\"w-full rounded-[28px] bg-muted p-7 text-foreground sm:p-8\">\n        <p className=\"text-xs font-medium uppercase tracking-[0.08em] text-muted-foreground\">Most popular</p>\n        <h3 className=\"mt-2 text-[22px] font-semibold tracking-[-0.02em]\">Smile Club Membership</h3>\n        <p className=\"mt-1 text-[15px] text-muted-foreground\">Bayside Family Dental, San Diego</p>\n\n        <div className=\"mt-7 flex items-baseline gap-1.5\">\n          <AnimatedPrice value={price} className=\"text-[56px] font-semibold leading-none tracking-[-0.04em]\" />\n          <span className=\"text-[15px] text-muted-foreground\">per month</span>\n        </div>\n        <p className=\"mt-2 h-5 text-[13px] text-muted-foreground\">\n          {period === \"yearly\" ? (\n            <>\n              Billed <span className=\"tabular-nums text-foreground\">$372</span> yearly. You save{\" \"}\n              <span className=\"tabular-nums text-foreground\">$96</span>.\n            </>\n          ) : (\n            \"Billed monthly. Cancel anytime.\"\n          )}\n        </p>\n\n        <ul className=\"mt-7 space-y-3\">\n          {features.map((f) => (\n            <li key={f} className=\"flex items-start gap-3 text-[15px]\">\n              <Check className=\"mt-1 size-4 shrink-0 text-foreground\" strokeWidth={2.5} aria-hidden />\n              {f}\n            </li>\n          ))}\n        </ul>\n\n        <a\n          href=\"#join\"\n          className=\"mt-8 inline-flex h-12 w-full items-center justify-center gap-2 rounded-full bg-brand text-[15px] font-medium text-brand-foreground transition-colors hover:bg-brand/90 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-muted\"\n        >\n          Join Smile Club\n          <ArrowRight className=\"size-4\" aria-hidden />\n        </a>\n      </article>\n    </div>\n  );\n}\n"}}