パッケージの詳細

react-svg-credit-card-payment-icons

marcovoliveira75kMIT5.1.2

React components library with 6 styles, 108 icons, TypeScript support, and a variety of types and formats for easy integration into React applications.

paypal, svg, paymenticons, mastercard

readme

React SVG Card Payment Icons

npm TypeScript ​npm​ PRs Welcome GitHub stars Contribute Buy Me A Coffee

SVG Credit Card & Payment Icons: 6 Styles, 80 Icons for React ⚛️

A collection of SVG based credit card logo icons. React component with Typescript support.

Live Demo

💿 Installation

  1. Install this package:
npm install react-svg-credit-card-payment-icons

or

yarn add react-svg-credit-card-payment-icons

or

pnpm add react-svg-credit-card-payment-icons

📦 Usage

Option 1: PaymentIcon Component

import { PaymentIcon } from 'react-svg-credit-card-payment-icons';

const App = () => {
  return <PaymentIcon type="Visa" format="flatRounded" width={100} />;
};

Note: The PaymentIcon component bundles all icons. For better tree-shaking and smaller bundle sizes, use Option 2-4.

Option 2: Unified Icon Components with Format and Variant Props

Import individual icon components that accept a format prop for dynamic style selection:

import { VisaIcon, MastercardIcon } from 'react-svg-credit-card-payment-icons';

const App = () => {
  return (
    <>
      <VisaIcon format="flatRounded" width={100} />
      <MastercardIcon format="logo" width={100} />
    </>
  );
};

Available formats: flat, flatRounded, logo, logoBorder, mono, monoOutline

Option 3: Format-Specific Icon Components (Recommended)

Import format-specific components for the smallest bundle size and best TypeScript IntelliSense:

import {
  VisaFlatRoundedIcon,
  MastercardLogoIcon,
} from 'react-svg-credit-card-payment-icons';

const App = () => {
  return (
    <>
      <VisaFlatRoundedIcon width={100} />
      <MastercardLogoIcon width={100} />
    </>
  );
};

Available component suffixes: FlatIcon, FlatRoundedIcon, LogoIcon, LogoBorderIcon, MonoIcon, MonoOutlineIcon

Option 4: Vendor-Specific Imports

Import all icon variants for a specific payment network:

import { VisaFlatIcon, VisaLogoIcon, VisaMonoIcon } from 'react-svg-credit-card-payment-icons/visa';
import { MastercardFlatRoundedIcon, MastercardLogoIcon } from 'react-svg-credit-card-payment-icons/mastercard';

const App = () => {
  return (
    <>
      <VisaFlatIcon width={100} />
      <MastercardFlatRoundedIcon width={100} />
    </>
  );
};

Option 5: Format-Specific Path Imports (Legacy)

Import from format-specific paths:

import {
  Visa as VisaIcon,
  Mastercard as MastercardIcon,
} from 'react-svg-credit-card-payment-icons/icons/flat-rounded';

const App = () => {
  return (
    <>
      <VisaIcon width={100} />
      <MastercardIcon width={100} />
    </>
  );
};

Available import paths:

  • react-svg-credit-card-payment-icons/icons/flat
  • react-svg-credit-card-payment-icons/icons/flat-rounded
  • react-svg-credit-card-payment-icons/icons/logo
  • react-svg-credit-card-payment-icons/icons/logo-border
  • react-svg-credit-card-payment-icons/icons/mono
  • react-svg-credit-card-payment-icons/icons/mono-outline

Card Variants and Aliases

Some payment cards have multiple visual styles or go by different names. The package supports both through variants and aliases:

Type Aliases - Alternative names for the same card:

<PaymentIcon type="Amex" />           // Resolves to AmericanExpress
<PaymentIcon type="CvvBack" />        // Resolves to Code (back CVV)
<PaymentIcon type="Diners" />         // Resolves to DinersClub

Variant Aliases - Different visual styles of the same card network:

// Method 1: Use the variant alias directly (recommended)
<PaymentIcon type="Hiper" format="flatRounded" />

// Method 2: Use explicit variant prop with base type
<PaymentIcon type="Hipercard" variant="hiper" format="flatRounded" />

The Hiper and Hipercard cards share the same IIN ranges but have distinct branding:

  • Hiper - Shows the Hiper-branded logo (orange/yellow colors)
  • Hipercard - Shows the Hipercard-branded logo

