UNPKG

react-native-irano

Version:

A customizable cross-platform toast notification library for React Native.

180 lines (169 loc) 4.38 kB
"use strict"; import { createContext, useContext, useRef, useCallback, useMemo, memo, useReducer } from 'react'; import { Alert } from "../components/alert.js"; import Toast from "../components/toast.js"; // Context types import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; const IranoContext = /*#__PURE__*/createContext(undefined); export const useIrano = () => { const context = useContext(IranoContext); if (!context) { throw new Error('useIrano must be used within an IranoProvider'); } return context; }; // Separate components for Alert and Toast to prevent unnecessary rerenders const AlertComponent = /*#__PURE__*/memo(({ isVisible, params, styleProps, onClose }) => { if (!isVisible || !params) return null; return /*#__PURE__*/_jsx(Alert, { title: params.title, subtitle: params.subtitle, visible: isVisible, onClose: onClose, preset: params.preset, ...styleProps }); }); const ToastComponent = /*#__PURE__*/memo(({ isVisible, params, styleProps, onHide }) => { if (!isVisible || !params) return null; return /*#__PURE__*/_jsx(Toast, { position: "top", title: params.title, subtitle: params.subtitle, onHide: onHide, preset: params.preset, ...styleProps }); }); // Provider const IranoProvider = ({ children, doneProps, loadingProps, errorProps, toastErrorProps, toastSuccessProps }) => { // State management using reducer for better organization const [state, dispatch] = useReducer(reducer, initialState); const { isAlertVisible, isToastVisible, alertPreset, toastPreset } = state; // Use refs for params to avoid unnecessary rerenders const alertParams = useRef(null); const toastParams = useRef(null); // Memoized style props const alertStyleProps = useMemo(() => { switch (alertPreset) { case 'done': return doneProps; case 'loading': return loadingProps; case 'error': return errorProps; default: return {}; } }, [doneProps, loadingProps, errorProps, alertPreset]); const toastStyleProps = useMemo(() => { return toastPreset === 'success' ? toastSuccessProps : toastErrorProps; }, [toastErrorProps, toastSuccessProps, toastPreset]); // Memoized handlers const showAlert = useCallback(param => { alertParams.current = param; dispatch({ type: 'SHOW_ALERT', preset: param.preset }); }, []); const onToast = useCallback(param => { toastParams.current = param; dispatch({ type: 'SHOW_TOAST', preset: param.preset }); }, []); const handleAlertClose = useCallback(() => { dispatch({ type: 'HIDE_ALERT' }); }, []); const handleToastHide = useCallback(() => { dispatch({ type: 'HIDE_TOAST' }); }, []); // Memoize context value const contextValue = useMemo(() => ({ showAlert, onToast }), [showAlert, onToast]); return /*#__PURE__*/_jsxs(IranoContext.Provider, { value: contextValue, children: [children, /*#__PURE__*/_jsx(AlertComponent, { isVisible: isAlertVisible, params: alertParams.current || null, styleProps: alertStyleProps, onClose: handleAlertClose }), /*#__PURE__*/_jsx(ToastComponent, { isVisible: isToastVisible, params: toastParams.current, styleProps: toastStyleProps || {} // Provide a default empty object to avoid undefined , onHide: handleToastHide })] }); }; // Reducer types and implementation const initialState = { isAlertVisible: false, isToastVisible: false, alertPreset: undefined, toastPreset: undefined }; const reducer = (state, action) => { switch (action.type) { case 'SHOW_ALERT': return { ...state, isAlertVisible: true, alertPreset: action.preset }; case 'HIDE_ALERT': return { ...state, isAlertVisible: false, alertPreset: undefined }; case 'SHOW_TOAST': return { ...state, isToastVisible: true, toastPreset: action.preset }; case 'HIDE_TOAST': return { ...state, isToastVisible: false, toastPreset: undefined }; default: return state; } }; export default IranoProvider; // Types remain the same //# sourceMappingURL=irano.provider.js.map