@backpackapp-io/react-native-toast
Version:
A toasting library for React Native. Built in features such as swipe to dismiss, multiple toasts, & no context power this library.
60 lines (53 loc) • 1.64 kB
text/typescript
import { useEffect, useState } from 'react';
import { Keyboard, KeyboardEventListener, KeyboardMetrics } from 'react-native';
/**
* Get keyboard status, height, and coordinates
*/
const emptyCoordinates = Object.freeze({
screenX: 0,
screenY: 0,
width: 0,
height: 0,
});
const initialValue = {
start: emptyCoordinates,
end: emptyCoordinates,
};
export function useKeyboard() {
const [shown, setShown] = useState(false);
const [coordinates, setCoordinates] = useState<{
start: undefined | KeyboardMetrics;
end: KeyboardMetrics;
}>(initialValue);
const [keyboardHeight, setKeyboardHeight] = useState<number>(0);
const handleKeyboardDidShow: KeyboardEventListener = (e) => {
setShown(true);
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
setKeyboardHeight(e.endCoordinates.height);
};
const handleKeyboardDidHide: KeyboardEventListener = (e) => {
setShown(false);
if (e) {
setCoordinates({ start: e.startCoordinates, end: e.endCoordinates });
} else {
setCoordinates(initialValue);
setKeyboardHeight(0);
}
};
useEffect(() => {
const subscriptions = [
Keyboard.addListener('keyboardWillShow', handleKeyboardDidShow),
Keyboard.addListener('keyboardDidShow', handleKeyboardDidShow),
Keyboard.addListener('keyboardWillHide', handleKeyboardDidHide),
Keyboard.addListener('keyboardDidHide', handleKeyboardDidHide),
];
return () => {
subscriptions.forEach((subscription) => subscription.remove());
};
}, []);
return {
keyboardShown: shown,
coordinates,
keyboardHeight,
};
}