# Wallet

A customer's stored balance, shown as a leather wallet. Saved cards sit in it, and picking one pulls it out of the pocket.

- Category: Money
- Built from: Payment card, Amount
- Has parts that work alone: Payment card
- Docs: https://bevel-ui.vercel.app/components/wallet
- Import from: `@/components/ui/wallet`

## Install

```bash
npx shadcn@latest add @bevel/wallet
```

npm dependencies (installed by the CLI): `@base-ui/react`, `cn`.

## Anatomy

```tsx
<Wallet>
  <WalletCards>
    <WalletCard>
      <PaymentCardBrand />
      <PaymentCardNumber />
    </WalletCard>
  </WalletCards>
  <WalletPocket>
    <WalletHeader>
      <WalletLabel />
      <WalletStatus />
    </WalletHeader>
    <WalletBalance />
    <WalletSeparator />
    <WalletDetails>
      <WalletDetail>
        <WalletDetailLabel />
        <WalletDetailValue>
          <WalletAmount />
        </WalletDetailValue>
      </WalletDetail>
    </WalletDetails>
    <WalletMeter>
      <WalletMeterLabel />
      <WalletMeterValue />
    </WalletMeter>
    <WalletFooter>
      <WalletAction />
    </WalletFooter>
  </WalletPocket>
</Wallet>
```

## Parts

| Part | Renders | Description |
| --- | --- | --- |
| `Wallet` | div | The leather panel. variant picks the finish (saddle, noir, oxblood); status sets the light and meter colour. Takes render to become a button or label. |
| `WalletCards` | div or RadioGroup | Holds any number of cards. Pass value, defaultValue or onValueChange to make them a radio group; the chosen card is pulled out. |
| `WalletCard` | PaymentCard | A PaymentCard placed in the stack, so it takes the same parts and tone. value identifies it when the cards are selectable. Cards added after mount slide in. |
| `WalletPocket` | div | The stitched front pocket that holds everything else. |
| `WalletStatus` | span | A status light with text from the wallet's status. Children replace the text. |
| `WalletBalance` | p | The amount, stamped into the leather. value, currency (ISO 4217, omit for plain numbers), locale and format. |
| `WalletAmount` | span | An inline amount with the same formatting, for detail rows. |
| `WalletDetails` | dl | Label and value rows: WalletDetail, WalletDetailLabel (dt), WalletDetailValue (dd). |
| `WalletMeter` | Meter | A groove that fills with the remaining amount. value, min and max, plus label and value parts. |
| `WalletAction` | Button | A raised leather tab that sinks when pressed. |

## Example: Checkout

Pay from stored balance. Pay once to drain it, then pick the card that covers the rest.

```tsx
"use client"

import * as React from "react"
import {
  Wallet,
  WalletAction,
  WalletAmount,
  WalletBalance,
  WalletCard,
  WalletCards,
  WalletDetail,
  WalletDetailLabel,
  WalletDetails,
  WalletDetailValue,
  WalletFooter,
  WalletHeader,
  WalletLabel,
  WalletPocket,
  WalletSeparator,
  WalletStatus,
} from "@/components/ui/wallet"
import { PaymentCardBrand, PaymentCardNumber } from "@/components/ui/payment-card"

const cards = [
  { id: "amex-1005", brand: "Amex", last4: "1005" },
  { id: "debit-8210", brand: "Debit", last4: "8210" },
  { id: "visa-4242", brand: "Visa", last4: "4242" },
]

const total = 86.4

export function WalletCheckout() {
  const [balance, setBalance] = React.useState(124.5)
  const [cardId, setCardId] = React.useState("visa-4242")
  const card = cards.find((c) => c.id === cardId)!
  const covers = balance >= total
  const remainder = Math.abs(balance - total)

  return (
    <Wallet status={covers ? "default" : "insufficient"}>
      <WalletCards value={cardId} onValueChange={setCardId}>
        {cards.map((c) => (
          <WalletCard key={c.id} value={c.id}>
            <PaymentCardBrand>{c.brand}</PaymentCardBrand>
            <PaymentCardNumber last4={c.last4} />
          </WalletCard>
        ))}
      </WalletCards>
      <WalletPocket>
        <WalletHeader>
          <WalletLabel>Pay with balance</WalletLabel>
          <WalletStatus />
        </WalletHeader>
        <WalletBalance value={balance} currency="USD" />
        <WalletSeparator />
        <WalletDetails>
          <WalletDetail>
            <WalletDetailLabel>Order total</WalletDetailLabel>
            <WalletDetailValue>
              <WalletAmount value={total} currency="USD" />
            </WalletDetailValue>
          </WalletDetail>
          <WalletDetail>
            <WalletDetailLabel>
              {covers ? "Left after payment" : `Rest on ${card.brand} ${card.last4}`}
            </WalletDetailLabel>
            <WalletDetailValue>
              <WalletAmount value={remainder} currency="USD" />
            </WalletDetailValue>
          </WalletDetail>
        </WalletDetails>
        <WalletFooter>
          {covers ? (
            <WalletAction onClick={() => setBalance((b) => +(b - total).toFixed(2))}>
              Pay $86.40
            </WalletAction>
          ) : (
            <WalletAction onClick={() => setBalance(124.5)}>
              Pay ${remainder.toFixed(2)} with •••• {card.last4}
            </WalletAction>
          )}
        </WalletFooter>
      </WalletPocket>
    </Wallet>
  )
}
```

