Skip to Content

Credit Card

Credit card for fintech and banking: the front and back of a payment card as separate parts, drawn in SVG with dark, black and glass finishes. Each prints the details you pass, masked or in full; labels are translatable. The demo flips between the faces and reveals the number.

Installation

pnpm dlx shadcn@latest add @01orbital/credit-card

Usage

"use client"

import { useId, useState } from "react"

import {
  CreditCardBack,
  CreditCardFront,
} from "@/components/credit-card/credit-card"
import { Button } from "@/components/ui/button"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"

// Every word on the page. Change copy here, not in the markup.
const COPY = {
  finish: "Card finish",
  showFront: "Show front",
  showBack: "Show back",
  showDetails: "Show details",
  hideDetails: "Hide details",
  frontStatus: "Front of card",
  backStatus: "Back of card",
  detailsShown: "Details shown",
  detailsHidden: "Details hidden",
} as const

const FINISHES = [
  { value: "dark", label: "Slate" },
  { value: "black", label: "Ink" },
  { value: "glass", label: "Glass" },
] as const

// The fictional card the demo shows.
const CARD = {
  number: "4242 4242 4242 4242",
  holder: "LENA ORVIK",
  expiry: "08/29",
  cvv: "382",
} as const

export default function CreditCardDemo() {
  const [variant, setVariant] = useState<"dark" | "black" | "glass">("dark")
  const [flipped, setFlipped] = useState(false)
  const [visible, setVisible] = useState(false)
  const id = useId()

  // Until details are shown, the front keeps the last four digits and the
  // back shows dots.
  const number = visible
    ? CARD.number
    : `••••  ••••  ••••  ${CARD.number.replace(/\s/g, "").slice(-4)}`
  const cvv = visible ? CARD.cvv : "•••"

  return (
    <>
      <ToggleGroup
        aria-label={COPY.finish}
        value={[variant]}
        onValueChange={(values) => {
          const value = values[0]
          if (value === "dark" || value === "black" || value === "glass")
            setVariant(value)
        }}
      >
        {FINISHES.map(({ value, label }) => (
          <ToggleGroupItem key={value} value={value}>
            {label}
          </ToggleGroupItem>
        ))}
      </ToggleGroup>
      <div className="w-full max-w-sm p-4">
        {/* The two faces back to back; the turned-away face is hidden from
            assistive tech. With reduced motion on, it flips without animating. */}
        <div id={id} className="relative aspect-856/540 perspective-[1000px]">
          <div
            data-flipped={flipped}
            className="relative size-full rounded-[4%] shadow-lg transition-transform duration-500 ease-out transform-3d motion-reduce:transition-none"
            style={{ transform: flipped ? "rotateY(180deg)" : "rotateY(0deg)" }}
          >
            <CreditCardFront
              variant={variant}
              cardNumber={number}
              cardHolder={CARD.holder}
              expiryDate={CARD.expiry}
              aria-hidden={flipped}
              className="absolute inset-0 max-w-none shadow-none [-webkit-backface-visibility:hidden] backface-hidden"
            />
            <CreditCardBack
              variant={variant}
              cvv={cvv}
              aria-hidden={!flipped}
              className="absolute inset-0 max-w-none transform-[rotateY(180deg)] shadow-none [-webkit-backface-visibility:hidden] backface-hidden"
            />
          </div>
        </div>
        <div className="mt-3 flex flex-wrap items-center justify-center gap-2">
          <Button
            type="button"
            variant="ghost"
            size="sm"
            aria-controls={id}
            onClick={() => setFlipped((value) => !value)}
          >
            {flipped ? COPY.showFront : COPY.showBack}
          </Button>
          <Button
            type="button"
            variant="ghost"
            size="sm"
            aria-controls={id}
            onClick={() => setVisible((value) => !value)}
          >
            {visible ? COPY.hideDetails : COPY.showDetails}
          </Button>
        </div>
        <span role="status" className="sr-only">
          {`${flipped ? COPY.backStatus : COPY.frontStatus}. ${visible ? COPY.detailsShown : COPY.detailsHidden}.`}
        </span>
      </div>
    </>
  )
}

This example installs with everything it uses:

pnpm dlx shadcn@latest add @01orbital/credit-card-demo

Examples

Dark Dashboard Tilecredit-card-dark-dashboard-tile

Credit Card Dark Dashboard Tile: an example of credit-card.

pnpm dlx shadcn@latest add @01orbital/credit-card-dark-dashboard-tile

Editorial Split Credit Cardcredit-card-editorial-split

Editorial Split Credit Card: an example of credit-card.

pnpm dlx shadcn@latest add @01orbital/credit-card-editorial-split

Spend Limit Progress Cardcredit-card-spend-limit-progress

Spend Limit Progress Card: an example of credit-card.

pnpm dlx shadcn@latest add @01orbital/credit-card-spend-limit-progress

Wallet Primarycredit-card-wallet-primary

Credit Card Wallet Primary: an example of credit-card.

pnpm dlx shadcn@latest add @01orbital/credit-card-wallet-primary

Anatomy

Credit Card is built from 2 parts. In the example they nest like this:

CreditCardFront
CreditCardBack
  • CreditCardFront: The front of a payment card, drawn in SVG: wordmark, chip, number, cardholder and expiry. It is an image named with its printed text, at most 384px wide. Presentational: the defaults are fictional, and it does not collect, tokenize or store payment information.
  • CreditCardBack: The back of a payment card, drawn in SVG: magnetic stripe, signature strip with the security code, and wordmark. It is an image named with its printed text, at most 384px wide. Presentational, like CreditCardFront.

API reference

CreditCardFront

The front of a payment card, drawn in SVG: wordmark, chip, number, cardholder and expiry. It is an image named with its printed text, at most 384px wide. Presentational: the defaults are fictional, and it does not collect, tokenize or store payment information.

Prop

Type

It also accepts the HTML attributes of the element it renders.

CreditCardBack

The back of a payment card, drawn in SVG: magnetic stripe, signature strip with the security code, and wordmark. It is an image named with its printed text, at most 384px wide. Presentational, like CreditCardFront.

Prop

Type

It also accepts the HTML attributes of the element it renders.

Dependencies

npm: class-variance-authority, cn. The CLI installs them for you.

Last updated on