Card Field
A one-row card input for checkout — the number groups by brand, slides to its last four digits when done, and expiry and CVC appear in the same row.
Card Field
A one-row card input for checkout — the number groups by brand, slides to its last four digits when done, and expiry and CVC appear in the same row.
The field detects the card brand from the first digits and groups the number the way it prints on the card: Visa, Mastercard, and Discover in 4-4-4-4, Amex in 4-6-5. Once the number checks out with the Luhn algorithm, it slides away to show only its last four digits, making room for expiry and security code in the same row. Invalid numbers shake and show an error. The brand mark switches to the back of the card when you reach the CVC field, showing where to find the code. Backspace in an empty field or arrow-left at the start moves to the previous field.
Installation
Install the component with the Craft-UI Blocks CLI — it drops card-field.tsx into your project and wires up its runtime dependencies:
npx mvpblocks add card-fieldpnpm dlx mvpblocks add card-fieldyarn dlx mvpblocks add card-fieldbunx mvpblocks add card-fieldFile Structure
The CLI saves the component to your project's components/ui folder:
Import the default export from @/components/ui/card-field. The props type is exported as CardFieldProps, and the returned card details as CardDetails.
Usage
CardField is a client component and every prop is optional — with no props it renders a payment form charging $48.00. Drop it into any checkout flow:
"use client";
import CardField from "@/components/ui/card-field";
export function CheckoutForm() {
return <CardField />;
}Customize the amount and currency, and handle the payment with onPay. The function receives the card details and the button waits for the promise to resolve:
async function handlePayment(card: CardDetails) {
const response = await fetch("/api/charge", {
method: "POST",
body: JSON.stringify({
brand: card.brand,
last4: card.last4,
expiry: card.expiry,
}),
});
if (!response.ok) {
throw new Error("Payment failed");
}
}
<CardField
amount={149.99}
currency="$"
onPay={handlePayment}
/>The onPay function receives a CardDetails object with:
brand:"visa" | "mastercard" | "amex" | "discover" | "unknown"last4: The last four digits of the card numberexpiry: The expiration date as"MM/YY"
Enable test mode to show a "Use test card" link that fills in 4242 4242 4242 4242 with a future expiry and CVC:
<CardField
amount={48}
currency="$"
testMode={true}
onPay={handlePayment}
/>Use className to adjust the card's styling or width:
<CardField
amount={99}
currency="€"
className="max-w-md bg-zinc-50 dark:bg-zinc-900"
/>Theming
The card uses zinc tones with semantic error and success colors. The surface is rounded-3xl border-zinc-200 bg-white in light mode, inverting to dark:border-zinc-800 dark:bg-zinc-950. Labels are text-zinc-900 (dark:text-zinc-100) and placeholders are text-zinc-400 (dark:text-zinc-500).
The input row follows focus and error states:
- Default:
border-zinc-200(dark:border-zinc-800) - Focused:
border-zinc-400with aring-4 ring-zinc-900/5(dark:border-zinc-600withdark:ring-white/5) - Error:
border-red-400(dark:border-red-500/70)
Error messages appear in text-red-600 (dark:text-red-400). The hint text is text-zinc-500 (dark:text-zinc-400) with a lock icon showing encryption.
The pay button transitions through three states:
- Disabled (incomplete form):
bg-zinc-100 text-zinc-400(dark:bg-zinc-900 dark:text-zinc-600) - Ready:
bg-zinc-900 text-white hover:bg-zinc-800(dark:bg-zinc-100 dark:text-zinc-900 dark:hover:bg-white) - Paid:
bg-emerald-600 text-white(dark:bg-emerald-500 dark:text-emerald-950)
Brand marks show Visa, Mastercard, Amex, and Discover logos with authentic colors. The generic card icon appears for unrecognized numbers.
Animations
- Number slide: When the card number validates, all groups except the last fade to
0opacity over200msand the entire number slides left to show only the last four digits. A spring animation with0.45sduration and zero bounce keeps the motion smooth and stops precisely. - Expiry and CVC entrance: The expiry and CVC fields slide in from
16pxto the right and fade in when the number collapses, using the same spring timing. - Brand mark flip: The brand mark pops out and back in when switching between front and back views, blurring
2pxduring the transition over200ms. - Invalid number shake: An invalid card number triggers a horizontal shake from
0to-8to7to-4to2and back to0over360mswith an ease-out curve. - Hint text swap: Error messages and hints fade in from
4pxbelow over200msease-out, and the previous message fades out upward. - Pay button states: The button label slides up and out while the next label slides in from below over
200ms. The processing state shows a spinning border animation. - Success state: After payment succeeds, the button turns green with a check icon. After
2.4s, the form resets to empty. - Ghost digits: Untyped digits appear as
0s in muted zinc, transitioning opacity over150mswhen focusing or blurring the field. - Reduced motion: All slides, scales, and shakes are disabled. Only fades remain, running instantly.
How it works
The component detects the card brand by testing the first digits against regex patterns for Visa (^4), Mastercard (^5[1-5] or ^2[2-7]), Amex (^3[47]), and Discover (^6011, ^65, or ^64[4-9]). Each brand has its own grouping pattern and CVC length: Amex uses 4-6-5 with a 4-digit CVV, while the others use 4-4-4-4 with a 3-digit CVC.
The number input renders a clear <input> with text-transparent over a drawn mirror copy. The mirror splits the typed digits into brand-specific groups with spaces, and untyped positions show as ghost 0s. This architecture lets the groups fade and slide independently while the caret and selection stay native.
After each keystroke, the component preserves the caret's position relative to the digits, not the spaces: typing the 5th digit lands the caret after it, whether that position has a space or not. When the brand changes mid-type (rare but possible with initial digits like 34 → 35), the grouping shifts but the caret tracks the same logical position.
The Luhn algorithm validates the card number once it reaches the brand's full length. The component sums every digit, doubling every second digit from the right and subtracting 9 if the result exceeds 9. A valid card produces a sum divisible by 10.
Expiry validation checks that the month is 1–12 and the date is not in the past. The component compares year-month pairs as integers: 2026 × 12 + 9 (October 2026) must be greater than or equal to the current month's equivalent.
The collapse animation measures the offsetLeft of the last group with a ref and animates translateX to that value, sliding the entire mirror leftward so the last group aligns where the first group started.
Backspace in an empty field or arrow-left at the start triggers a move to the previous field, implemented by calling focus() and setSelectionRange(length, length) on the ref. The expiry field deletes the month digit when you backspace through the " / " separator.
The "Use test card" button types the digits one by one with 45ms intervals, pausing 260ms after the last digit so you see the slide before expiry starts filling. Each step calls writeNumber with the next slice of 4242424242424242.
Props
Prop
Type
CardDetails Object
Prop
Type
Registry Bundle
Install the exact registry entry shown on the right when you want the component file and its declared runtime dependencies together. Dependencies: motion (for slide and fade animations), lucide-react (for CreditCard, Lock, and Check icons).
Contact
If you find any bug or issue, feel free to raise an issue.
Last updated on
Boarding PassNew
A boarding pass that keeps itself current — when gate or times change, only moved characters roll over, new values turn orange, and old ones stay underneath struck through.
Flip CardNew
An interactive 3D flip card component with hover animations. Perfect for showcasing MVP features with engaging visual effects and smooth transitions.