1
+ import {
2
+ View ,
3
+ Text ,
4
+ useWindowDimensions ,
5
+ TouchableOpacity
6
+ } from 'react-native'
7
+ import Animated , {
8
+ useSharedValue ,
9
+ useAnimatedStyle ,
10
+ useAnimatedGestureHandler ,
11
+ withSpring ,
12
+ interpolate ,
13
+ runOnJS
14
+ } from 'react-native-reanimated'
15
+ import {
16
+ PanGestureHandler ,
17
+ ScrollView
18
+ } from 'react-native-gesture-handler'
19
+ import React , { useContext , useEffect } from 'react'
20
+ import navigationContext from '../../dataManager/contexts/navigationContext'
21
+ import styles from '../screens/home/styles/bottomHalfModalStyle'
22
+ import Ionicons from '@expo/vector-icons/Ionicons'
23
+
24
+ type BottomHalfModalItemPropType = {
25
+ icon : "trash" | "pencil" ,
26
+ text : string ,
27
+ color : string
28
+ }
29
+
30
+ const BottomHalfModalItem = ( { icon, text, color } : BottomHalfModalItemPropType ) => {
31
+ return (
32
+ < TouchableOpacity onPress = { ( ) => { console . log ( "hello" ) } } style = { styles . modalItem } >
33
+ < React . Fragment >
34
+ < Ionicons color = { color } size = { 25 } style = { styles . modalItemIcon } name = { icon } />
35
+
36
+ < Text style = { [ styles . modalItemText , { color } ] } > { text } </ Text >
37
+ </ React . Fragment >
38
+ </ TouchableOpacity >
39
+ )
40
+ }
41
+
42
+ const BottomHalfModal = ( ) => {
43
+ const { modalVisible, changeModalVisible } = useContext ( navigationContext )
44
+ const dimensions = useWindowDimensions ( )
45
+
46
+ const HEIGHT = dimensions . height
47
+ const MIDDLE = HEIGHT - 150
48
+
49
+ const top = useSharedValue ( HEIGHT )
50
+
51
+ useEffect ( ( ) => {
52
+ if ( modalVisible ) {
53
+ top . value = MIDDLE
54
+ }
55
+ } , [ modalVisible ] )
56
+
57
+ const style = useAnimatedStyle ( ( ) => {
58
+ return {
59
+ top : withSpring (
60
+ interpolate ( top . value ,
61
+ [ 0 , HEIGHT ] ,
62
+ [ - 20 , HEIGHT ] ,
63
+ "clamp"
64
+ )
65
+ ) ,
66
+ opacity : withSpring (
67
+ interpolate ( top . value ,
68
+ [ MIDDLE , HEIGHT ] ,
69
+ [ 1 , .6 ] ,
70
+ "clamp"
71
+ )
72
+ )
73
+ }
74
+ } )
75
+
76
+ const backgroundStyle = useAnimatedStyle ( ( ) => {
77
+ return {
78
+ opacity : withSpring (
79
+ interpolate ( top . value ,
80
+ [ MIDDLE , HEIGHT - 120 ] ,
81
+ [ .6 , 0 ] ,
82
+ "clamp"
83
+ )
84
+ ) ,
85
+ transform : [
86
+ {
87
+ translateY : interpolate ( top . value ,
88
+ [ MIDDLE , HEIGHT ] ,
89
+ [ 1 , 0 ]
90
+ ) > 0 ? 0 : dimensions . height ,
91
+ }
92
+ ]
93
+ }
94
+ } )
95
+
96
+ const changeModalVisibleWrapper = ( ) => {
97
+ changeModalVisible ( )
98
+ }
99
+
100
+ type contextType = {
101
+ startTop : number
102
+ }
103
+
104
+ const eventHandler = useAnimatedGestureHandler ( {
105
+ onStart : ( _ , context : contextType ) => {
106
+ context . startTop = top . value
107
+ } ,
108
+ onActive : ( event , context ) => {
109
+ top . value = context . startTop + event . translationY
110
+ } ,
111
+ onEnd : ( ) => {
112
+ if ( top . value > MIDDLE + 100 ) {
113
+ top . value = HEIGHT
114
+
115
+ runOnJS ( changeModalVisibleWrapper ) ( )
116
+ } else if ( top . value < MIDDLE - 100 ) {
117
+ console . log ( "La" )
118
+ top . value = 0
119
+ } else {
120
+ top . value = MIDDLE
121
+ }
122
+ }
123
+ } )
124
+
125
+ return (
126
+ < >
127
+ < PanGestureHandler onGestureEvent = { eventHandler } >
128
+ < Animated . View
129
+ style = { [
130
+ styles . sheet ,
131
+ style
132
+ ] }
133
+ >
134
+ < View style = { styles . sheetIndicator } />
135
+
136
+ < ScrollView
137
+ style = { styles . modalContainer }
138
+ >
139
+ < BottomHalfModalItem icon = "pencil" text = "Update Task" color = "#212529" />
140
+ < BottomHalfModalItem icon = "trash" text = "Delete Task" color = "#ef233c" />
141
+ </ ScrollView >
142
+ </ Animated . View >
143
+ </ PanGestureHandler >
144
+
145
+ < Animated . View
146
+ style = { [
147
+ {
148
+ position : "absolute" ,
149
+ top : 0 ,
150
+ left : 0 ,
151
+ right : 0 ,
152
+ bottom : 0 ,
153
+ backgroundColor : "black"
154
+ } ,
155
+ backgroundStyle
156
+ ] }
157
+ />
158
+ </ >
159
+ )
160
+ }
161
+
162
+ export default BottomHalfModal
0 commit comments