Format-Specific Components with Variants:

import { HipercardFlatRoundedIcon } from 'react-svg-credit-card-payment-icons';

// Default Hipercard branding
<HipercardFlatRoundedIcon width={80} />

// Hiper variant branding
<HipercardFlatRoundedIcon variant="Hiper" width={80} />

Direct Imports with Variants:

import { Hiper, Hipercard } from 'react-svg-credit-card-payment-icons/icons/flat-rounded';

<Hiper width={80} />      // Hiper-branded variant
<Hipercard width={80} />  // Hipercard-branded variant

Unified Icon Components with Variants:

import { HipercardIcon } from 'react-svg-credit-card-payment-icons';

<HipercardIcon format="flatRounded" width={80} />
<HipercardIcon format="logo" variant="Hiper" width={80} />

🔧 Card Utilities

As of version 4, the package includes powerful card detection and validation utilities:

import {
  getCardType,
  detectCardType, // deprecated - use getCardType instead
  validateCardNumber,
  formatCardNumber,
  maskCardNumber,
  isCardNumberPotentiallyValid,
} from 'react-svg-credit-card-payment-icons';

// Detect card type from number (recommended)
const cardType = getCardType('4242424242424242');  // Returns 'Visa'
const amexType = getCardType('378282246310005');   // Returns 'AmericanExpress'
const dinersType = getCardType('30569309025904');  // Returns 'DinersClub'

// Legacy function (deprecated but still works)
const legacyType = detectCardType('378282246310005'); // Returns 'Americanexpress'

// Validate card number using Luhn algorithm
const isValid = validateCardNumber('4242424242424242'); // Returns true

// Format card number with appropriate spacing
const formatted = formatCardNumber('4242424242424242'); // Returns '4242 4242 4242 4242'

// Mask card number (shows only last 4 digits)
const masked = maskCardNumber('4242424242424242'); // Returns '**** **** **** 4242'

// Check if card number is potentially valid (correct length, etc.)
const isPotentiallyValid = isCardNumberPotentiallyValid('4242424242424242'); // Returns true

Available Utility Functions

Function Description Example
getCardType(cardNumber) Detects card type from number getCardType('4242...') // 'Visa'
Returns canonical type names getCardType('3782...') // 'AmericanExpress'
detectCardType(cardNumber) (deprecated) Legacy card type detection detectCardType('3782...') // 'Americanexpress'
validateCardNumber(cardNumber) Validates using Luhn algorithm validateCardNumber('4242...') // true
formatCardNumber(cardNumber) Formats with appropriate spacing formatCardNumber('4242...') // '4242 4242 4242 4242'
maskCardNumber(cardNumber) Masks all but last 4 digits maskCardNumber('4242...') // '**** **** **** 4242'
isCardNumberPotentiallyValid(cardNumber) Checks if potentially valid isCardNumberPotentiallyValid('4242') // false
validateCardForType(cardNumber, type) Validates for specific card type validateCardForType('4242...', 'Visa') // true
getCardLengthRange(cardType) Gets min/max length for card type getCardLengthRange('Visa') // {min: 13, max: 19}
sanitizeCardNumber(cardNumber) Removes non-digit characters sanitizeCardNumber('4242-4242') // '42424242'

Complete Example with Card Input

import React, { useState } from 'react';
import {
  PaymentIcon,
  detectCardType,
  validateCardNumber,
  formatCardNumber,
} from 'react-svg-credit-card-payment-icons';

function CardInput() {
  const [cardNumber, setCardNumber] = useState('');
  const cardType = detectCardType(cardNumber);
  const isValid = validateCardNumber(cardNumber);

  return (
    <div>
      <input
        type="text"
        value={cardNumber}
        onChange={(e) => setCardNumber(e.target.value)}
        placeholder="Enter card number"
      />
      <PaymentIcon type={cardType} width={40} />
      <div>Type: {cardType}</div>
      <div>Valid: {isValid ? 'Yes' : 'No'}</div>
      <div>Formatted: {formatCardNumber(cardNumber)}</div>
    </div>
  );
}

Tree-Shakeable Example

For better bundle optimization, import only the cards you need:

import { Visa, Mastercard } from 'react-svg-credit-card-payment-icons/icons/flat-rounded';
import { detectCardType } from 'react-svg-credit-card-payment-icons';

