@artsy/palette-mobile 8.7.5 → 9.0.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.
@@ -1,7 +1,7 @@
1
1
  /// <reference types="react" />
2
2
  import { PressableProps } from "react-native";
3
3
  import { HapticFeedbackTypes } from "react-native-haptic-feedback";
4
- import { BoxProps } from "../..";
4
+ import { BoxProps } from "../../atoms/Box";
5
5
  type ButtonSize = "small" | "large";
6
6
  type ButtonVariant = "fillDark" | "fillLight" | "fillGray" | "fillSuccess" | "outline" | "outlineGray" | "outlineLight" | "text";
7
7
  export interface ButtonProps extends BoxProps {
@@ -32,20 +32,23 @@ const react_1 = require("react");
32
32
  const react_native_1 = require("react-native");
33
33
  const react_native_haptic_feedback_1 = __importDefault(require("react-native-haptic-feedback"));
34
34
  const react_native_reanimated_1 = __importStar(require("react-native-reanimated"));
35
- const __1 = require("../..");
35
+ const Spacer_1 = require("../../atoms/Spacer");
36
+ const Box_1 = require("../../atoms/Box");
37
+ const Flex_1 = require("../../atoms/Flex");
36
38
  const MeasuredView_1 = require("../../utils/MeasuredView");
39
+ const Text_1 = require("../Text");
37
40
  const colors_1 = require("./colors");
41
+ const Spinner_1 = require("../Spinner");
42
+ const ANIMATION_DURATION = 150;
38
43
  const Button = ({ children, disabled: disabledProp, haptic, icon, iconPosition = "left", loading: loadingProp, block, longestText, onPress, size = "large", variant = "fillDark", testOnly_pressed, testID, ...restProps }) => {
39
- // these are basically booleans
40
- const disabled = (0, react_native_reanimated_1.useSharedValue)(!!disabledProp ? 1 : 0);
41
- const loading = (0, react_native_reanimated_1.useSharedValue)(!!loadingProp ? 1 : 0);
42
- const pressed = (0, react_native_reanimated_1.useSharedValue)(!!testOnly_pressed ? 1 : 0);
43
- (0, react_1.useEffect)(() => {
44
- disabled.value = disabledProp ? 1 : 0;
45
- loading.value = loadingProp ? 1 : 0;
46
- pressed.value = testOnly_pressed ? 1 : 0;
47
- }, [disabledProp, loadingProp, testOnly_pressed]);
48
- const textStyle = (0, __1.useTextStyleForPalette)(size === "small" ? "xs" : "sm");
44
+ const [disabled, setDisabled, disabledV] = useStateWithProp(!!disabledProp);
45
+ const [loading, setLoading, loadingV] = useStateWithProp(!!loadingProp);
46
+ const [pressed, setPressed, pressedV] = useStateWithProp(!!testOnly_pressed);
47
+ const pressAnimationProgress = (0, react_native_reanimated_1.useSharedValue)(0);
48
+ (0, react_native_reanimated_1.useAnimatedReaction)(() => pressedV.value, (pressedVal) => {
49
+ pressAnimationProgress.value = (0, react_native_reanimated_1.withTiming)(pressedVal, { duration: ANIMATION_DURATION });
50
+ });
51
+ const textStyle = (0, Text_1.useTextStyleForPalette)(size === "small" ? "xs" : "sm");
49
52
  const [longestTextMeasurements, setLongestTextMeasurements] = (0, react_1.useState)({
50
53
  width: 0,
51
54
  height: 0,
@@ -63,7 +66,7 @@ const Button = ({ children, disabled: disabledProp, haptic, icon, iconPosition =
63
66
  if (onPress === undefined || onPress === null) {
64
67
  return;
65
68
  }
66
- if (disabled.value === 1 || loading.value === 1) {
69
+ if (disabled || loading) {
67
70
  return;
68
71
  }
69
72
  if (haptic !== undefined) {
@@ -71,40 +74,50 @@ const Button = ({ children, disabled: disabledProp, haptic, icon, iconPosition =
71
74
  }
72
75
  onPress(event);
73
76
  };
74
- const bgColor = (0, react_native_reanimated_1.useSharedValue)("black");
75
- const borderColor = (0, react_native_reanimated_1.useSharedValue)("black");
76
- const containerColorsAnim = (0, react_native_reanimated_1.useAnimatedStyle)(() => ({
77
- backgroundColor: bgColor.value,
78
- borderColor: borderColor.value,
79
- }));
80
- const textColor = (0, react_native_reanimated_1.useSharedValue)("black");
81
- const underline = (0, react_native_reanimated_1.useSharedValue)(0);
82
- const textAnim = (0, react_native_reanimated_1.useAnimatedStyle)(() => ({
83
- color: textColor.value,
84
- textDecorationLine: underline.value === 1 ? "underline" : "none",
85
- }));
86
77
  const colorsForVariantAndState = (0, colors_1.useColorsForVariantAndState)();
87
- (0, react_native_reanimated_1.useAnimatedReaction)(() => [disabled.value, loading.value, pressed.value], ([disabledVal, loadingVal, pressedVal]) => {
88
- const toColor = (c) => (0, react_native_reanimated_1.withTiming)(c, { duration: 150 });
89
- const states = colorsForVariantAndState[variant];
90
- const colors = disabledVal === 1 ? states.disabled : pressedVal === 1 ? states.pressed : states.active;
91
- const { bg, border, text } = colors;
92
- bgColor.value = toColor(bg);
93
- borderColor.value = toColor(border);
94
- textColor.value = loadingVal === 1 ? "rgba(0, 0, 0, 0)" : text;
95
- underline.value = pressedVal;
96
- }, [variant]);
97
- return ((0, jsx_runtime_1.jsx)(react_native_1.Pressable, { disabled: disabled.value === 1, onPressIn: () => {
98
- if (loading.value === 1) {
78
+ const containerColorsAnim = (0, react_native_reanimated_1.useAnimatedStyle)(() => {
79
+ const colors = colorsForVariantAndState[variant];
80
+ if (disabled) {
81
+ return {
82
+ backgroundColor: colors.disabled.bg,
83
+ borderColor: colors.disabled.border,
84
+ };
85
+ }
86
+ return {
87
+ backgroundColor: (0, react_native_reanimated_1.interpolateColor)(pressAnimationProgress.value, [0, 1], [colors.active.bg, colors.pressed.bg]),
88
+ borderColor: (0, react_native_reanimated_1.interpolateColor)(pressAnimationProgress.value, [0, 1], [colors.active.border, colors.pressed.border]),
89
+ };
90
+ });
91
+ const textAnim = (0, react_native_reanimated_1.useAnimatedStyle)(() => {
92
+ const colors = colorsForVariantAndState[variant];
93
+ if (loading) {
94
+ return { color: "rgba(0, 0, 0, 0)" };
95
+ }
96
+ return {
97
+ color: (0, react_native_reanimated_1.interpolateColor)(pressAnimationProgress.value, [0, 1], [colors.active.text, colors.pressed.text]),
98
+ textDecorationLine: pressAnimationProgress.value > 0 ? "underline" : "none",
99
+ };
100
+ });
101
+ return ((0, jsx_runtime_1.jsx)(react_native_1.Pressable, { disabled: disabled, onPressIn: () => {
102
+ if (loading) {
99
103
  return;
100
104
  }
101
- pressed.value = 1;
105
+ setPressed(true);
102
106
  }, onPressOut: () => {
103
- if (loading.value === 1) {
107
+ if (loading) {
104
108
  return;
105
109
  }
106
- pressed.value = 0;
107
- }, onPress: handlePress, testID: testID, testOnly_pressed: testOnly_pressed, children: (0, jsx_runtime_1.jsx)(__1.Flex, { ...restProps, height: height, width: block ? "100%" : undefined, borderRadius: 50, overflow: "hidden", children: (0, jsx_runtime_1.jsxs)(react_native_reanimated_1.default.View, { style: [{ borderWidth: 1, borderRadius: 50, overflow: "hidden" }, containerColorsAnim], children: [(0, jsx_runtime_1.jsxs)(__1.Flex, { height: "100%", mx: "25px", flexDirection: "row", alignItems: "center", justifyContent: "center", children: [iconPosition === "left-start" && !!icon ? ((0, jsx_runtime_1.jsxs)(__1.Box, { position: "absolute", left: 0, children: [icon, (0, jsx_runtime_1.jsx)(__1.Spacer, { x: 0.5 })] })) : null, iconPosition === "left" && !!icon ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [icon, (0, jsx_runtime_1.jsx)(__1.Spacer, { x: 0.5 })] })) : null, (0, jsx_runtime_1.jsx)(AText, { style: [{ width: Math.ceil(longestTextMeasurements.width) }, textStyle, textAnim], textAlign: "center", children: children }), !__TEST__ && ((0, jsx_runtime_1.jsx)(MeasuredView_1.MeasuredView, { setMeasuredState: setLongestTextMeasurements, children: (0, jsx_runtime_1.jsx)(__1.Text, { color: "red", style: textStyle, children: longestText ? longestText : children }) })), iconPosition === "right" && !!icon ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(__1.Spacer, { x: 0.5 }), icon] })) : null] }), loading.value === 1 ? ((0, jsx_runtime_1.jsx)(__1.Box, { position: "absolute", width: "100%", height: "100%", alignItems: "center", justifyContent: "center", children: (0, jsx_runtime_1.jsx)(__1.Spinner, { size: size, color: spinnerColor }) })) : null] }) }) }));
110
+ setPressed(false);
111
+ }, onPress: handlePress, testID: testID, testOnly_pressed: testOnly_pressed, children: (0, jsx_runtime_1.jsx)(Flex_1.Flex, { flexDirection: "row", children: (0, jsx_runtime_1.jsx)(Flex_1.Flex, { ...restProps, height: height, width: block ? "100%" : undefined, borderRadius: 50, overflow: "hidden", children: (0, jsx_runtime_1.jsx)(AFlex, { borderWidth: 1, borderRadius: 50, overflow: "hidden", style: containerColorsAnim, children: (0, jsx_runtime_1.jsxs)(Flex_1.Flex, { height: "100%", mx: "25px", flexDirection: "row", alignItems: "center", justifyContent: "center", children: [iconPosition === "left-start" && !!icon ? ((0, jsx_runtime_1.jsxs)(Box_1.Box, { position: "absolute", left: 0, children: [icon, (0, jsx_runtime_1.jsx)(Spacer_1.Spacer, { x: 0.5 })] })) : null, iconPosition === "left" && !!icon ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [icon, (0, jsx_runtime_1.jsx)(Spacer_1.Spacer, { x: 0.5 })] })) : null, (0, jsx_runtime_1.jsx)(AText, { style: [{ width: Math.ceil(longestTextMeasurements.width) }, textStyle, textAnim], textAlign: "center", children: children }), (0, jsx_runtime_1.jsx)(MeasuredView_1.MeasuredView, { setMeasuredState: setLongestTextMeasurements, children: (0, jsx_runtime_1.jsx)(Text_1.Text, { color: "red", style: textStyle, children: longestText ? longestText : children }) }), iconPosition === "right" && !!icon ? ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(Spacer_1.Spacer, { x: 0.5 }), icon] })) : null, loading ? ((0, jsx_runtime_1.jsx)(Box_1.Box, { position: "absolute", width: "100%", height: "100%", alignItems: "center", justifyContent: "center", children: (0, jsx_runtime_1.jsx)(Spinner_1.Spinner, { size: size, color: spinnerColor }) })) : null] }) }) }) }) }));
108
112
  };
