-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
extract hook to custom hook for the sake of clarity
- Loading branch information
1 parent
fff79cf
commit 6e8cc8e
Showing
2 changed files
with
39 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
}; |