-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathscrollToNewlyfocusedElement.ts
More file actions
32 lines (30 loc) · 1007 Bytes
/
scrollToNewlyfocusedElement.ts
File metadata and controls
32 lines (30 loc) · 1007 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { RefObject } from 'react';
import { CustomScrollViewRef } from '../components/ScrollView/types';
export type Props = {
newlyFocusedElementDistanceToLeftRelativeToLayout: number;
newlyFocusedElementDistanceToTopRelativeToLayout: number;
horizontal?: boolean;
offsetFromStart: number;
scrollViewRef: RefObject<CustomScrollViewRef | null>;
};
export const scrollToNewlyFocusedElement = ({
newlyFocusedElementDistanceToLeftRelativeToLayout,
newlyFocusedElementDistanceToTopRelativeToLayout,
horizontal,
offsetFromStart,
scrollViewRef,
}: Props) => {
if (horizontal) {
scrollViewRef?.current?.scrollTo({
x: newlyFocusedElementDistanceToLeftRelativeToLayout - offsetFromStart,
// @todo make this a props of the component
animated: true,
});
} else {
scrollViewRef?.current?.scrollTo({
y: newlyFocusedElementDistanceToTopRelativeToLayout - offsetFromStart,
// @todo make this a props of the component
animated: true,
});
}
};