forked from ReactTraining/hooks-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgressCircle.js
82 lines (77 loc) · 2.12 KB
/
ProgressCircle.js
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
import React from "react"
const style = window.getComputedStyle(document.body)
const gray = style.getPropertyValue(`--middle-gray`)
const red = style.getPropertyValue(`--red`)
const accent = style.getPropertyValue(`--accent`)
const green = style.getPropertyValue(`--green`)
export default function ProgressCircle({
radius = 30,
stroke = 10,
progress = 100,
expectedProgress = 100,
bg = gray
}) {
const normalizedRadius = radius - stroke / 2
const circumference = normalizedRadius * 2 * Math.PI
const strokeDashoffset = circumference - (progress / 100) * circumference
const expectedStrokeDashoffset =
circumference - (expectedProgress / 100) * circumference
const deficit = progress < expectedProgress
const progressBar = (
<circle
key="progress"
stroke={deficit ? accent : green}
fill="transparent"
strokeWidth={stroke}
strokeDasharray={circumference + " " + circumference}
style={{
strokeDashoffset,
transform: "rotate(-90deg)",
transformOrigin: "50% 50%"
}}
r={normalizedRadius}
cx={radius}
cy={radius}
/>
)
const expectedBar = (
<circle
key="expected"
stroke={deficit ? red : accent}
fill="transparent"
strokeWidth={stroke}
strokeDasharray={circumference + " " + circumference}
style={{
strokeDashoffset: expectedStrokeDashoffset,
transform: "rotate(-90deg)",
transformOrigin: "50% 50%"
}}
r={normalizedRadius}
cx={radius}
cy={radius}
/>
)
return (
<svg
aria-label={`Progress: ${progress}%`}
height={radius * 2}
width={radius * 2}
>
<circle
stroke={bg}
fill="transparent"
strokeWidth={stroke}
strokeDasharray={circumference + " " + circumference}
style={{
boxShadow: "1px 1px 1px hsla(0, 0%, 0%, 0.5)",
transform: "rotate(-90deg)",
transformOrigin: "50% 50%"
}}
r={normalizedRadius}
cx={radius}
cy={radius}
/>
{deficit ? [expectedBar, progressBar] : [progressBar, expectedBar]}
</svg>
)
}