UNPKG

@equinor/mad-toast

Version:

Toast package built on top of `react-native-toast-message`

48 lines (45 loc) 1.31 kB
import { ToastQueue } from "./chunk-3LMJABWC.js"; import { ToastTypes } from "./chunk-ZB2RMTQB.js"; import { CustomToastTranslator } from "./chunk-NQVB5MMG.js"; import { __spreadProps, __spreadValues } from "./chunk-N3ETBM74.js"; // src/ToastEmitter.tsx import React, { useEffect, useSyncExternalStore } from "react"; import Toast from "react-native-toast-message"; var toastConfig = { [ToastTypes.ERROR]: CustomToastTranslator, [ToastTypes.SUCCESS]: CustomToastTranslator, [ToastTypes.WARNING]: CustomToastTranslator, [ToastTypes.INFO]: CustomToastTranslator }; var ToastEmitter = (props) => { const store = useSyncExternalStore(ToastQueue.subscribe, ToastQueue.getSnapshot); const nextToast = store.at(0); useEffect(() => { if (!nextToast) return; const { type, text, duration, onPress } = nextToast; const onPressWithHide = () => onPress == null ? void 0 : onPress(Toast.hide); Toast.show({ type, swipeable: true, text1: text, onPress: onPressWithHide, onHide: () => setTimeout(() => { ToastQueue.shift(); }, 150), visibilityTime: duration }); }, [nextToast]); return /* @__PURE__ */ React.createElement(Toast, __spreadProps(__spreadValues({}, props), { config: toastConfig })); }; export { ToastEmitter };