"use client";
import { useEffect, useId, useState, type CSSProperties } from "react";
import { AnimatePresence, animate, motion, useMotionValue, useReducedMotion, useTransform } from "motion/react";
type Plan = {
name: string;
/** Price for one month. */
monthly: number;
/** Price for the whole year. The saving badge is worked out from these two. */
yearly: number;
/** Small line under the price. */
note?: string;
features?: string[];
/** Marks this plan with the accent colour. */
featured?: boolean;
};
type PricingSwitchProps = {
plans: Plan[];
/** Start on the yearly side. */
defaultYearly?: boolean;
/** Text on the two sides of the switch. */
labels?: [string, string];
/** Text after the price on each side. */
units?: [string, string];
/** Text before the price, like "$" or "₹". */
prefix?: string;
/** Digits after the point. */
decimals?: number;
/** Number format, like "en-IN" for lakh commas. */
locale?: string;
/** Called when the switch moves. */
onChange?: (yearly: boolean) => void;
/** Called when a plan button is pressed. Leave out to hide the buttons. */
onSelect?: (plan: Plan, yearly: boolean) => void;
/** Text on the plan buttons. */
buttonLabel?: string;
/** Switch, featured plan and tick colour. Text on it stays dark. */
accent?: string;
/** Replaces the plan card look. */
className?: string;
};
function Price({ value, format, reduce }: { value: number; format: (n: number) => string; reduce: boolean | null }) {
const count = useMotionValue(value);
const text = useTransform(count, format);
useEffect(() => {
if (reduce) {
count.set(value);
return;
}
const run = animate(count, value, { duration: 0.6, ease: [0.16, 1, 0.3, 1] });
return () => run.stop();
}, [count, reduce, value]);
return (
<>
{plan.note}
} {plan.features && (