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.
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.
Installation
Usage
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.
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.
| Prop | Type | Default | Description |
|---|---|---|---|
wallet | Wallet | – | How much of each currency the player has, keyed by currency id: `{ coin: 1250, gem: 40 }`. Required. |
currencies | Record<string, Currency> | DEFAULT_CURRENCIES | Currency definitions `{ label, icon?, color? }` by id. `icon` is a node or an image URL; `color` tints the cap bar. |
show | string[] | keys of wallet | Which 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. |
caps | Record<string, number> | – | Upper limit per currency, e.g. `{ energy: 60 }`. Shows "35 / 60" and a thin fill bar. |
highlight / onHighlightEnd | string | null / (id) => void | null | The 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. |
compact | boolean | false | Short 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. |
labels | Partial<CurrencyBarLabels> | English | Strings with `{currency}`, `{value}`, `{max}`, `{delta}` placeholders: label, add, amount, amountOfMax, gained, lost. |