|
| 1 | +import React, { useCallback, useMemo, useRef } from 'react'; |
| 2 | +import { View, StyleSheet } from 'react-native'; |
| 3 | +import { BottomSheetModal, useBottomSheetModal } from '@gorhom/bottom-sheet'; |
| 4 | +import Button from '../../components/button'; |
| 5 | +import ContactListContainer from '../../components/contactListContainer'; |
| 6 | +import withModalProvider from '../withModalProvider'; |
| 7 | + |
| 8 | +const footerHeight = 100; |
| 9 | + |
| 10 | +const StackExample = () => { |
| 11 | + // hooks |
| 12 | + const { dismiss, dismissAll } = useBottomSheetModal(); |
| 13 | + |
| 14 | + // refs |
| 15 | + const bottomSheetModalARef = useRef<BottomSheetModal>(null); |
| 16 | + const bottomSheetModalBRef = useRef<BottomSheetModal>(null); |
| 17 | + const bottomSheetModalCRef = useRef<BottomSheetModal>(null); |
| 18 | + |
| 19 | + // variables |
| 20 | + const snapPoints = useMemo(() => ['25%', '50%'], []); |
| 21 | + |
| 22 | + // callbacks |
| 23 | + const handlePresentAPress = useCallback(() => { |
| 24 | + if (bottomSheetModalARef.current) { |
| 25 | + bottomSheetModalARef.current.present(); |
| 26 | + } |
| 27 | + }, []); |
| 28 | + const handleDismissAPress = useCallback(() => { |
| 29 | + if (bottomSheetModalARef.current) { |
| 30 | + bottomSheetModalARef.current.dismiss(); |
| 31 | + } |
| 32 | + }, []); |
| 33 | + const handlePresentBPress = useCallback(() => { |
| 34 | + if (bottomSheetModalBRef.current) { |
| 35 | + bottomSheetModalBRef.current.present(); |
| 36 | + } |
| 37 | + }, []); |
| 38 | + const handleDismissBPress = useCallback(() => { |
| 39 | + if (bottomSheetModalBRef.current) { |
| 40 | + bottomSheetModalBRef.current.dismiss(); |
| 41 | + } |
| 42 | + }, []); |
| 43 | + const handlePresentCPress = useCallback(() => { |
| 44 | + if (bottomSheetModalCRef.current) { |
| 45 | + bottomSheetModalCRef.current.present(); |
| 46 | + } |
| 47 | + }, []); |
| 48 | + const handleDismissCPress = useCallback(() => { |
| 49 | + if (bottomSheetModalCRef.current) { |
| 50 | + bottomSheetModalCRef.current.dismiss(); |
| 51 | + } |
| 52 | + }, []); |
| 53 | + const handleDismissAllPress = useCallback(() => { |
| 54 | + dismissAll(); |
| 55 | + }, [dismissAll]); |
| 56 | + |
| 57 | + const handleDismissByHookPress = useCallback(() => { |
| 58 | + dismiss('A'); |
| 59 | + }, [dismiss]); |
| 60 | + |
| 61 | + // renders |
| 62 | + |
| 63 | + const renderBottomSheetContent = useCallback( |
| 64 | + (title, onPress) => ( |
| 65 | + <ContactListContainer |
| 66 | + title={title} |
| 67 | + type="FlatList" |
| 68 | + onItemPress={onPress} |
| 69 | + /> |
| 70 | + ), |
| 71 | + [] |
| 72 | + ); |
| 73 | + return ( |
| 74 | + <> |
| 75 | + <View style={styles.container}> |
| 76 | + <Button |
| 77 | + label="Present Modal A" |
| 78 | + style={styles.buttonContainer} |
| 79 | + onPress={handlePresentAPress} |
| 80 | + /> |
| 81 | + <Button |
| 82 | + label="Dismiss Modal A" |
| 83 | + style={styles.buttonContainer} |
| 84 | + onPress={handleDismissAPress} |
| 85 | + /> |
| 86 | + <Button |
| 87 | + label="Present Modal B" |
| 88 | + style={styles.buttonContainer} |
| 89 | + onPress={handlePresentBPress} |
| 90 | + /> |
| 91 | + <Button |
| 92 | + label="Dismiss Modal B" |
| 93 | + style={styles.buttonContainer} |
| 94 | + onPress={handleDismissBPress} |
| 95 | + /> |
| 96 | + <Button |
| 97 | + label="Present Modal C" |
| 98 | + style={styles.buttonContainer} |
| 99 | + onPress={handlePresentCPress} |
| 100 | + /> |
| 101 | + <Button |
| 102 | + label="Dismiss Modal C" |
| 103 | + style={styles.buttonContainer} |
| 104 | + onPress={handleDismissCPress} |
| 105 | + /> |
| 106 | + |
| 107 | + <Button |
| 108 | + label="Dismiss A By Hook" |
| 109 | + style={styles.buttonContainer} |
| 110 | + onPress={handleDismissByHookPress} |
| 111 | + /> |
| 112 | + |
| 113 | + <BottomSheetModal |
| 114 | + name="A" |
| 115 | + ref={bottomSheetModalARef} |
| 116 | + snapPoints={snapPoints} |
| 117 | + bottomInset={footerHeight} |
| 118 | + children={renderBottomSheetContent('Modal A', handlePresentBPress)} |
| 119 | + /> |
| 120 | + |
| 121 | + <BottomSheetModal |
| 122 | + name="B" |
| 123 | + ref={bottomSheetModalBRef} |
| 124 | + snapPoints={snapPoints} |
| 125 | + bottomInset={footerHeight} |
| 126 | + children={renderBottomSheetContent('Modal B', handlePresentCPress)} |
| 127 | + /> |
| 128 | + |
| 129 | + <BottomSheetModal |
| 130 | + name="C" |
| 131 | + ref={bottomSheetModalCRef} |
| 132 | + index={1} |
| 133 | + snapPoints={snapPoints} |
| 134 | + dismissOnPanDown={false} |
| 135 | + bottomInset={footerHeight} |
| 136 | + children={renderBottomSheetContent('Modal C', handleDismissCPress)} |
| 137 | + /> |
| 138 | + </View> |
| 139 | + <View style={styles.footer}> |
| 140 | + <Button |
| 141 | + label="Dismiss All Modal" |
| 142 | + style={styles.buttonContainer} |
| 143 | + onPress={handleDismissAllPress} |
| 144 | + /> |
| 145 | + </View> |
| 146 | + </> |
| 147 | + ); |
| 148 | +}; |
| 149 | + |
| 150 | +const styles = StyleSheet.create({ |
| 151 | + container: { |
| 152 | + flex: 1, |
| 153 | + padding: 24, |
| 154 | + }, |
| 155 | + buttonContainer: { |
| 156 | + marginBottom: 6, |
| 157 | + }, |
| 158 | + footer: { |
| 159 | + height: footerHeight, |
| 160 | + position: 'absolute', |
| 161 | + bottom: 0, |
| 162 | + width: '100%', |
| 163 | + zIndex: 10, |
| 164 | + backgroundColor: 'white', |
| 165 | + padding: 24, |
| 166 | + |
| 167 | + shadowColor: 'black', |
| 168 | + shadowOffset: { |
| 169 | + width: 0, |
| 170 | + height: -20, |
| 171 | + }, |
| 172 | + shadowOpacity: 0.1, |
| 173 | + shadowRadius: 10, |
| 174 | + elevation: 16, |
| 175 | + }, |
| 176 | +}); |
| 177 | + |
| 178 | +export default withModalProvider(StackExample); |
0 commit comments