UNPKG

@devvie/bottom-sheet

Version:

The 😎smart , 📦tiny , and 🎗flexible bottom sheet your app craves 🚀

31 lines (30 loc) • 1.14 kB
"use strict"; import { useEffect, useRef } from 'react'; import { BackHandler, Platform } from 'react-native'; /** * Handles closing sheet when back button is pressed on * android and sheet is opened * * @param {boolean} shouldClose Whether to close sheet when back button is pressed * @param {boolean} closeSheet Function to call to close the sheet * @param {boolean} sheetOpen Determines the visibility of the sheet */ const useHandleAndroidBackButtonClose = (shouldClose = true, closeSheet, sheetOpen = false) => { const handler = useRef(undefined); useEffect(() => { if (Platform.OS !== 'android') return; handler.current = BackHandler.addEventListener('hardwareBackPress', () => { if (sheetOpen) { if (shouldClose) { closeSheet?.(); } return true; // prevent back press event bubbling as long as sheet is open } else return false; // when sheet is closed allow bubbling }); return () => { handler.current?.remove?.(); }; }, [shouldClose, closeSheet, sheetOpen]); }; export default useHandleAndroidBackButtonClose; //# sourceMappingURL=index.js.map