/
Button.purs
310 lines (296 loc) · 9.67 KB
/
Button.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
module Lumi.Styles.Button where
import Prelude
import Color (Color, darken, desaturate, lighten)
import Data.Foldable (fold)
import Data.Maybe (Maybe, fromMaybe)
import Lumi.Components.Size (Size(..))
import Lumi.Components.ZIndex (ziButtonGroup)
import Lumi.Styles (StyleModifier, merge, none, style, style_)
import Lumi.Styles.Box (FlexAlign(..), _align, _focusable, _interactive, _justify, _row, box)
import Lumi.Styles.Link as Link
import Lumi.Styles.Loader (mkLoader, spin)
import Lumi.Styles.Theme (LumiTheme(..))
import React.Basic.Emotion (color, css, int, nested, str)
data ButtonKind
= Primary
| Secondary
| Link
data ButtonState
= Enabled
| Disabled
| Loading
button ::
Maybe Color ->
ButtonKind ->
ButtonState ->
Size ->
StyleModifier
button colo kind state size = case kind of
Primary ->
buttonStyle
<<< style \theme@(LumiTheme { colors }) ->
let
{ hue, hueDarker, hueDarkest, hueDisabled, white } =
makeColorShades
{ hue: fromMaybe colors.primary colo
, black: colors.black
, white: colors.white
}
disabledStyles =
css
{ cursor: str "default"
, color: color white
, borderColor: color hueDisabled
, backgroundColor: color hueDisabled
}
in
case state of
Enabled ->
css
{ borderColor: color hue
, color: color white
, backgroundColor: color hue
, "&:hover":
nested
$ css
{ borderColor: color hueDarker
, backgroundColor: color hueDarker
}
, "&:active":
nested
$ css
{ borderColor: color hueDarkest
, backgroundColor: color hueDarkest
}
, "&:disabled": nested disabledStyles
}
Disabled -> disabledStyles
Loading ->
merge
[ disabledStyles
, loadingStyles theme
]
Secondary ->
buttonStyle
<<< style \theme@(LumiTheme { colors }) ->
let
{ hueDarker, hueDarkest, grey1, grey2, white, black } =
makeColorShades
{ hue: fromMaybe colors.primary colo
, black: colors.black
, white: colors.white
}
disabledStyles =
css
{ cursor: str "default"
, color: color grey1
, borderColor: color grey2
, backgroundColor: color white
}
in
case state of
Enabled ->
css
{ borderColor: color grey1
, color: color black
, backgroundColor: color white
, "&:hover":
nested
$ css
{ borderColor: color hueDarker
, color: color hueDarker
, backgroundColor: color white
}
, "&:active":
nested
$ css
{ borderColor: color hueDarkest
, color: color hueDarkest
, backgroundColor: color white
}
, "&:disabled": nested disabledStyles
}
Disabled -> disabledStyles
Loading ->
merge
[ disabledStyles
, loadingStyles theme
]
Link ->
Link.link
<<< style \(LumiTheme { colors }) ->
let
{ hueDisabled } =
makeColorShades
{ hue: fromMaybe colors.primary colo
, black: colors.black
, white: colors.white
}
disabledStyles =
css
{ cursor: str "default"
, color: color hueDisabled
, "&:hover, &:active":
nested
$ css
{ cursor: str "default"
, textDecoration: none
}
}
in
merge
[ css
{ label: str "button"
, appearance: none
, outline: none
, padding: int 0
, background: none
, border: none
}
, case state of
Disabled -> disabledStyles
Loading -> disabledStyles
Enabled -> mempty
]
where
buttonStyle =
box
<<< _row
<<< _align Center
<<< _justify Center
<<< case state of
Disabled -> identity
Loading -> identity
Enabled -> _interactive <<< _focusable
<<< style_
( css
{ label: str "button"
, appearance: none
, outline: none
, minWidth: int 70
, padding: str "10px 20px"
, fontSize: int 14
, lineHeight: int 1
, whiteSpace: str "nowrap"
, textOverflow: str "ellipsis"
, overflow: str "hidden"
, height: int 40
, borderRadius: int 3
, borderWidth: int 1
, borderStyle: str "solid"
, "@media (min-width: 860px)":
nested
$ fold
[ css
{ padding: str "6px 16px"
, height: int 32
}
, case size of
Small ->
css
{ fontSize: int 12
, height: int 28
}
Medium -> mempty
Large ->
css
{ fontSize: int 15
, padding: str "12px 24px"
, height: int 48
}
ExtraLarge ->
css
{ fontSize: int 20
, padding: str "16px 32px"
, height: int 64
}
ExtraExtraLarge ->
css
{ fontSize: int 20
, padding: str "16px 32px"
, height: int 64
}
]
}
)
loadingStyles theme =
merge
[ spin
, css
{ label: str "loading"
, "&:after": nested $ mkLoader theme { radius: "16px", borderWidth: "2px" }
, "@media (min-width: 860px)":
nested case size of
Small ->
css
{ "&:after":
nested
$ mkLoader theme { radius: "12px", borderWidth: "2px" }
}
Medium -> mempty
Large ->
css
{ "&:after":
nested
$ mkLoader theme { radius: "24px", borderWidth: "3px" }
}
ExtraLarge ->
css
{ "&:after":
nested
$ mkLoader theme { radius: "34px", borderWidth: "4px" }
}
ExtraExtraLarge ->
css
{ "&:after":
nested
$ mkLoader theme { radius: "34px", borderWidth: "4px" }
}
}
]
makeColorShades { hue, white, black } =
let
hueDarker = darken 0.1 hue
hueDarkest = darken 0.15 hue
hueDisabled = lighten 0.4137 $ desaturate 0.1972 hue
grey1 = lighten 0.7 black
grey2 = lighten 0.82 black
in
{ hue, hueDarker, hueDarkest, hueDisabled, grey1, grey2, white, black }
buttonGroup :: Boolean -> StyleModifier
buttonGroup joined =
box
<<< _row
<<< style_ (css { label: str "buttonGroup" })
<<< style_
if not joined then
css
{ label: str "notJoined"
, "& > *:not(:last-child)":
nested
$ css
{ marginRight: int 8
}
}
else
css
{ label: str "joined"
, "& > *:not(:last-child)":
nested
$ css
{ marginRight: int (-1)
, borderTopRightRadius: int 0
, borderBottomRightRadius: int 0
}
, "& > *:not(:first-child)":
nested
$ css
{ borderTopLeftRadius: int 0
, borderBottomLeftRadius: int 0
}
, "& > *:focus, & > *:hover":
nested
$ css
{ zIndex: int ziButtonGroup
}
}