UNPKG

react-native-irano

Version:

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

184 lines (174 loc) 4.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useIrano = exports.default = void 0; var _react = require("react"); var _alert = require("../components/alert.js"); var _toast = _interopRequireDefault(require("../components/toast.js")); var _jsxRuntime = require("react/jsx-runtime"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } // Context types const IranoContext = /*#__PURE__*/(0, _react.createContext)(undefined); const useIrano = () => { const context = (0, _react.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 exports.useIrano = useIrano; const AlertComponent = /*#__PURE__*/(0, _react.memo)(({ isVisible, params, styleProps, onClose }) => { if (!isVisible || !params) return null; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_alert.Alert, { title: params.title, subtitle: params.subtitle, visible: isVisible, onClose: onClose, preset: params.preset, ...styleProps }); }); const ToastComponent = /*#__PURE__*/(0, _react.memo)(({ isVisible, params, styleProps, onHide }) => { if (!isVisible || !params) return null; return /*#__PURE__*/(0, _jsxRuntime.jsx)(_toast.default, { 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] = (0, _react.useReducer)(reducer, initialState); const { isAlertVisible, isToastVisible, alertPreset, toastPreset } = state; // Use refs for params to avoid unnecessary rerenders const alertParams = (0, _react.useRef)(null); const toastParams = (0, _react.useRef)(null); // Memoized style props const alertStyleProps = (0, _react.useMemo)(() => { switch (alertPreset) { case 'done': return doneProps; case 'loading': return loadingProps; case 'error': return errorProps; default: return {}; } }, [doneProps, loadingProps, errorProps, alertPreset]); const toastStyleProps = (0, _react.useMemo)(() => { return toastPreset === 'success' ? toastSuccessProps : toastErrorProps; }, [toastErrorProps, toastSuccessProps, toastPreset]); // Memoized handlers const showAlert = (0, _react.useCallback)(param => { alertParams.current = param; dispatch({ type: 'SHOW_ALERT', preset: param.preset }); }, []); const onToast = (0, _react.useCallback)(param => { toastParams.current = param; dispatch({ type: 'SHOW_TOAST', preset: param.preset }); }, []); const handleAlertClose = (0, _react.useCallback)(() => { dispatch({ type: 'HIDE_ALERT' }); }, []); const handleToastHide = (0, _react.useCallback)(() => { dispatch({ type: 'HIDE_TOAST' }); }, []); // Memoize context value const contextValue = (0, _react.useMemo)(() => ({ showAlert, onToast }), [showAlert, onToast]); return /*#__PURE__*/(0, _jsxRuntime.jsxs)(IranoContext.Provider, { value: contextValue, children: [children, /*#__PURE__*/(0, _jsxRuntime.jsx)(AlertComponent, { isVisible: isAlertVisible, params: alertParams.current || null, styleProps: alertStyleProps, onClose: handleAlertClose }), /*#__PURE__*/(0, _jsxRuntime.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; } }; var _default = exports.default = IranoProvider; // Types remain the same //# sourceMappingURL=irano.provider.js.map