Skip to content

Commit

Permalink
clean modal locking process as it was dirty
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasrebam committed Feb 7, 2024
1 parent 8d0bafe commit c4ce02c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
8 changes: 5 additions & 3 deletions 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 { useIsFirstRender } from '../../hooks/useIsFirstRender';

interface SubtitlesModalProps {
isModalVisible: boolean;
Expand All @@ -18,17 +19,18 @@ export const SubtitlesModal = ({
setSubtitles,
}: SubtitlesModalProps) => {
const { lock, unlock } = useLockSpatialNavigation();
const isFirstRender = useIsFirstRender();

// Locks the parent navigator when the modal is open and unlocks it when it's closed
useEffect(() => {
return () => {
if (!isFirstRender) {
if (isModalVisible) {
lock();
} else {
unlock();
}
};
}, [isModalVisible, lock, unlock]);
}
}, [isModalVisible, lock, unlock, isFirstRender]);

return (
<SpatialNavigationRoot>
Expand Down
9 changes: 9 additions & 0 deletions packages/example/src/hooks/useIsFirstRender.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useEffect, useRef } from 'react';

export const useIsFirstRender = () => {
const isFirstRenderRef = useRef(true);
useEffect(() => {
isFirstRenderRef.current = false;
}, []);
return isFirstRenderRef.current;
};

0 comments on commit c4ce02c

Please sign in to comment.