1
- import React from 'react' ;
1
+ import React , { useRef } from 'react' ;
2
2
import { StyleSheet , Text , View , I18nManager } from 'react-native' ;
3
3
4
- import { FlatList , RectButton } from 'react-native-gesture-handler' ;
4
+ import { FlatList , Pressable , RectButton } from 'react-native-gesture-handler' ;
5
+
6
+ import Reanimated , {
7
+ SharedValue ,
8
+ useAnimatedStyle ,
9
+ } from 'react-native-reanimated' ;
5
10
6
11
import AppleStyleSwipeableRow from './AppleStyleSwipeableRow' ;
7
12
import GmailStyleSwipeableRow from './GmailStyleSwipeableRow' ;
8
13
14
+ import ReanimatedSwipeable , {
15
+ SwipeableMethods ,
16
+ } from 'react-native-gesture-handler/ReanimatedSwipeable' ;
17
+
9
18
// To toggle LTR/RTL change to `true`
10
19
I18nManager . allowRTL ( false ) ;
11
20
@@ -42,25 +51,111 @@ const SwipeableRow = ({ item, index }: { item: DataRow; index: number }) => {
42
51
}
43
52
} ;
44
53
54
+ function LeftAction ( prog : SharedValue < number > , drag : SharedValue < number > ) {
55
+ const styleAnimation = useAnimatedStyle ( ( ) => {
56
+ console . log ( '[R] showLeftProgress:' , prog . value ) ;
57
+ console . log ( '[R] appliedTranslation:' , drag . value ) ;
58
+
59
+ return {
60
+ transform : [ { translateX : drag . value - 60 } ] ,
61
+ } ;
62
+ } ) ;
63
+
64
+ return (
65
+ < Reanimated . View style = { styleAnimation } >
66
+ < Text style = { styles . leftAction } > Text</ Text >
67
+ </ Reanimated . View >
68
+ ) ;
69
+ }
70
+
71
+ function RightAction ( prog : SharedValue < number > , drag : SharedValue < number > ) {
72
+ const styleAnimation = useAnimatedStyle ( ( ) => {
73
+ console . log ( '[R] showRightProgress:' , prog . value ) ;
74
+ console . log ( '[R] appliedTranslation:' , drag . value ) ;
75
+
76
+ return {
77
+ transform : [ { translateX : drag . value + 60 } ] ,
78
+ } ;
79
+ } ) ;
80
+
81
+ return (
82
+ < Reanimated . View style = { styleAnimation } >
83
+ < Text style = { styles . rightAction } > Text</ Text >
84
+ </ Reanimated . View >
85
+ ) ;
86
+ }
87
+
45
88
const Separator = ( ) => < View style = { styles . separator } /> ;
46
89
47
90
export default function App ( ) {
91
+ const reanimatedRef = useRef < SwipeableMethods > ( null ) ;
48
92
return (
49
- < FlatList
50
- data = { DATA }
51
- ItemSeparatorComponent = { Separator }
52
- renderItem = { ( { item, index } ) => (
53
- < SwipeableRow item = { item } index = { index } />
54
- ) }
55
- keyExtractor = { ( _item , index ) => `message ${ index } ` }
56
- />
93
+ < View >
94
+ < Separator />
95
+
96
+ < View style = { styles . controlPanelWrapper } >
97
+ < Text style = { styles . fromText } > Programatical controls</ Text >
98
+ < View style = { styles . controlPanel } >
99
+ < Pressable
100
+ style = { styles . control }
101
+ onPress = { ( ) => {
102
+ reanimatedRef . current ! . openLeft ( ) ;
103
+ } } >
104
+ < Text > open left</ Text >
105
+ </ Pressable >
106
+ < Pressable
107
+ style = { styles . control }
108
+ onPress = { ( ) => {
109
+ reanimatedRef . current ! . close ( ) ;
110
+ } } >
111
+ < Text > close</ Text >
112
+ </ Pressable >
113
+ < Pressable
114
+ style = { styles . control }
115
+ onPress = { ( ) => {
116
+ reanimatedRef . current ! . reset ( ) ;
117
+ } } >
118
+ < Text > reset</ Text >
119
+ </ Pressable >
120
+ < Pressable
121
+ style = { styles . control }
122
+ onPress = { ( ) => {
123
+ reanimatedRef . current ! . openRight ( ) ;
124
+ } } >
125
+ < Text > open right</ Text >
126
+ </ Pressable >
127
+ </ View >
128
+ </ View >
129
+
130
+ < Separator />
131
+
132
+ < ReanimatedSwipeable
133
+ ref = { reanimatedRef }
134
+ containerStyle = { styles . swipeable }
135
+ friction = { 2 }
136
+ leftThreshold = { 80 }
137
+ enableTrackpadTwoFingerGesture
138
+ rightThreshold = { 40 }
139
+ renderLeftActions = { LeftAction }
140
+ renderRightActions = { RightAction } >
141
+ < Text > Use the programatic control panel</ Text >
142
+ </ ReanimatedSwipeable >
143
+
144
+ < Separator />
145
+ < FlatList
146
+ data = { DATA }
147
+ ItemSeparatorComponent = { Separator }
148
+ renderItem = { ( { item, index } ) => (
149
+ < SwipeableRow item = { item } index = { index } />
150
+ ) }
151
+ keyExtractor = { ( _item , index ) => `message ${ index } ` }
152
+ />
153
+ </ View >
57
154
) ;
58
155
}
59
156
60
157
const styles = StyleSheet . create ( {
61
158
rectButton : {
62
- flex : 1 ,
63
- height : 80 ,
64
159
paddingVertical : 10 ,
65
160
paddingHorizontal : 20 ,
66
161
justifyContent : 'space-between' ,
@@ -87,6 +182,31 @@ const styles = StyleSheet.create({
87
182
color : '#999' ,
88
183
fontWeight : 'bold' ,
89
184
} ,
185
+ leftAction : { width : 60 , height : 60 , backgroundColor : '#ff5ca3' } ,
186
+ rightAction : { width : 60 , height : 60 , backgroundColor : '#b658b6' } ,
187
+ swipeable : {
188
+ height : 60 ,
189
+ backgroundColor : 'white' ,
190
+ alignItems : 'center' ,
191
+ justifyContent : 'center' ,
192
+ } ,
193
+ controlPanelWrapper : {
194
+ backgroundColor : 'white' ,
195
+ alignItems : 'center' ,
196
+ } ,
197
+ controlPanel : {
198
+ backgroundColor : 'white' ,
199
+ alignItems : 'center' ,
200
+ flexDirection : 'row' ,
201
+ } ,
202
+ control : {
203
+ flex : 1 ,
204
+ height : 40 ,
205
+ borderWidth : StyleSheet . hairlineWidth ,
206
+ borderColor : 'rgb(200, 199, 204)' ,
207
+ alignItems : 'center' ,
208
+ justifyContent : 'center' ,
209
+ } ,
90
210
} ) ;
91
211
92
212
const DATA : DataRow [ ] = [
0 commit comments