|
1 | | -import { ReactElement, useCallback, useEffect, useRef } from "react"; |
| 1 | +import { ReactElement, useCallback, useEffect, useMemo, useRef } from "react"; |
2 | 2 | import { |
3 | 3 | ActionSheetIOS, |
4 | 4 | Appearance, |
| 5 | + Dimensions, |
5 | 6 | Modal, |
6 | 7 | Platform, |
7 | 8 | Pressable, |
@@ -29,6 +30,18 @@ let lastIndexRef = -1; |
29 | 30 | export const NativeBottomSheet = (props: NativeBottomSheetProps): ReactElement => { |
30 | 31 | const bottomSheetRef = useRef<BottomSheet>(null); |
31 | 32 |
|
| 33 | + const snapPoints = useMemo(() => { |
| 34 | + // @gorhom/bottom-sheet relies on Reanimated worklets; passing a stable snapPoints array |
| 35 | + // avoids UI-thread "non-worklet function" crashes caused by unstable/undefined inputs. |
| 36 | + const itemCount = props.itemsBasic.length; |
| 37 | + const itemHeight = 44; |
| 38 | + const verticalPadding = 24; |
| 39 | + const maxHeight = Dimensions.get("window").height * 0.9; |
| 40 | + const estimatedHeight = itemCount * itemHeight + verticalPadding; |
| 41 | + |
| 42 | + return [Math.min(maxHeight, estimatedHeight)]; |
| 43 | + }, [props.itemsBasic.length]); |
| 44 | + |
32 | 45 | const isAvailable = props.triggerAttribute && props.triggerAttribute.status === ValueStatus.Available; |
33 | 46 | const isOpen = |
34 | 47 | props.triggerAttribute && |
@@ -139,12 +152,12 @@ export const NativeBottomSheet = (props: NativeBottomSheetProps): ReactElement = |
139 | 152 | ); |
140 | 153 | }; |
141 | 154 |
|
142 | | - const getContainerStyle = () => { |
| 155 | + const containerStyle = useMemo(() => { |
143 | 156 | if (props.useNative) { |
144 | 157 | return [nativeAndroidStyles.sheetContainer]; |
145 | 158 | } |
146 | 159 | return [styles.sheetContainer, props.styles.container]; |
147 | | - }; |
| 160 | + }, [props.useNative, props.styles.container]); |
148 | 161 |
|
149 | 162 | const handleSheetChanges = (index: number) => { |
150 | 163 | if (!isAvailable) { |
@@ -175,11 +188,12 @@ export const NativeBottomSheet = (props: NativeBottomSheetProps): ReactElement = |
175 | 188 | <BottomSheet |
176 | 189 | ref={bottomSheetRef} |
177 | 190 | index={isOpen ? 0 : -1} // Start closed. |
| 191 | + snapPoints={snapPoints} |
178 | 192 | enablePanDownToClose |
179 | 193 | animateOnMount |
180 | 194 | onClose={() => handleSheetChanges(-1)} |
181 | 195 | onChange={handleSheetChanges} |
182 | | - style={getContainerStyle()} |
| 196 | + style={containerStyle} |
183 | 197 | backdropComponent={renderBackdrop} |
184 | 198 | backgroundStyle={props.styles.container} |
185 | 199 | handleComponent={null} |
|
0 commit comments