- Accordion
- Attachment
- Avatar
- Badge
- Breadcrumb
- Button
- Button Group
- Card
- Checkbox
- Code Block
- Collapsible
- Dialog
- Dropdown Menu
- Empty
- Field
- Hover Card
- Input
- Input Group
- Input OTP
- Item
- Label
- Marker
- Message
- Number Flow
- Pagination
- Popover
- Progress
- Radio Group
- Select
- Slider
- Sonner
- Spinner
- Switch
- Tooltip
- Separator
- Sheet
- Tabs
- Textarea
- Toggle
- Toggle Group
- Trace
4•2•4•2•  4•2•4•2•  4•2•4•2•  44224422
Card holder
John Doe
Expires
12/28
import { FrakmentMark } from "@/components/site/brand";
import {
CreditCard,
CreditCardBrand,
CreditCardExpiry,
CreditCardFooter,
CreditCardHeader,
CreditCardHolder,
CreditCardIssuer,
CreditCardNumber,
} from "@/components/ui/credit-card";
export function CreditCardDemo() {
return (
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
);
}Credit Card renders a card display from raw data. It detects the brand from the number prefix, groups the digits, and formats the expiry - with a masked mode for saved-card lists and a glass variant for translucent surfaces.
Installation
$ bunx --bun frakment add credit-card$ npx frakment add credit-card$ pnpm dlx frakment add credit-card$ yarn dlx frakment add credit-card
Install the following dependencies:
$ bun add class-variance-authority @icons-pack/react-simple-icons motion$ npm install class-variance-authority @icons-pack/react-simple-icons motion$ pnpm add class-variance-authority @icons-pack/react-simple-icons motion$ yarn add class-variance-authority @icons-pack/react-simple-icons motion
Copy and paste the following code into your project.
"use client";
import {
SiAmericanexpress,
SiDinersclub,
SiDiscover,
SiJcb,
SiMastercard,
SiVisa,
} from "@icons-pack/react-simple-icons";
import { cva, type VariantProps } from "class-variance-authority";
import { motion, useReducedMotion } from "motion/react";
import type * as React from "react";
import {
delayTight,
durationSlow,
easeInOutQuad,
parseAnimation,
} from "@/lib/animation";
import { cn } from "@/lib/utils";
type CardBrand = "mastercard" | "visa" | "amex" | "discover" | "jcb" | "diners";
const creditCardVariants = cva(
"relative flex aspect-[1.586] w-full max-w-sm flex-col justify-between overflow-hidden rounded-xl p-4 sm:p-5",
{
defaultVariants: { variant: "default" },
variants: {
variant: {
default:
"bg-neutral-100 text-neutral-900 dark:bg-neutral-800 dark:text-neutral-100",
glass:
"bg-neutral-900/20 bg-[radial-gradient(circle_at_top_left,rgba(129,140,248,0.22),transparent_45%),radial-gradient(circle_at_bottom_right,rgba(244,114,182,0.22),transparent_45%)] text-neutral-900 shadow-2xl backdrop-blur-[80px] dark:bg-white/20 dark:text-neutral-100",
},
},
}
);
const brandIcons = {
amex: SiAmericanexpress,
diners: SiDinersclub,
discover: SiDiscover,
jcb: SiJcb,
mastercard: SiMastercard,
visa: SiVisa,
} as const;
export function detectBrand(cardNumber: string): CardBrand {
const clean = cardNumber.replace(/\s/g, "");
if (/^4/.test(clean)) {
return "visa";
}
if (/^(5[1-5]|2[2-7])/.test(clean)) {
return "mastercard";
}
if (/^3[47]/.test(clean)) {
return "amex";
}
if (/^(6011|65|64[4-9])/.test(clean)) {
return "discover";
}
if (/^35/.test(clean)) {
return "jcb";
}
if (/^(30|36|38)/.test(clean)) {
return "diners";
}
return "mastercard";
}
export function formatCardNumber(
cardNumber: string,
brand: CardBrand,
masked = false
): string {
const clean = cardNumber.replace(/\s/g, "");
if (masked) {
const last4 = clean.slice(-4);
if (brand === "amex") {
return `•••• •••••• ${last4}`;
}
return `•••• •••• •••• ${last4}`;
}
if (brand === "amex") {
return [clean.slice(0, 4), clean.slice(4, 10), clean.slice(10, 15)]
.filter(Boolean)
.join(" ");
}
if (brand === "diners") {
return [clean.slice(0, 4), clean.slice(4, 10), clean.slice(10, 14)]
.filter(Boolean)
.join(" ");
}
return [
clean.slice(0, 4),
clean.slice(4, 8),
clean.slice(8, 12),
clean.slice(12, 16),
]
.filter(Boolean)
.join(" ");
}
export function formatExpiry(expiry: string): string {
const clean = expiry.replace(/\D/g, "");
if (clean.length <= 2) {
return clean;
}
return `${clean.slice(0, 2)}/${clean.slice(2, 4)}`;
}
function BrandLogo({ brand }: { brand: CardBrand }) {
const Icon = brandIcons[brand];
return <Icon aria-label={brand} className="size-6 sm:size-8" />;
}
function CreditCard({
className,
variant,
...props
}: React.ComponentProps<"div"> & VariantProps<typeof creditCardVariants>) {
return (
<div
className={cn(creditCardVariants({ variant }), className)}
data-slot="credit-card"
{...props}
/>
);
}
function CreditCardHeader({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
className={cn("flex items-center justify-between", className)}
{...props}
/>
);
}
function CreditCardIssuer({
className,
children,
logo,
...props
}: React.ComponentProps<"div"> & {
logo?: React.ReactNode;
}) {
return (
<div className={cn("flex items-center", className)} {...props}>
{children ?? logo ?? null}
</div>
);
}
function CreditCardBrand({
className,
brand,
children,
...props
}: React.ComponentProps<"div"> & { brand?: CardBrand }) {
return (
<div className={cn("flex items-center", className)} {...props}>
{children ?? (brand ? <BrandLogo brand={brand} /> : null)}
</div>
);
}
function CreditCardNumber({
className,
value,
brand,
masked = false,
animation,
duration = durationSlow,
stagger = delayTight,
...props
}: React.ComponentProps<"p"> & {
value: string;
brand?: CardBrand;
masked?: boolean;
animation?: false | string;
duration?: number;
stagger?: number;
}) {
const resolvedBrand = brand ?? detectBrand(value);
const fullText = formatCardNumber(value, resolvedBrand, false);
const maskedText = formatCardNumber(value, resolvedBrand, true);
const displayText = masked ? maskedText : fullText;
const prefersReduced = useReducedMotion();
const baseClass = cn(
"overflow-hidden font-mono text-sm tracking-[0.1em] whitespace-nowrap tabular-nums sm:text-lg sm:tracking-[0.15em]",
className
);
if (animation === false) {
return (
<p className={baseClass} {...props}>
{displayText}
</p>
);
}
const anim = parseAnimation(animation);
const baseTransition = prefersReduced
? { duration: 0 }
: (anim?.transition ?? { duration, ease: easeInOutQuad });
const maxLen = Math.max(fullText.length, maskedText.length);
const chars: React.ReactNode[] = [];
for (let i = 0; i < maxLen; i++) {
const fullChar = fullText[i] ?? " ";
const maskedChar = maskedText[i] ?? " ";
const isSpace = fullChar === " " && maskedChar === " ";
const delay = i * stagger;
chars.push(
<span
className="relative inline-flex h-[1.2em] w-[1ch] items-center justify-center"
key={i}
>
<motion.span
animate={{
filter: masked ? "blur(6px)" : "blur(0px)",
opacity: masked ? 0 : 1,
scale: masked ? 0.15 : 1,
}}
className="absolute inset-0 flex items-center justify-center will-change-transform"
initial={false}
transition={{ ...baseTransition, delay }}
>
{isSpace ? "\u00A0" : fullChar}
</motion.span>
<motion.span
animate={{
filter: masked ? "blur(0px)" : "blur(6px)",
opacity: masked ? 1 : 0,
scale: masked ? 1 : 0.15,
}}
className="absolute inset-0 flex items-center justify-center will-change-transform"
initial={false}
transition={{ ...baseTransition, delay }}
>
{isSpace ? "\u00A0" : maskedChar}
</motion.span>
</span>
);
}
return (
<p aria-label={displayText} className={baseClass} {...props}>
<span aria-hidden="true" className="inline-flex">
{chars}
</span>
</p>
);
}
function CreditCardFooter({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
className={cn("flex items-end justify-between gap-4", className)}
{...props}
/>
);
}
function CreditCardHolder({
className,
label = "Card holder",
value,
...props
}: React.ComponentProps<"div"> & { label?: string; value: string }) {
return (
<div className={cn("space-y-0.5 sm:space-y-1", className)} {...props}>
<p className="text-[9px] tracking-wider uppercase opacity-60 sm:text-[10px]">
{label}
</p>
<p className="text-[10px] font-medium uppercase sm:text-xs">{value}</p>
</div>
);
}
function CreditCardExpiry({
className,
label = "Expires",
value,
...props
}: React.ComponentProps<"div"> & { label?: string; value: string }) {
return (
<div
className={cn("space-y-0.5 text-right sm:space-y-1", className)}
{...props}
>
<p className="text-[9px] tracking-wider uppercase opacity-60 sm:text-[10px]">
{label}
</p>
<p className="text-[10px] font-medium sm:text-xs">
{formatExpiry(value)}
</p>
</div>
);
}
export {
type CardBrand,
CreditCard,
CreditCardBrand,
CreditCardExpiry,
CreditCardFooter,
CreditCardHeader,
CreditCardHolder,
CreditCardIssuer,
CreditCardNumber,
creditCardVariants,
};Update the import paths to match your project setup.
Usage
import {
CreditCard,
CreditCardBrand,
CreditCardExpiry,
CreditCardFooter,
CreditCardHeader,
CreditCardHolder,
CreditCardIssuer,
CreditCardNumber,
} from "@/components/ui/credit-card";<CreditCard>
<CreditCardHeader>
<CreditCardIssuer>ACME Bank</CreditCardIssuer>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>Pass raw data to sub-components. CreditCardNumber handles brand detection and grouping; CreditCardExpiry normalizes the expiry.
Anatomy
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer>ACME Bank</CreditCardIssuer>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>- CreditCard - root container. Accepts
variant. - CreditCardHeader - top row, flex with space-between.
- CreditCardIssuer - left slot, defaults to the Frakment mark. Accepts children for a custom logo.
- CreditCardBrand - right slot. Pass
brandfor a network logo or children for a custom mark. - CreditCardNumber - number, accepts
value, optionalbrand,masked. - CreditCardFooter - bottom row.
- CreditCardHolder - cardholder name, accepts
valueand optionallabel. - CreditCardExpiry - expiry, accepts
valueand optionallabel.
Brands
4•2•4•2•  4•2•4•2•  4•2•4•2•  44224422
Card holder
John Doe
Expires
12/28
5•5•5•5•  5•5•5•5•  5•5•5•5•  44444444
Card holder
John Doe
Expires
12/28
3•7•8•2•  8•2•2•4•6•3•  100000055
Card holder
John Doe
Expires
12/28
6•0•1•1•  1•1•1•1•  1•1•1•1•  11111177
Card holder
John Doe
Expires
12/28
import { FrakmentMark } from "@/components/site/brand";
import {
CreditCard,
CreditCardBrand,
CreditCardExpiry,
CreditCardFooter,
CreditCardHeader,
CreditCardHolder,
CreditCardIssuer,
CreditCardNumber,
} from "@/components/ui/credit-card";
export function CreditCardBrands() {
return (
<div className="flex w-full max-w-sm flex-col gap-4">
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber brand="visa" value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="mastercard" />
</CreditCardHeader>
<CreditCardNumber brand="mastercard" value="5555555555554444" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="amex" />
</CreditCardHeader>
<CreditCardNumber brand="amex" value="378282246310005" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="discover" />
</CreditCardHeader>
<CreditCardNumber brand="discover" value="6011111111111117" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
</div>
);
}The network is inferred from the number prefix. Pass brand to override.
| Prefix | Brand |
|---|---|
4 | Visa |
51-55, 22-27 | Mastercard |
34, 37 | American Express |
6011, 65, 644-649 | Discover |
35 | JCB |
30, 36, 38 | Diners Club |
Masked
4•2•4•2•  4•2•4•2•  4•2•4•2•  44224422
Card holder
John Doe
Expires
12/28
4•2•4•2•  4•2•4•2•  4•2•4•2•  44224422
Card holder
John Doe
Expires
12/28
import { FrakmentMark } from "@/components/site/brand";
import {
CreditCard,
CreditCardBrand,
CreditCardExpiry,
CreditCardFooter,
CreditCardHeader,
CreditCardHolder,
CreditCardIssuer,
CreditCardNumber,
} from "@/components/ui/credit-card";
export function CreditCardMasked() {
return (
<div className="flex w-full max-w-sm flex-col gap-4">
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber masked value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
</div>
);
}Pass masked to hide all but the last four digits.
Animated mask
4•2•4•2•  4•2•4•2•  4•2•4•2•  44224422
Card holder
John Doe
Expires
12/28
"use client";
import { useEffect, useState } from "react";
import { FrakmentMark } from "@/components/site/brand";
import { Button } from "@/components/ui/button";
import {
CreditCard,
CreditCardBrand,
CreditCardExpiry,
CreditCardFooter,
CreditCardHeader,
CreditCardHolder,
CreditCardIssuer,
CreditCardNumber,
} from "@/components/ui/credit-card";
import { ArrowClockwiseIcon } from "@/lib/icons";
export function CreditCardMaskedMotion() {
const [masked, setMasked] = useState(false);
const [cycle, setCycle] = useState(0);
useEffect(() => {
const t = setTimeout(() => setMasked(true), 1300);
return () => clearTimeout(t);
}, [cycle]);
return (
<div className="flex w-full max-w-sm flex-col items-start gap-4">
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber masked={masked} value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
<Button
onClick={() => {
setMasked(false);
setCycle((c) => c + 1);
}}
size="sm"
variant="outline"
>
<ArrowClockwiseIcon />
Replay
</Button>
</div>
);
}A staggered per-character morph from the full number to the masked form.
Glass variant
4•2•4•2•  4•2•4•2•  4•2•4•2•  44224422
Card holder
John Doe
Expires
12/28
5•5•5•5•  5•5•5•5•  5•5•5•5•  44444444
Card holder
John Doe
Expires
12/28
import { FrakmentMark } from "@/components/site/brand";
import {
CreditCard,
CreditCardBrand,
CreditCardExpiry,
CreditCardFooter,
CreditCardHeader,
CreditCardHolder,
CreditCardIssuer,
CreditCardNumber,
} from "@/components/ui/credit-card";
export function CreditCardVariants() {
return (
<div className="flex w-full max-w-sm flex-col gap-4">
<CreditCard>
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="visa" />
</CreditCardHeader>
<CreditCardNumber value="4242424242424242" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
<CreditCard variant="glass">
<CreditCardHeader>
<CreditCardIssuer
logo={<FrakmentMark className="size-5 sm:size-6" />}
/>
<CreditCardBrand brand="mastercard" />
</CreditCardHeader>
<CreditCardNumber value="5555555555554444" />
<CreditCardFooter>
<CreditCardHolder value="John Doe" />
<CreditCardExpiry value="1228" />
</CreditCardFooter>
</CreditCard>
</div>
);
}default follows the page theme. glass renders a translucent blurred surface for gradient or image backgrounds.
Animation
CreditCardNumber accepts an animation prop. The transition applies per character when the number or mask state changes, using stagger as the per-character delay.
<CreditCardNumber
value="4242424242424242"
masked={masked}
animation="spring(300, 25) delay(50ms)"
/>| Form | Example |
|---|---|
| Named easing | "easeInOut", "easeOutExpo", "easeSmoothOut" |
| Named easing + duration | "easeInOut 300ms", "easeOutExpo 0.5" |
| Custom curve | "cubic-bezier(0.4, 0, 0.2, 1) 300ms" |
| Spring | "spring(300, 25)" |
Any form supports delay(...) and repeat(n | infinite). Pass animation={false} to render plain text without motion markup.
API Reference
CreditCard
| Prop | Type | Default | Description |
|---|---|---|---|
variant | defaultglass | — | Visual style of the card surface. |
className | string | — | Merged into the surface. |
Extends React.ComponentProps<"div">.
CreditCardIssuer
| Prop | Type | Default | Description |
|---|---|---|---|
logo | ReactNode | — | Custom logo. Falls back to children. Renders nothing when omitted. |
CreditCardBrand
| Prop | Type | Default | Description |
|---|---|---|---|
brand | mastercardvisaamexdiscoverjcbdiners | — | Network logo to render. |
children | ReactNode | — | Custom mark. Overrides brand. |
CreditCardNumber
| Prop | Type | Default | Description |
|---|---|---|---|
value* | string | — | Raw number. Spaces stripped before formatting. |
brand | CardBrand | — | Overrides auto-detection. |
masked | boolean | — | Shows only the last four digits. |
animation | falsestring | — | Motion DSL applied per character. Pass false to render plain text without motion markup. |
duration | number | — | Per-character transition duration in seconds. Ignored when animation is set. |
stagger | number | — | Delay between characters in seconds. Applies whether or not animation is set. |
CreditCardHolder / CreditCardExpiry
| Prop | Type | Default | Description |
|---|---|---|---|
value* | string | — | Value to render. |
label | string | — | Caption above the value. Defaults: Card holder / Expires. |
Accessibility
- The number uses
tabular-numsso columns do not shift when toggling masked mode. - Brand icons carry an
aria-labelwith the network name. - The root container is not focusable. Wrap in an anchor or button if the card opens a detail view.
- In
maskedmode the visible text is masked. Do not expose the full number in adjacent layout if it is meant to be hidden.
Best Practices
- Use
maskedfor saved-card lists, never for the primary display of the card being confirmed. - Pass raw numbers - do not pre-format. The component handles grouping and expiry normalization.
- Reserve
glassfor layered surfaces. On plain backgrounds the blur has nothing to sample and reads flat. - Keep labels short. Card holder and Expires fit two words. Longer labels wrap and break the footer layout.