From fff79cf2885fa7a8af778366ef4f842bba0ec479 Mon Sep 17 00:00:00 2001 From: Thomas Renaud Date: Thu, 8 Feb 2024 11:56:09 +0100 Subject: [PATCH] add navigation event handling, preventing goBack on back key press --- .../example/src/components/modals/SubtitlesModal.tsx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/example/src/components/modals/SubtitlesModal.tsx b/packages/example/src/components/modals/SubtitlesModal.tsx index 7f00f414..10650ee9 100644 --- a/packages/example/src/components/modals/SubtitlesModal.tsx +++ b/packages/example/src/components/modals/SubtitlesModal.tsx @@ -5,6 +5,7 @@ import { useLockSpatialNavigation } from '../../../../lib/src/spatial-navigation import { Button } from '../../design-system/components/Button'; import { Spacer } from '../../design-system/components/Spacer'; import { GenericModal } from './GenericModal'; +import { EventArg, useNavigation } from '@react-navigation/native'; interface SubtitlesModalProps { isModalVisible: boolean; @@ -18,16 +19,23 @@ export const SubtitlesModal = ({ setSubtitles, }: SubtitlesModalProps) => { const { lock, unlock } = useLockSpatialNavigation(); + const navigation = useNavigation(); // Locks the parent navigator when the modal is open and unlocks it when it's closed useEffect(() => { if (isModalVisible) { + const navigationListener = (e: EventArg<'beforeRemove', true>) => { + e.preventDefault(); + setIsModalVisible(false); + }; + navigation.addListener('beforeRemove', navigationListener); lock(); return () => { + navigation.removeListener('beforeRemove', navigationListener); unlock(); }; } - }, [isModalVisible, lock, unlock]); + }, [isModalVisible, lock, unlock, navigation, setIsModalVisible]); return (