Specification
- Catalogue
- No. 45 of 110
- Section
- Pricing
- Interaction
- Press, Layout Animation
- Element
- Toggle, Counter
- Preview
- Live component
- Imports
- react 19.3.1
- Added
Notes
Switching between monthly and yearly billing rolls the price digits instead of swapping the number, so the change is visible even at a glance.
The thumb slides under the selected option, the savings badge pops in with an overshoot, and the billing note cross-fades in place. Each of these is a CSS transition keyed off one attribute, data-period, on the switcher or the selected state.
The options behave as a radio group, and the full price sentence is announced in a hidden live region.
Code
React and plain CSS"use client";
import { useEffect, useState, type CSSProperties } from "react";
import type { LivePreviewProps } from "@/components/live/types";
import styles from "./pricing-toggle.module.css";
const PRICES = { monthly: 24, yearly: 19 } as const;
type Period = keyof typeof PRICES;
const DIGITS = "0123456789".split("");
function RollingDigits({ value }: { value: number }) {
return (
<span className={styles.digits} aria-hidden="true">
{String(value)
.split("")
.map((d, i) => (
<span key={i} className={styles.digit} style={{ "--d": Number(d), "--i": i } as CSSProperties}>
<span className={styles.column}>
{DIGITS.map((n) => (
<span key={n}>{n}</span>
))}
</span>
</span>
))}
</span>
);
}
export default function PricingToggle({ active, context }: LivePreviewProps) {
const [period, setPeriod] = useState<Period>("monthly");
const [step, setStep] = useState(0);
const demo = active && context === "card";
// Each time the demo starts, it starts from the first step.
const [demoRunning, setDemoRunning] = useState(demo);
if (demoRunning !== demo) {
setDemoRunning(demo);
setStep(0);
}
useEffect(() => {
if (!demo) return;
let id = window.setTimeout(function tick() {
setStep((s) => s + 1);
id = window.setTimeout(tick, 1400);
}, 300);
return () => window.clearTimeout(id);
}, [demo]);
const shown: Period = context === "card" ? (demo && step % 2 === 1 ? "yearly" : "monthly") : period;
const price = PRICES[shown];
return (
<div className={styles.root}>
<div className={styles.switcher} role="radiogroup" aria-label="Billing period" data-period={shown}>
<span className={styles.thumb} aria-hidden="true" />
{(["monthly", "yearly"] as const).map((p) => (
<button
key={p}
type="button"
role="radio"
aria-checked={shown === p}
tabIndex={context === "card" ? -1 : shown === p ? 0 : -1}
className={styles.option}
onClick={() => setPeriod(p)}
onKeyDown={(e) => {
if (e.key === "ArrowRight" || e.key === "ArrowLeft") {
e.preventDefault();
setPeriod(p === "monthly" ? "yearly" : "monthly");
const next = e.currentTarget.parentElement?.querySelectorAll("button")[p === "monthly" ? 1 : 0];
next?.focus();
}
}}
>
{p === "monthly" ? "Monthly" : "Yearly"}
</button>
))}
</div>
<div className={styles.plan}>
<div className={styles.planHead}>
<span className={styles.planName}>Team</span>
<span className={styles.badge} data-visible={shown === "yearly" || undefined}>
2 months free
</span>
</div>
<p className={styles.price}>
<span className="visually-hidden" aria-live="polite">
${price} per seat per month, billed {shown}
</span>
<span className={styles.currency} aria-hidden="true">
$
</span>
<RollingDigits value={price} />
<span className={styles.per} aria-hidden="true">
/seat
</span>
</p>
<p className={styles.note}>
<span data-visible={shown === "monthly" || undefined}>Billed monthly</span>
<span data-visible={shown === "yearly" || undefined}>Billed yearly</span>
</p>
</div>
</div>
);
}