Currency Bar

The HUD wallet: one chip per currency with its icon and amount. When an amount changes it counts up or down and a small "+120" or "−60" floats off the chip. Chips can show a cap with a fill bar (energy), a "+" button, a clickable state, and a shake with a red face for "not enough coins". Screen readers hear one polite message per change, not every frame. The game owns the wallet; this only draws it.

View as Markdown

Earn and spend to see the count animation and the floating delta. Energy has a cap of 60. "Buy something too expensive" sets highlight to the coin chip: it shakes once and turns red, then onHighlightEnd clears it.

Coins: 1,250
Gems: 40
Energy: 35 of 60

Installation

npx shadcn@latest add @cozy/currency-bar

Usage

import { CurrencyBar } from "@/components/ui/currency-bar"

const [wallet, setWallet] = useState({ coin: 1250, gem: 40, energy: 35 })

<CurrencyBar
  wallet={wallet}
  caps={{ energy: 60 }}
  onAdd={(id) => openShop(id)}
  highlight={notEnough ? "coin" : null}
  onHighlightEnd={() => setNotEnough(false)}
/>

The number counts for about 0.65 s and jumps straight to the new value under prefers-reduced-motion; the delta then stays still for a moment instead of rising.

Screen readers read a static text per chip ("Coins: 1,250", "Energy: 35 of 60") and get one announcement after changes settle ("Coins up 120, now 1,370"). Rapid changes are merged into one net announcement.

Each chip is a plain group; it becomes a button only when onCurrencySelect is set. The "+" button has a 40 px touch target.

Examples

Sizes, compact and vertical

size is "sm", "md" or "lg". compact shortens big numbers (1.3M, 4.8K). show picks currencies and their order. orientation="vertical" stacks the chips, handy for a side HUD.

Coins: 1,250,000
Gems: 4,820
Energy: 60 of 60
Coins: 1,250,000
Gems: 4,820
Energy: 60
Gems: 4,820
Coins: 1,250,000
Coins: 1,250,000
Gems: 4,820
Energy: 60

Clickable chips

onCurrencySelect turns each chip into a button. addable limits the "+" button to some currencies.

Chips are buttons here. Only gems have a + button.

API

Everything else is passed through to the underlying element or Radix primitive.

PropTypeDefaultDescription
walletWallet–How much of each currency the player has, keyed by currency id: `{ coin: 1250, gem: 40 }`. Required.
currenciesRecord<string, Currency>DEFAULT_CURRENCIESCurrency definitions `{ label, icon?, color? }` by id. `icon` is a node or an image URL; `color` tints the cap bar.
showstring[]keys of walletWhich currencies to show and in what order. Default: the keys of `wallet` that exist in `currencies`.
onAdd / addable(id) => void / string[]–With `onAdd` every chip gets a "+" button (accessible name "Get more Coins"); `addable` limits it to some ids.
capsRecord<string, number>–Upper limit per currency, e.g. `{ energy: 60 }`. Shows "35 / 60" and a thin fill bar.
highlight / onHighlightEndstring | null / (id) => voidnullThe chip shakes and turns red once each time `highlight` changes to a currency id. Set it back to null in `onHighlightEnd` so the same id can fire again. Under reduced motion it only turns red.
compactbooleanfalseShort numbers: 12500 shows as "12.5K" (from 10,000 up).
size"sm" | "md" | "lg""md"Chip size.
orientation"horizontal" | "vertical""horizontal"Chips in a row (wrapping) or a column.
onCurrencySelect(id) => void–Makes each chip a button, e.g. to open the shop at that currency.
labelsPartial<CurrencyBarLabels>EnglishStrings with `{currency}`, `{value}`, `{max}`, `{delta}` placeholders: label, add, amount, amountOfMax, gained, lost.