@jobber/components-native 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (28) hide show
  1. package/dist/src/Button/Button.js +78 -0
  2. package/dist/src/Button/Button.style.js +92 -0
  3. package/dist/src/Button/components/InternalButtonLoading/InternalButtonLoading.js +36 -0
  4. package/dist/src/Button/components/InternalButtonLoading/InternalButtonLoading.style.js +4 -0
  5. package/dist/src/Button/components/InternalButtonLoading/index.js +1 -0
  6. package/dist/src/Button/index.js +1 -0
  7. package/dist/src/Button/types.js +1 -0
  8. package/dist/src/index.js +1 -0
  9. package/dist/tsconfig.tsbuildinfo +1 -1
  10. package/dist/types/src/Button/Button.d.ts +71 -0
  11. package/dist/types/src/Button/Button.style.d.ts +86 -0
  12. package/dist/types/src/Button/components/InternalButtonLoading/InternalButtonLoading.d.ts +11 -0
  13. package/dist/types/src/Button/components/InternalButtonLoading/InternalButtonLoading.style.d.ts +4 -0
  14. package/dist/types/src/Button/components/InternalButtonLoading/index.d.ts +1 -0
  15. package/dist/types/src/Button/index.d.ts +2 -0
  16. package/dist/types/src/Button/types.d.ts +3 -0
  17. package/dist/types/src/index.d.ts +1 -0
  18. package/package.json +3 -2
  19. package/src/Button/Button.style.ts +116 -0
  20. package/src/Button/Button.test.tsx +298 -0
  21. package/src/Button/Button.tsx +223 -0
  22. package/src/Button/components/InternalButtonLoading/InternalButtonLoading.style.ts +5 -0
  23. package/src/Button/components/InternalButtonLoading/InternalButtonLoading.test.tsx +39 -0
  24. package/src/Button/components/InternalButtonLoading/InternalButtonLoading.tsx +77 -0
  25. package/src/Button/components/InternalButtonLoading/index.ts +1 -0
  26. package/src/Button/index.ts +2 -0
  27. package/src/Button/types.ts +3 -0
  28. package/src/index.ts +1 -0
