-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbutton.jsx
234 lines (196 loc) · 6.97 KB
/
button.jsx
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
// https://github.com/EnCiv/civil-pursuit/issues/43
import React, {useState, useRef, useEffect} from 'react';
import { createUseStyles } from 'react-jss';
import { PositioningPortal } from '@codastic/react-positioning-portal';
import cx from 'classnames';
/**
* Button component(without stretch goal version) that is styled using react-jss.
* It supports various states like hover, active, and disabled,
* and can be configured with custom styles, titles, and callbacks.
*
* @param {Object} props - The props for the Button component.
*/
function Button(props) {
const {
className = "", // may or may not be passed. Should be applied to the outer most tag, after local classNames
onDone = () => {}, // a function that is called when the button is clicked. - if it exists
title = "", // text to display on hover
disabled = false,
disableOnClick = false, // if true, the button gets disabled after click and stays disabled - prevents resubmission
children,
...otherProps
} = props;
const [isDisabled, setIsDisabled] = useState(disabled);
const [isPortalOpen, setIsPortalOpen] = useState(false);
const timeRef = useRef(null);
const classes = buttonStyles();
const combinedClassName = cx(classes.buttonBase, className);
const handleMouseDown = () => {
timeRef.current = setTimeout(() => {
setIsPortalOpen(true);
}, 500);
}
const handleMouseUp = () => {
clearTimeout(timeRef.current);
setIsPortalOpen(false);
}
useEffect(() => {
if (isPortalOpen) {
const displayTime = Math.min(8, 0.1 * title.length) * 1000;
const timeout = setTimeout(() => {
setIsPortalOpen(false);
}, displayTime);
return () => clearTimeout(timeout);
}
}, [isPortalOpen, title.length]);
return (
<span
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
onMouseLeave={handleMouseUp}
>
<PositioningPortal
isOpen={isPortalOpen}
portalContent={
<span>
{title}
</span>
}>
<button
className={combinedClassName}
title={title}
disabled={isDisabled}
onClick={() => {
if (onDone) onDone();
if (disableOnClick) setIsDisabled(true);
}}
{...otherProps}
>
{children}
</button>
</PositioningPortal>
</span>
)
}
function ModifierButton(props) {
const { className, ...otherProps } = props;
const classes = buttonStyles();
return <Button {...otherProps} className={cx(classes.modifierButton, className)} />;
}
function SecondaryButton(props) {
const { className, ...otherProps } = props;
const classes = buttonStyles();
return <Button {...otherProps} className={cx(classes.secondaryButton, className)} />;
}
function PrimaryButton(props) {
const { className, ...otherProps } = props;
const classes = buttonStyles();
return <Button {...otherProps} className={cx(classes.primaryButton, className)} />;
}
function TextButton(props) {
const { className, ...otherProps } = props;
const classes = buttonStyles();
return <Button {...otherProps} className={cx(classes.textButton, className)} />;
}
const buttonStyles = createUseStyles(theme => ({
buttonBase: { // These are common styles
width: 'auto',
height: 'auto',
borderRadius: '0.5rem',
padding: '0.5rem 1.25rem',
fontFamily: 'Inter, sans-serif',
fontWeight: 600,
fontSize: '1rem',
lineHeight: '1.5rem',
textAlign: 'center',
// Add any other common styles here
},
secondaryButton: {
extend: 'buttonBase',
backgroundColor: theme.colors.white,
color: theme.colors.primaryButtonBlue,
border: `0.125rem solid ${theme.colors.primaryButtonBlue}`,
'&:focus': {
},
'&:disabled': {
backgroundColor: theme.colors.white,
color: theme.colors.disableGray,
border: `0.125rem solid ${theme.colors.disableSecBorderGray}`,
textDecoration: 'none',
transition: 'none',
},
'&:hover, &.hover': {
textDecoration: 'underline',
backgroundColor: theme.colors.white,
borderColor: theme.colors.primaryButtonBlue
},
'&:active': {
backgroundColor: theme.colors.primaryButtonBlue,
color: theme.colors.white,
border: `0.125rem solid ${theme.colors.primaryButtonBlue}`,
textDecoration: 'none',
},
},
modifierButton: {
extend: 'buttonBase',
backgroundColor: theme.colors.white,
color: theme.colors.textBrown,
border: `0.125rem solid ${theme.colors.encivYellow}`,
'&:focus': {
},
'&:hover, &.hover': {
textDecoration: 'underline',
backgroundColor: theme.colors.white,
borderColor: theme.colors.encivYellow
},
'&:active': {
backgroundColor: theme.colors.encivYellow,
color: theme.colors.textBrown,
border: `0.125rem solid ${theme.colors.encivYellow}`,
textDecoration: 'none',
},
},
primaryButton: {
extend: 'buttonBase',
backgroundColor: theme.colors.primaryButtonBlue,
color: theme.colors.white,
border: `0.125rem solid ${theme.colors.primaryButtonBlue}`,
'&:focus': {
},
'&:disabled': {
backgroundColor: theme.colors.borderGray,
color: theme.colors.disableTextBlack,
border: `0.0625rem solid ${theme.colors.borderGray}`,
textDecoration: 'none',
transition: 'none',
},
'&:hover, &.hover': {
textDecoration: 'underline',
backgroundColor: theme.colors.primaryButtonBlue,
borderColor: theme.colors.primaryButtonBlue
},
'&:active': {
backgroundColor: theme.colors.mouseDownPrimeBlue,
border: `0.125rem solid ${theme.colors.primaryButtonBlue}`,
textDecoration: 'none',
},
},
textButton: {
extend: 'buttonBase',
backgroundColor: 'transparent',
color: theme.colors.title,
border: 'none',
textAlign: 'left',
textDecoration: 'underline',
'&:hover, &.hover': {
textDecoration: 'underline',
backgroundColor: 'transparent',
borderColor: 'none'
},
'&:active': {
color: theme.colors.title,
textDecoration: 'none',
},
}
}))
export { Button, ModifierButton, SecondaryButton, PrimaryButton, TextButton };