components / form
Pricing Toggle
Monthly/yearly switch with an optional saving badge.
885 B gzip client component
MonthlyYearlySave 20%
$24 / month
pricing-toggle-default.tsx
"use client";
import { useState } from "react";
import { PricingToggle } from "@/components/ui/pricing-toggle";
export default function PricingToggleDefault() {
const [yearly, setYearly] = useState(true);
return (
<div className="flex flex-col items-center gap-3">
<PricingToggle badge="Save 20%" onYearlyChange={setYearly} yearly={yearly} />
<p className="font-heading font-bold text-3xl">
${yearly ? 24 : 30}
<span className="font-normal text-muted-foreground text-sm"> / month</span>
</p>
</div>
);
}Installation
$ npx shadcn@latest add https://cd-ui.vercel.app/r/pricing-toggle.jsonfirst time? set up components.json and the theme first: see installation.
Usage
import { PricingToggle } from "@/components/ui/pricing-toggle"
const [yearly, setYearly] = useState(true)
<PricingToggle yearly={yearly} onYearlyChange={setYearly} badge="Save 20%" />API
PricingToggle
Switch between two billing periods, with an optional badge. Controlled: you keep yearly and compute the prices.
| prop | type | default |
|---|---|---|
yearlyWhether yearly billing is selected. | boolean | - |
onYearlyChangeCalled when the switch flips. | (yearly: boolean) => void | - |
badgeContent of the badge, such as "Save 20%". | ReactNode | - |
monthlyLabelText of the monthly side. | string | "Monthly" |
yearlyLabelText of the yearly side. It also names the switch. | string | "Yearly" |
Accessibility
- It is a
role="switch"named byyearlyLabel: on means yearly billing.