How to add button above the bottomSheet #1737
Replies: 3 comments
-
| You can use the https://gorhom.dev/react-native-bottom-sheet/props#animatedposition |
Beta Was this translation helpful? Give feedback.
-
Is there a solution ? |
Beta Was this translation helpful? Give feedback.
-
| You can use animatedPosition with useAnimatedStyle so the button follows the sheet. Here's a working example: Complete Exampleimport React, { useMemo, useSharedValue } from 'react'; import { Dimensions, Pressable, StyleSheet, View } from 'react-native'; import BottomSheet from '@gorhom/bottom-sheet'; import Animated, { useAnimatedStyle } from 'react-native-reanimated'; const { height: SCREEN_HEIGHT } = Dimensions.get('window'); const BUTTON_SIZE = 56; const BUTTON_OFFSET = 16; // Space between button and sheet function ExampleScreen() { // Create a SharedValue for animatedPosition // Initialize to the starting position of your sheet const animatedPosition = useSharedValue(SCREEN_HEIGHT * 0.75); // Example: starts at 75% down const snapPoints = useMemo(() => ['25%', '75%'], []); return ( <View style={styles.container}> {/* Your main content */} {/* Button that follows the sheet */} <FloatingButton animatedPosition={animatedPosition} /> {/* Bottom Sheet */} <BottomSheet index={0} snapPoints={snapPoints} animatedPosition={animatedPosition} > <View style={styles.sheetContent}> {/* Your sheet content */} </View> </BottomSheet> </View> ); } // Button Component function FloatingButton({ animatedPosition }) { // Calculate the minimum Y position (when sheet is collapsed) // Adjust this based on your collapsed snap point const collapsedSheetHeight = SCREEN_HEIGHT * 0.25; // 25% from snapPoints const lockedYPosition = SCREEN_HEIGHT - collapsedSheetHeight; const animatedStyle = useAnimatedStyle(() => { const sheetTopY = animatedPosition.value; const buttonYFromTop = sheetTopY - BUTTON_SIZE - BUTTON_OFFSET; // Keep button above sheet, but don't go below the locked position const minimumY = lockedYPosition - BUTTON_SIZE - BUTTON_OFFSET; const targetY = Math.min(buttonYFromTop, minimumY); return { transform: [{ translateY: targetY }], }; }, [animatedPosition]); return ( <Animated.View style={[styles.buttonContainer, animatedStyle]}> <Pressable style={({ pressed }) => [ styles.button, pressed && styles.buttonPressed, ]} onPress={() => { // Handle button press }} > {/* Your button content */} </Pressable> </Animated.View> ); } const styles = StyleSheet.create({ container: { flex: 1, }, buttonContainer: { position: 'absolute', right: 16, top: 0, zIndex: 1000, }, button: { width: BUTTON_SIZE, height: BUTTON_SIZE, borderRadius: BUTTON_SIZE / 2, backgroundColor: '#fff', alignItems: 'center', justifyContent: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, elevation: 5, }, buttonPressed: { opacity: 0.7, }, sheetContent: { padding: 16, }, });How It Works
Notes
|
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
How can we add a button above the button sheet as mentioned in the attached screenshot, noting that the button will be always above the bottomSheet even on dragging, i've been looking for an event or dedicated method to achieve that using this library
Beta Was this translation helpful? Give feedback.
All reactions