Skip to content

Commit

Permalink
add navigation event handling, preventing goBack on back key press
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrebam committed Feb 8, 2024
1 parent 8959ac8 commit fff79cf
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/example/src/components/modals/SubtitlesModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 (
<SpatialNavigationRoot>
Expand Down

0 comments on commit fff79cf

Please sign in to comment.