diff --git a/src/EmojiKeyboard.tsx b/src/EmojiKeyboard.tsx index 4007bba..141d24f 100644 --- a/src/EmojiKeyboard.tsx +++ b/src/EmojiKeyboard.tsx @@ -6,8 +6,8 @@ import { FlatList, useWindowDimensions, Animated, - NativeSyntheticEvent, - NativeScrollEvent, + // NativeSyntheticEvent, + // NativeScrollEvent, } from 'react-native'; import { CATEGORIES, CategoryTypes } from './types'; import { EmojiCategory } from './components/EmojiCategory'; @@ -20,7 +20,7 @@ export const EmojiKeyboard = () => { const flatListRef = React.useRef(null); - const scrollX = React.useRef(new Animated.Value(0)).current; + // const scrollX = React.useRef(new Animated.Value(0)).current; const scrollNav = React.useRef(new Animated.Value(0)).current; const getItemLayout = ( @@ -36,28 +36,36 @@ export const EmojiKeyboard = () => { (props) => , [] ); - const onScroll = Animated.event( - [{ nativeEvent: { contentOffset: { x: scrollX } } }], - { - listener: ({ - nativeEvent: { - contentOffset: { x }, - }, - }: NativeSyntheticEvent) => { - Animated.spring(scrollNav, { - toValue: (x / width) * (28 + 9), - useNativeDriver: false, - }).start(); - }, - useNativeDriver: false, - } - ); + // const onScroll = Animated.event( + // [{ nativeEvent: { contentOffset: { x: scrollX } } }], + // { + // listener: ({ + // nativeEvent: { + // contentOffset: { x }, + // }, + // }: NativeSyntheticEvent) => { + // console.log(Math.round(x / width)); + // ctx.setActiveCategoryIndex(Math.round(x / width)); + // // Animated.spring(scrollNav, { + // // toValue: (x / width) * (28 + 9), + // // useNativeDriver: true, + // // }).start(); + // }, + // useNativeDriver: true, + // } + // ); + React.useEffect(() => { + Animated.spring(scrollNav, { + toValue: ctx.activeCategoryIndex * (28 + 9), + useNativeDriver: true, + }).start(); + }, [ctx, scrollNav]); return ( - item} + keyExtractor={(item: CategoryTypes) => item} renderItem={renderItem} removeClippedSubviews={true} ref={flatListRef} @@ -68,7 +76,8 @@ export const EmojiKeyboard = () => { scrollEventThrottle={16} decelerationRate="fast" getItemLayout={getItemLayout} - onScroll={onScroll} + scrollEnabled={false} + // onScroll={onScroll} /> diff --git a/src/EmojiPicker.tsx b/src/EmojiPicker.tsx index 6b03d69..b82c47a 100644 --- a/src/EmojiPicker.tsx +++ b/src/EmojiPicker.tsx @@ -28,6 +28,15 @@ export const EmojiPicker = ({ const { height: screenHeight } = useWindowDimensions(); const offsetY = React.useRef(new Animated.Value(0)).current; const height = React.useRef(new Animated.Value(screenHeight * 0.4)).current; + // const backdropOpacity = React.useRef(new Animated.Value(0)).current; + + // React.useEffect(() => { + // Animated.timing(backdropOpacity, { + // toValue: isOpen ? 1 : 0, + // useNativeDriver: false, + // duration: 2000, + // }).start(); + // }, [backdropOpacity, isOpen]); return ( diff --git a/src/KeyboardContext.tsx b/src/KeyboardContext.tsx index 9cfb00a..b17f264 100644 --- a/src/KeyboardContext.tsx +++ b/src/KeyboardContext.tsx @@ -10,6 +10,7 @@ export type KeyboardProps = { onEmojiSelected: (emoji: EmojiType) => void; emojiSize?: number; containerStyles?: ViewStyle; + isOpen?: boolean; }; export type ContextValues = { activeCategoryIndex: number; diff --git a/src/KeyboardProvider.tsx b/src/KeyboardProvider.tsx index c8b0e27..3fbaf30 100644 --- a/src/KeyboardProvider.tsx +++ b/src/KeyboardProvider.tsx @@ -14,6 +14,7 @@ export const defaultKeyboardContext: Required = { onEmojiSelected: (_emoji: EmojiType) => {}, emojiSize: 24, containerStyles: {}, + isOpen: false, }; export const defaultKeyboardValues: ContextValues = { @@ -23,11 +24,11 @@ export const defaultKeyboardValues: ContextValues = { }; export const KeyboardProvider: React.FC = React.memo((props) => { - const [activeCategoryIndex, setActive] = React.useState(0); + const [activeCategoryIndex, setActiveCategoryIndex] = React.useState(0); - const setActiveCategoryIndex = React.useCallback((index: number) => { - setActive(index); - }, []); + React.useEffect(() => { + if (props.isOpen) setActiveCategoryIndex(0); + }, [props.isOpen]); const value: Required & ContextValues = { ...defaultKeyboardContext, diff --git a/src/components/Categories.tsx b/src/components/Categories.tsx index 4890bde..f3dd2c9 100644 --- a/src/components/Categories.tsx +++ b/src/components/Categories.tsx @@ -33,7 +33,15 @@ export const Categories = ({ flatListRef, scrollNav }: CategoriesProps) => { const activeIndicator = React.useCallback( () => ( - + // + ), [scrollNav] );