From 76f3ce3972b5f2467d477a7b2e36badbdb6345f8 Mon Sep 17 00:00:00 2001 From: Lei Nelissen Date: Sat, 24 May 2025 00:32:50 +0200 Subject: [PATCH] fix: improve alphabetscroller working --- src/components/AlphabetScroller.tsx | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/src/components/AlphabetScroller.tsx b/src/components/AlphabetScroller.tsx index f4c309b..aa129b3 100644 --- a/src/components/AlphabetScroller.tsx +++ b/src/components/AlphabetScroller.tsx @@ -9,6 +9,7 @@ import { TapGestureHandlerGestureEvent } from 'react-native-gesture-handler'; import useDefaultStyles from './Colors'; +import { useNavigationOffsets } from './SafeNavigatorView'; // interface LetterContainerProps { // onPress: (letter: string) => void; @@ -17,18 +18,16 @@ import useDefaultStyles from './Colors'; const Container = styled.View` position: absolute; - right: 5px; - top: 0; - height: 100%; + right: 0; z-index: 10; - padding: 5px; margin: auto 0; - justify-content: space-around; + justify-content: center; + align-items: center; `; -const Letter = styled.Text` +const Letter = styled.Text<{ isSelected?: boolean }>` text-align: center; - padding: 1px 0; + padding: 1.5px 10px; font-size: 12px; `; @@ -44,6 +43,7 @@ const AlphabetScroller: React.FC = ({ onSelect }) => { const styles = useDefaultStyles(); const [ height, setHeight ] = useState(0); const [ index, setIndex ] = useState(); + const { top, bottom } = useNavigationOffsets(); // Handler for setting the correct height for a single alphabet item const handleLayout = useCallback((event: LayoutChangeEvent) => { @@ -52,7 +52,11 @@ const AlphabetScroller: React.FC = ({ onSelect }) => { // Handler for passing on a new index when it is tapped or swiped const handleGestureEvent = useCallback((event: PanGestureHandlerGestureEvent | TapGestureHandlerGestureEvent) => { - const newIndex = Math.floor(event.nativeEvent.y / height); + const { y } = event.nativeEvent; + const newIndex = Math.min( + Math.max(0, Math.floor(y / height)), + ALPHABET_LETTERS.length - 1 + ); if (newIndex !== index) { setIndex(newIndex); @@ -61,7 +65,7 @@ const AlphabetScroller: React.FC = ({ onSelect }) => { }, [height, index, onSelect]); return ( - + @@ -70,7 +74,10 @@ const AlphabetScroller: React.FC = ({ onSelect }) => { key={l} onLayout={i === 0 ? handleLayout : undefined} > - + {l}