$ git show scroll-progress
Scroll progress bar
A thin bar at the top fills as you read down an article, tied directly to the scroll offset.
// Scroll-driven, so Reanimated 4 uses the same scroll handler and animated style
const offset = useSharedValue(0);
const content = useSharedValue(1);
const viewport = useSharedValue(1);
const onScroll = useAnimatedScrollHandler((e) => {
offset.value = e.contentOffset.y;
content.value = e.contentSize.height;
viewport.value = e.layoutMeasurement.height;
});
const bar = useAnimatedStyle(() => {
const max = Math.max(content.value - viewport.value, 1);
return { transform: [{ scaleX: Math.min(Math.max(offset.value / max, 0), 1) }] };
});
<View style={{ flex: 1 }}>
<Animated.ScrollView onScroll={onScroll} scrollEventThrottle={16}>
{article}
</Animated.ScrollView>
{/* position absolute, top 0, left 0, right 0, height 3; grows from the left edge */}
<Animated.View style={[styles.bar, { transformOrigin: 'left' }, bar]} />
</View>