/
pressable.tsx
188 lines (173 loc) · 5.21 KB
/
pressable.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
import React from "react";
import { BoxProps } from "../box/box";
import { boxStyleFunctions } from "../../../theme/src/style-functions";
import { BasicComponentProps, StateProps } from "../../../theme/src/types";
import { MotiPressableProps, mergeAnimateProp } from "moti/interactions";
import { PressableProps as RNPressableProps } from "react-native";
import { usePressedState } from "../../../hooks/state/usePressedState";
import { useDisabledState } from "../../../hooks/state/useDisabledState";
import { useStyleProps } from "../../../hooks/useStyleProps";
import { useMotiWithStyleProps } from "../../../hooks/useMotiWithStyleProps";
import { MotiPressable } from "./moti-pressable";
import _ from "lodash";
import { useHoveredState } from "../../../hooks/state/useHoveredState";
// Define the properties for the BasePressable component
export type PressableProps = Omit<BoxProps, keyof MotiPressableProps> &
Omit<
MotiPressableProps,
| "unstable_pressDelay"
| "disabled"
| "animate"
| "from"
| "transition"
| "delay"
| "state"
| "stylePriority"
| "onDidAnimate"
| "exit"
| "exitTransition"
| "animateInitialState"
> &
Pick<
BoxProps,
| "animate"
| "from"
| "transition"
| "delay"
| "state"
| "stylePriority"
| "onDidAnimate"
| "exit"
| "exitTransition"
| "animateInitialState"
> & { testID?: RNPressableProps["testID"] } & StateProps<
"_pressed" | "_hovered" | "_disabled"
> & {
/**
* Duration (in milliseconds) to wait after press down before calling onPressIn.
*
* @default 100
*/
onPressInDelay?: number;
/**
* Whether the press behavior is disabled.
*
* @default false
*/
isDisabled?: boolean;
};
const STYLE_PROPS_FOR_CONTAINER = [
"width",
"height",
"flex",
"alignSelf",
"position",
"top",
"right",
"bottom",
"left",
];
/**
* Pressable is a functional component use to make interactive elements such as buttons, links, and more.
* @returns A MotiPressable component with updated props and styles.
*/
const Pressable = React.memo(
React.forwardRef(
(
{
children,
onPressInDelay = 100,
isDisabled = false,
accessibilityLabel = "Press me!",
accessibilityState = undefined,
onPress = undefined,
onPressIn = undefined,
onHoverIn = undefined,
onHoverOut = undefined,
onPressOut = undefined,
onLongPress = undefined,
...props
}: BasicComponentProps<PressableProps>,
ref: any
) => {
props = useStyleProps(props, boxStyleFunctions);
props = useMotiWithStyleProps(props, boxStyleFunctions);
// Use State for dynamic styles
const { setHovered, propsWithHoveredStyles } = useHoveredState(
props,
boxStyleFunctions
);
// Update props with hovered styles
props = propsWithHoveredStyles;
const { setPressed, propsWithPressedStyles } = usePressedState(
props,
boxStyleFunctions
);
// Update props with pressed styles
props = propsWithPressedStyles;
// Use State for disabled styles
const { propsWithDisabledStyles } = useDisabledState(
props,
boxStyleFunctions,
"basic",
true,
isDisabled
);
// Update props with disabled styles
props = propsWithDisabledStyles;
const containerStyles = {
alignSelf: "flex-start",
..._.pick(props.animate, STYLE_PROPS_FOR_CONTAINER),
..._.pick(props.style, STYLE_PROPS_FOR_CONTAINER),
};
const childStyles = _.omit(props.style || {}, STYLE_PROPS_FOR_CONTAINER);
// Methods to handle local pressable state
const onPressInHandler = () => {
setPressed(true);
if (onPressIn) onPressIn();
};
const onPressOutHandler = () => {
setPressed(false);
if (onPressOut) onPressOut();
};
// Methods to handle local hovered state
const onHoverInHandler = () => {
setHovered(true);
if (onHoverIn) onHoverIn();
};
const onHoverOutHandler = () => {
setHovered(false);
if (onHoverOut) onHoverOut();
};
return (
<MotiPressable
ref={ref}
accessibilityLabel={accessibilityLabel}
onPress={!isDisabled ? onPress : undefined}
onPressIn={!isDisabled ? onPressInHandler : undefined}
onPressOut={!isDisabled ? onPressOutHandler : undefined}
onHoverIn={!isDisabled ? onHoverInHandler : undefined}
onHoverOut={!isDisabled ? onHoverOutHandler : undefined}
onLongPress={!isDisabled ? onLongPress : undefined}
disabled={isDisabled}
transition={{
duration: 50,
}}
{...(props as any)}
styles={childStyles}
containerStyle={containerStyles}
accessibilityRole="button"
animate={(interaction) => {
"worklet";
// Merge the interaction and animate props
return mergeAnimateProp(interaction, props.animate as any);
}}
>
{children}
</MotiPressable>
);
}
)
);
Pressable.displayName = "Pressable";
export default Pressable;