"use client"; import { EllipsisVertical, Eye, EyeOff } from "lucide-react"; import { AnimatePresence, animate, motion, useMotionValue, useReducedMotion, useTransform, } from "motion/react"; import { type ReactNode, useCallback, useEffect, useState } from "react"; import { DigitSwap } from "@/components/motion/digit-swap"; import { EASE_IN_OUT, EASE_OUT, SPRING_LAYOUT, SPRING_PRESS, } from "@/lib/ease"; import { useMessages } from "@/lib/i18n"; import { cn } from "@/lib/utils"; // The two purse layers collapse as one material surface after the card starts // moving, then reverse immediately so closing feels like the card is caught. const PURSE_MORPH_TRANSITION = { duration: 0.28, ease: EASE_IN_OUT, } as const; const PURSE_REDUCED_TRANSITION = { duration: 0.16, ease: EASE_OUT, } as const; /** English defaults. Override per instance with `labels`, or app-wide with `LocaleProvider`. */ export const cardFolderMessages = { open: "Open {title}, card ending in {lastFour}, expires {expiry}", close: "Close {title}, card ending in {lastFour}, expires {expiry}", showDetails: "Show card details", hideDetails: "Hide card details", expiry: "Expiry", cvv: "CVV", actions: "Open actions for {title}", }; export type CardFolderMessages = typeof cardFolderMessages; export interface CardFolderProps { /** Card name shown on the folder front and used in labels. */ title: string; /** Full card number. Non-digits are stripped; only the last 4 show until revealed. */ cardNumber: string; /** Expiry date text shown on the folder, e.g. "08/29". */ expiry: string; /** Security code, masked until details are revealed. */ cvv: string; /** The card face that lifts out of the folder. */ card: ReactNode; /** Whether the card is lifted out, for controlled use. */ open?: boolean; /** Whether the card starts lifted out when uncontrolled. Default: false */ defaultOpen?: boolean; /** Called with the next state when the folder opens or closes. */ onOpenChange?: (open: boolean) => void; /** Whether the number and CVV are revealed, for controlled use. */ detailsVisible?: boolean; /** Whether the number and CVV start revealed when uncontrolled. Default: false */ defaultDetailsVisible?: boolean; /** Called with the next state when the details are shown or hidden. */ onDetailsVisibleChange?: (visible: boolean) => void; /** Called after the folder is pressed, alongside the open toggle. */ onClick?: () => void; /** Called when the actions button is pressed. The button shows only when set. */ onAction?: () => void; /** Accessible name for the folder button. Defaults to the `open` or `close` label. */ ariaLabel?: string; /** Accessible name for the actions button. Defaults to the `actions` label. */ actionLabel?: string; /** Disables opening, revealing details and the actions button. Default: false */ disabled?: boolean; /** Classes merged onto the root element. */ className?: string; /** Classes merged onto the wrapper around the card face. */ cardClassName?: string; /** Override any built-in string. `{title}`, `{lastFour}` and `{expiry}` fill `open`, `close` and `actions`. */ labels?: Partial; } /** * A landscape card tucked into an animated folder sleeve. Pressing the folder * lifts the card forward while the purse compresses into its bottom seam; a * separate privacy control reveals its number and CVV. */ export function CardFolder({ title, cardNumber, expiry, cvv, card, open, defaultOpen = false, onOpenChange, detailsVisible, defaultDetailsVisible = false, onDetailsVisibleChange, onClick, onAction, ariaLabel, actionLabel, disabled = false, className, cardClassName, labels, }: CardFolderProps) { const t = useMessages("cardFolder", cardFolderMessages, labels); const reduce = useReducedMotion(); const [internalOpen, setInternalOpen] = useState(defaultOpen); const [internalDetailsVisible, setInternalDetailsVisible] = useState( defaultDetailsVisible, ); const openControlled = open !== undefined; const detailsControlled = detailsVisible !== undefined; const isOpen = open ?? internalOpen; const areDetailsVisible = detailsVisible ?? internalDetailsVisible; const transition = reduce ? { duration: 0 } : SPRING_LAYOUT; const normalizedCardNumber = cardNumber.replace(/\D/g, ""); const visibleLastFour = normalizedCardNumber.slice(-4).padStart(4, "•"); const revealedCardNumber = normalizedCardNumber.match(/.{1,4}/g)?.join(" ") ?? visibleLastFour; const maskedCvv = "•".repeat(Math.max(3, cvv.length)); const defaultAriaLabel = t(isOpen ? "close" : "open", { title, lastFour: visibleLastFour, expiry, }); const progress = useMotionValue(isOpen ? 1 : 0); const cardTransform = useTransform(progress, (value) => { const boundedProgress = Math.min(1, Math.max(0, value)); const lift = Math.sin(Math.PI * boundedProgress); return `translateY(${-8 * lift}%) scale(${1 + 0.01 * lift})`; }); const backTransform = useTransform( progress, [0, 1], ["translateY(0%) scaleY(1)", "translateY(18%) scaleY(0.18)"], ); const frontTransform = useTransform( progress, [0, 1], ["translateY(0%) rotateX(0deg)", "translateY(18%) rotateX(-72deg)"], ); const purseOpacity = useTransform(progress, [0, 0.76, 1], [1, 1, 0]); useEffect(() => { const controls = animate( progress, isOpen ? 1 : 0, reduce ? { duration: 0 } : PURSE_MORPH_TRANSITION, ); return () => controls.stop(); }, [isOpen, progress, reduce]); const setOpen = useCallback( (nextOpen: boolean) => { if (disabled) return; if (!openControlled) setInternalOpen(nextOpen); onOpenChange?.(nextOpen); }, [disabled, onOpenChange, openControlled], ); const handleClick = () => { setOpen(!isOpen); onClick?.(); }; const toggleDetails = () => { if (disabled) return; const nextVisible = !areDetailsVisible; if (!detailsControlled) setInternalDetailsVisible(nextVisible); onDetailsVisibleChange?.(nextVisible); }; return (
{areDetailsVisible ? ( {t("expiry")} {expiry} {t("cvv")} {title} {onAction ? ( ) : null}
); }