Blocks/Cartes
Usage Card
Une carte de consommation de crédits avec répartition, graphique journalier et limites.
BetaWhoa UI est en bêta : certains éléments peuvent encore changer. Un bug ? Écrivez-moi à hello@ugolin-olle.com.
Halden AI
Pro plan · Resets in 9 days
10,000of 10,000 credits
100%- Chat5,21052%
- Search2,14021%
- Image1,69017%
- API96010%
Per daySep 30 418 credits
- Seats
10 of 10
- Storage
2.9 of 5 GB
- Projects
11 of ∞
Limit reached
../../apps/web/examples/usage-card.tsx
'use client';import type { UsageDay, UsagePeriod } from '@/components/blocks/usage-card';import { UsageCard } from '@/components/blocks/usage-card';function mulberry32(seed: number) { return function random() { seed |= 0; seed = (seed + 0x6d2b79f5) | 0; let t = Math.imul(seed ^ (seed >>> 15), 1 | seed); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; };}// Fixed dates (not "today") so server and client render identical markup.function generateDaily(seed: number, start: string, days: number, total: number): UsageDay[] { const random = mulberry32(seed); const weights = Array.from({ length: days }, () => 0.35 + random()); const weightSum = weights.reduce((sum, weight) => sum + weight, 0); const values = weights.map((weight) => Math.round((weight / weightSum) * total)); // Absorb rounding so the days add up exactly to the period total. values[values.length - 1] += total - values.reduce((sum, value) => sum + value, 0); const startTime = Date.parse(`${start}T00:00:00Z`); return values.map((value, index) => ({ date: new Date(startTime + index * 86_400_000).toISOString().slice(0, 10), value, }));}const COLORS = { chat: '#2563eb', search: '#60a5fa', image: '#bfdbfe', api: '#a1a1aa' };function categories(chat: number, search: number, image: number, api: number) { return [ { id: 'chat', label: 'Chat', value: chat, color: COLORS.chat }, { id: 'search', label: 'Search', value: search, color: COLORS.search }, { id: 'image', label: 'Image', value: image, color: COLORS.image }, { id: 'api', label: 'API', value: api, color: COLORS.api }, ];}const periods: UsagePeriod[] = [ { id: 'this-month', label: 'This month', used: 4286, limit: 10000, categories: categories(2231, 912, 731, 412), daily: generateDaily(7, '2026-10-01', 14, 4286), }, { id: 'last-month', label: 'Last month', used: 10000, limit: 10000, categories: categories(5210, 2140, 1690, 960), daily: generateDaily(3, '2026-09-01', 30, 10000), }, { id: '7-days', label: '7 days', used: 2138, limit: 10000, categories: categories(1108, 457, 369, 204), daily: generateDaily(11, '2026-10-08', 7, 2138), },];export default function Demo() { return ( <UsageCard title="Halden AI" subtitle="Pro plan · Resets in 9 days" periods={periods} defaultPeriod="last-month" meters={[ { id: 'seats', label: 'Seats', value: 10, limit: 10 }, { id: 'storage', label: 'Storage', value: 2.9, limit: 5, unit: 'GB' }, { id: 'projects', label: 'Projects', value: 11, limit: null }, ]} /> );}Installation#
Via shadcn — copie le code source
$pnpm dlx shadcn@latest add https://www.ugolin-olle.com/r/usage-card.jsonInstallation manuelle
'use client';import { AnimatePresence, motion } from 'motion/react';import * as React from 'react';import { cn } from '@/lib/utils';import type { UsageCardLabels, UsageMeter, UsagePeriod } from './types';import { UsageBreakdown } from './usage-breakdown';import { UsageDailyChart } from './usage-daily-chart';import { UsageMeters } from './usage-meters';import { UsagePeriodTabs } from './usage-period-tabs';import { formatNumber, getUsageTone, percentOf, TONE_DOT_CLASS } from './utils';export interface UsageCardProps { title: string; /** Line under the title, e.g. "Pro plan · Resets in 9 days". */ subtitle?: React.ReactNode; periods: UsagePeriod[]; /** Uncontrolled initial period. Defaults to the first one. */ defaultPeriod?: string; /** Controlled active period id. */ activePeriod?: string; onPeriodChange?: (id: string) => void; meters?: UsageMeter[]; labels?: UsageCardLabels; /** BCP 47 locale for numbers and dates. */ locale?: string; onAction?: () => void; /** Full override for the footer action. */ action?: React.ReactNode; className?: string;}const DEFAULT_LABELS: Required<UsageCardLabels> = { of: 'of', unit: 'credits', perDay: 'Per day', limitReached: 'Limit reached', remaining: (remaining) => `${remaining} credits left`, action: 'Add credits',};export function UsageCard({ title, subtitle, periods, defaultPeriod, activePeriod: controlledPeriod, onPeriodChange, meters, labels: labelOverrides, locale = 'en-US', onAction, action, className,}: UsageCardProps) { const labels = { ...DEFAULT_LABELS, ...labelOverrides }; const [internalPeriod, setInternalPeriod] = React.useState(defaultPeriod ?? periods[0]?.id); const activeId = controlledPeriod ?? internalPeriod; const period = periods.find((item) => item.id === activeId) ?? periods[0]; if (!period) return null; const tone = getUsageTone(period.used, period.limit); const remaining = Math.max(period.limit - period.used, 0); const handlePeriodChange = (id: string) => { setInternalPeriod(id); onPeriodChange?.(id); }; return ( <div className={cn( 'w-full max-w-sm rounded-3xl border border-border/60 bg-muted p-1.5', className, )} > <div className="space-y-5 rounded-[20px] border border-border/40 bg-card p-4 shadow-sm"> <div className="flex flex-wrap items-start justify-between gap-x-3 gap-y-2"> <div className="min-w-0"> <h3 className="truncate text-sm font-semibold">{title}</h3> {subtitle && <p className="text-xs text-pretty text-muted-foreground">{subtitle}</p>} </div> {periods.length > 1 && ( <UsagePeriodTabs periods={periods} activePeriod={period.id} onChange={handlePeriodChange} /> )} </div> <div className="flex flex-wrap items-end justify-between gap-x-3 gap-y-1"> <p className="flex flex-wrap items-baseline gap-x-1.5 tabular-nums"> <AnimatePresence initial={false} mode="popLayout"> <motion.span key={period.id} className="text-4xl font-semibold tracking-tight" initial={{ opacity: 0, y: 8 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -8 }} transition={{ duration: 0.25, ease: [0.16, 1, 0.3, 1] }} > {formatNumber(period.used, locale)} </motion.span> </AnimatePresence> <span className="text-sm text-muted-foreground"> {labels.of} {formatNumber(period.limit, locale)} {labels.unit} </span> </p> <span className="flex items-center gap-1.5 pb-1 text-sm font-medium tabular-nums"> <span aria-hidden="true" className={cn('size-2 rounded-full', TONE_DOT_CLASS[tone])} /> {percentOf(period.used, period.limit)}% </span> </div> <UsageBreakdown categories={period.categories} limit={period.limit} locale={locale} /> <UsageDailyChart // Remount per period so the highlighted day resets to the latest one. key={period.id} className="border-t border-dashed border-border pt-4" days={period.daily} label={labels.perDay} unit={labels.unit} locale={locale} color={period.categories[0]?.color} /> {meters && meters.length > 0 && ( <UsageMeters className="border-t border-dashed border-border pt-4" meters={meters} locale={locale} ofLabel={labels.of} /> )} </div> <div className="flex items-center justify-between gap-3 px-3 pt-2.5 pb-1.5"> <span className="flex items-center gap-2 text-xs font-medium"> <span aria-hidden="true" className={cn('size-2 rounded-full', TONE_DOT_CLASS[tone])} /> {tone === 'limit' ? labels.limitReached : labels.remaining(formatNumber(remaining, locale))} </span> {action ?? ( <button type="button" onClick={onAction} className={cn( 'rounded-lg bg-foreground px-3.5 py-1.5 text-xs font-medium text-background', 'transition-[opacity,transform] hover:opacity-90 active:scale-[0.98]', 'focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:outline-none', )} > {labels.action} </button> )} </div> </div> );}export type { UsageCardLabels, UsageCategory, UsageDay, UsageMeter, UsagePeriod, UsageTone,} from './types';Une carte de facturation à l'usage pour un produit SaaS ou IA : total consommé par rapport au
forfait, répartition par catégorie, graphique jour par jour (survol ou flèches du clavier) et
jauges de limites (sièges, stockage, projets illimités). Les périodes se changent via des onglets
animés avec motion/react, et tous les libellés, couleurs et formats (locale) sont
personnalisables.