Repository navigation
Expand file tree
/
Copy pathbutton.tsx
More file actions
437 lines (404 loc) · 13.6 KB
/
Copy pathbutton.tsx
File metadata and controls
437 lines (404 loc) · 13.6 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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import './button.css'
import * as React from 'react'
import * as Styles from '@/styles'
import Badge from './badge'
import ClickableBox from './clickable-box'
import Icon, {type SizeType} from './icon'
import Text, {type StylesTextCrossPlatform} from './text'
import WithTooltip from './with-tooltip'
import type {IconType} from './icon.constants-gen'
import {Box, Box2} from './box'
import type {MeasureRef} from './measure-ref'
import type AnimationType from './animation'
const Kb = {
Badge,
Box,
Box2,
ClickableBox,
Icon,
Text,
WithTooltip,
}
export type ButtonType = 'Default' | 'Success' | 'Danger' | 'Wallet' | 'Dim'
export type ButtonColor = 'blue' | 'red' | 'green' | 'purple' | 'black' | 'yellow'
// if icon exists, tooltip MUST exist
type WithIconProps =
| {
icon?: never
}
| {
icon: IconType
iconSizeType?: SizeType
iconColor?: Styles.Color
tooltip?: string
label?: never
}
| {
icon: IconType
iconSizeType?: SizeType
iconColor?: Styles.Color
tooltip?: never
label: string
}
// Either type or backgroundColor must be set
type DefaultProps = {
backgroundColor?: ButtonColor
badgeNumber?: number
children?: React.ReactNode
className?: string
disabled?: boolean
fullWidth?: boolean
label?: string
labelContainerStyle?: Styles.StylesCrossPlatform
labelStyle?: Styles.StylesCrossPlatform
mode?: 'Primary' | 'Secondary'
narrow?: boolean
onClick?: (event: React.BaseSyntheticEvent) => void
onMouseEnter?: (e: React.MouseEvent) => void
onMouseLeave?: (e: React.MouseEvent) => void
onMouseDown?: (e: React.MouseEvent) => void
small?: boolean
style?: Styles.StylesCrossPlatform
subLabel?: string
subLabelStyle?: Styles.StylesCrossPlatform
tooltip?: string
type?: ButtonType
waiting?: boolean
disabledStopClick?: boolean // if disabled it'll by default let clicks bleed through
}
export type Props = DefaultProps & WithIconProps
const Progress = ({small, white}: {small?: boolean; white: boolean}) => {
const {default: Animation} = require('./animation') as {default: typeof AnimationType}
return (
<Kb.Box style={styles.progressContainer}>
<Animation
animationType={white ? 'spinnerWhite' : 'spinner'}
style={small ? styles.progressSmall : styles.progressNormal}
/>
</Kb.Box>
)
}
const stopClick = (e: React.BaseSyntheticEvent) => {
e.stopPropagation()
}
type ModeAndType =
| 'PrimaryDefault'
| 'PrimarySuccess'
| 'PrimaryDanger'
| 'PrimaryWallet'
| 'PrimaryDim'
| 'SecondaryDefault'
| 'SecondarySuccess'
| 'SecondaryDanger'
| 'SecondaryWallet'
| 'SecondaryDim'
type ModeAndBGColor =
| 'Primaryblue'
| 'Primaryred'
| 'Primarygreen'
| 'Primarypurple'
| 'Primaryblack'
| 'Primaryyellow'
| 'Secondary'
const Button = React.forwardRef<MeasureRef, Props>(function ButtonInner(
props: Props,
ref: React.Ref<MeasureRef>
) {
const {mode = 'Primary', type = 'Default'} = props
const modeAndType = (mode + type) as ModeAndType
const modeAndBGColor = (mode + (props.backgroundColor ?? '')) as ModeAndBGColor
let containerStyle = props.backgroundColor
? (backgroundColorContainerStyles[mode] as Styles.StylesCrossPlatform)
: (containerStyles[modeAndType] as Styles.StylesCrossPlatform)
let labelStyle = props.backgroundColor
? (backgroundColorLabelStyles[mode === 'Secondary' ? mode : modeAndBGColor] as StylesTextCrossPlatform)
: (labelStyles[modeAndType] as StylesTextCrossPlatform)
if (props.fullWidth) {
containerStyle = Styles.collapseStyles([containerStyle, styles.fullWidth])
}
if (props.small) {
containerStyle = Styles.collapseStyles([containerStyle, styles.small])
}
if (props.icon && !props.label) {
containerStyle = Styles.collapseStyles([containerStyle, styles.icon])
}
if (props.narrow) {
containerStyle = Styles.collapseStyles([containerStyle, styles.narrow])
}
const unclickable = props.disabled || props.waiting
if (unclickable) {
containerStyle = Styles.collapseStyles([containerStyle, styles.opacity30])
}
if (props.waiting) {
labelStyle = Styles.collapseStyles([labelStyle, styles.opacity0]) as StylesTextCrossPlatform
}
containerStyle = Styles.collapseStyles([containerStyle, props.style])
const {onClick: ponClick} = props
const _onClick = React.useCallback(
(e: React.BaseSyntheticEvent) => {
e.stopPropagation()
ponClick?.(e)
},
[ponClick]
)
const onClick = !unclickable && props.onClick ? _onClick : props.disabledStopClick ? stopClick : undefined
const whiteSpinner =
(mode === 'Primary' && !(props.backgroundColor || type === 'Dim')) ||
(mode === 'Secondary' && !!props.backgroundColor)
// Hover border colors
const classNames: Array<string> = []
if (mode === 'Secondary' && !props.backgroundColor) {
// base grey border
classNames.push('button__border')
if (!unclickable) {
// hover effect
classNames.push(`button__border_${typeToColorName[type]}`)
}
}
// Hover background colors
const underlayClassNames: Array<string> = []
if (mode === 'Primary' && !unclickable) {
underlayClassNames.push(
'button__underlay',
props.backgroundColor ? `button__underlay_${props.backgroundColor}` : 'button__underlay_black10'
)
} else if (mode === 'Secondary' && !unclickable) {
// default 0.2 opacity + 0.15 here = 0.35 hover
underlayClassNames.push(
'button__underlay',
props.backgroundColor ? 'button__underlay_black' : `button__underlay_${typeToColorName[type]}`
)
}
const underlay =
!Styles.isMobile && underlayClassNames.length ? (
<Kb.Box className={Styles.classNames(underlayClassNames)} />
) : null
if (props.className) classNames.push(props.className)
const content = (
<Kb.ClickableBox
ref={ref}
className={Styles.classNames(classNames)}
style={containerStyle}
onClick={onClick}
onMouseEnter={props.onMouseEnter}
onMouseLeave={props.onMouseLeave}
onMouseDown={props.onMouseDown}
hoverColor={Styles.globalColors.transparent}
tooltip={props.tooltip}
>
{underlay}
<Kb.Box
style={Styles.collapseStyles([
Styles.globalStyles.flexBoxRow,
Styles.globalStyles.flexBoxCenter,
styles.labelContainer,
props.labelContainerStyle,
])}
>
{!props.waiting && props.children}
<Kb.Box2 direction={props.icon && props.label ? 'horizontal' : 'vertical'} centerChildren={true}>
{!!props.icon && (
<Kb.Icon
type={props.icon}
color={props.iconColor}
sizeType={props.iconSizeType ?? 'Default'}
style={Styles.collapseStyles([
labelStyle,
!!props.label && styles.iconWithLabel,
props.labelStyle,
])}
/>
)}
{!!props.label && (
<Kb.Text type="BodySemibold" style={Styles.collapseStyles([labelStyle, props.labelStyle])}>
{props.label}
</Kb.Text>
)}
{!!props.subLabel && (
<Kb.Text
type="BodyTiny"
style={Styles.collapseStyles([props.waiting && styles.opacity0, props.subLabelStyle])}
>
{props.subLabel}
</Kb.Text>
)}
</Kb.Box2>
{!!props.badgeNumber && <Kb.Badge badgeNumber={props.badgeNumber} badgeStyle={styles.badge} />}
{!!props.waiting && <Progress small={props.small} white={whiteSpinner} />}
</Kb.Box>
</Kb.ClickableBox>
)
if (props.disabled && props.tooltip && Styles.isMobile) {
return (
<Kb.WithTooltip tooltip={props.tooltip} showOnPressMobile={props.disabled}>
{content}
</Kb.WithTooltip>
)
}
return content
})
const typeToColorName = {
Danger: 'red',
Default: 'blue',
Dim: 'black',
Success: 'green',
Wallet: 'purple',
}
export const smallHeight = Styles.isMobile ? 32 : 28
export const regularHeight = Styles.isMobile ? 40 : 32
const common: () => Styles._StylesCrossPlatform = () =>
Styles.platformStyles({
common: {
...Styles.globalStyles.flexBoxColumn,
alignItems: 'center',
borderRadius: Styles.borderRadius,
height: regularHeight,
justifyContent: 'center',
},
isElectron: {
display: 'inline-block',
lineHeight: 'inherit',
minWidth: 100,
paddingLeft: Styles.globalMargins.medium,
paddingRight: Styles.globalMargins.medium,
},
isMobile: {
minWidth: 120,
paddingLeft: Styles.globalMargins.small,
paddingRight: Styles.globalMargins.small,
},
isTablet: {
alignSelf: 'center',
},
})
const styles = Styles.styleSheetCreate(() => ({
badge: {
marginLeft: Styles.globalMargins.xtiny,
marginRight: 0,
},
fullWidth: {
flexGrow: 1,
maxWidth: 460,
width: '100%',
},
icon: {
borderRadius: Styles.borderRadius,
minWidth: undefined,
paddingLeft: Styles.isMobile ? Styles.globalMargins.xtiny : Styles.globalMargins.tiny,
paddingRight: Styles.isMobile ? Styles.globalMargins.xtiny : Styles.globalMargins.tiny,
width: regularHeight,
},
iconWithLabel: {
paddingRight: Styles.globalMargins.xtiny,
},
labelContainer: Styles.platformStyles({
common: {height: '100%', position: 'relative'},
isElectron: {pointerEvents: 'none'}, // need hover etc. to go through to underlay
}),
narrow: Styles.platformStyles({
isElectron: {
minWidth: 50,
paddingLeft: Styles.globalMargins.small,
paddingRight: Styles.globalMargins.small,
},
isMobile: {
minWidth: 80,
paddingLeft: Styles.globalMargins.tiny,
paddingRight: Styles.globalMargins.tiny,
},
}),
opacity0: {opacity: 0},
opacity30: {opacity: 0.3},
progressContainer: {...Styles.globalStyles.fillAbsolute, ...Styles.globalStyles.flexBoxCenter},
progressNormal: {height: Styles.isMobile ? 32 : 24, width: Styles.isMobile ? 32 : 24},
progressSmall: {height: Styles.isMobile ? 28 : 20, width: Styles.isMobile ? 28 : 20},
small: {
borderRadius: Styles.borderRadius,
height: smallHeight,
minWidth: undefined,
paddingLeft: Styles.isMobile ? Styles.globalMargins.small : Styles.globalMargins.xsmall,
paddingRight: Styles.isMobile ? Styles.globalMargins.small : Styles.globalMargins.xsmall,
},
}))
const containerStyles = Styles.styleSheetCreate(() => {
const commonSecondaryWhiteBg = Styles.platformStyles({
common: common(),
isElectron: {
backgroundColor: Styles.globalColors.white,
transition: 'border 0.3s ease-out',
},
isMobile: {
backgroundColor: Styles.globalColors.white,
borderColor: Styles.globalColors.black_20,
borderStyle: 'solid',
borderWidth: 1,
},
})
return {
PrimaryDanger: {...common(), backgroundColor: Styles.globalColors.red},
PrimaryDefault: {...common(), backgroundColor: Styles.globalColors.blue},
PrimaryDim: {...common(), backgroundColor: Styles.globalColors.grey},
PrimarySuccess: {...common(), backgroundColor: Styles.globalColors.green},
PrimaryWallet: {...common(), backgroundColor: Styles.globalColors.purple},
SecondaryDanger: commonSecondaryWhiteBg,
SecondaryDefault: commonSecondaryWhiteBg,
SecondaryDim: commonSecondaryWhiteBg,
SecondarySuccess: commonSecondaryWhiteBg,
SecondaryWallet: commonSecondaryWhiteBg,
} as const
})
const commonLabel = () =>
Styles.platformStyles({
common: {
color: Styles.globalColors.whiteOrWhite,
display: 'flex',
textAlign: 'center',
},
isElectron: {userSelect: 'none', whiteSpace: 'pre'},
isMobile: {lineHeight: undefined},
})
const labelStyles = Styles.styleSheetCreate(() => {
const primaryWhiteBgLabel = {
...commonLabel(),
color: Styles.globalColors.whiteOrWhite,
}
const secondaryLabel = {
backgroundColor: Styles.globalColors.fastBlank,
}
return {
PrimaryDanger: primaryWhiteBgLabel,
PrimaryDefault: primaryWhiteBgLabel,
PrimaryDim: {...primaryWhiteBgLabel, color: Styles.globalColors.black},
PrimarySuccess: primaryWhiteBgLabel,
PrimaryWallet: primaryWhiteBgLabel,
SecondaryDanger: {...commonLabel(), ...secondaryLabel, color: Styles.globalColors.redDark},
SecondaryDefault: {...commonLabel(), ...secondaryLabel, color: Styles.globalColors.blueDark},
SecondaryDim: {...commonLabel(), ...secondaryLabel, color: Styles.globalColors.black_50},
SecondarySuccess: {...commonLabel(), ...secondaryLabel, color: Styles.globalColors.greenDark},
SecondaryWallet: {...commonLabel(), ...secondaryLabel, color: Styles.globalColors.purpleDark},
} as const
})
// With backgroundColor styles
const backgroundColorContainerStyles = Styles.styleSheetCreate(
() =>
({
Primary: {...common(), backgroundColor: Styles.globalColors.white},
Secondary: Styles.platformStyles({
common: {...common(), backgroundColor: Styles.globalColors.black_20},
isElectron: {transition: 'background-color 0.2s ease-out, border 0.2s ease-out'},
}),
}) as const
)
const backgroundColorLabelStyles = Styles.styleSheetCreate(
() =>
({
Primaryblack: {...commonLabel(), color: Styles.globalColors.black},
Primaryblue: {...commonLabel(), color: Styles.globalColors.blueDark},
Primarygreen: {...commonLabel(), color: Styles.globalColors.greenDark},
Primarypurple: {...commonLabel(), color: Styles.globalColors.purpleDark},
Primaryred: {...commonLabel(), color: Styles.globalColors.redDark},
Primaryyellow: {...commonLabel(), color: Styles.globalColors.brown_75OrYellow},
Secondary: {...commonLabel(), color: Styles.globalColors.white},
}) as const
)
export default Button