109
113
  exports.Button = Button;
110
- const AText = react_native_reanimated_1.default.createAnimatedComponent(__1.Text);
114
+ const AText = react_native_reanimated_1.default.createAnimatedComponent(Text_1.Text);
115
+ const AFlex = react_native_reanimated_1.default.createAnimatedComponent(Flex_1.Flex);
116
+ const useStateWithProp = (prop) => {
117
+ const [state, setState] = (0, react_1.useState)(!!prop);
118
+ (0, react_1.useEffect)(() => {
119
+ setState(!!prop);
120
+ }, [prop]);
121
+ const stateV = (0, react_native_reanimated_1.useDerivedValue)(() => (!!state ? 1 : 0), [state]);
122
+ return [state, setState, stateV];
123
+ };
@@ -54,7 +54,7 @@ const Playground = () => {
54
54
  const [loading, setLoading] = (0, react_1.useState)(false);
55
55
  const [disabled, setDisabled] = (0, react_1.useState)(true);
56
56
  const [block, setBlock] = (0, react_1.useState)(false);
57
- return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_native_1.Button, { title: "loading", onPress: () => setLoading((v) => !v) }), (0, jsx_runtime_1.jsx)(react_native_1.Button, { title: "disabled", onPress: () => setDisabled((v) => !v) }), (0, jsx_runtime_1.jsx)(react_native_1.Button, { title: "block", onPress: () => setBlock((v) => !v) }), (0, jsx_runtime_1.jsx)(storybookHelpers_1.List, { children: (0, jsx_runtime_1.jsxs)(_1.Button, { loading: loading, disabled: disabled, block: block, children: ["loading ", loading ? "true" : "false", ", disabled ", disabled ? "true" : "false", ", block", " ", block ? "true" : "false"] }) })] }));
57
+ return ((0, jsx_runtime_1.jsxs)(jsx_runtime_1.Fragment, { children: [(0, jsx_runtime_1.jsx)(react_native_1.Button, { title: "loading", onPress: () => setLoading((v) => !v) }), (0, jsx_runtime_1.jsx)(react_native_1.Button, { title: "disabled", onPress: () => setDisabled((v) => !v) }), (0, jsx_runtime_1.jsx)(react_native_1.Button, { title: "block", onPress: () => setBlock((v) => !v) }), (0, jsx_runtime_1.jsx)(storybookHelpers_1.List, { children: (0, jsx_runtime_1.jsxs)(_1.Button, { loading: loading, disabled: disabled, block: block, longestText: "loading false, disabled false, block false", children: ["loading ", loading ? "true" : "false", ", disabled ", disabled ? "true" : "false", ", block", " ", block ? "true" : "false"] }) })] }));
58
58
  };
