-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathDraggable.tsx
More file actions
86 lines (79 loc) · 1.89 KB
/
Draggable.tsx
File metadata and controls
86 lines (79 loc) · 1.89 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
import React from 'react';
import { StyleSheet } from 'react-native';
import {
PanGestureHandlerEventPayload,
Gesture,
GestureDetector,
PanGesture,
TapGesture,
} from 'react-native-gesture-handler';
import Animated, { runOnJS, useAnimatedStyle } from 'react-native-reanimated';
type AnimatedPostion = {
x: Animated.SharedValue<number>;
y: Animated.SharedValue<number>;
};
interface DraggableProps {
id: string;
onLongPress: (id: string) => void;
onPositionUpdate: (e: PanGestureHandlerEventPayload) => void;
enabled: boolean;
isActive: boolean;
translation: AnimatedPostion;
position: { x: number; y: number };
dragGesture: PanGesture;
tapEndGesture: TapGesture;
tileSize: number;
rowGap: number;
columnGap: number;
children?: React.ReactNode;
}
const Draggable = ({
id,
children,
onLongPress,
isActive,
translation,
dragGesture,
tapEndGesture,
columnGap,
rowGap,
position,
}: DraggableProps) => {
const tapGesture = Gesture.LongPress()
.minDuration(300)
.onStart(() => runOnJS(onLongPress)(id))
.simultaneousWithExternalGesture(dragGesture)
.simultaneousWithExternalGesture(tapEndGesture);
const translateStyle = useAnimatedStyle(() => {
return {
transform: [
{
translateX: isActive ? translation.x.value : 0,
},
{
translateY: isActive ? translation.y.value : 0,
},
],
};
});
return (
<GestureDetector gesture={tapGesture}>
<Animated.View
style={[
isActive
? { ...styles.absolute, left: position.x, top: position.y }
: { marginHorizontal: columnGap / 2, marginVertical: rowGap / 2 },
translateStyle,
]}>
{children}
</Animated.View>
</GestureDetector>
);
};
const styles = StyleSheet.create({
absolute: {
position: 'absolute',
zIndex: 10,
},
});
export default Draggable;