forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgress.tsx
More file actions
133 lines (122 loc) 路 3.23 KB
/
Copy pathProgress.tsx
File metadata and controls
133 lines (122 loc) 路 3.23 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
'use client';
type Color = 'brand' | 'green' | 'yellow' | 'red' | 'blue';
type Size = 'sm' | 'md' | 'lg';
interface ProgressBarProps {
value: number;
max?: number;
color?: Color;
size?: Size;
label?: string;
showPercent?: boolean;
}
interface CircularProgressProps {
value: number;
max?: number;
color?: Color;
size?: number;
label?: string;
showPercent?: boolean;
}
const trackColor: Record<Color, string> = {
brand: 'bg-brand',
green: 'bg-green-500',
yellow: 'bg-yellow-500',
red: 'bg-red-500',
blue: 'bg-blue-500',
};
const strokeColor: Record<Color, string> = {
brand: 'stroke-brand',
green: 'stroke-green-500',
yellow: 'stroke-yellow-500',
red: 'stroke-red-500',
blue: 'stroke-blue-500',
};
const barHeight: Record<Size, string> = {
sm: 'h-1.5',
md: 'h-2.5',
lg: 'h-4',
};
export function ProgressBar({
value,
max = 100,
color = 'brand',
size = 'md',
label,
showPercent = false,
}: ProgressBarProps) {
const pct = Math.min(100, Math.max(0, (value / max) * 100));
return (
<div className="w-full">
{(label || showPercent) && (
<div className="mb-1 flex justify-between text-xs text-gray-600 dark:text-gray-400">
{label && <span>{label}</span>}
{showPercent && <span>{Math.round(pct)}%</span>}
</div>
)}
<div
className={`w-full rounded-full bg-gray-200 dark:bg-gray-700 ${barHeight[size]}`}
role="progressbar"
aria-valuenow={value}
aria-valuemin={0}
aria-valuemax={max}
>
<div
className={`${barHeight[size]} rounded-full transition-all duration-300 ${trackColor[color]}`}
style={{ width: `${pct}%` }}
/>
</div>
</div>
);
}
export function CircularProgress({
value,
max = 100,
color = 'brand',
size = 64,
label,
showPercent = true,
}: CircularProgressProps) {
const pct = Math.min(100, Math.max(0, (value / max) * 100));
const r = (size - 8) / 2;
const circ = 2 * Math.PI * r;
const offset = circ - (pct / 100) * circ;
return (
<div className="inline-flex flex-col items-center gap-1">
<svg width={size} height={size} className="-rotate-90">
<circle
cx={size / 2}
cy={size / 2}
r={r}
strokeWidth={6}
className="stroke-gray-200 dark:stroke-gray-700"
fill="none"
/>
<circle
cx={size / 2}
cy={size / 2}
r={r}
strokeWidth={6}
className={`${strokeColor[color]} transition-all duration-300`}
fill="none"
strokeDasharray={circ}
strokeDashoffset={offset}
strokeLinecap="round"
/>
{showPercent && (
<text
x={size / 2}
y={size / 2}
textAnchor="middle"
dominantBaseline="middle"
className="rotate-90 fill-gray-700 dark:fill-gray-300 text-xs font-semibold"
style={{ transform: `rotate(90deg)`, transformOrigin: `${size / 2}px ${size / 2}px`, fontSize: size * 0.2 }}
>
{Math.round(pct)}%
</text>
)}
</svg>
{label && <span className="text-xs text-gray-600 dark:text-gray-400">{label}</span>}
</div>
);
}
export default ProgressBar;