@@ -0,0 +1,223 @@
1
+ import React from "react";
2
+ import { TouchableHighlight, View } from "react-native";
3
+ import { IconColorNames, IconNames } from "@jobber/design";
4
+ import { XOR } from "ts-xor";
5
+ import { styles } from "./Button.style";
6
+ // eslint-disable-next-line import/no-internal-modules
7
+ import { InternalButtonLoading } from "./components/InternalButtonLoading";
8
+ import { ButtonSize, ButtonType, ButtonVariation } from "./types";
9
+ import { ActionLabel, ActionLabelVariation } from "../ActionLabel";
10
+ import { Icon } from "../Icon";
11
+ import { tokens } from "../utils/design";
12
+
13
+ interface CommonButtonProps {
14
+ /**
15
+ * Press handler
16
+ */
17
+ readonly onPress?: () => void;
18
+
19
+ /**
20
+ * Themes the button to the type of action it performs
21
+ */
22
+ readonly variation?: ButtonVariation;
23
+
24
+ /**
25
+ * Sets the visual hierarchy
26
+ */
27
+ readonly type?: ButtonType;
28
+
29
+ /**
30
+ * Defines the size of the button
31
+ *
32
+ * @default "base"
33
+ */
34
+ readonly size?: ButtonSize;
35
+
36
+ /**
37
+ * Will make the button scale to take up all the available height
38
+ */
39
+ readonly fullHeight?: boolean;
40
+
41
+ /**
42
+ * Will make the button scale to take up all of the available width
43
+ */
44
+ readonly fullWidth?: boolean;
45
+
46
+ /**
47
+ * Makes the button un-clickable
48
+ */
49
+ readonly disabled?: boolean;
50
+
51
+ /**
52
+ * Accessibility hint to help users understand what will happen when they press the button
53
+ */
54
+ readonly accessibilityHint?: string;
55
+
56
+ /**
57
+ * Changes the button interface to imply loading and prevents the press callback
58
+ *
59
+ * @default false
60
+ */
61
+ readonly loading?: boolean;
62
+
63
+ /**
64
+ * Adds an leading icon beside the label.
65
+ */
66
+ readonly icon?: IconNames;
67
+
68
+ /**
69
+ * Accessibility label for the component. This is required for components that
70
+ * have an `icon` but not a `label`.
71
+ *
72
+ * If the string is the same as the `label` prop, you don't need to add an
73
+ * `accessibilityLabel`. **Don't use this for testing purposes.**
74
+ */
75
+ readonly accessibilityLabel?: string;
76
+ }
77
+
78
+ interface LabelButton extends CommonButtonProps {
79
+ /**
80
+ * Text to be displayed on the button
81
+ */
82
+ readonly label: string;
83
+ }
84
+
85
+ interface IconButton extends CommonButtonProps {
86
+ readonly icon: IconNames;
87
+ readonly accessibilityLabel: string;
88
+ }
89
+
90
+ export type ButtonProps = XOR<LabelButton, IconButton>;
91
+ export function Button({
92
+ label,
93
+ onPress,
94
+ variation = "work",
95
+ type = "primary",
96
+ fullHeight = false,
97
+ fullWidth = true,
98
+ disabled = false,
99
+ loading = false,
100
+ size = "base",
101
+ accessibilityLabel,
102
+ accessibilityHint,
103
+ icon,
104
+ }: ButtonProps): JSX.Element {
105
+ const buttonStyle = [
106
+ styles.button,
107
+ styles[variation],
108
+ styles[type],
109
+ styles[size],
110
+ disabled && styles.disabled,
111
+ fullHeight && styles.fullHeight,
112
+ fullWidth && styles.reducedPaddingForFullWidth,
113
+ ];
114
+
115
+ // attempts to use Pressable caused problems. When a ScrollView contained
116
+ // an InputText that was focused, it required two presses to activate the
117
+ // Pressable. Using a TouchableHighlight made things register correctly
118
+ // in a single press
119
+
120
+ return (
121
+ <TouchableHighlight
122
+ onPress={onPress}
123
+ testID={accessibilityLabel || label}
124
+ accessibilityLabel={accessibilityLabel || label}
125
+ accessibilityHint={accessibilityHint}
126
+ accessibilityRole="button"
127
+ accessibilityState={{ disabled, busy: loading }}
128
+ disabled={disabled || loading}
129
+ underlayColor={tokens["color-greyBlue--dark"]}
130
+ activeOpacity={tokens["opacity-pressed"]}
131
+ style={[
132
+ styles.touchable,
133
+ fullWidth && styles.fullWidth,
134
+ fullHeight && styles.fullHeight,
135
+ ]}
136
+ >
137
+ <View style={buttonStyle}>
138
+ {loading && <InternalButtonLoading variation={variation} type={type} />}
139
+ <View style={getContentStyles(label, icon)}>
140
+ {icon && (
141
+ <View style={styles.iconStyle}>
142
+ <Icon
143
+ name={icon}
144
+ color={getIconColorVariation(variation, type, disabled)}
145
+ />
146
+ </View>
147
+ )}
148
+ {label && (
149
+ <View style={styles.labelStyle}>
150
+ <ActionLabel
151
+ variation={getActionLabelVariation(variation, type)}
152
+ disabled={disabled}
153
+ align={icon ? "start" : undefined}
154
+ >
155
+ {label}
156
+ </ActionLabel>
157
+ </View>
158
+ )}
159
+ </View>
160
+ </View>
161
+ </TouchableHighlight>
162
+ );
163
+ }
164
+
165
+ function getActionLabelVariation(
166
+ variation: string,
167
+ type: string,
168
+ ): ActionLabelVariation {
169
+ if (type === "primary" && variation !== "cancel") {
170
+ return "onPrimary";
171
+ }
172
+
173
+ switch (variation) {
174
+ case "learning":
175
+ return "learning";
176
+ case "destructive":
177
+ return "destructive";
178
+ case "cancel":
179
+ return "subtle";
180
+ default:
181
+ return "interactive";
182
+ }
183
+ }
184
+
185
+ function getIconColorVariation(
186
+ variation: ButtonVariation,
187
+ type: string,
188
+ disabled: boolean,
189
+ ): IconColorNames {
190
+ if (disabled) {
191
+ return "disabled";
192
+ }
193
+
194
+ if (type === "primary" && variation !== "cancel") {
195
+ return "white";
196
+ }
197
+
198
+ switch (variation) {
199
+ case "learning":
200
+ return "informative";
201
+ case "destructive":
202
+ return "destructive";
203
+ case "cancel":
204
+ return "interactiveSubtle";
205
+ default:
206
+ return "interactive";
207
+ }
208
+ }
209
+
210
+ function getContentStyles(
211
+ label: string | undefined,
212
+ icon: IconNames | undefined,
213
+ ) {
214
+ if (label && !icon) {
215
+ return undefined;
216
+ }
217
+
218
+ return [
219
+ styles.content,
220
+ icon && !!label && styles.iconPaddingOffset,
221
+ !!label && styles.contentWithLabel,
222
+ ];
223
+ }
@@ -0,0 +1,5 @@
1
+ import { StyleSheet } from "react-native";
2
+
3
+ export const styles = StyleSheet.create({
4
+ image: StyleSheet.absoluteFillObject,
5
+ });
@@ -0,0 +1,39 @@
1
+ import React from "react";
2
+ import { cleanup, render } from "@testing-library/react-native";
3
+ import {
4
+ InternalButtonLoading,
5
+ darkPattern,
6
+ lightPattern,
7
+ } from "./InternalButtonLoading";
8
+ import { ButtonType, ButtonVariation } from "../../types";
9
+
10
+ afterEach(cleanup);
11
+
12
+ describe("Loading pattern", () => {
13
+ it.each<[string, ButtonType, ButtonVariation]>([
14
+ [lightPattern, "primary", "work"],
15
+ [lightPattern, "primary", "destructive"],
16
+ [lightPattern, "primary", "learning"],
17
+ [darkPattern, "primary", "cancel"],
18
+ [darkPattern, "secondary", "cancel"],
19
+ [darkPattern, "secondary", "work"],
20
+ [darkPattern, "secondary", "destructive"],
21
+ [darkPattern, "secondary", "learning"],
22
+ [darkPattern, "tertiary", "cancel"],
23
+ [darkPattern, "tertiary", "work"],
24
+ [darkPattern, "tertiary", "destructive"],
25
+ [darkPattern, "tertiary", "learning"],
26
+ ])(
27
+ "should render a %s pattern on %s %s combination",
28
+ (pattern, type, variation) => {
29
+ const { getByTestId } = render(
30
+ <InternalButtonLoading type={type} variation={variation} />,
31
+ );
32
+
33
+ const component = getByTestId("loadingImage");
34
+ expect(component.props.source).toMatchObject({
35
+ uri: expect.stringContaining(pattern),
36
+ });
37
+ },
38
+ );
39
+ });
@@ -0,0 +1,77 @@
1
+ import React from "react";
2
+ import { ImageBackground, PixelRatio } from "react-native";
3
+ import Animated, {
4
+ Easing,
5
+ useAnimatedStyle,
6
+ useSharedValue,
7
+ withDelay,
8
+ withRepeat,
9
+ withTiming,
10
+ } from "react-native-reanimated";
11
+ import { styles } from "./InternalButtonLoading.style";
12
+ import { tokens } from "../../../utils/design";
13
+ import { ButtonType, ButtonVariation } from "../../types";
14
+
15
+ interface InternalButtonLoadingProps {
16
+ readonly variation: ButtonVariation;
17
+ readonly type: ButtonType;
18
+ }
19
+
20
+ const imageWidth = 96;
21
+ const offset = PixelRatio.roundToNearestPixel(imageWidth / PixelRatio.get());
22
+ const leftOffset = -1 * offset;
23
+
24
+ const AnimatedImage = Animated.createAnimatedComponent(ImageBackground);
25
+
26
+ function InternalButtonLoadingInternal({
27
+ variation,
28
+ type,
29
+ }: InternalButtonLoadingProps): JSX.Element {
30
+ const translateX = useSharedValue(0);
31
+ translateX.value = withRepeat(
32
+ withTiming(offset, {
33
+ duration: tokens["timing-loading"],
34
+ easing: Easing.linear,
35
+ }),
36
+ -1,
37
+ );
38
+
39
+ const opacity = useSharedValue(0);
40
+ opacity.value = withDelay(
41
+ tokens["timing-quick"],
42
+ withTiming(1, {
43
+ duration: tokens["timing-base"],
44
+ easing: Easing.linear,
45
+ }),
46
+ );
47
+
48
+ const animations = useAnimatedStyle(() => ({
49
+ opacity: opacity.value,
50
+ transform: [{ translateX: translateX.value }],
51
+ }));
52
+
53
+ return (
54
+ <AnimatedImage
55
+ testID="loadingImage"
56
+ source={{ uri: getLoadingPattern({ variation, type }) }}
57
+ resizeMode="repeat"
58
+ style={[styles.image, { left: leftOffset }, animations]}
59
+ />
60
+ );
61
+ }
62
+
63
+ export const darkPattern =
64
+ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgAgMAAACf9p+rAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJcEhZcwAAITgAACE4AUWWMWAAAAAMUExURQAAAEdwTAAAAAAAAKDh18UAAAAEdFJOUxkADQwimkzpAAAAtUlEQVRIx+3NqxHDQBRDUc0YuxyXokxgSkmT7sdgP++3YoYrqAsOYDto+7gfpwtfHy4Xfj7cLvw3sYlNbOINAoI4IIgTgrggiBuCIAThQyB8CIQLkXAhEi5EwoVIWEiEhURYSISFRMyQiRkyMUMmZsjECIUYoRAjFGKEQvRQiR4q0UMleqhECwuihQXRwoJoYUEQgiAEQQiCEAQhCEIQhCAIQRCCIARBCIIQBCEIQhCEIAhB8AEuzZ5wHe17xgAAAABJRU5ErkJggg==";
65
+ export const lightPattern =
66
+ "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGAAAABgAgMAAACf9p+rAAAABGdBTUEAALGPC/xhBQAAAAFzUkdCAK7OHOkAAAAJcEhZcwAAITgAACE4AUWWMWAAAAAJUExURf///0dwTP///0SistEAAAADdFJOU0AAILGCadYAAAC0SURBVEjH7c2pFcNAFENRHTMX4pKUE5hS0oT7NZjlbyNmOIJ64AK2g7aP+3G68PXhcuHnw+3CfxOb2MQm3iAgiAOCOCGIC4K4IQhCED4EwodAuBAJFyLhQiRciISFRFhIhIVEWEjEDJmYIRMzZGKGTIxQiBEKMUIhRihED5XooRI9VKKHSrSwIFpYEC0siBYWBCEIQhCEIAhBEIIgBEEIghAEIQhCEIQgCEEQgiAEQQiCEAQfva6WeBniVLgAAAAASUVORK5CYII=";
67
+
68
+ function getLoadingPattern({
69
+ variation,
70
+ type,
71
+ }: InternalButtonLoadingProps): string {
72
+ if (variation === "cancel") return darkPattern;
73
+ if (type === "primary") return lightPattern;
74
+ return darkPattern;
75
+ }
76
+
77
+ export const InternalButtonLoading = React.memo(InternalButtonLoadingInternal);
@@ -0,0 +1 @@
1
+ export { InternalButtonLoading } from "./InternalButtonLoading";
@@ -0,0 +1,2 @@
1
+ export { Button } from "./Button";
2
+ export type { ButtonVariation, ButtonType } from "./types";
@@ -0,0 +1,3 @@
1
+ export type ButtonVariation = "work" | "cancel" | "destructive" | "learning";
2
+ export type ButtonType = "primary" | "secondary" | "tertiary";
3
+ export type ButtonSize = "small" | "base";
package/src/index.ts CHANGED
@@ -9,4 +9,5 @@ export * from "./ActivityIndicator";
9
9
  export * from "./Card";
10
10
  export * from "./StatusLabel";
11
11
  export * from "./AtlantisContext";
12
+ export * from "./Button";
12
13
  export * from "./InputFieldWrapper";