Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions docs/backlog.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,6 @@ a device yet.
Possible fix: scroll once the list has mounted (for example `initialScrollIndex`
or an effect on the active section), instead of in `show()`.

### `FastScrollIndicator` drops offsets outside 0..1 instead of clamping them

[FastScrollIndicator.tsx:243](../src/FastScrollIndicator/FastScrollIndicator.tsx#L243):
`onScrollToOffsetPercentage` returns early for values below 0 or above 1. On an
iOS bounce the list reports those values, so the thumb stays where it was
instead of reaching the top or the bottom. Clamp to 0..1 instead.

### Sections built from `stickyHeaderIndices` all read "1"

[utils.ts:232](../src/utils.ts#L232): `createFullSectionData` gives each section
Expand Down
1 change: 1 addition & 0 deletions example/src/ExamplesScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const ExamplesScreen = () => {
{ title: 'MemoryLeak', destination: 'MemoryLeak' },
{ title: 'ReplaceSections', destination: 'ReplaceSections' },
{ title: 'IndicatorCost', destination: 'IndicatorCost' },
{ title: 'IndicatorBounce', destination: 'IndicatorBounce' },
];
return (
<>
Expand Down
2 changes: 2 additions & 0 deletions example/src/NavigationTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import DotsLayout from './dotsLayout/DotsLayout';
import MemoryLeak from './memoryLeak/MemoryLeak';
import ReplaceSections from './replaceSections/ReplaceSections';
import IndicatorCost from './indicatorCost/IndicatorCost';
import IndicatorBounce from './indicatorBounce/IndicatorBounce';

const Stack = createStackNavigator<RootStackParamList>();

Expand All @@ -27,6 +28,7 @@ const NavigationTree = () => {
<Stack.Screen name="MemoryLeak" component={MemoryLeak} />
<Stack.Screen name="ReplaceSections" component={ReplaceSections} />
<Stack.Screen name="IndicatorCost" component={IndicatorCost} />
<Stack.Screen name="IndicatorBounce" component={IndicatorBounce} />
</Stack.Group>

<Stack.Group screenOptions={{ presentation: 'modal' }}>
Expand Down
127 changes: 127 additions & 0 deletions example/src/indicatorBounce/IndicatorBounce.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import React, { useCallback, useRef, useState } from 'react';
import {
LayoutChangeEvent,
NativeScrollEvent,
NativeSyntheticEvent,
StyleSheet,
Text,
View,
} from 'react-native';
import { FlashList } from '@shopify/flash-list';
import { FastScrollIndicator } from 'react-native-fast-scroll';

/**
* A short list under a `FastScrollIndicator`, to watch the thumb while the list
* bounces past either end on iOS. Like the production app, the offset passed
* to `onScrollToOffsetPercentage` is over the scrollable height (content minus
* the list's own height) and is not clamped, so a bounce reports values below
* 0 and above 1. The panel shows the last value reported.
*
* On a short list one frame of a fling moves the thumb a long way, so a thumb
* that ignores the out-of-range frames visibly stops short of the end while
* the list bounces.
*/

const ROWS = Array.from({ length: 30 }, (_, i) => i);
const ROW_HEIGHT = 44;

const IndicatorBounce = () => {
const indicatorRef =
useRef<React.ElementRef<typeof FastScrollIndicator>>(null);
const listRef = useRef<FlashList<number>>(null);
const contentHeight = useRef(0);
const listHeight = useRef(0);
const [reported, setReported] = useState(0);

const scrollableHeight = useCallback(
() => Math.max(0, contentHeight.current - listHeight.current),
[]
);

const onContentSizeChange = useCallback((_width: number, height: number) => {
contentHeight.current = height;
}, []);

const onListLayout = useCallback((event: LayoutChangeEvent) => {
listHeight.current = event.nativeEvent.layout.height;
}, []);

const onScroll = useCallback(
(event: NativeSyntheticEvent<NativeScrollEvent>) => {
const scrollable = scrollableHeight();
const offsetPercentage =
scrollable > 0 ? event.nativeEvent.contentOffset.y / scrollable : 0;
indicatorRef.current?.onScrollToOffsetPercentage(offsetPercentage);
setReported(offsetPercentage);
},
[scrollableHeight]
);

const scrollToOffsetPercentage = useCallback(
(offsetPercentage: number) => {
listRef.current?.scrollToOffset({
offset: offsetPercentage * scrollableHeight(),
animated: false,
});
},
[scrollableHeight]
);

const outOfRange = reported < 0 || reported > 1;

return (
<View style={styles.container}>
<View style={styles.panel}>
<Text
testID="indicator-bounce-reported"
style={[styles.report, outOfRange && styles.reportOutOfRange]}
>
Offset reported: {reported.toFixed(3)}
</Text>
</View>
<View style={styles.listContainer}>
<FlashList
ref={listRef}
data={ROWS}
estimatedItemSize={ROW_HEIGHT}
onScroll={onScroll}
scrollEventThrottle={16}
onContentSizeChange={onContentSizeChange}
onLayout={onListLayout}
renderItem={({ item }) => (
<View style={styles.row}>
<Text>Row {item + 1}</Text>
</View>
)}
/>
<View style={styles.indicator}>
<FastScrollIndicator
ref={indicatorRef}
scrollToOffsetPercentage={scrollToOffsetPercentage}
thumbColor={'#1F3A5F'}
scrollBarColor={'rgba(31, 58, 95, 0.12)'}
hideFastScrollIndicatorTimeout={0}
/>
</View>
</View>
</View>
);
};

const styles = StyleSheet.create({
container: { flex: 1 },
panel: { padding: 12, backgroundColor: '#1F3A5F' },
report: { color: 'white', fontSize: 16, fontWeight: '600' },
reportOutOfRange: { color: '#FFB347' },
listContainer: { flex: 1 },
row: {
height: ROW_HEIGHT,
justifyContent: 'center',
paddingHorizontal: 16,
borderBottomWidth: StyleSheet.hairlineWidth,
borderBottomColor: '#ddd',
},
indicator: { position: 'absolute', top: 0, bottom: 0, right: 1 },
});

export default IndicatorBounce;
1 change: 1 addition & 0 deletions example/src/screens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type RootStackParamList = {
MemoryLeak: undefined;
ReplaceSections: undefined;
IndicatorCost: undefined;
IndicatorBounce: undefined;
};
17 changes: 7 additions & 10 deletions src/FastScrollIndicator/FastScrollIndicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export type FastScrollIndicatorHandle = {
/**
* Call this method when the onScroll of your list changes
* You need to calculate the percentage between 0 and 1
* @param offsetPercentage between 0 and 1
* @returns
* @param offsetPercentage between 0 and 1. Values outside it, such as the
* ones an iOS list reports while it bounces, keep the thumb at that end.
*/
onScrollToOffsetPercentage: (offsetPercentage: number) => void;
};
Expand Down Expand Up @@ -272,20 +272,17 @@ export const FastScrollIndicator = React.forwardRef(
forwardedRef,
(): FastScrollIndicatorHandle => ({
onScrollToOffsetPercentage(offsetPercentage: number) {
if (offsetPercentage < 0 || offsetPercentage > 1) {
// console.warn(
// `onScrollToOffsetPercentage only allow values between 0 and 1. Received = ${offsetPercentage}`
// );
return;
}

if (fingerOnTrack.current) {
// event is ignored as the scrolling is being done by the thumbs
return;
}

// A list that bounces past either end (iOS) reports offsets below 0
// and above 1, and can come to rest just past 1. The thumb stays at
// that end of the track.
const clampedOffset = Math.min(1, Math.max(0, offsetPercentage));
translateY.value =
offsetPercentage * (trackHeight.current - thumbSize.height);
clampedOffset * (trackHeight.current - thumbSize.height);

showFastScrollBar();
},
Expand Down
21 changes: 16 additions & 5 deletions src/FastScrollIndicator/__tests__/FastScrollIndicator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,25 @@ describe('FastScrollIndicator', () => {
expect(isScrollBarShown()).toBe(true);
});

it.each([-0.1, 1.1])(
'ignores a scroll offset of %p outside the 0..1 range',
(offset) => {
const { scrollListTo } = renderIndicator();
// An iOS list bounces past either end: from the middle, one scroll frame can
// already report an offset beyond the end it is flung to.
it.each([
[-0.1, 0],
[1.1, 500],
])(
'pins the thumb to the end of the track when the list overscrolls to %p',
(offset, thumbPosition) => {
const { scrollListTo } = renderIndicator({
hideFastScrollIndicatorTimeout: 1000,
});
scrollListTo(0.5);
act(() => jest.advanceTimersByTime(800));

scrollListTo(offset);

expect(isScrollBarShown()).toBe(false);
expect(getThumbPosition()).toBe(thumbPosition);
act(() => jest.advanceTimersByTime(800));
expect(isScrollBarShown()).toBe(true);
}
);

Expand Down
Loading