/
Progress.purs
139 lines (130 loc) · 3.93 KB
/
Progress.purs
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
module Lumi.Components.Progress where
import Prelude
import Color (cssStringHSLA)
import Data.Int (toNumber)
import Data.Maybe (Maybe(..))
import Data.Nullable (toNullable)
import Effect.Unsafe (unsafePerformEffect)
import Lumi.Components.Color (colors)
import React.Basic.Classic (Component, JSX, createComponent, displayNameFromComponent, element, makeStateless)
import React.Basic.DOM as R
import React.Basic.DOM.SVG as RS
type ProgressProps =
{ total :: Int
, completed :: Int
, style :: R.CSS
, testId :: Maybe String
}
progressDefaults :: ProgressProps
progressDefaults =
{ total: 100
, completed: 0
, style: R.css {}
, testId: Nothing
}
progressBar :: ProgressProps -> JSX
progressBar = makeProgressComponent (createComponent "ProgressBar") \{ total, completed } ->
RS.svg
{ xmlns: "http://www.w3.org/2000/svg"
, height: "6"
, width: "120"
, children:
[ backgroundPath
{ fill
, fillRule
, strokeWidth
, strokeMiterlimit
, d
, stroke: cssStringHSLA colors.black3
}
, meterPath
{ className: "meter"
, fill
, fillRule
, strokeWidth
, strokeMiterlimit
, d
, stroke: cssStringHSLA colors.primary
, pathLength: total
, strokeDasharray: total
, strokeDashoffset: completed
, style: progressAnimationStyles
}
]
}
where
fill = "none"
fillRule = "nonzero"
strokeWidth = "6"
strokeMiterlimit = "round"
d = "M0,3 L120,3"
backgroundPath = RS.path
meterPath = RS.path
progressCircle :: ProgressProps -> JSX
progressCircle = makeProgressComponent (createComponent "ProgressCircle") \{ total, completed } ->
RS.svg
{ xmlns: "http://www.w3.org/2000/svg"
, height: "24"
, width: "24"
, style: R.css { transform: "rotate(-90deg)" }
, children:
[ backgroundPath
{ fill
, fillRule
, strokeWidth
, strokeMiterlimit
, cx
, cy
, r
, stroke: cssStringHSLA colors.black3
}
, meterPath
{ className: "meter"
, fill
, fillRule
, strokeWidth
, strokeMiterlimit
, cx
, cy
, r
, stroke: cssStringHSLA colors.primary
, pathLength: total
, strokeDasharray: total
, strokeDashoffset: completed
, style: progressAnimationStyles
}
]
}
where
fill = "none"
fillRule = "evenodd"
strokeWidth = "3"
strokeMiterlimit = "round"
cx = "12"
cy = "12"
r = "10"
backgroundPath = RS.circle
meterPath = RS.circle
makeProgressComponent :: Component ProgressProps -> ({ total :: String, completed :: String } -> JSX) -> ProgressProps -> JSX
makeProgressComponent component renderShape = makeStateless component render
where
render props =
let
total = toNumber (max 1 props.total)
completed = min total (toNumber (max 0 props.completed))
totalString = show total
completedString = show (total - (total * (completed / total)))
in
lumiProgress
{ "data-testid": toNullable props.testId
, "data-variant": displayNameFromComponent component
, role: "progressbar"
, "aria-valuenow": show completed
, "aria-valuemin": "0"
, "aria-valuemax": totalString
, style: R.mergeStyles [ R.css { display: "flex" }, props.style ]
, children: renderShape { total: totalString, completed: completedString }
}
lumiProgress = element (unsafePerformEffect $ R.unsafeCreateDOMComponent "lumi-progress")
progressAnimationStyles :: R.CSS
progressAnimationStyles = R.css { willChange: "auto", transition: "stroke-dashoffset 400ms ease-in-out" }