diff --git a/docs/backlog.md b/docs/backlog.md index 7a4889e..12cd9e4 100644 --- a/docs/backlog.md +++ b/docs/backlog.md @@ -65,16 +65,6 @@ until the list stops, so during a fling the thumb may point at the first section the whole time. Not confirmed on a device yet. The fix is the one in "The dots bar remounts every time it shows" below. -### The section list can hide while the finger is still on the dots - -[FastScrollSectionFullList.tsx:73-88](../src/FastScrollSectionDots/FastScrollSectionFullList.tsx#L73-L88): -the list hides 3 s after the last `show()`, and `show()` only runs while the -finger moves. Holding the finger still on the dots for 3 s hides the list and -unlocks the bar mid-touch. Lock the list's timer from the pan's `onBegin` and -start it from `onFinalize` instead of refreshing it per frame. Best done -together with "Dragging the dots does work on every frame" below. Not confirmed -on a device yet. - ## Performance None of these grows memory over time: they cost CPU and short-lived garbage. @@ -117,8 +107,8 @@ animation per frame, even while the finger stays on one section. Possible fix: find the section in the worklet (`findSectionOnPosition` already is one), keep the last `sectionIndex` in a shared value, and only cross to JS -when it changes. Lock the list's timer during the drag instead of refreshing it -(see "The section list can hide while the finger is still on the dots" above). +when it changes. The finger already holds the list open from `onBegin` to +`onFinalize`, so `show()` would not need to touch its hide timer at all. ### Both components restart the hide timer on every scroll frame diff --git a/src/FastScrollSectionDots/FastScrollSectionDots.tsx b/src/FastScrollSectionDots/FastScrollSectionDots.tsx index 6b0c6f9..61f34e8 100644 --- a/src/FastScrollSectionDots/FastScrollSectionDots.tsx +++ b/src/FastScrollSectionDots/FastScrollSectionDots.tsx @@ -161,6 +161,16 @@ const FastScrollSectionDots = React.forwardRef( side === 'left' ? SlideOutLeft : SlideOutRight ).duration(500); + // The section list stays open while a finger rests on the dots. + const holdSectionList = useCallback( + () => fastScrollSectionFullListRef.current?.hold(true), + [] + ); + const releaseSectionList = useCallback( + () => fastScrollSectionFullListRef.current?.hold(false), + [] + ); + const onHide = useCallback(() => { lockScrollBarVisibility(false); }, [lockScrollBarVisibility]); @@ -243,6 +253,8 @@ const FastScrollSectionDots = React.forwardRef( dotColor={dotColor} scrollBarColor={scrollBarColor} onSectionChange={onSectionChange} + onFingerDown={holdSectionList} + onFingerUp={releaseSectionList} /> )} diff --git a/src/FastScrollSectionDots/FastScrollSectionDotsBar.tsx b/src/FastScrollSectionDots/FastScrollSectionDotsBar.tsx index f00e0c0..dc83989 100644 --- a/src/FastScrollSectionDots/FastScrollSectionDotsBar.tsx +++ b/src/FastScrollSectionDots/FastScrollSectionDotsBar.tsx @@ -66,6 +66,12 @@ export type Props = { */ onSectionChange: (section: SectionFullData) => void; + /** A finger touches the dots: called before the first `onSectionChange`. */ + onFingerDown: () => void; + + /** The finger lifts off the dots: called after the last `onSectionChange`. */ + onFingerUp: () => void; + sections: SectionFullData[]; }; @@ -87,6 +93,8 @@ const FastScrollSectionDotsBar = React.forwardRef( scrollBarColor = 'rgba(65, 64, 66, 0.9)', dotMargin = 12, onSectionChange, + onFingerDown, + onFingerUp, ...props }: Props, forwardedRef: React.ForwardedRef @@ -174,10 +182,10 @@ const FastScrollSectionDotsBar = React.forwardRef( }; }); - // The pan gesture below calls onThumbPositionChange through runOnJS, and on + // The pan gesture below calls JS functions through runOnJS, and on // a device Reanimated holds every function a worklet calls, and a copy of // every value it reads, from native code. So the pan is built once and - // captures no app data: the function reads the sections and the callback + // captures no app data: the functions read the sections and the callbacks // through refs, and the worklets read only the dots' positions, from a // shared value. Rebuilt on each render, it kept each render of the app's // screen alive through the sections and callbacks it closed over. @@ -186,12 +194,19 @@ const FastScrollSectionDotsBar = React.forwardRef( const onSectionChangeRef = useRef(onSectionChange); onSectionChangeRef.current = onSectionChange; + const onFingerDownRef = useRef(onFingerDown); + onFingerDownRef.current = onFingerDown; + const onFingerUpRef = useRef(onFingerUp); + onFingerUpRef.current = onFingerUp; + const onThumbPositionChange = useCallback((positionY: number) => { const section = findSectionOnPosition(positionY, sectionsRef.current); if (section) { onSectionChangeRef.current(section); } }, []); + const fingerDown = useCallback(() => onFingerDownRef.current(), []); + const fingerUp = useCallback(() => onFingerUpRef.current(), []); const dotPositions = useMemo( () => sections.map(({ sectionIndex, y }) => ({ sectionIndex, y })), @@ -227,6 +242,7 @@ const FastScrollSectionDotsBar = React.forwardRef( // when the user start a gesture we avoid noise coming from the list enableExternalScrollEvents.value = false; scale.value = thumbScaleOnPress; + runOnJS(fingerDown)(); followFinger(event.y); }) // The finger has moved far enough to activate the pan, then moves on @@ -250,6 +266,7 @@ const FastScrollSectionDotsBar = React.forwardRef( scale.value = 1; runOnJS(onThumbPositionChange)(pointStylePositionY.value); + runOnJS(fingerUp)(); }) ); }, [ @@ -259,6 +276,8 @@ const FastScrollSectionDotsBar = React.forwardRef( dotPositionsValue, dotSize, enableExternalScrollEvents, + fingerDown, + fingerUp, onThumbPositionChange, pointStylePositionY, scale, diff --git a/src/FastScrollSectionDots/FastScrollSectionFullList.tsx b/src/FastScrollSectionDots/FastScrollSectionFullList.tsx index fd4d456..4918937 100644 --- a/src/FastScrollSectionDots/FastScrollSectionFullList.tsx +++ b/src/FastScrollSectionDots/FastScrollSectionFullList.tsx @@ -48,6 +48,12 @@ export type FastScrollSectionFullListHandle = { * @param index The first index */ show: (activeSection: SectionFullData) => void; + + /** + * Keeps the list open while `held` (a finger rests on the dots), then + * closes it after the usual delay once released. + */ + hold: (held: boolean) => void; }; const FastScrollSectionFullList = React.forwardRef( @@ -89,10 +95,21 @@ const FastScrollSectionFullList = React.forwardRef( }; }, [onHide]); - const [startShowActiveIndexVisibleTimer] = useTimer( + const [startHideTimer, clearHideTimer, scheduleHideTimer] = useTimer( activeIndexVisibleCallbacks ); + // The list closes a while after the last show() or scroll. show() only + // runs while the finger moves on the dots, so a finger resting on them + // holds the list open instead. + const held = useRef(false); + const startShowActiveIndexVisibleTimer = useCallback(() => { + startHideTimer(); + if (held.current) { + clearHideTimer(); + } + }, [clearHideTimer, startHideTimer]); + const closeAndScrollToIndex = useCallback( (index: number) => { setActiveIndexVisible(false); @@ -139,6 +156,14 @@ const FastScrollSectionFullList = React.forwardRef( }, 1000); } }, + hold(isHeld: boolean) { + held.current = isHeld; + if (isHeld) { + clearHideTimer(); + } else { + scheduleHideTimer(); + } + }, }) ); diff --git a/src/FastScrollSectionDots/__tests__/FastScrollSectionDots.test.tsx b/src/FastScrollSectionDots/__tests__/FastScrollSectionDots.test.tsx index bc60e42..6da3d98 100644 --- a/src/FastScrollSectionDots/__tests__/FastScrollSectionDots.test.tsx +++ b/src/FastScrollSectionDots/__tests__/FastScrollSectionDots.test.tsx @@ -653,6 +653,26 @@ describe('FastScrollSectionDots', () => { expect(screen.queryByText('Dairy')).not.toBeOnTheScreen(); }); + it('keeps the section list and the dots open while the finger rests on the dots', () => { + const { scrollList } = renderDots({ hideFastScrollIndicatorTimeout: 1000 }); + scrollList(); + + // The finger lands on a dot, moves to another, then stays still. + const finger = touchDown(FINGER_ON.fruits); + finger.moveTo(FINGER_ON.vegetables); + act(() => jest.advanceTimersByTime(10_000)); + expect(screen.getByText('Vegetables')).toBeOnTheScreen(); + + // Once it lifts, the section list closes 3 s later, then the dots. + finger.lift(); + act(() => jest.advanceTimersByTime(2999)); + expect(screen.getByText('Vegetables')).toBeOnTheScreen(); + act(() => jest.advanceTimersByTime(1)); + expect(screen.queryByText('Vegetables')).not.toBeOnTheScreen(); + act(() => jest.advanceTimersByTime(1000)); + expect(isAnythingShown()).toBe(false); + }); + it('scrolls the open section list to center the section under the finger, and again a second later', () => { renderDots({ hideFastScrollIndicatorTimeout: 0 }); drag([FINGER_ON.fruits]);