Skip to content

Instantly share code, notes, and snippets.

@bogoslavskiy
Created March 5, 2020 18:16
Show Gist options
  • Save bogoslavskiy/d48999fec7abdbd94f6abef5fc135e9e to your computer and use it in GitHub Desktop.
Save bogoslavskiy/d48999fec7abdbd94f6abef5fc135e9e to your computer and use it in GitHub Desktop.
import * as React from 'react';
import { FlatListProps, FlatList, Keyboard, PanResponderGestureState, PanResponder } from 'react-native';
export const MessageListMobile = <T extends {}>(props: FlatListProps<T> & { innerRef?: React.RefObject<FlatList<T>> }) => {
const keyboardPosY = React.useRef(0);
const isVisibleKeyboard = React.useRef(false);
const accessoryViewHeight = 53 / 2;
React.useEffect(() => {
const keyboardDidShowListener = Keyboard.addListener(
'keyboardDidShow',
(ev) => {
keyboardPosY.current = ev.endCoordinates.screenY;
isVisibleKeyboard.current = true;
}
);
const keyboardDidHideListener = Keyboard.addListener(
'keyboardDidHide',
() => {
isVisibleKeyboard.current = false;
}
);
return () => {
keyboardDidShowListener.remove();
keyboardDidHideListener.remove();
}
}, []);
const isDropZone = React.useCallback((gesture: PanResponderGestureState) => {
return (gesture.moveY + accessoryViewHeight) > keyboardPosY.current;
}, []);
const panResponder = React.useMemo(() => PanResponder.create({
onStartShouldSetPanResponder: () => true,
onPanResponderMove: (_, gesture) => {
if(isVisibleKeyboard.current) {
if (isDropZone(gesture)) {
Keyboard.dismiss();
isVisibleKeyboard.current = false;
}
}
}
}), []);
return (
<FlatList
{...panResponder.panHandlers}
{...props}
ref={props.innerRef}
/>
);
};
export const MessageList = MessageListMobile;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment