React Native Bottom Sheet

repository·master·Indexed 27 days ago

https://github.com/gorhom/react-native-bottom-sheet

A performant, interactive bottom sheet library for React Native (v5.2.14) supporting dynamic sizing, modal presentations, and smooth gesture interactions. Compatible with Expo, React Native Web, and Reanimated v1, v2, and v3. It integrates with scrolling components like FlashList, FlatList, SectionList, and ScrollView, and provides specialized components such as BottomSheetModal, BottomSheetBackdrop, and BottomSheetHandle.

Tokens
30K
Snippets
53
Records
152
Agent score
88%

What's inside @gorhom/bottom-sheet

  1. Key Features of React Native Bottom Sheet

    master

    The library provides several advanced features:

    • Platform Support: Works with React Native Web and is compatible with Expo.
    • Sizing & Scrolling: Supports Dynamic Sizing and integrates with FlashList, FlatList, SectionList, ScrollView, and View for scrolling interactions.
    • Modal Support: Includes a Bottom Sheet Modal for modal presentation views.
    • Interactions: Smooth gesture interactions, snapping animations, and support for pull to refresh on scrollables.
    • Integrations: Supports React Navigation integration.
    • Compatibility: Compatible with Reanimated v1, v2, and v3.
    • Development: Written in TypeScript with accessibility support.
  2. Choose the correct version based on Reanimated

    master

    The library is maintained in different branches depending on the version of Reanimated you are using. It is highly recommended to use v5 for the most stability and latest features.

    • v5 (Recommended): Written with Reanimated v3 and Gesture Handler v2.
    • v4 (Not maintained): Written with Reanimated v2.
    • v2 (Not maintained): Written with Reanimated v1 and compatible with Reanimated v2.
  3. Implement Pull to Refresh in Bottom Sheet

    master

    Pull to refresh is enabled by default and is activated when the bottom sheet reaches its top snap point. To implement it, provide the refreshing and onRefresh props to any supported Scrollable component (such as BottomSheetFlatList, BottomSheetScrollView, or BottomSheetSectionList).

    Note: The refreshControl prop is currently not supported.

    import React, { useCallback, useMemo } from "react";
    import { StyleSheet, View, Text } from "react-native";
    import BottomSheet, { BottomSheetFlatList } from "@gorhom/bottom-sheet";
    
    const App = () => {
      const data = useMemo(
        () =>
          Array(50)
            .fill(0)
            .map((_, index) => `index-${index}`),
        []
      );
      const snapPoints = useMemo(() => ["25%", "50%"], []);
    
      const handleRefresh = useCallback(() => {
        console.log("handleRefresh");
      }, []);
    
      const renderItem = useCallback(
        ({ item }) => (
          <View style={styles.itemContainer}>
            <Text>{item}</Text>
          </View>
        ),
        []
      );
    
      return (
        <View style={styles.container}>
          <BottomSheet snapPoints={snapPoints}>
            <BottomSheetFlatList
              data={data}
              keyExtractor={(i) => i}
              renderItem={renderItem}
              contentContainerStyle={styles.contentContainer}
              refreshing={false}
              onRefresh={handleRefresh}
            />
          </BottomSheet>
        </View>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
      },
      contentContainer: {
        backgroundColor: "white",
      },
      itemContainer: {
        padding: 6,
        margin: 6,
        backgroundColor: "#eee",
      },
    });
    
    export default App;
  4. Use Scrollable components with Bottom Sheet

    master

    To ensure smooth panning interactions and proper synchronization between scrolling and bottom sheet movement, use the pre-integrated 'Scrollable' components instead of standard React Native scrollable components. These components are specifically designed to work with the bottom sheet container's internal functionalities.

    Available Scrollable components:

    • BottomSheetFlatList
    • BottomSheetSectionList
    • BottomSheetScrollView
    • BottomSheetVirtualizedList
    • BottomSheetView
  5. Handle keyboard appearance with BottomSheetTextInput

    master

    To ensure the Bottom Sheet reacts correctly to keyboard appearance on both iOS and Android, use the pre-integrated BottomSheetTextInput component instead of the standard React Native TextInput. This component communicates internally with the Bottom Sheet to manage layout adjustments when the keyboard opens.

    If you need to use a custom TextInput component, you must manually implement the handleOnFocus and handleOnBlur logic from the BottomSheetTextInput source code to maintain compatibility.

    import React, { useMemo } from "react";
    import { View, StyleSheet } from "react-native";
    import BottomSheet, { BottomSheetTextInput } from "@gorhom/bottom-sheet";
    
    const App = () => {
      const snapPoints = useMemo(() => ["25%"], []);
    
      return (
        <View style={styles.container}>
          <BottomSheet snapPoints={snapPoints}>
            <View style={styles.contentContainer}>
              <BottomSheetTextInput value="Awesome 🎉" style={styles.textInput} />
            </View>
          </BottomSheet>
        </View>
      );
    };
    
    const styles = StyleSheet.create({
      container: {
        flex: 1,
        padding: 24,
        backgroundColor: "grey",
      },
      textInput: {
        alignSelf: "stretch",
        marginHorizontal: 12,
        marginBottom: 12,
        padding: 12,
        borderRadius: 12,
        backgroundColor: "grey",
        color: "white",
        textAlign: "center",
      },
      contentContainer: {
        flex: 1,
        alignItems: "center",
      },
    });
    
    export default App;
  6. Access Bottom Sheet Modal methods via ref

    master

    To use the specific methods of BottomSheetModal, you must create a reference using useRef<BottomSheetModal>(null) and pass it to the ref prop of the BottomSheetModal component. This allows you to call present() and dismiss() programmatically.

    import React, { useRef } from 'react';
    import { Button, BottomSheetModal } from '@gorhom/bottom-sheet';
    
    const App = () => {
      const bottomSheetModalRef = useRef<BottomSheetModal>(null);
    
      const handlePresentPress = () => bottomSheetModalRef.current?.present();
    
      return (
        <>
          <Button title="Present Sheet" onPress={handlePresentPress} />
          <BottomSheetModal ref={bottomSheetModalRef}>
            {/* Modal Content */}
          </BottomSheetModal>
        </>
      );
    };
  7. Create a custom footer using BottomSheetFooter

    master

    To implement a custom footer that stays positioned at the bottom of the BottomSheet and reacts to keyboard appearance, wrap your component with the BottomSheetFooter component.

    Your custom footer component will receive an animatedFooterPosition prop, which is a calculated animated position. To ensure your component can receive this prop, extend the BottomSheetFooterProps interface.

    Key Props for BottomSheetFooter:

    • animatedFooterPosition: The calculated animated position for the footer.
    • bottomInset: A value (typically from useSafeAreaInsets) to avoid bottom notches or safe area obstructions.
    import { BottomSheetFooter, BottomSheetFooterProps } from '@gorhom/bottom-sheet';
    
    interface CustomFooterProps extends BottomSheetFooterProps {}
    
    const CustomFooter = ({ animatedFooterPosition }: CustomFooterProps) => {
      return (
        <BottomSheetFooter
          bottomInset={bottomSafeArea}
          animatedFooterPosition={animatedFooterPosition}
        >
          {/* Your footer content here */}
        </BottomSheetFooter>
      );
    };