-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathGluuButton.tsx
More file actions
145 lines (137 loc) · 3.97 KB
/
GluuButton.tsx
File metadata and controls
145 lines (137 loc) · 3.97 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
import React, { useMemo, useState, useCallback } from 'react'
import { fontFamily } from '@/styles/fonts'
import { useTheme } from '@/context/theme/themeContext'
import getThemeColor from '@/context/theme/config'
import { THEME_DARK } from '@/context/theme/constants'
import { OPACITY } from '@/constants'
import { resolveBackgroundColor } from '@/utils/buttonUtils'
import type { GluuButtonProps } from './types'
const SIZES = {
sm: { fontSize: '12px', padding: '6px 12px', minHeight: '30px' },
md: { fontSize: '14px', padding: '8px 16px', minHeight: '38px' },
lg: { fontSize: '16px', padding: '10px 24px', minHeight: '46px' },
}
const GluuButton: React.FC<GluuButtonProps> = (props) => {
const {
children,
size = 'md',
outlined = false,
disabled = false,
loading = false,
block = false,
'theme': themeProp,
backgroundColor,
textColor,
borderColor,
borderRadius,
fontSize,
fontWeight,
padding,
minHeight,
style,
className,
useOpacityOnHover = false,
hoverOpacity,
disableHoverStyles = false,
onClick,
type = 'button',
title,
'aria-expanded': ariaExpanded,
'aria-label': ariaLabel,
} = props
const [isHovered, setIsHovered] = useState(false)
const { state } = useTheme()
const activeTheme = themeProp || state.theme
const themeColors = getThemeColor(activeTheme)
const isDark = activeTheme === THEME_DARK
const sizeConfig = SIZES[size]
const isDisabled = disabled || loading
const buttonStyle = useMemo<React.CSSProperties>(() => {
const bg = backgroundColor ?? themeColors.background
const text = textColor ?? themeColors.fontColor
const border = borderColor ?? (isDark ? 'transparent' : themeColors.borderColor)
const hoverBg = isDark ? themeColors.lightBackground : themeColors.borderColor
const keepBgOnHover = !disableHoverStyles && useOpacityOnHover && isHovered && !isDisabled
const opacityOnHover = hoverOpacity ?? OPACITY.DIMMED
return {
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
gap: '8px',
fontFamily,
fontSize: fontSize ?? sizeConfig.fontSize,
fontWeight: fontWeight ?? 500,
lineHeight: 1.4,
padding: padding ?? sizeConfig.padding,
minHeight: minHeight ?? sizeConfig.minHeight,
borderRadius: borderRadius ?? '6px',
border: `1px solid ${border}`,
backgroundColor: resolveBackgroundColor(
disableHoverStyles,
keepBgOnHover,
outlined,
isHovered,
isDisabled,
bg,
hoverBg,
),
color: outlined ? themeColors.fontColor : text,
cursor: isDisabled ? 'not-allowed' : 'pointer',
opacity: isDisabled ? OPACITY.DIMMED : keepBgOnHover ? opacityOnHover : 1,
width: block ? '100%' : 'auto',
transition: 'background-color 0.15s ease-in-out, opacity 0.15s ease-in-out',
...style,
}
}, [
sizeConfig,
outlined,
isHovered,
isDisabled,
block,
themeColors,
isDark,
backgroundColor,
textColor,
borderColor,
borderRadius,
fontSize,
fontWeight,
padding,
minHeight,
useOpacityOnHover,
hoverOpacity,
style,
])
const handleClick = useCallback(() => {
if (!isDisabled && onClick) onClick()
}, [isDisabled, onClick])
return (
<button
type={type}
className={className}
style={buttonStyle}
onClick={handleClick}
disabled={isDisabled}
title={title}
aria-expanded={ariaExpanded}
aria-label={ariaLabel}
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
{loading && (
<span
style={{
width: 14,
height: 14,
border: '2px solid transparent',
borderTopColor: themeColors.fontColor,
borderRadius: '50%',
animation: 'spin 0.6s linear infinite',
}}
/>
)}
{children}
</button>
)
}
export default GluuButton