Pricing Toggle
Spring-physics billing switch.
$19One-time access
1. Install Dependencies
$npm install framer-motion lucide-react
2. Copy Component Code
components/ui/PricingToggle.tsx
1"use client";2
3import React, { useState } from "react";4import { AnimatePresence, motion, useReducedMotion } from "framer-motion";5import { Check } from "lucide-react";6
7interface PricingToggleProps {8 monthlyPrice?: string;9 lifetimePrice?: string;10 defaultLifetime?: boolean;11}12
13export function PricingToggle({14 monthlyPrice = "$0",15 lifetimePrice = "$19",16 defaultLifetime = true,17}: PricingToggleProps) {18 const [lifetime, setLifetime] = useState(defaultLifetime);19 const prefersReducedMotion = useReducedMotion();20
21 return (22 <div className="w-full max-w-xs rounded-2xl border border-black/[0.08] bg-white p-5 dark:border-white/[0.08] dark:bg-[#0A0A0A]">23 <div24 role="group"25 aria-label="Billing cycle"26 className="inline-flex w-full rounded-full border border-black/[0.08] bg-[#FAFAFA] p-1 dark:border-white/[0.1] dark:bg-black"27 >28 {(["Monthly", "Lifetime"] as const).map((label) => {29 const active = (label === "Lifetime") === lifetime;30 return (31 <button32 key={label}33 type="button"34 aria-pressed={active}35 onClick={() => setLifetime(label === "Lifetime")}36 className="relative flex-1 rounded-full py-1.5 text-xs font-medium"37 >38 {active && (39 <motion.span40 layoutId="pricing-toggle-pill"41 transition={42 prefersReducedMotion43 ? { duration: 0 }44 : { duration: 0.2, ease: [0.22, 1, 0.36, 1] }45 }46 className="absolute inset-0 rounded-full bg-emerald-500 shadow-[0_0_12px_rgba(16,185,129,0.3)]"47 />48 )}49 <span50 className={`relative z-10 ${51 active52 ? "text-white"53 : "text-neutral-500 dark:text-neutral-400"54 }`}55 >56 {label}57 </span>58 </button>59 );60 })}61 </div>62
63 <div className="mt-5 flex items-baseline justify-between">64 <AnimatePresence mode="wait" initial={false}>65 <motion.span66 key={lifetime ? "lifetime" : "monthly"}67 initial={prefersReducedMotion ? false : { opacity: 0, y: 6 }}68 animate={{ opacity: 1, y: 0 }}69 exit={prefersReducedMotion ? undefined : { opacity: 0, y: -6 }}70 transition={prefersReducedMotion ? { duration: 0 } : { duration: 0.18 }}71 className="font-mono text-3xl font-semibold tabular-nums text-black dark:text-white"72 >73 {lifetime ? lifetimePrice : monthlyPrice}74 </motion.span>75 </AnimatePresence>76 <span className="inline-flex items-center gap-1 text-xs text-neutral-500 dark:text-neutral-400">77 <Check className="h-3.5 w-3.5 text-emerald-500" aria-hidden="true" />78 {lifetime ? "One-time access" : "MIT components"}79 </span>80 </div>81 </div>82 );83}Usage Example
import { PricingToggle } from "@/components/ui/PricingToggle";
export default function PricingSection() { return ( <PricingToggle monthlyPrice="$0" lifetimePrice="$19" defaultLifetime={true} /> );}3. Props Reference
| Prop | Type | Default | Description |
|---|---|---|---|
| monthlyPrice | string | "$0" | Price string displayed when Monthly mode is active. |
| lifetimePrice | string | "$19" | Price string displayed when Lifetime mode is selected. |
| defaultLifetime | boolean | true | Sets whether Lifetime mode is initially active. |