# Credit card input

> A single-line hairline card composer with live Visa, Mastercard, Amex and Discover detection, 3D CVC back-stripe flip, Luhn validation and seamless field handoff.

Multi-box payment forms waste vertical space and lose rhythm between card number, expiry and security code. CreditCardInput packs the complete payment line into one crisp hairline capsule: the left vector emblem switches dynamically as Visa, Mastercard, Amex or Discover prefixes arrive, adjusts grouping automatically between 4-4-4-4 and Amex 4-6-5, and flips 180 degrees in 3D to highlight the back security stripe the instant focus moves into CVC. Months 2 through 9 auto-pad to 02 through 09, valid Luhn numbers compress cleanly into a clickable trailing-four chip while editing Expiry and CVC, Backspace at position zero hands focus back cleanly, and a quiet emerald 8×8 Bayer floor pulse confirms the moment every field validates.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/credit-card-input.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: none
- Hairline UI components it needs (the CLI installs them too): `text-morph`
- Docs and live preview: https://hairlineui.com/ui/credit-card-input
- Registry item (source): https://hairlineui.com/r/credit-card-input.json

## Usage

```tsx
import { CreditCardInput } from "@/components/credit-card-input";

<CreditCardInput
  showPostal
  onChange={(card) => {
    setCanSubmit(card.valid);
  }}
/>
```

## Billing card update form guarded by live Luhn and expiry checks

Use CreditCardInput for client-side formatting and pre-validation, or pair its state cleanly with your tokenisation workflow.

```tsx
"use client";
import { useState } from "react";
import { CreditCardInput, type CardValue } from "@/components/credit-card-input";

export function BillingCardForm({ onSave }: { onSave: (card: CardValue) => Promise<void> }) {
  const [card, setCard] = useState<CardValue | null>(null);

  return (
    <form
      onSubmit={(e) => {
        e.preventDefault();
        if (card?.valid) void onSave(card);
      }}
      className="space-y-3"
    >
      <CreditCardInput showPostal onChange={setCard} />
      <button
        type="submit"
        disabled={!card?.valid}
        className="h-10 w-full rounded-xl bg-foreground text-sm font-medium text-background disabled:opacity-40"
      >
        Save payment method
      </button>
    </form>
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onChange` | `(card: CardValue) => void` | – | Emits { number, formattedNumber, last4, brand, expMonth, expYear, cvc, postal, valid, error } on every edit. |
| `compactOnAdvance / showPostal` | `boolean / boolean` | `true / false` | Compress valid number to clickable •••• 4242 while editing Expiry/CVC, and toggle optional ZIP slot. |
| `defaultNumber / defaultExpiry / defaultCvc` | `string` | – | Optional initial values for previews or test fixtures (omit in production forms). |
| `label / disabled / sound` | `string / boolean / boolean` | `"Card details" / false / false` | Accessible group label, disabled state and opt-in Web Audio ticks. |
| `detectCardBrand / isValidLuhn` | `exported utility functions` | – | BIN network detector and Mod-10 Luhn validator exported directly from the component file. |

## States

- **empty**: Minimal hairline card silhouette with clean placeholders for card number, MM / YY and CVC.
- **network detected**: Typing 4, 51 to 55, 34/37 or 6011 switches the left emblem to the matching vector mark and adapts digit grouping and CVC length.
- **compact handoff**: Once Luhn passes and focus advances to Expiry, the number compresses cleanly into a clickable •••• 4242 pill.
- **CVC flipped**: Focusing CVC rotates the left emblem 180 degrees to highlight the security-code zone on the card back.
- **verified**: When Luhn, expiry and CVC (plus postal if shown) all pass, an emerald check morphs in alongside a quiet emerald 8×8 Bayer floor sweep.
- **invalid**: A Luhn failure or expired date nudges the field with a damped horizontal recoil, rose Bayer floor pulse and inline alert.

## What it handles

Good card inputs feel effortless to type: network formatting adapts immediately, focus advances at the exact right digit, and mistakes explain themselves without erasing what you typed.

- **Live vector network detection**: Detects Visa, Mastercard, Amex and Discover from BIN prefixes, switching bespoke SVG card emblems, spacing (4-4-4-4 vs 4-6-5) and CVC length (3 vs 4) automatically.
- **3D CVC back-stripe flip**: Focusing CVC rotates the left card emblem 180 degrees to highlight the security-code strip on the back, flipping cleanly back on blur.
- **Real Mod-10 Luhn and expiry checks**: Validates full card numbers with real Luhn checksum math and rejects expired MM / YY dates with a damped horizontal recoil and inline alert.
- **Smart single-line compact handoff**: Compresses a verified number into a clickable •••• 4242 chip while editing Expiry and CVC so tight containers never clip; Backspace at slot start expands it right back.
- **Auto-padding and keyboard flow**: Auto-pads single-digit months 2 to 9 as 02 to 09, auto-advances across Number, Expiry, CVC and optional ZIP, and moves backwards cleanly on Backspace.
- **Exported validation helpers**: Exports detectCardBrand() and isValidLuhn() alongside CreditCardInput so forms and tests share identical rules.

## Replaces

multi-field card form, card-validator wrapper, custom Luhn checker, brand logo sprites

## Where it fits

- **SaaS billing and plan upgrades**: Update a workspace payment method inside a compact settings row without a tall multi-field form.
- **Single-page checkout drawers**: Keep card number, MM / YY, CVC and optional ZIP on one crisp line that stays readable even on narrow viewports.
- **Card validation before tokenisation**: Catch Mod-10 Luhn typos and expired dates instantaneously with clear inline feedback before calling your payment provider.
- **Corporate and virtual card entry**: Handle both 16-digit / 3-digit CVC cards and 15-digit / 4-digit CID Amex cards automatically.

## Notes

- In production PCI-scoped checkouts, tokenise directly through your payment processor SDK rather than transmitting raw PANs to your own backend.
- All card brand icons are inline SVGs with zero external image requests.
- The 8×8 Bayer optical floor sleeps at 0% CPU at rest and wakes only for ~0.5s on valid completion or validation error.
- Under prefers-reduced-motion: reduce, the 3D CVC flip, horizontal recoil and Bayer sweeps switch cleanly without motion.

License: MIT (https://hairlineui.com/license#components)
