forked from gorhom/react-native-bottom-sheet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapExample.tsx
More file actions
207 lines (198 loc) · 6.01 KB
/
MapExample.tsx
File metadata and controls
207 lines (198 loc) · 6.01 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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import React, { useCallback, useMemo, useRef } from 'react';
import { View, StyleSheet, Dimensions, StatusBar } from 'react-native';
import MapView from 'react-native-maps';
import { interpolate, Extrapolate, Easing, max } from 'react-native-reanimated';
import { useValue } from 'react-native-redash';
import { useSafeArea } from 'react-native-safe-area-context';
import BottomSheet, {
BottomSheetScrollView,
BottomSheetOverlay,
TouchableOpacity,
useBottomSheetModal,
} from '@gorhom/bottom-sheet';
import withModalProvider from '../withModalProvider';
import { createLocationListMockData, Location } from '../../utils';
import SearchHandle, {
SEARCH_HANDLE_HEIGHT,
} from '../../components/searchHandle';
import LocationItem from '../../components/locationItem';
import LocationDetails, {
LOCATION_DETAILS_HEIGHT,
} from '../../components/locationDetails';
import LocationDetailsHandle from '../../components/locationDetailsHandle';
import Weather from '../../components/weather';
import BlurredBackground from '../../components/blurredBackground';
const { height: SCREEN_HEIGHT } = Dimensions.get('window');
const MapExample = () => {
// refs
const mapRef = useRef<MapView>(null);
const bottomSheetRef = useRef<BottomSheet>(null);
const didRestoreMainSheetPosition = useRef(false);
// hooks
const { present, dismiss } = useBottomSheetModal();
const { top: topSafeArea, bottom: bottomSafeArea } = useSafeArea();
//#region variables
const data = useMemo(() => createLocationListMockData(15), []);
const snapPoints = useMemo(
() => [
SEARCH_HANDLE_HEIGHT + bottomSafeArea,
LOCATION_DETAILS_HEIGHT + bottomSafeArea,
SCREEN_HEIGHT - topSafeArea - (StatusBar.currentHeight ?? 0),
],
[topSafeArea, bottomSafeArea]
);
const animatedPosition = useValue<number>(0);
const animatedModalPosition = useValue<number>(0);
const animatedPositionIndex = useValue<number>(0);
const animatedOverlayOpacity = useMemo(
() =>
interpolate(animatedPosition, {
inputRange: [snapPoints[1], snapPoints[2]],
outputRange: [0, 0.25],
extrapolate: Extrapolate.CLAMP,
}),
[animatedPosition, snapPoints]
);
const weatherAnimatedPosition = useMemo(
() => max(animatedModalPosition, animatedPosition),
[animatedModalPosition, animatedPosition]
);
//#endregion
//#region callbacks
const handleLocationDetailSheetChanges = useCallback((index: number) => {
if (index === 0) {
if (!didRestoreMainSheetPosition.current) {
bottomSheetRef.current?.snapTo(1);
}
didRestoreMainSheetPosition.current = false;
}
}, []);
const handleSheetChanges = useCallback((index: number) => {
console.log('handleSheetChanges', index);
}, []);
const handleTouchStart = useCallback(() => {
bottomSheetRef.current?.collapse();
}, []);
const handleCloseLocationDetails = useCallback(() => {
didRestoreMainSheetPosition.current = true;
bottomSheetRef.current?.snapTo(1);
dismiss();
}, [dismiss]);
const handlePresentLocationDetails = useCallback(
(item: Location) => {
bottomSheetRef.current?.close();
present(
<LocationDetails onClose={handleCloseLocationDetails} {...item} />,
{
initialSnapIndex: 1,
snapPoints,
animatedPosition: animatedModalPosition,
animationDuration: 500,
animationEasing: Easing.out(Easing.exp),
onChange: handleLocationDetailSheetChanges,
handleComponent: LocationDetailsHandle,
backgroundComponent: BlurredBackground,
}
);
},
[
snapPoints,
animatedModalPosition,
present,
handleCloseLocationDetails,
handleLocationDetailSheetChanges,
]
);
//#endregion
//#region styles
const contentContainerStyle = useMemo(
() => [
styles.contentContainerStyle,
{
opacity: interpolate(animatedPositionIndex, {
inputRange: [0, 1],
outputRange: [0, 1],
extrapolate: Extrapolate.CLAMP,
}),
},
],
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
);
//#endregion
// renders
const renderItem = useCallback(
(item, index) => (
<TouchableOpacity
key={`${item.name}.${index}`}
onPress={() => handlePresentLocationDetails(item)}
>
<LocationItem title={item.name} subTitle={item.address} />
</TouchableOpacity>
),
[handlePresentLocationDetails]
);
return (
<View style={styles.container}>
<MapView
ref={mapRef}
initialCamera={{
center: {
latitude: 52.3791,
longitude: 4.9003,
},
heading: 0,
pitch: 0,
zoom: 0,
altitude: 40000,
}}
zoomEnabled={false}
style={styles.mapContainer}
onTouchStart={handleTouchStart}
/>
<BottomSheetOverlay
pointerEvents="none"
animatedOpacity={animatedOverlayOpacity}
/>
<Weather
animatedPosition={weatherAnimatedPosition}
snapPoints={snapPoints}
/>
<BottomSheet
ref={bottomSheetRef}
snapPoints={snapPoints}
initialSnapIndex={1}
topInset={topSafeArea}
animatedPosition={animatedPosition}
animatedPositionIndex={animatedPositionIndex}
handleComponent={SearchHandle}
animationDuration={500}
animationEasing={Easing.out(Easing.exp)}
backgroundComponent={BlurredBackground}
onChange={handleSheetChanges}
>
<BottomSheetScrollView
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="never"
style={contentContainerStyle}
>
{data.map(renderItem)}
</BottomSheetScrollView>
</BottomSheet>
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
padding: 24,
},
mapContainer: {
...StyleSheet.absoluteFillObject,
},
contentContainerStyle: {
flex: 1,
paddingHorizontal: 16,
},
});
export default withModalProvider(MapExample);