-
Notifications
You must be signed in to change notification settings - Fork 2
/
ButtonSolid.tsx
349 lines (325 loc) · 9.29 KB
/
ButtonSolid.tsx
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
import React, { ComponentProps, useCallback, useEffect, useRef } from "react";
import {
GestureResponderEvent,
Pressable,
StyleSheet,
View
} from "react-native";
import ReactNativeHapticFeedback from "react-native-haptic-feedback";
import Animated, {
Extrapolate,
interpolate,
interpolateColor,
useAnimatedStyle,
useDerivedValue,
useSharedValue,
withSpring
} from "react-native-reanimated";
import {
IOButtonLegacyStyles,
IOButtonStyles,
IOColors,
IOScaleValues,
IOSpringValues,
enterTransitionInnerContent,
enterTransitionInnerContentSmall,
exitTransitionInnerContent,
useIOExperimentalDesign
} from "../../core";
import { WithTestID } from "../../utils/types";
import { IOIconSizeScale, IOIcons, Icon } from "../icons";
import { LoadingSpinner } from "../loadingSpinner";
import { HSpacer } from "../spacer/Spacer";
import { ButtonText } from "../typography/ButtonText";
export type ButtonSolidColor = "primary" | "danger" | "contrast";
type ColorStates = {
default: string;
pressed: string;
label: {
default: IOColors;
disabled: IOColors;
};
};
// Disabled state
// TODO: Remove this when legacy look is deprecated https://pagopa.atlassian.net/browse/IOPLT-153
const colorPrimaryLegacyButtonDisabled: IOColors = "bluegreyLight";
const legacyStyles = StyleSheet.create({
backgroundDisabled: {
backgroundColor: IOColors[colorPrimaryLegacyButtonDisabled]
}
});
const colorPrimaryButtonDisabled: IOColors = "grey-200";
const DISABLED_OPACITY = 0.5;
// Icon size
const iconSize: IOIconSizeScale = 20;
const styles = StyleSheet.create({
backgroundDisabled: {
backgroundColor: IOColors[colorPrimaryButtonDisabled],
opacity: DISABLED_OPACITY
}
});
export type ButtonSolidProps = WithTestID<
{
/**
* @default primary
*/
color?: ButtonSolidColor;
label: string;
/**
* @default false
*/
fullWidth?: boolean;
/**
* @default false
*/
loading?: boolean;
icon?: IOIcons;
/**
* @default start
*/
iconPosition?: "start" | "end";
onPress: (event: GestureResponderEvent) => void;
} & Pick<
ComponentProps<typeof Pressable>,
"disabled" | "accessibilityLabel" | "accessibilityHint"
>
>;
const mapColorStates: Record<
NonNullable<ButtonSolidProps["color"]>,
ColorStates
> = {
// Primary button
primary: {
default: IOColors["blueIO-500"],
pressed: IOColors["blueIO-600"],
label: {
default: "white",
disabled: "grey-700"
}
},
// Danger button
danger: {
default: IOColors["error-600"],
pressed: IOColors["error-500"],
label: {
default: "white",
disabled: "grey-700"
}
},
// Contrast button
contrast: {
default: IOColors.white,
pressed: IOColors["blueIO-50"],
label: {
default: "blueIO-500",
disabled: "grey-700"
}
}
};
// TODO: Remove this when legacy look is deprecated https://pagopa.atlassian.net/browse/IOPLT-153
const mapLegacyColorStates: Record<
NonNullable<ButtonSolidProps["color"]>,
ColorStates
> = {
// Primary button
primary: {
default: IOColors.blue,
pressed: IOColors["blue-600"],
label: {
default: "white",
disabled: "white"
}
},
// Danger button
danger: {
default: IOColors["error-600"],
pressed: IOColors["error-500"],
label: {
default: "white",
disabled: "white"
}
},
// Contrast button
contrast: {
default: IOColors.white,
pressed: IOColors["blue-50"],
label: {
default: "blue",
disabled: "white"
}
}
};
export const ButtonSolid = React.forwardRef<View, ButtonSolidProps>(
(
{
color = "primary",
label,
fullWidth = false,
disabled = false,
loading = false,
icon,
iconPosition = "start",
onPress,
accessibilityLabel,
accessibilityHint,
testID
},
ref
) => {
const isPressed = useSharedValue(0);
const { isExperimental } = useIOExperimentalDesign();
// Scaling transformation applied when the button is pressed
const animationScaleValue = IOScaleValues?.basicButton?.pressedState;
/* Prevent the component from triggering the `isEntering' transition
on the on the first render. Solution from this discussion:
https://github.com/software-mansion/react-native-reanimated/discussions/2513
*/
const isMounted = useRef(false);
useEffect(() => {
// eslint-disable-next-line functional/immutable-data
isMounted.current = true;
}, []);
const colorMap = React.useMemo(
() => (isExperimental ? mapColorStates : mapLegacyColorStates),
[isExperimental]
);
const buttonStyles = React.useMemo(
() => (isExperimental ? IOButtonStyles : IOButtonLegacyStyles),
[isExperimental]
);
// Using a spring-based animation for our interpolations
const progressPressed = useDerivedValue(() =>
withSpring(isPressed.value, IOSpringValues.button)
);
// Interpolate animation values from `isPressed` values
const pressedAnimationStyle = useAnimatedStyle(() => {
// Link color states to the pressed states
const backgroundColor = interpolateColor(
progressPressed.value,
[0, 1],
[colorMap[color].default, colorMap[color].pressed]
);
// Scale down button slightly when pressed
const scale = interpolate(
progressPressed.value,
[0, 1],
[1, animationScaleValue],
Extrapolate.CLAMP
);
return {
backgroundColor,
transform: [{ scale }]
};
});
const onPressIn = useCallback(() => {
// eslint-disable-next-line functional/immutable-data
isPressed.value = 1;
}, [isPressed]);
const onPressOut = useCallback(() => {
// eslint-disable-next-line functional/immutable-data
isPressed.value = 0;
}, [isPressed]);
const handleOnPress = useCallback(
(event: GestureResponderEvent) => {
/* Don't call `onPress` if the button is
in loading state */
if (loading) {
return;
}
ReactNativeHapticFeedback.trigger("impactLight");
onPress(event);
},
[loading, onPress]
);
// Label & Icons colors
const foregroundColor: IOColors = disabled
? colorMap[color]?.label?.disabled
: colorMap[color]?.label?.default;
return (
<Pressable
testID={testID}
ref={ref}
accessible={true}
// Using || operator because empty string is not an accepted value
accessibilityLabel={accessibilityLabel || label}
accessibilityHint={accessibilityHint}
accessibilityState={{
busy: loading,
disabled: disabled || false
}}
accessibilityRole={"button"}
onPress={handleOnPress}
onPressIn={onPressIn}
onPressOut={onPressOut}
disabled={disabled}
style={!fullWidth ? IOButtonStyles.dimensionsDefault : {}}
>
<Animated.View
style={[
buttonStyles.button,
{ overflow: "hidden" },
isExperimental && fullWidth && { paddingHorizontal: 16 },
buttonStyles.buttonSizeDefault,
disabled
? isExperimental
? styles.backgroundDisabled
: legacyStyles.backgroundDisabled
: { backgroundColor: colorMap[color]?.default },
/* Prevent Reanimated from overriding background colors
if button is disabled */
!disabled && pressedAnimationStyle
]}
>
{loading && (
<Animated.View
style={buttonStyles.buttonInner}
entering={
isMounted.current ? enterTransitionInnerContentSmall : undefined
}
exiting={exitTransitionInnerContent}
>
<LoadingSpinner color={foregroundColor} />
</Animated.View>
)}
{!loading && (
<Animated.View
style={[
buttonStyles.buttonInner,
iconPosition === "end" && { flexDirection: "row-reverse" }
]}
entering={
isMounted.current ? enterTransitionInnerContent : undefined
}
/* Temporarily disable the exiting transition because it caused
a weird glitch on page exit */
// exiting={exitTransitionInnerContent}
>
{icon && (
<>
{/* If 'iconPosition' is set to 'end', we use
reverse flex property to invert the position */}
<Icon name={icon} size={iconSize} color={foregroundColor} />
{/* Once we have support for 'gap' property,
we can get rid of that spacer */}
<HSpacer size={8} />
</>
)}
<ButtonText
color={foregroundColor}
style={IOButtonStyles.label}
numberOfLines={1}
ellipsizeMode="tail"
accessible={false}
accessibilityElementsHidden
importantForAccessibility="no-hide-descendants"
>
{label}
</ButtonText>
</Animated.View>
)}
</Animated.View>
</Pressable>
);
}
);
export default ButtonSolid;