react-native-scroll-into-view
Version:
React-Native port of DOMElement.scrollIntoView() web function, for ScrollView
48 lines • 1.76 kB
JavaScript
export const computeScrollY = (scrollViewLayout, viewLayout, scrollY, insets, align) => {
const scrollViewHeight = scrollViewLayout.height;
const childHeight = viewLayout.height;
// layout measures are relative to window, so we make them relative to ScrollView instead
const viewTopY = viewLayout.y - scrollViewLayout.y;
const viewBottomY = viewTopY + childHeight;
const computationData = {
scrollViewHeight,
scrollY,
viewTopY,
viewBottomY,
insets,
};
switch (align) {
case 'auto':
return computeScrollYAuto(computationData);
case 'top':
return computeScrollYTop(computationData);
case 'bottom':
return computeScrollYBottom(computationData);
case 'center':
return computeScrollYCenter(computationData);
default:
throw new Error(`align=${align} not supported`);
}
};
export const computeScrollYAuto = (data) => {
const { scrollY } = data;
const scrollYTop = computeScrollYTop(data);
if (scrollY > scrollYTop) {
return scrollYTop;
}
const scrollYBottom = computeScrollYBottom(data);
if (scrollY < scrollYBottom) {
return scrollYBottom;
}
return scrollY;
};
export const computeScrollYTop = ({ scrollViewHeight, scrollY, viewTopY, insets, }) => {
return scrollY + viewTopY - insets.top;
};
export const computeScrollYBottom = ({ scrollViewHeight, scrollY, viewTopY, viewBottomY, insets, }) => {
return scrollY + viewBottomY - scrollViewHeight + insets.bottom;
};
export const computeScrollYCenter = (data) => {
return (computeScrollYTop(data) + computeScrollYBottom(data)) / 2;
};
//# sourceMappingURL=computeScrollY.js.map