Skip to content

Commit

Permalink
extract hook to custom hook for the sake of clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrebam committed Feb 8, 2024
1 parent fff79cf commit 6e8cc8e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 21 deletions.
23 changes: 2 additions & 21 deletions packages/example/src/components/modals/SubtitlesModal.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useEffect } from 'react';
import { SpatialNavigationRoot } from '../../../../lib/src/spatial-navigation/components/Root';
import { DefaultFocus } from '../../../../lib/src/spatial-navigation/context/DefaultFocusContext';
import { useLockSpatialNavigation } from '../../../../lib/src/spatial-navigation/context/LockSpatialNavigationContext';
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';
import { useLockModal } from '../../hooks/useLockModal';

interface SubtitlesModalProps {
isModalVisible: boolean;
Expand All @@ -18,24 +16,7 @@ export const SubtitlesModal = ({
setIsModalVisible,
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, navigation, setIsModalVisible]);
useLockModal({ isModalVisible, setIsModalVisible });

return (
<SpatialNavigationRoot>
Expand Down
37 changes: 37 additions & 0 deletions packages/example/src/hooks/useLockModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useEffect } from 'react';
import { useLockSpatialNavigation } from '../../../lib/src/spatial-navigation/context/LockSpatialNavigationContext';
import { EventArg, useNavigation } from '@react-navigation/native';

interface UseLockProps {
isModalVisible: boolean;
setIsModalVisible: (isVisible: boolean) => void;
}

// This hook is used to lock the spatial navigation of parent navigator when a modal is open
// and to prevent the user from closing the modal by pressing the back button

export const useLockModal = ({ isModalVisible, setIsModalVisible }: UseLockProps) => {
// Locks the parent navigator when the modal is open and unlocks it when it's closed
const { lock, unlock } = useLockSpatialNavigation();
useEffect(() => {
if (isModalVisible) {
lock();
}
return () => {
unlock();
};
}, [isModalVisible, lock, unlock]);

// Prevents the user from closing the modal by pressing the back button
const navigation = useNavigation();
useEffect(() => {
const navigationListener = (e: EventArg<'beforeRemove', true>) => {
e.preventDefault();
setIsModalVisible(false);
};
navigation.addListener('beforeRemove', navigationListener);
return () => {
navigation.removeListener('beforeRemove', navigationListener);
};
}, [navigation, isModalVisible, setIsModalVisible]);
};

0 comments on commit 6e8cc8e

Please sign in to comment.