All files / packages/components/src view.base.tsx

83.33% Statements 15/18
50% Branches 3/6
100% Functions 4/4
83.33% Lines 15/18

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                    332x 164883x 164883x 164883x 705x 1x     1x 1x     1x 1x     705x       705x 705x 705x   164883x    
import React from "react";
// oxlint-disable-next-line eslint-js/no-restricted-syntax
import type { View } from "react-native";
 
import { ScrollContext } from "~components/scroll-view";
 
export type ViewHandle = {
	scrollIntoView: () => void;
};
 
export const useScrollView = (ref?: React.Ref<ViewHandle | null>) => {
	const innerRef = React.useRef<View>(null);
	const context = React.use(ScrollContext);
	React.useImperativeHandle(ref, () => {
		const scrollIntoView = () => {
			Iif (!innerRef.current) {
				return;
			}
			const handle = context.getHandle();
			Iif (!handle) {
				return;
			}
			innerRef.current.measureLayout(handle, (x, y) =>
				context.getRef()?.scrollTo({ x, y }),
			);
		};
		Iif (!innerRef.current) {
			return { scrollIntoView };
		}
		// This is needed for Trigger components in web version to hook into child views
		const viewHandle = innerRef.current as unknown as ViewHandle;
		viewHandle.scrollIntoView = scrollIntoView;
		return viewHandle;
	}, [context]);
	return innerRef;
};