Rocket.Chat.ReactNative/app/containers/Toast.tsx

58 lines
1.2 KiB
TypeScript
Raw Normal View History

import React, { useEffect } from 'react';
2019-07-23 14:02:57 +00:00
import { StyleSheet } from 'react-native';
import EasyToast from 'react-native-easy-toast';
import EventEmitter from '../lib/methods/helpers/events';
import { useTheme } from '../theme';
import sharedStyles from '../views/Styles';
2019-07-23 14:02:57 +00:00
const styles = StyleSheet.create({
toast: {
maxWidth: 300,
padding: 10
},
text: {
fontSize: 14,
...sharedStyles.textRegular,
...sharedStyles.textAlignCenter
2019-07-23 14:02:57 +00:00
}
});
export const LISTENER = 'Toast';
let listener: Function;
let toast: EasyToast | null | undefined;
2019-07-23 14:02:57 +00:00
const Toast = (): React.ReactElement => {
const { colors } = useTheme();
2019-07-23 14:02:57 +00:00
useEffect(() => {
listener = EventEmitter.addEventListener(LISTENER, showToast);
return () => {
EventEmitter.removeListener(LISTENER, listener);
};
}, []);
2019-07-23 14:02:57 +00:00
const getToastRef = (newToast: EasyToast | null) => (toast = newToast);
2020-02-17 16:06:08 +00:00
const showToast = ({ message }: { message: string }) => {
if (toast && toast.show) {
toast.show(message, 1000);
2020-02-17 16:06:08 +00:00
}
};
2019-07-23 14:02:57 +00:00
return (
<EasyToast
ref={getToastRef}
// @ts-ignore
position='center'
// @ts-ignore
style={[styles.toast, { backgroundColor: colors.toastBackground }]}
textStyle={[styles.text, { color: colors.buttonText }]}
opacity={0.9}
/>
);
};
export default Toast;