function PaymentForm() {
  const cardType = detectCardType(cardNumber);

  return (
    <div>
      {cardType === 'Visa' && <Visa width={40} />}
      {cardType === 'Mastercard' && <Mastercard width={40} />}
    </div>
  );
}

Types and Formats

Available types and their images

If the type does not exist, the default setting is generic. Type names are case-insensitive but PascalCase is recommended.

Type Image
Alipay Alipay
AmericanExpress American Express
DinersClub Diners Club
Discover Discover
Elo Elo
Hiper Hiper
Hipercard Hipercard
Jcb JCB
Maestro Maestro
Mastercard Mastercard
Mir Mir
Paypal Paypal
Swish Swish
Unionpay Unionpay
Visa Visa
Generic Generic
Code Code
CodeFront Code Front

Images from aaronfagan/svg-credit-card-payment-icons

Available formats

If the format is not specified, the default setting is flat.

Format Image
flat Flat Mastercard
flatRounded Flat Rounded Mastercard
logo Logo Mastercard
logoBorder Logo Border Mastercard
mono Mono Mastercard
monoOutline Mono Outline Mastercard
  • Specify either width or height; there's no requirement to define both. The aspect ratio is preset at 780:500 for SVGs. If neither width nor height is defined, width will default to 40.

  • The component also allows all the properties (props) of the Svg component, including attributes like style.

  • If an invalid type is provided, the default setting is generic.

Contributing

Contributions are welcome! Please open an issue or submit a pull request on GitHub.

Development Setup

This project uses pnpm as its package manager for local development and CI/CD.

# Install pnpm if you don't have it
npm install -g pnpm

# Install dependencies
pnpm install

# Run linting
pnpm run lint

# Build the project
pnpm run build

更新履歴

react-svg-credit-card-payment-icons

Unreleased

5.1.0

Contributed by @codybrom.

Deprecations

  • detectCardType() is now deprecated in favor of the new getCardType() function:

    • detectCardType() continues to work and returns legacy v4.x type names ('Americanexpress', 'Diners', 'Unionpay', 'Paypal', 'Jcb')
    • New getCardType() function returns canonical type names ('AmericanExpress', 'DinersClub', 'UnionPay', 'PayPal', 'JCB')
    • Migration is optional - both functions will continue to work
    • Recommended migration:

      // v4/v5 (deprecated but still works)
      const type = detectCardType(cardNumber); // 'Americanexpress'
      
      // v5+ (recommended)
      const type = getCardType(cardNumber);    // 'AmericanExpress'
    • Component usage: <PaymentIcon type="..." /> accepts all type names and aliases (Amex, Diners, Americanexpress, AmericanExpress, etc.) for full compatibility

Major Changes

  • JSON Schema Validation for Card Metadata

    • Added JSON schema (schemas/card-metadata.schema.json) for YAML configuration validation
    • VS Code integration provides autocomplete and validation for card YAML files
    • Ensures correct field types and required properties
    • Helpful descriptions and examples for each field
  • Card Variant Support

    • Added support for card variants through YAML configuration
    • Variant aliases automatically resolve to the correct visual style
    • Example: <PaymentIcon type="Hiper" /> automatically uses the Hiper-branded variant
    • Explicit variant prop also supported: <PaymentIcon type="Hipercard" variant="hiper" />
    • Variants can have their own display names, authors, licenses, and aliases
    • Hiper and Hipercard variants now properly distinguished:
      • Hiper - Orange/yellow branded Hiper logo
      • Hipercard - Standard Hipercard branding
      • Both Hiper variants share the same IIN ranges but have distinct visual identities
    • Code and CodeFront are now variants of Generic:
      • Generic - Standard generic card front
      • Code (aliases: Cvv, Cvc) - Generic card back with security code
      • CodeFront (aliases: CvvFront, CvcFront) - Generic card front with security code
    • Tree-shakeable variant imports: import { Hiper, Hipercard, Code, CodeFront } from 'react-svg-credit-card-payment-icons/icons/flat-rounded'
  • Unified Icon Components with Dynamic Format Selection

    • Added individual icon components with format and variant props: <VisaIcon format="flatRounded" variant="hiper" />
    • All card types also available as dedicated components with "Icon" suffix for clearer semantics
    • Full TypeScript IntelliSense support with per-vendor format type unions
    • Still backward compatible with existing PaymentIcon component
  • Format-Specific Icon Components

    • Added format-specific components for all card types and formats for smallest possible bundle size (only includes the specific format needed)
    • Examples: VisaFlatIcon, VisaFlatRoundedIcon, VisaLogoIcon, MastercardMonoIcon
    • Supports Variant props on format-specific components: <HipercardFlatRoundedIcon variant="Hiper" />
  • Vendor-Specific Package Exports

    • Added vendor-specific import paths for all payment networks
    • No configuration import for each format for a specific vendor: import { VisaFlatIcon, VisaLogoIcon } from 'react-svg-credit-card-payment-icons/visa'
  • Fully Dynamic Build System

    • No hardcoding of formats or vendors throughout the system
    • Formats automatically discovered from filesystem during build
    • New formats can be added by simply adding SVG files
    • Dynamic tsup entry point generation
    • Tests automatically adapt to new formats without code changes

