Components
Wallet
Cards standing in a leather pocket, each showing its top edge: a name and a number. Press a card to bring it up.
Introduction
Wallet shows a set of things that each have a name and a number, such as accounts and their balances, as cards standing in the pocket of a wallet. Only the top edge of each card shows, with its name and its number, and the front of the wallet has a line and a number of its own, such as a total. Press a card and it comes up out of the pocket.
- Any number of cards, painted with any CSS background: a color, a theme token, or a gradient. The back cards are a little narrower, so the wallet has depth.
- The leather has a grain, a raised middle and stitching, and is dark in the light and in the dark theme alike.
- The text on a card is chosen by how light the card is, and again when the theme changes, so a card painted with a token stays readable in both themes. Set
toneto choose it yourself. - The part of a number after the point is written fainter, as in the balance of a wallet app.
- The cards are buttons with their state, so it works with the keyboard and screen readers, and the movement stops for people who prefer reduced motion.
Use it for the tokens of a theme, as on this site's Theming page, for accounts, balances or plans. It is a Client Component.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# bun
bun add tailwind-variants tailwind-merge clsx
# pnpm
pnpm add tailwind-variants tailwind-merge clsx
# npm
npm install tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/wallet-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Wallet. Where each card sits (its top, its inset, how far it is
// lifted) is set from numbers in wallet.tsx as inline styles, because it depends
// on how many cards there are. What is here is the look: the leather, the
// stitching, the cards and their text.
export const walletStyles = tv({
slots: {
root: "relative isolate w-full select-none text-white",
// The back of the wallet, behind the cards.
back: "absolute inset-x-0 bottom-0 rounded-[2.25rem] bg-[#232427] shadow-[0_18px_40px_-12px_rgb(0_0_0/0.5)]",
card: [
"group/card absolute flex cursor-pointer items-start justify-between gap-4 overflow-hidden rounded-2xl px-5 text-left outline-none",
"shadow-[0_-1px_0_rgb(255_255_255/0.25)_inset,0_6px_16px_-6px_rgb(0_0_0/0.45)]",
"transition-transform duration-300 ease-[cubic-bezier(0.22,1,0.36,1)] motion-reduce:transition-none",
"focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-[#232427]",
"data-[static=true]:cursor-default",
],
cardTitle: "truncate font-semibold tracking-tight",
cardValue: "shrink-0 font-semibold tracking-tight tabular-nums",
// The front of the wallet, in front of the bottom of the cards.
front: "pointer-events-none absolute inset-x-0 bottom-0",
leather:
"absolute inset-0 bg-[#2a2b2e] shadow-[0_-2px_0_rgb(255_255_255/0.06)_inset,0_10px_30px_-10px_rgb(0_0_0/0.6)]",
stitch:
"absolute rounded-[inherit] border-[1.5px] border-dashed border-white/20",
label: "m-0 text-sm text-white/55",
value: "m-0 font-semibold tracking-tight tabular-nums",
decimals: "text-white/45",
},
variants: {
size: {
sm: {
card: "text-base",
cardTitle: "text-base",
cardValue: "text-base",
label: "text-xs",
value: "text-2xl",
},
md: {
card: "text-lg",
cardTitle: "text-lg",
cardValue: "text-xl",
label: "text-sm",
value: "text-3xl",
},
lg: {
card: "text-xl",
cardTitle: "text-xl",
cardValue: "text-2xl",
label: "text-base",
value: "text-4xl",
},
},
tone: {
light: { card: "text-white" },
dark: { card: "text-black" },
},
},
defaultVariants: { size: "md" },
});
/** The measures of the wallet, in pixels, for each size. */
export const walletSizes = {
// peek: how much of each card shows above the next. pocket: the height of the
// front. lift: how far a chosen card comes up, and the room kept above for it.
sm: { peek: 34, pocket: 128, lift: 40, pad: 16 },
md: { peek: 44, pocket: 168, lift: 52, pad: 20 },
lg: { peek: 56, pocket: 212, lift: 64, pad: 24 },
} as const;
/** A fine grain, as an image, for the leather and the cards. */
export const grain =
"url(\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 .5 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E\")";2. Add the Wallet file
src/components/ui/wallet/wallet.tsxShow code
"use client";
import {
type CSSProperties,
type ReactNode,
useEffect,
useRef,
useState,
} from "react";
import { cn } from "@/lib/cn";
import { grain, walletSizes, walletStyles } from "@/types/wallet-styles";
export interface WalletCard {
/** Names the card, at the left of its top edge. */
title: ReactNode;
/** A number or a word at the right of the top edge, such as `"$2,260.99"`. */
value?: string;
/**
* What the card is painted with: any CSS background, such as a color
* (`"#3b82f6"`), a theme token (`"var(--primary)"`), or a gradient
* (`"linear-gradient(135deg, #f97316, #ec4899)"`).
*/
background: string;
/**
* The color of the text on the card: `light` (white) on a dark card, `dark`
* (black) on a light one, or `auto` to choose by how light the card is. `auto`
* works for a solid color and follows the theme, so it suits a token; a
* gradient gets `light`. Defaults to `auto`.
*/
tone?: "light" | "dark" | "auto";
/** A stable name for the card. Defaults to its position. */
id?: string;
}
export interface WalletProps {
/** The cards, the first at the back and the last at the front. */
cards: WalletCard[];
/** The small line in the front of the wallet, such as "Wallet Balance". */
title?: ReactNode;
/** The big number in the front of the wallet, such as `"$240.95"`. */
value?: string;
/** The part of a number after the point is written fainter, as in the front and the cards. Defaults to `true`. */
fadeDecimals?: boolean;
/** Which card is brought up, by its `id` or its position, or `null` for none. For a controlled wallet. */
selected?: string | null;
/** The card to bring up first, for an uncontrolled wallet. */
defaultSelected?: string | null;
/** Called when a card is brought up or put back, with its `id`, or `null`. */
onSelectedChange?: (id: string | null) => void;
/** Cards can be pressed to bring them up. Turn off for a wallet that only shows. Defaults to `true`. */
isInteractive?: boolean;
size?: "sm" | "md" | "lg";
/** Names the wallet for screen readers, such as "Theme colors". Defaults to the title. */
"aria-label"?: string;
className?: string;
}
// "$2,260.99" is written as "$2,260" and ".99", so the cents can be fainter.
function Amount({
text,
fade,
decimalsClass,
}: {
text: string;
fade: boolean;
decimalsClass: string;
}) {
const match = /^(.*?\d)([.,]\d+)(\D*)$/.exec(text);
if (!fade || !match) return <>{text}</>;
return (
<>
{match[1]}
<span className={decimalsClass}>
{match[2]}
{match[3]}
</span>
</>
);
}
// The color a browser paints for a CSS color, as numbers: one pixel is drawn and
// read back, which works for any color it understands and gives sRGB.
function paintedRgb(css: string): [number, number, number] | null {
const canvas = document.createElement("canvas");
canvas.width = 1;
canvas.height = 1;
const context = canvas.getContext("2d", { willReadFrequently: true });
if (!context) return null;
// The leather is dark in both themes, so a translucent card is read as it is
// seen: over the leather, not over nothing.
context.fillStyle = "#1c1c1e";
context.fillRect(0, 0, 1, 1);
context.fillStyle = css;
context.fillRect(0, 0, 1, 1);
const [r, g, b] = context.getImageData(0, 0, 1, 1).data;
return [r, g, b];
}
// Chooses the text color of a card from the color it has now, and again when the
// theme changes: a token can be light in one theme and dark in the other.
function useAutoTone(enabled: boolean) {
const ref = useRef<HTMLButtonElement | HTMLDivElement>(null);
const [tone, setTone] = useState<"light" | "dark">("light");
useEffect(() => {
const element = ref.current;
if (!enabled || !element) return;
const update = () => {
const style = getComputedStyle(element);
// A gradient or an image has no single color to read.
if (style.backgroundImage !== "none") return setTone("light");
const rgb = paintedRgb(style.backgroundColor);
if (!rgb) return;
const brightness =
(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]) / 255;
setTone(brightness > 0.6 ? "dark" : "light");
};
update();
const observer = new MutationObserver(update);
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ["class", "style"],
});
const media = window.matchMedia("(prefers-color-scheme: dark)");
media.addEventListener("change", update);
return () => {
observer.disconnect();
media.removeEventListener("change", update);
};
}, [enabled]);
return { ref, tone };
}
function Card({
card,
index,
count,
isPicked,
isInteractive,
size,
fade,
onPress,
}: {
card: WalletCard;
index: number;
count: number;
isPicked: boolean;
isInteractive: boolean;
size: "sm" | "md" | "lg";
fade: boolean;
onPress: () => void;
}) {
const styles = walletStyles({ size });
const measure = walletSizes[size];
const wanted = card.tone ?? "auto";
const { ref, tone: auto } = useAutoTone(wanted === "auto");
const tone = wanted === "auto" ? auto : wanted;
// The cards at the back are a little narrower, so the wallet has depth.
const inset = measure.pad + (count - 1 - index) * 5;
const top = measure.lift + index * measure.peek;
const style: CSSProperties = {
top,
bottom: 26,
left: inset,
right: inset,
zIndex: index + 1 + (isPicked ? count : 0),
paddingTop: Math.round(measure.peek * 0.24),
background: card.background,
transform: isPicked ? `translateY(${-measure.lift}px)` : undefined,
};
const inner = (
<>
{/* The grain, over the color, so a card has a surface. */}
<span
aria-hidden="true"
className="pointer-events-none absolute inset-0 opacity-[0.22] mix-blend-overlay"
style={{ backgroundImage: grain }}
/>
<span className={cn(styles.cardTitle(), "relative")}>{card.title}</span>
{card.value ? (
<span className={cn(styles.cardValue(), "relative")}>
<Amount
text={card.value}
fade={fade}
decimalsClass={tone === "dark" ? "opacity-55" : "opacity-60"}
/>
</span>
) : null}
</>
);
const className = cn(styles.card(), walletStyles({ size, tone }).card());
if (!isInteractive) {
return (
<div
ref={ref as React.RefObject<HTMLDivElement>}
data-static="true"
className={className}
style={style}
>
{inner}
</div>
);
}
const name =
typeof card.title === "string"
? `${card.title}${card.value ? `, ${card.value}` : ""}`
: undefined;
return (
<button
ref={ref as React.RefObject<HTMLButtonElement>}
type="button"
aria-pressed={isPicked}
aria-label={name}
onClick={onPress}
className={cn(
className,
// Pointing at a card raises it a little, and not one that is picked.
!isPicked && "hover:-translate-y-2 focus-visible:-translate-y-2",
)}
style={style}
>
{inner}
</button>
);
}
/**
* A wallet with cards standing in its pocket, each showing only its top edge: a
* name and a number. Use it to show a set of things that each have a name and a
* value, such as accounts and their balances, or the colors of a theme and their
* codes, in a way that is more to look at than a list. The front of the wallet
* has a small line and a big number of its own, such as a total.
*
* Pressing a card brings it up out of the pocket, and pressing it again puts it
* back. It is a set of buttons with their state (`aria-pressed`), so it works
* with the keyboard and screen readers, and the movement stops for people who
* prefer reduced motion. The leather is dark in light and in dark themes alike.
* The text of each card follows how light the card is, unless you set it.
*
* @example
* <Wallet
* title="Wallet Balance"
* value="$240.95"
* cards={[
* { title: "Ethereum", value: "$2,260.99", background: "#3b82f6" },
* { title: "Bitcoin", value: "$42,80.24", background: "linear-gradient(135deg,#f97316,#ec4899)" },
* ]}
* />
*/
export function Wallet({
cards,
title,
value,
fadeDecimals = true,
selected,
defaultSelected = null,
onSelectedChange,
isInteractive = true,
size = "md",
"aria-label": ariaLabel,
className,
}: WalletProps) {
const styles = walletStyles({ size });
const measure = walletSizes[size];
const [inner, setInner] = useState<string | null>(defaultSelected);
const picked = selected !== undefined ? selected : inner;
const count = cards.length;
const ids = cards.map((card, i) => card.id ?? String(i));
// Where the front starts, and how tall the whole wallet is.
const frontTop =
measure.lift +
Math.max(count - 1, 0) * measure.peek +
Math.round(measure.peek * 0.92);
const height = frontTop + measure.pocket;
const tabHeight = measure.pocket - 30;
function press(id: string) {
const next = picked === id ? null : id;
if (selected === undefined) setInner(next);
onSelectedChange?.(next);
}
return (
// biome-ignore lint/a11y/useSemanticElements: a group of buttons, not a set of form fields
<div
role="group"
aria-label={ariaLabel ?? (typeof title === "string" ? title : "Wallet")}
className={cn(styles.root(), className)}
style={{ height }}
>
<div
aria-hidden="true"
className={styles.back()}
style={{
top: measure.lift + measure.peek - 4,
backgroundImage: `linear-gradient(180deg, rgb(255 255 255 / 0.05), transparent 40%), ${grain}`,
backgroundBlendMode: "overlay, soft-light",
}}
/>
{cards.map((card, i) => (
<Card
key={ids[i]}
card={card}
index={i}
count={count}
isPicked={picked === ids[i]}
isInteractive={isInteractive}
size={size}
fade={fadeDecimals}
onPress={() => press(ids[i])}
/>
))}
<div
className={styles.front()}
style={{ top: frontTop, zIndex: count * 2 + 2 }}
>
{/* The two shoulders, and between them the raised middle of the pocket. */}
<div
className={cn(
styles.leather(),
"rounded-b-[2.25rem] rounded-t-[2rem]",
)}
style={{
top: 26,
backgroundImage: grain,
backgroundBlendMode: "soft-light",
}}
/>
<div
className={cn(styles.leather(), "rounded-t-[1.75rem]")}
style={{
top: 0,
left: "9%",
right: "9%",
bottom: "auto",
height: tabHeight,
backgroundImage: grain,
backgroundBlendMode: "soft-light",
}}
/>
<div
aria-hidden="true"
className={cn(
styles.stitch(),
"rounded-b-[1.75rem] rounded-t-[1.25rem]",
)}
style={{ top: 44, left: 12, right: 12, bottom: 12 }}
/>
<div
className="absolute flex flex-col gap-1"
style={{ left: 28, bottom: 30 }}
>
{title ? <p className={styles.label()}>{title}</p> : null}
{value ? (
<p className={styles.value()}>
<Amount
text={value}
fade={fadeDecimals}
decimalsClass={styles.decimals()}
/>
</p>
) : null}
</div>
</div>
</div>
);
}3. Use it
import { Wallet } from "@/components/ui/wallet/wallet";Anatomy
A back, cards in front of it, and the front of the pocket over their bottoms.
<Wallet
title="Wallet Balance"
value="$240.95"
cards={[
{ title: "Ethereum", value: "$2,260.99", background: "linear-gradient(180deg, #4f8cff, #2f6bf0)", tone: "light" },
{ title: "WorldCoin", value: "$5,020.35", background: "linear-gradient(180deg, #d9dce1, #b9bec7)", tone: "dark" },
{ title: "Bitcoin", value: "$42,800.24", background: "linear-gradient(135deg, #ff9a3c, #ff4d6d)", tone: "light" },
]}
/>| Part | Rendered as | Notes |
|---|---|---|
| Back | div | The dark back of the wallet, wider than the cards, so it shows at their sides. |
| Card | button | One per item, showing only its top edge: a name at the left and a number at the right. The first is at the back and the last at the front. |
| Front | div | The pocket, with its raised middle, its stitching and its grain, over the bottom of the cards. It holds a small line and a big number of its own. |
Keyboard
Each card is a button.
| Key | What it does |
|---|---|
| Tab | Moves to the next card, from the back to the front. |
| Enter or Space | Brings the card up out of the pocket, or puts it back. |
Examples
Usage
Point at a card, and it rises a little. Press it to bring it up.
Wallet Balance
$240.95
Theme tokens
Cards painted with the tokens of the theme. Their text is chosen by how light each color is, so it reads in the light theme and in the dark one.
Theme colors
5 tokens
Controlled
Own which card is up with selected and onSelectedChange. The id of a card is its id, or its position.
Wallet Balance
$240.95
picked: "btc"
Text color
tone is the color of the text on a card: light (white), dark (black), or auto, which reads the color of the card. Auto works for a solid color or a token, and a gradient gets light.
{ background: "#22c55e", tone: "light" } // white text
{ background: "#fde68a", tone: "dark" } // black text
{ background: "var(--primary)" } // auto: by how light the color is nowSizes
Balance
$240.95
Balance
$240.95
Balance
$240.95
Only to look at
isInteractive off makes the cards plain elements that cannot be pressed.
Wallet Balance
$240.95
Decimals
fadeDecimals writes the part after the point fainter. Turn it off to write the whole number the same.
Wallet Balance
$240.95
Component API
Wallet takes the props below, and each item of cards takes the second table.
| Prop | Type | Default |
|---|---|---|
| cards | WalletCard[] | - |
| title | ReactNode | - |
| value | string | - |
| fadeDecimals | boolean | true |
| selected | string | null | - |
| defaultSelected | string | null | null |
| onSelectedChange | (id: string | null) => void | - |
| isInteractive | boolean | true |
| size | "sm" | "md" | "lg" | "md" |
| aria-label | string | the title |
| className | string | - |
WalletCard
| Prop | Type | Default |
|---|---|---|
| title | ReactNode | - |
| value | string | - |
| background | string (any CSS background) | - |
| tone | "light" | "dark" | "auto" | "auto" |
| id | string | its position |