diff --git a/src/components/Categories.tsx b/src/components/Categories.tsx index 7b51347..f6f5df8 100644 --- a/src/components/Categories.tsx +++ b/src/components/Categories.tsx @@ -6,12 +6,13 @@ import { CategoryItem } from './CategoryItem' import { exhaustiveTypeCheck } from '../utils/exhaustiveTypeCheck' import { defaultTheme } from '../contexts/KeyboardContext' -const CATEGORY_ELEMENT_WIDTH = 37 +export const CATEGORY_ELEMENT_WIDTH = 37 const Separator = () => type Props = { scrollNav?: Animated.Value + scrollEmojiCategoryListToIndex: (index: number) => void } export const Categories = (p: Props) => { @@ -27,9 +28,13 @@ export const Categories = (p: Props) => { } = React.useContext(KeyboardContext) const scrollNav = React.useRef(new Animated.Value(0)).current - const handleScrollToCategory = React.useCallback(() => { - setShouldAnimateScroll(enableCategoryChangeAnimation) - }, [setShouldAnimateScroll, enableCategoryChangeAnimation]) + const handleScrollToCategory = React.useCallback( + (index: number) => { + setShouldAnimateScroll(enableCategoryChangeAnimation) + p.scrollEmojiCategoryListToIndex(index) + }, + [setShouldAnimateScroll, enableCategoryChangeAnimation, p], + ) const renderItem = React.useCallback( ({ item, index }: { item: CategoryNavigationItem; index: number }) => ( diff --git a/src/components/CategoryItem.tsx b/src/components/CategoryItem.tsx index d46d865..cfef131 100644 --- a/src/components/CategoryItem.tsx +++ b/src/components/CategoryItem.tsx @@ -1,20 +1,20 @@ import * as React from 'react' import { View, StyleSheet, TouchableOpacity } from 'react-native' import { KeyboardContext } from '../contexts/KeyboardContext' -import type { CategoryNavigationItem, CategoryTypes } from '../types' +import type { CategoryNavigationItem } from '../types' import { Icon } from './Icon' type CategoryItemProps = { item: CategoryNavigationItem index: number - handleScrollToCategory: (category: CategoryTypes) => void + handleScrollToCategory: (index: number) => void } export const CategoryItem = ({ item, index, handleScrollToCategory }: CategoryItemProps) => { const { activeCategoryIndex, theme, setActiveCategoryIndex } = React.useContext(KeyboardContext) const handleSelect = () => { - handleScrollToCategory(item.category) + handleScrollToCategory(index) setActiveCategoryIndex(index) } diff --git a/src/components/EmojiStaticKeyboard.tsx b/src/components/EmojiStaticKeyboard.tsx index d1934c9..d80cb76 100644 --- a/src/components/EmojiStaticKeyboard.tsx +++ b/src/components/EmojiStaticKeyboard.tsx @@ -13,13 +13,12 @@ import { import { type EmojisByCategory } from '../types' import { EmojiCategory } from './EmojiCategory' import { KeyboardContext } from '../contexts/KeyboardContext' -import { Categories } from './Categories' +import { Categories, CATEGORY_ELEMENT_WIDTH } from './Categories' import { SearchBar } from './SearchBar' import { useKeyboardStore } from '../store/useKeyboardStore' import { ConditionalContainer } from './ConditionalContainer' import { SkinTones } from './SkinTones' -const CATEGORY_ELEMENT_WIDTH = 37 const isAndroid = Platform.OS === 'android' export const EmojiStaticKeyboard = React.memo( @@ -85,13 +84,19 @@ export const EmojiStaticKeyboard = React.memo( [activeCategoryIndex], ) + const scrollEmojiCategoryListToIndex = React.useCallback( + (index: number) => { + flatListRef.current?.scrollToIndex({ + index, + animated: shouldAnimateScroll && enableCategoryChangeAnimation, + }) + }, + [enableCategoryChangeAnimation, shouldAnimateScroll], + ) + React.useEffect(() => { - flatListRef.current?.scrollToIndex({ - index: activeCategoryIndex, - animated: shouldAnimateScroll && enableCategoryChangeAnimation, - }) setKeyboardScrollOffsetY(0) - }, [activeCategoryIndex, enableCategoryChangeAnimation, shouldAnimateScroll]) + }, [activeCategoryIndex]) const keyExtractor = React.useCallback((item: EmojisByCategory) => item.title, []) const scrollNav = React.useRef(new Animated.Value(0)).current @@ -155,7 +160,10 @@ export const EmojiStaticKeyboard = React.memo( keyboardShouldPersistTaps="handled" onMomentumScrollEnd={onScrollEnd} /> - +