-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(spatial-navigation): add spatial navigation scroll view
- Loading branch information
Showing
6 changed files
with
155 additions
and
4 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
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
63 changes: 63 additions & 0 deletions
63
packages/core/src/spatial-navigation/components/ScrollView.tsx
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,63 @@ | ||
import React, { useCallback, RefObject, useRef } from 'react'; | ||
import { ScrollView, View, ViewStyle } from 'react-native'; | ||
import { | ||
SpatialNavigatorParentScrollContext, | ||
useSpatialNavigatorParentScroll, | ||
} from '../context/ParentScrollContext'; | ||
import { scrollToNewlyFocusedElement } from '../helpers/scrollToNewlyfocusedElement'; | ||
|
||
type Props = { | ||
horizontal?: boolean; | ||
offsetFromStart?: number; | ||
children: React.ReactNode; | ||
style?: ViewStyle; | ||
}; | ||
|
||
export const SpatialNavigationScrollView = ({ | ||
horizontal = false, | ||
style, | ||
offsetFromStart = 0, | ||
children, | ||
}: Props) => { | ||
const { scrollToNodeIfNeeded: makeParentsScrollToNodeIfNeeded } = | ||
useSpatialNavigatorParentScroll(); | ||
const scrollViewRef = useRef<ScrollView>(null); | ||
|
||
const scrollToNode = useCallback( | ||
(newlyFocusedElementRef: RefObject<View>) => { | ||
try { | ||
newlyFocusedElementRef?.current?.measureLayout( | ||
scrollViewRef?.current?.getInnerViewNode(), | ||
(left, top) => | ||
scrollToNewlyFocusedElement({ | ||
newlyFocusedElementDistanceToLeftRelativeToLayout: left, | ||
newlyFocusedElementDistanceToTopRelativeToLayout: top, | ||
horizontal, | ||
offsetFromStart, | ||
scrollViewRef, | ||
}), | ||
() => {}, | ||
); | ||
} catch { | ||
// A crash can happen when calling measureLayout when a page unmounts. No impact on focus detected in regular use cases. | ||
} | ||
makeParentsScrollToNodeIfNeeded(newlyFocusedElementRef); // We need to propagate the scroll event for parents if we have nested ScrollViews/VirtualizedLists. | ||
}, | ||
[makeParentsScrollToNodeIfNeeded, horizontal, offsetFromStart], | ||
); | ||
|
||
return ( | ||
<SpatialNavigatorParentScrollContext.Provider value={scrollToNode}> | ||
<ScrollView | ||
ref={scrollViewRef} | ||
horizontal={horizontal} | ||
style={style} | ||
showsHorizontalScrollIndicator={false} | ||
showsVerticalScrollIndicator={false} | ||
scrollEnabled={false} | ||
> | ||
{children} | ||
</ScrollView> | ||
</SpatialNavigatorParentScrollContext.Provider> | ||
); | ||
}; |
12 changes: 12 additions & 0 deletions
12
packages/core/src/spatial-navigation/context/ParentScrollContext.ts
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,12 @@ | ||
import { createContext, RefObject, useContext } from 'react'; | ||
import { View } from 'react-native'; | ||
|
||
export type ScrollToNodeCallback = (ref: RefObject<View>) => void; | ||
export const SpatialNavigatorParentScrollContext = createContext<ScrollToNodeCallback>(() => {}); | ||
|
||
export const useSpatialNavigatorParentScroll = (): { | ||
scrollToNodeIfNeeded: ScrollToNodeCallback; | ||
} => { | ||
const scrollToNodeIfNeeded = useContext(SpatialNavigatorParentScrollContext); | ||
return { scrollToNodeIfNeeded }; | ||
}; |
30 changes: 30 additions & 0 deletions
30
packages/core/src/spatial-navigation/helpers/scrollToNewlyfocusedElement.ts
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,30 @@ | ||
import { RefObject } from 'react'; | ||
import { ScrollView } from 'react-native'; | ||
|
||
export type Props = { | ||
newlyFocusedElementDistanceToLeftRelativeToLayout: number; | ||
newlyFocusedElementDistanceToTopRelativeToLayout: number; | ||
horizontal?: boolean; | ||
offsetFromStart: number; | ||
scrollViewRef: RefObject<ScrollView>; | ||
}; | ||
|
||
export const scrollToNewlyFocusedElement = ({ | ||
newlyFocusedElementDistanceToLeftRelativeToLayout, | ||
newlyFocusedElementDistanceToTopRelativeToLayout, | ||
horizontal, | ||
offsetFromStart, | ||
scrollViewRef, | ||
}: Props) => { | ||
if (horizontal) { | ||
scrollViewRef?.current?.scrollTo({ | ||
x: newlyFocusedElementDistanceToLeftRelativeToLayout - offsetFromStart, | ||
animated: true, | ||
}); | ||
} else { | ||
scrollViewRef?.current?.scrollTo({ | ||
y: newlyFocusedElementDistanceToTopRelativeToLayout - offsetFromStart, | ||
animated: true, | ||
}); | ||
} | ||
}; |