-
Notifications
You must be signed in to change notification settings - Fork 374
Expand file tree
/
Copy pathMessageBubble.tsx
More file actions
145 lines (128 loc) · 4.51 KB
/
MessageBubble.tsx
File metadata and controls
145 lines (128 loc) · 4.51 KB
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
import React, { ReactNode, useMemo, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import Animated, {
interpolate,
runOnJS,
useAnimatedStyle,
useSharedValue,
withSpring,
} from 'react-native-reanimated';
import { MessageItemViewPropsWithContext } from './MessageItemView';
import { MessagesContextValue, useTheme } from '../../../contexts';
import { NativeHandlers } from '../../../native';
const AnimatedWrapper = Animated.createAnimatedComponent(View);
type SwipableMessageWrapperProps = Pick<MessagesContextValue, 'MessageSwipeContent'> &
Pick<MessageItemViewPropsWithContext, 'messageSwipeToReplyHitSlop'> & {
children: ReactNode;
onSwipe: () => void;
};
export const SwipableMessageWrapper = React.memo((props: SwipableMessageWrapperProps) => {
const { MessageSwipeContent, children, messageSwipeToReplyHitSlop, onSwipe } = props;
const styles = useStyles();
const translateX = useSharedValue(0);
const touchStart = useSharedValue<{ x: number; y: number } | null>(null);
const isSwiping = useSharedValue<boolean>(false);
const [shouldRenderAnimatedWrapper, setShouldRenderAnimatedWrapper] = useState<boolean>(false);
const SWIPABLE_THRESHOLD = 25;
const MINIMUM_DISTANCE = 8;
const triggerHaptic = NativeHandlers.triggerHaptic;
const swipeGesture = useMemo(
() =>
Gesture.Pan()
.hitSlop(messageSwipeToReplyHitSlop)
.onBegin((event) => {
touchStart.value = { x: event.x, y: event.y };
})
.onTouchesMove((event, state) => {
if (!touchStart.value || !event.changedTouches.length) {
state.fail();
return;
}
const xDiff = Math.abs(event.changedTouches[0].x - touchStart.value.x);
const yDiff = Math.abs(event.changedTouches[0].y - touchStart.value.y);
const isHorizontalPanning = xDiff > yDiff;
const hasMinimumDistance = xDiff > MINIMUM_DISTANCE || yDiff > MINIMUM_DISTANCE;
// Only activate if there's significant horizontal movement
if (isHorizontalPanning && hasMinimumDistance) {
state.activate();
if (!isSwiping.value) {
runOnJS(setShouldRenderAnimatedWrapper)(true);
}
isSwiping.value = true;
} else if (hasMinimumDistance) {
// If there's significant movement but not horizontal, fail the gesture
state.fail();
}
})
.onStart(() => {
translateX.value = 0;
})
.onChange(({ translationX }) => {
if (translationX > 0) {
translateX.value = translationX;
}
})
.onEnd(() => {
if (translateX.value >= SWIPABLE_THRESHOLD) {
runOnJS(onSwipe)();
if (triggerHaptic) {
runOnJS(triggerHaptic)('impactMedium');
}
}
isSwiping.value = false;
translateX.value = withSpring(
0,
{
dampingRatio: 1,
duration: 500,
overshootClamping: true,
stiffness: 1,
},
() => {
runOnJS(setShouldRenderAnimatedWrapper)(false);
},
);
}),
[messageSwipeToReplyHitSlop, touchStart, isSwiping, translateX, onSwipe, triggerHaptic],
);
const swipeContentAnimatedStyle = useAnimatedStyle(
() => ({
opacity: interpolate(translateX.value, [0, SWIPABLE_THRESHOLD], [0, 1]),
width: translateX.value,
}),
[],
);
return (
<GestureDetector gesture={swipeGesture}>
<View hitSlop={messageSwipeToReplyHitSlop} style={styles.contentWrapper}>
{shouldRenderAnimatedWrapper ? (
<AnimatedWrapper style={[styles.swipeContentContainer, swipeContentAnimatedStyle]}>
{MessageSwipeContent ? <MessageSwipeContent /> : null}
</AnimatedWrapper>
) : null}
{children}
</View>
</GestureDetector>
);
});
const useStyles = () => {
const {
theme: {
messageItemView: { contentWrapper, swipeContentContainer },
},
} = useTheme();
return useMemo(() => {
return StyleSheet.create({
contentWrapper: {
alignItems: 'center',
flexDirection: 'row',
zIndex: 1, // To hide the stick inside the message content
...contentWrapper,
},
swipeContentContainer: {
...swipeContentContainer,
},
});
}, [contentWrapper, swipeContentContainer]);
};