59
59
  exports.Playground = Playground;
60
60
  const SpacingAround = () => ((0, jsx_runtime_1.jsxs)(atoms_1.Box, { children: [(0, jsx_runtime_1.jsx)(_1.Button, { m: 2, children: "wow 1" }), (0, jsx_runtime_1.jsx)(_1.Button, { children: "wow 2" })] }));
@@ -12,7 +12,8 @@ interface Props {
12
12
  }
13
13
  /**
14
14
  * A view that renders off-screen, measures the width and height of the view, and reports it back.
15
+ * Note: it is rendering just null when in test mode.
15
16
  */
16
- export declare const MeasuredView: ({ children, setMeasuredState, show }: Props) => JSX.Element;
17
+ export declare const MeasuredView: ({ children, setMeasuredState, show }: Props) => JSX.Element | null;
17
18
  export declare const useOffscreenStyle: (notOffscreen?: boolean) => ViewStyle;
18
19
  export {};
@@ -7,13 +7,16 @@ const react_native_1 = require("react-native");
7
7
  const atoms_1 = require("../atoms");
8
8
  /**
9
9
  * A view that renders off-screen, measures the width and height of the view, and reports it back.
10
+ * Note: it is rendering just null when in test mode.
10
11
  */
11
12
  const MeasuredView = ({ children, setMeasuredState, show }) => {
12
13
  const offscreenStyle = (0, exports.useOffscreenStyle)(show);
13
14
  const onLayout = (0, react_1.useCallback)((event) => {
14
15
  setMeasuredState(event.nativeEvent.layout);
15
16
  }, []);
16
- return ((0, jsx_runtime_1.jsx)(atoms_1.Box, { style: offscreenStyle, backgroundColor: "pink", onLayout: onLayout, children: children }));
17
+ if (__TEST__)
18
+ return null;
19
+ return ((0, jsx_runtime_1.jsx)(atoms_1.Box, { style: offscreenStyle, backgroundColor: "pink", onLayout: onLayout, accessibilityElementsHidden: true, children: children }));
17
20
  };
18
21
  exports.MeasuredView = MeasuredView;
19
22
  const useOffscreenStyle = (notOffscreen) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artsy/palette-mobile",
3
- "version": "8.7.5",
3
+ "version": "9.0.0",
4
4
  "description": "Artsy's design system for React Native",
5
5
  "scripts": {
6
6
  "android": "react-native run-android",