Skip to content

Commit

Permalink
fix: measure top of content natively
Browse files Browse the repository at this point in the history
  • Loading branch information
johnhaup committed Oct 10, 2024
1 parent 3c3a964 commit 4b34249
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/components/LazyScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
type LayoutChangeEvent,
} from 'react-native';
import Animated, {
measure,
runOnUI,
useAnimatedRef,
useDerivedValue,
useScrollViewOffset,
Expand Down Expand Up @@ -54,6 +56,7 @@ const LazyScrollView = forwardRef<LazyScrollViewMethods, Props>(
const _containerHeight = useSharedValue(0);
const _contentHeight = useSharedValue(0);
const _hasProvider = useSharedValue(true);
const _statusBarHeight = useSharedValue(StatusBar.currentHeight || 0);

useImperativeHandle(ref, () => ({
scrollTo: (options) => {
Expand All @@ -76,7 +79,7 @@ const LazyScrollView = forwardRef<LazyScrollViewMethods, Props>(
*/
const scrollValue = useScrollViewOffset(_scrollRef);

const topYValue = useSharedValue(0);
const topYValue = useSharedValue(StatusBar.currentHeight || 0);
const bottomYValue = useDerivedValue(
() => _containerHeight.value + topYValue.value
);
Expand All @@ -91,12 +94,12 @@ const LazyScrollView = forwardRef<LazyScrollViewMethods, Props>(
const onLayout = useCallback(
(e: LayoutChangeEvent) => {
_containerHeight.value = e.nativeEvent.layout.height;
_wrapperRef.current?.measureInWindow(
(_: number, y: number, _2: number, height: number) => {
topYValue.value = y + (StatusBar.currentHeight || 0);
_contentHeight.value = height;
runOnUI(() => {
const measurement = measure(_scrollRef);
if (measurement) {
topYValue.value = measurement.pageY + _statusBarHeight.value;
}
);
})();
},
// eslint-disable-next-line react-hooks/exhaustive-deps -- shared values do not trigger re-renders
[]
Expand Down

0 comments on commit 4b34249

Please sign in to comment.