Spring Billing Switch
Shared layoutId spring toggle with price transition.
$19one-time
1. Install Dependencies
$npm install framer-motion lucide-react
2. Copy Component Code
components/ui/SpringBillingSwitch.tsx
1"use client";2
3import React, { useState } from "react";4import { AnimatePresence, motion, useReducedMotion } from "framer-motion";5
6interface SpringBillingSwitchProps {7 onChange?: (isLifetime: boolean) => void;8}9
10export function SpringBillingSwitch({ onChange }: SpringBillingSwitchProps) {11 const [lifetime, setLifetime] = useState(true);12 const prefersReducedMotion = useReducedMotion();13
14 function select(next: boolean) {15 setLifetime(next);16 onChange?.(next);17 }18
19 return (20 <div className="flex flex-col items-center gap-4">21 <div22 role="group"23 aria-label="Billing interval"24 className="inline-flex rounded-full border border-black/[0.08] bg-white p-1 dark:border-white/[0.12] dark:bg-black"25 >26 {(["Monthly", "Lifetime"] as const).map((mode) => {27 const active = (mode === "Lifetime") === lifetime;28 return (29 <button30 key={mode}31 type="button"32 aria-pressed={active}33 onClick={() => select(mode === "Lifetime")}34 className="relative rounded-full px-4 py-1.5 text-xs font-medium"35 >36 {active && (37 <motion.span38 layoutId="spring-billing-pill"39 transition={40 prefersReducedMotion41 ? { duration: 0 }42 : { duration: 0.2, ease: [0.22, 1, 0.36, 1] }43 }44 className="absolute inset-0 rounded-full bg-emerald-500 shadow-[0_0_12px_rgba(16,185,129,0.35)]"45 />46 )}47 <span48 className={`relative z-10 ${49 active50 ? "text-white"51 : "text-neutral-500 dark:text-neutral-400"52 }`}53 >54 {mode}55 </span>56 </button>57 );58 })}59 </div>60
61 <div className="flex items-baseline gap-1.5">62 <AnimatePresence mode="wait" initial={false}>63 <motion.span64 key={lifetime ? "19" : "0"}65 initial={prefersReducedMotion ? false : { opacity: 0, y: 6 }}66 animate={{ opacity: 1, y: 0 }}67 exit={prefersReducedMotion ? undefined : { opacity: 0, y: -6 }}68 transition={prefersReducedMotion ? { duration: 0 } : { duration: 0.18 }}69 className="font-mono text-3xl font-semibold tabular-nums text-black dark:text-white"70 >71 {lifetime ? "$19" : "$0"}72 </motion.span>73 </AnimatePresence>74 <span className="text-xs text-neutral-500 dark:text-neutral-400">75 {lifetime ? "one-time" : "/ forever"}76 </span>77 </div>78 </div>79 );80}Usage Example
import { SpringBillingSwitch } from "@/components/ui/SpringBillingSwitch";
export default function CheckoutHeader() { return <SpringBillingSwitch />;}3. Props Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| onChange | (isLifetime: boolean) => void | undefined | Optional callback fired whenever the billing pill toggles. |