Minor Changes

  • Storybook v10 Upgrade (PR #1)
    • Upgraded Storybook from v9.1.10 to v10.0.4
    • ESM-only distribution with 29% smaller installation size
    • Migrated from renderer-based to framework-based configuration
    • Added eslint-plugin-storybook for enhanced linting
    • Updated TypeScript configuration to support ESM modules (moduleResolution: bundler)
    • Improved documentation and component stories

Patch Changes

  • Fixed component resolution logic to properly handle variant aliases
  • Fixed test failures in Storybook interaction tests for Hiper color validation
  • Updated icon paths in documentation to reflect new hipercard folder structure
  • Enhanced generator to use YAML type field for component naming, allowing precise control over casing

5.0.0

@codybrom contributed a major refactor to the library, improving architecture, developer experience, and bundle optimization.

Major Changes

  • Architecture Refactor (PR #18)

    • Complete modernization of library architecture
    • Vendor-based icon organization for easier maintenance
    • Build-time component generation using @svgr/core using pre-hooks
    • Removed ~200 files of generalizable components from version control
    • Fixed improper vendor detection (#16) with a new longest-match algorithm for handling overlapping IIN ranges
    • Added package subpath exports for tree-shakable bundles
      • Import specific icons: import { Visa } from 'react-svg-credit-card-payment-icons/icons/flat'
      • Reduces bundle size from ~700KB to ~5-15KB when importing individual icons
      • Available paths: /icons/flat, /icons/flat-rounded, /icons/logo, /icons/logo-border, /icons/mono, /icons/mono-outline
    • Added comprehensive Jest unit tests for card utilities and Vitest integration for Storybook component tests
    • Added ESLint 9 with a modern flat configuration
    • Standardized package manager to pnpm (#12)
  • React 19 Compatibility (PR #13)

    • Full support for React 19 with proper camelCase SVG attribute handling (#11)
    • Fixed SVG attribute naming to use React 19-compatible format
    • Fixed CSS properties in style tags (hyphenated format for CSS, camelCase for JSX attributes)
  • Storybook v9 Upgrade (PR #15)

    • Upgraded Storybook to v9 with Vite
    • Modernized Storybook deployment workflow (#14)
    • Improved documentation and component stories

4.2.1

Patch Changes

  • fix: add utils export for card utility functions

4.2.0

Minor Changes

  • Add Vite compatibility

4.1.0

Minor Changes

  • 3f4fd3b: add Swish payment provider

3.1.1

Patch Changes

  • update dependencies

3.1.0

Minor Changes

  • update readme

3.0.0

Major Changes

  • Thanks to Adrian Rubio we have mastercard, maestro and visa new cards following brand guidelines, also added code front, code and generic to logo-border and logo types. PaymentTypesExtended is deprecated and you should use PaymentTypes type that contains the same set of types.

2.1.3

Patch Changes

  • update readme

2.1.2

Patch Changes

  • fix docs add string formatting

2.1.1

Patch Changes

  • storybook fix

2.1.0

Minor Changes

  • 87db82a: Update svg
  • Storbook demo app

2.0.0

Major Changes

  • 5b5c788: SVG generation

Minor Changes

  • 44c93de: SVG build

1.0.1

Patch Changes

  • df73860: Update github jobs
  • 8a0f7d9: readme update
  • d04521e: versioning