## Example: Billing

Account credit and saved cards. Add a card and it slides into the wallet.

```tsx
"use client"

import * as React from "react"
import { History, Plus } from "lucide-react"
import {
  Wallet,
  WalletAction,
  WalletAmount,
  WalletBalance,
  WalletCard,
  WalletCards,
  WalletDetail,
  WalletDetailLabel,
  WalletDetails,
  WalletDetailValue,
  WalletFooter,
  WalletHeader,
  WalletLabel,
  WalletPocket,
  WalletStatus,
} from "@/components/ui/wallet"
import { PaymentCardBrand, PaymentCardNumber } from "@/components/ui/payment-card"

type SavedCard = { id: string; brand: string; last4: string }

export function WalletBilling() {
  const [cards, setCards] = React.useState<SavedCard[]>([
    { id: "amex-1005", brand: "Amex", last4: "1005" },
    { id: "debit-8210", brand: "Debit", last4: "8210" },
  ])

  function addCard() {
    const last4 = String(1000 + Math.floor(Math.random() * 9000))
    setCards((c) => [...c, { id: `visa-${last4}`, brand: "Visa", last4 }])
  }

  return (
    <Wallet variant="noir">
      <WalletCards>
        {cards.map((c) => (
          <WalletCard key={c.id}>
            <PaymentCardBrand>{c.brand}</PaymentCardBrand>
            <PaymentCardNumber last4={c.last4} />
          </WalletCard>
        ))}
      </WalletCards>
      <WalletPocket>
        <WalletHeader>
          <WalletLabel>Account credit</WalletLabel>
          <WalletStatus />
        </WalletHeader>
        <WalletBalance value={1240} currency="USD" />
        <WalletDetails>
          <WalletDetail>
            <WalletDetailLabel>Next invoice, Nov 1</WalletDetailLabel>
            <WalletDetailValue>
              <WalletAmount value={320} currency="USD" />
            </WalletDetailValue>
          </WalletDetail>
          <WalletDetail>
            <WalletDetailLabel>Auto top-up below</WalletDetailLabel>
            <WalletDetailValue>
              <WalletAmount value={100} currency="USD" format={{ maximumFractionDigits: 0 }} />
            </WalletDetailValue>
          </WalletDetail>
        </WalletDetails>
        <WalletFooter>
          <WalletAction onClick={addCard} disabled={cards.length >= 5}>
            <Plus /> Add card
          </WalletAction>
          <WalletAction>
            <History /> History
          </WalletAction>
        </WalletFooter>
      </WalletPocket>
    </Wallet>
  )
}
```

## Example: Usage

Prepaid credits running down over a billing cycle. Low balance turns the light and meter amber.

```tsx
"use client"

import { ArrowUpRight } from "lucide-react"
import {
  Wallet,
  WalletAction,
  WalletBalance,
  WalletCard,
  WalletCards,
  WalletFooter,
  WalletHeader,
  WalletLabel,
  WalletMeter,
  WalletMeterLabel,
  WalletMeterValue,
  WalletPocket,
  WalletStatus,
} from "@/components/ui/wallet"

const allowance = 50000
const remaining = 9480

export function WalletUsage() {
  return (
    <Wallet variant="oxblood" status={remaining / allowance < 0.25 ? "low" : "default"}>
      <WalletCards>
        <WalletCard />
        <WalletCard />
      </WalletCards>
      <WalletPocket>
        <WalletHeader>
          <WalletLabel>Credits remaining</WalletLabel>
          <WalletStatus />
        </WalletHeader>
        <WalletBalance value={remaining} />
        <WalletMeter value={remaining} max={allowance}>
          <WalletMeterLabel>Of 50,000 this cycle</WalletMeterLabel>
          <WalletMeterValue>
            {(_, value) => `${Math.round((value / allowance) * 100)}%`}
          </WalletMeterValue>
        </WalletMeter>
        <WalletFooter>
          <WalletAction>
            Buy credits <ArrowUpRight />
          </WalletAction>
        </WalletFooter>
      </WalletPocket>
    </Wallet>
  )
}
```
