All files / packages/components/src scroll-view.tsx

62.5% Statements 10/16
100% Branches 2/2
44.44% Functions 4/9
66.66% Lines 10/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 33 34 35 36 37 38 39 40 41 42 43                      332x                   332x 1596x 1596x 1596x 1251x 1x 1x               1251x 1251x            
import React from "react";
import {
	Platform,
	ScrollView as ScrollViewRaw,
	findNodeHandle,
} from "react-native";
 
export type ScrollHandle = {
	getHandle: () => number | null;
	getRef: () => ScrollViewRaw | null;
};
export const ScrollContext = React.createContext<ScrollHandle>({
	getHandle: () => 0,
	getRef: () => null,
});
 
export type Props = {
	children?: React.ReactNode;
	ref?: React.RefObject<ScrollHandle | null>;
} & Omit<React.ComponentProps<typeof ScrollViewRaw>, "children" | "ref">;
 
export const ScrollView: React.FC<Props> = ({ children, ref, ...props }) => {
	const scrollRef = React.useRef<ScrollViewRaw>(null);
	const context = React.useMemo<React.ContextType<typeof ScrollContext>>(() => {
		if (Platform.OS === "web") {
			return {
				getHandle: () => scrollRef.current as unknown as number,
				getRef: () => scrollRef.current,
			};
		}
		return {
			getHandle: () => findNodeHandle(scrollRef.current),
			getRef: () => scrollRef.current,
		};
	}, []);
	React.useImperativeHandle(ref, () => context, [context]);
	return (
		<ScrollViewRaw ref={scrollRef} {...props}>
			<ScrollContext value={context}>{children}</ScrollContext>
		</ScrollViewRaw>
	);
};