-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSettingsToggle.jsx
262 lines (253 loc) · 6.98 KB
/
SettingsToggle.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
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
/**
* TEAM: frontend_infra
* @flow strict
*/
import * as React from "react";
import {css} from "aphrodite";
import colors from "./colors";
import {createThemedStylesheet, type ThemeData} from "./styles";
import ThemeNameContext, {
TRANSMISSION,
type Theme,
} from "./context/ThemeNameContext";
type Props = {|
/** Whether the toggle is on. */
+checked?: boolean,
/** Whether the toggle can be interacted with. */
+disabled?: boolean,
/** The string or react node that describes what the toggle will change. */
+label?: string | React.Node,
/** The function invoked when the input is clicked. */
+onChange: (checked: boolean) => void,
/** The function called when a user blurs off the input */
+onBlur?: Event => void,
/** Whether to allow the label text to wrap */
+wrapLabel?: boolean,
|};
/**
* @short Allows users to switch a setting on or off.
* @brandStatus V2
* @status Beta
* @category Interaction
*
* **Differences between toggles, checkboxes, and radios**
*
* - Checkboxes and radios are placed to the left of their label, to indicate there’s a selection to be made from a list of related options.
*
* - Whereas toggles are placed to the right of their label, to indicate there’s a setting to be turned off or on.
*
*
* **Usage**
*
* - If users must acknowledge something, use a single checkbox as opposed to a toggle.
*
* - Use radio groups and checkbox lists for data entry (usually inside forms), as opposed to toggles.
*
* - When a toggle is clicked on, there should be an immediate change to the view.
*
*/
export default function SettingsToggle({
checked = false,
disabled = false,
label = null,
onBlur = null,
onChange,
wrapLabel = true,
}: Props) {
const theme = React.useContext(ThemeNameContext);
const styles = themedStyles(theme);
const containsLabel = label !== "" && label !== null;
const handleClick = () => onChange(!checked);
return (
// eslint-disable-next-line jsx-a11y/label-has-for
<label
className={css(
styles.label,
disabled && styles.disabled,
containsLabel === false && styles.inlineToggle
)}
onBlur={event => onBlur && onBlur(event)}
>
<span
className={css(
styles.inputLabel,
wrapLabel === false && styles.inputLabelNoWrap,
containsLabel === false && styles.inlineToggleLabel
)}
>
{label}
</span>
<input
type="checkbox"
checked={checked}
disabled={disabled}
onChange={handleClick}
className={css(
styles.input,
disabled && styles.disabledInput,
containsLabel === false && styles.inlineToggleInput
)}
/>
</label>
);
// }
}
type SettingsToggleColors = {|
+primary1: string,
+primary2: string,
+primary3: string,
+highlight: string,
+shadow: string,
+focusOutline: string,
|};
function getColors(theme: Theme): SettingsToggleColors {
if (theme === TRANSMISSION) {
return {
primary1: colors.green30,
primary2: colors.green40,
primary3: colors.green50,
highlight: colors.green20,
shadow: `${colors.green40}33`,
focusOutline: `${colors.green20}80`,
};
}
return {
primary1: colors.indigo30,
primary2: colors.indigo40,
primary3: colors.indigo50,
highlight: colors.indigo20,
shadow: "rgba(39, 44, 52, 0.25)",
focusOutline: "rgba(162,172,242,0.5)",
};
}
const themedStyles = createThemedStylesheet(({themeName}: ThemeData) => {
const knobSize = 16;
const trackHeight = 8;
const trackWidth = 28;
const offset = 4;
const themedColors = getColors(themeName);
return {
label: {
cursor: "pointer",
display: "flex",
width: "100%",
flexDirection: "row",
alignItems: "center",
alignContent: "space-between",
lineHeight: "18px",
":hover input": {
borderColor: "transparent",
":after": {
boxShadow: `0px 2px 2px ${themedColors.shadow}`,
border: `2px solid ${colors.grey40}`,
backgroundColor: colors.white,
},
},
":active input": {
":after": {
boxShadow: "none",
backgroundColor: colors.grey40,
},
},
":hover input:checked": {
borderColor: "transparent",
":after": {
boxShadow: `0px 2px 2px ${themedColors.shadow}`,
border: `2px solid ${themedColors.primary2}`,
backgroundColor: themedColors.primary2,
},
},
":active input:checked": {
":after": {
border: `2px solid ${themedColors.primary2}`,
backgroundColor: themedColors.primary2,
},
},
},
input: {
cursor: "pointer",
verticalAlign: "center",
position: "relative",
appearance: "none",
outline: "none",
backgroundColor: colors.grey20,
width: `${trackWidth}px`,
height: `${trackHeight}px`,
transitionProperty: "background, border, box-shadow",
transitionDuration: "150ms",
transform: "ease-in-out",
margin: `0 ${offset}px 0 0`,
flexShrink: 0,
":after": {
content: '""',
position: "absolute",
top: `-${(knobSize - trackHeight) / 2}px`,
left: `-${offset}px`,
width: `${knobSize}px`,
height: `${knobSize}px`,
backgroundColor: colors.white,
border: `2px solid ${colors.grey20}`,
verticalAlign: "center",
transitionProperty: "left, background, border, box-shadow",
transitionDuration: "150ms",
transform: "ease-in-out",
},
":focus:after": {
borderColor: themedColors.primary1,
boxShadow: `0 0 0 4px ${themedColors.focusOutline}`,
},
":checked": {
backgroundColor: themedColors.highlight,
":after": {
left: `${trackWidth + offset - knobSize}px`,
borderColor: themedColors.primary1,
backgroundColor: themedColors.primary1,
},
":focus:after": {
boxShadow: `0 0 0 4px ${themedColors.focusOutline}`,
},
},
},
inputLabel: {
width: "100%",
textOverflow: "ellipsis",
overflow: "hidden",
paddingRight: `${8 + offset}px`,
},
inlineToggle: {
display: "inline-block",
width: `${trackWidth + 2 * offset}px`,
height: `${knobSize}px`,
lineHeight: "inherit",
},
inlineToggleLabel: {
paddingRight: 0,
},
inlineToggleInput: {
marginLeft: `${offset}px`,
},
disabled: {
pointerEvents: "none",
cursor: "default",
color: colors.grey40,
},
disabledInput: {
cursor: "not-allowed",
backgroundColor: colors.grey10,
":after": {
backgroundColor: colors.white,
borderColor: colors.grey20,
},
":checked": {
backgroundColor: colors.grey20,
":after": {
backgroundColor: colors.grey30,
borderColor: colors.grey30,
},
},
},
inputLabelNoWrap: {
whiteSpace: "nowrap",
},
};
});