-
Notifications
You must be signed in to change notification settings - Fork 202
Expand file tree
/
Copy pathDrawingLimit.tsx
More file actions
143 lines (133 loc) · 4.4 KB
/
Copy pathDrawingLimit.tsx
File metadata and controls
143 lines (133 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"use client";
import { useId } from "react";
interface DrawingLimitProps {
/** Current amount drawn (utilized) */
drawnAmount: number;
/** Total credit limit available */
totalLimit: number;
/** Optional label for the drawn amount (default: "Drawn") */
drawnLabel?: string;
/** Optional label for the available amount (default: "Available") */
availableLabel?: string;
/** Optional className for custom styling */
className?: string;
}
/**
* DrawingLimit visual indicator showing utilization vs available credit.
*
* Features:
* - Semantic colors based on utilization percentage
* - Accessible ARIA labels and live region updates
* - Responsive design with appropriate text sizes
* - Dark mode compatible with design tokens
*
* Color thresholds:
* - Low (0-50%): Green (safe)
* - Medium (50-80%): Amber (warning)
* - High (80-100%): Red (danger)
* - Exceeded (>100%): Red with error state
*/
export function DrawingLimit({
drawnAmount,
totalLimit,
drawnLabel = "Drawn",
availableLabel = "Available",
className = "",
}: DrawingLimitProps) {
const id = useId();
const percentage = totalLimit > 0 ? (drawnAmount / totalLimit) * 100 : 0;
const available = Math.max(0, totalLimit - drawnAmount);
const isExceeded = drawnAmount > totalLimit;
// Determine color based on utilization
let barColor = "bg-green-500";
let barBackground = "bg-green-500/20";
let textColor = "text-green-400";
if (isExceeded) {
barColor = "bg-red-500";
barBackground = "bg-red-500/20";
textColor = "text-red-400";
} else if (percentage > 80) {
barColor = "bg-red-500";
barBackground = "bg-red-500/20";
textColor = "text-red-400";
} else if (percentage > 50) {
barColor = "bg-amber-500";
barBackground = "bg-amber-500/20";
textColor = "text-amber-400";
}
const clampedPercentage = Math.min(percentage, 100);
return (
<div className={`space-y-3 ${className}`}>
{/* Header with labels */}
<div className="flex items-center justify-between text-sm">
<div className="flex items-center gap-4">
<span className="font-medium text-foreground">
{drawnLabel}:{" "}
<span className={`font-semibold ${textColor} tabular-nums`}>
{formatCurrency(drawnAmount)}
</span>
</span>
<span className="text-muted-foreground">/</span>
<span className="text-muted-foreground">
{availableLabel}:{" "}
<span className="font-semibold text-foreground tabular-nums">
{formatCurrency(available)}
</span>
</span>
</div>
<span
className={`text-sm font-semibold ${textColor} tabular-nums`}
aria-live="polite"
aria-atomic="true"
>
{isExceeded ? "Exceeded" : `${Math.round(clampedPercentage)}%`}
</span>
</div>
{/* Progress bar */}
<div
className={`relative h-3 w-full overflow-hidden rounded-full ${barBackground}`}
role="progressbar"
aria-valuenow={Math.round(clampedPercentage)}
aria-valuemin={0}
aria-valuemax={100}
aria-label={`Credit utilization: ${Math.round(clampedPercentage)} percent`}
>
<div
className={`h-full rounded-full transition-all duration-500 ease-out ${barColor}`}
style={{ width: `${clampedPercentage}%` }}
>
<span className="sr-only">
{isExceeded
? `Credit limit exceeded by ${formatCurrency(drawnAmount - totalLimit)}`
: `${Math.round(clampedPercentage)}% of credit limit utilized`}
</span>
</div>
</div>
{/* Detailed breakdown */}
<div className="flex items-center justify-between text-xs text-muted-foreground">
<span>
Limit:{" "}
<span className="font-medium text-foreground tabular-nums">
{formatCurrency(totalLimit)}
</span>
</span>
{isExceeded && (
<span className="font-medium text-red-400 tabular-nums">
Overdrawn by {formatCurrency(drawnAmount - totalLimit)}
</span>
)}
</div>
</div>
);
}
/**
* Format a number as currency with USD symbol
*/
function formatCurrency(amount: number): string {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
minimumFractionDigits: 2,
maximumFractionDigits: 2,
}).format(amount);
}