{
    "id": "bhaskar/pricing-period-toggle",
    "kind": "component",
    "title": "Pricing Period Toggle",
    "summary": "Monthly/yearly switch with a sliding thumb, a savings badge and prices that count to the new value.",
    "author": "Admin",
    "stars": 0,
    "url": "https://ownlyplans.com/bhaskar/pricing-period-toggle/",
    "stack": [
        "CSS",
        "HTML",
        "JavaScript",
        "React"
    ],
    "tools": [
        "claude-code",
        "cursor",
        "lovable"
    ],
    "difficulty": "beginner",
    "license": "mit",
    "version": "1.0",
    "overview": "The small control that decides how people read your pricing page. The thumb slides, the price rolls to its new value and the per-period label updates, so the change is obvious without a page jump. It is a real radio group, so keyboard and screen reader users get the same control.\n\nThe prices in the preview are placeholders: pass your own.",
    "steps": [],
    "files": [],
    "install": "npx shadcn@latest add https://ownlyplans.com/r/bhaskar/pricing-period-toggle.json",
    "how_to_use": "Use the component code (html, css, js, or react) directly, or run the install command.",
    "component": {
        "category": "Toggles and inputs",
        "html": "<div class=\"pt\">\n  <div class=\"pt-switch\" role=\"radiogroup\" aria-label=\"Billing period\">\n    <span class=\"pt-thumb\" aria-hidden=\"true\"></span>\n    <button type=\"button\" role=\"radio\" aria-checked=\"true\" data-p=\"m\">Monthly</button>\n    <button type=\"button\" role=\"radio\" aria-checked=\"false\" data-p=\"y\">Yearly <em>2 months free</em></button>\n  </div>\n  <div class=\"pt-card\"><p class=\"pt-name\">Pro</p><p class=\"pt-price\"><span id=\"pt-amt\">12</span><small id=\"pt-per\">/ month</small></p>\n  <p class=\"pt-note\" id=\"pt-note\">Billed monthly. Cancel any time.</p></div>\n</div>",
        "css": ".pt{display:grid;gap:16px;justify-items:center;width:min(340px,100%)}\n.pt-switch{position:relative;display:grid;grid-template-columns:1fr 1fr;padding:4px;border-radius:999px;background:color-mix(in srgb,var(--ink) 7%,transparent);border:1px solid var(--line);width:100%}\n.pt-thumb{position:absolute;top:4px;bottom:4px;left:4px;width:calc(50% - 4px);border-radius:999px;background:var(--stage);box-shadow:0 2px 8px rgba(0,0,0,.12);transition:transform .35s cubic-bezier(.2,.8,.2,1)}\n.pt[data-p=y] .pt-thumb{transform:translateX(100%)}\n.pt-switch button{position:relative;border:0;background:none;padding:9px 10px;font:600 13px/1.2 system-ui,sans-serif;color:var(--muted);cursor:pointer;border-radius:999px}\n.pt-switch button[aria-checked=true]{color:var(--ink)}\n.pt-switch button:focus-visible{outline:2px solid var(--accent);outline-offset:2px}\n.pt-switch em{font-style:normal;font-size:10px;font-weight:700;color:var(--accent);margin-left:4px}\n.pt-card{width:100%;border:1px solid var(--line);border-radius:16px;padding:18px 20px;background:var(--stage)}\n.pt-name{margin:0;font-weight:600;color:var(--muted);font-size:13px}\n.pt-price{margin:4px 0;font:700 40px/1.1 system-ui,sans-serif;letter-spacing:-.02em;color:var(--ink)}\n.pt-price::before{content:\"$\";font-size:22px;vertical-align:top;margin-right:2px}\n.pt-price small{font-size:14px;font-weight:500;color:var(--muted);margin-left:6px}\n.pt-note{margin:0;font-size:13px;color:var(--muted)}\n@media (prefers-reduced-motion:reduce){.pt-thumb{transition:none}}",
        "js": "const root=document.querySelector('.pt'),amt=document.getElementById('pt-amt'),per=document.getElementById('pt-per'),note=document.getElementById('pt-note');\nconst P={m:{price:12,per:'/ month',note:'Billed monthly. Cancel any time.'},y:{price:120,per:'/ year',note:'Billed yearly: $10 a month.'}};\nconst still=matchMedia('(prefers-reduced-motion: reduce)').matches;let cur=12,raf;\nfunction count(to){cancelAnimationFrame(raf);if(still){amt.textContent=to;cur=to;return}const from=cur,t0=performance.now();\n const step=t=>{const k=Math.min((t-t0)/450,1),e=1-Math.pow(1-k,3);amt.textContent=Math.round(from+(to-from)*e);if(k<1)raf=requestAnimationFrame(step);else cur=to};raf=requestAnimationFrame(step)}\nconst btns=[...document.querySelectorAll('.pt-switch button')];\nfunction pick(p){root.dataset.p=p;btns.forEach(b=>b.setAttribute('aria-checked',b.dataset.p===p));per.textContent=P[p].per;note.textContent=P[p].note;count(P[p].price)}\nbtns.forEach(b=>{b.addEventListener('click',()=>pick(b.dataset.p));b.addEventListener('keydown',e=>{if(['ArrowLeft','ArrowRight'].includes(e.key)){e.preventDefault();const n=btns.find(x=>x!==b);n.focus();pick(n.dataset.p)}})});",
        "react": "\"use client\";\nimport { useEffect, useRef, useState } from \"react\";\n\ntype Period = \"monthly\" | \"yearly\";\nconst css = `\n.pt{display:grid;gap:16px;justify-items:center;width:min(340px,100%);font-family:system-ui,sans-serif;color:#0b1020}\n.pt-switch{position:relative;display:grid;grid-template-columns:1fr 1fr;padding:4px;border-radius:999px;background:#eef1f7;border:1px solid #d8deeb;width:100%}\n.pt-thumb{position:absolute;top:4px;bottom:4px;left:4px;width:calc(50% - 4px);border-radius:999px;background:#fff;box-shadow:0 2px 8px rgba(0,0,0,.12);transition:transform .35s cubic-bezier(.2,.8,.2,1)}\n.pt-switch[data-p=yearly] .pt-thumb{transform:translateX(100%)}\n.pt-switch button{position:relative;border:0;background:none;padding:9px 10px;font:600 13px/1.2 system-ui,sans-serif;color:#5c627c;cursor:pointer;border-radius:999px}\n.pt-switch button[aria-checked=true]{color:#0b1020}\n.pt-switch button:focus-visible{outline:2px solid #1659f5;outline-offset:2px}\n.pt-switch em{font-style:normal;font-size:10px;font-weight:700;color:#1659f5;margin-left:4px}\n@media (prefers-reduced-motion:reduce){.pt-thumb{transition:none}}`;\n\nfunction useCountTo(value: number) {\n  const [shown, setShown] = useState(value);\n  const from = useRef(value);\n  useEffect(() => {\n    if (window.matchMedia(\"(prefers-reduced-motion: reduce)\").matches) { setShown(value); from.current = value; return; }\n    const start = from.current, t0 = performance.now();\n    let raf = 0;\n    const step = (t: number) => {\n      const k = Math.min((t - t0) / 450, 1);\n      const v = Math.round(start + (value - start) * (1 - Math.pow(1 - k, 3)));\n      setShown(v);\n      if (k < 1) raf = requestAnimationFrame(step); else from.current = value;\n    };\n    raf = requestAnimationFrame(step);\n    return () => cancelAnimationFrame(raf);\n  }, [value]);\n  return shown;\n}\n\nexport function PricingPeriodToggle({ monthly = 12, yearly = 120, badge = \"2 months free\", onChange }:\n  { monthly?: number; yearly?: number; badge?: string; onChange?: (p: Period) => void }) {\n  const [period, setPeriod] = useState<Period>(\"monthly\");\n  const price = useCountTo(period === \"monthly\" ? monthly : yearly);\n  const pick = (p: Period) => { setPeriod(p); onChange?.(p); };\n  const onKey = (e: React.KeyboardEvent) => {\n    if (e.key === \"ArrowLeft\" || e.key === \"ArrowRight\") { e.preventDefault(); pick(period === \"monthly\" ? \"yearly\" : \"monthly\"); }\n  };\n  return (\n    <div className=\"pt\">\n      <style>{css}</style>\n      <div className=\"pt-switch\" role=\"radiogroup\" aria-label=\"Billing period\" data-p={period} onKeyDown={onKey}>\n        <span className=\"pt-thumb\" aria-hidden=\"true\" />\n        <button type=\"button\" role=\"radio\" aria-checked={period === \"monthly\"} onClick={() => pick(\"monthly\")}>Monthly</button>\n        <button type=\"button\" role=\"radio\" aria-checked={period === \"yearly\"} onClick={() => pick(\"yearly\")}>Yearly <em>{badge}</em></button>\n      </div>\n      <p style={{ margin: 0, font: \"700 40px/1.1 system-ui\", letterSpacing: \"-.02em\" }}>\n        ${price}<small style={{ fontSize: 14, fontWeight: 500, color: \"#5c627c\", marginLeft: 6 }}>/ {period === \"monthly\" ? \"month\" : \"year\"}</small>\n      </p>\n    </div>\n  );\n}",
        "dependencies": [],
        "install": "npx shadcn@latest add https://ownlyplans.com/r/bhaskar/pricing-period-toggle.json",
        "licence_notice": ""
    }
}