UNPKG

react-alert-toastify-wrapper

Version:

A replacement for react-alert using react-toastify under the hood.

46 lines (45 loc) 1.77 kB
import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime"; import { toast, ToastContainer as NativeToastContainer, Slide, Zoom, Flip, Bounce } from "react-toastify"; import "react-toastify/dist/ReactToastify.css"; // Position constants export const positions = { TOP_LEFT: "top-left", TOP_RIGHT: "top-right", TOP_CENTER: "top-center", BOTTOM_LEFT: "bottom-left", BOTTOM_RIGHT: "bottom-right", BOTTOM_CENTER: "bottom-center", }; // Transition constants export const transitions = { SLIDE: Slide, ZOOM: Zoom, FLIP: Flip, BOUNCE: Bounce, }; // Global toast options to be updated by Provider let activeOptions = { position: "top-right", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, transition: Slide }; // Toast API const alert = { show: (msg, opts) => toast(msg, Object.assign(Object.assign({}, activeOptions), opts)), success: (msg, opts) => toast.success(msg, Object.assign(Object.assign({}, activeOptions), opts)), error: (msg, opts) => toast.error(msg, Object.assign(Object.assign({}, activeOptions), opts)), info: (msg, opts) => toast.info(msg, Object.assign(Object.assign({}, activeOptions), opts)), warning: (msg, opts) => toast.warn(msg, Object.assign(Object.assign({}, activeOptions), opts)), removeAll: () => toast.dismiss(), }; // Provider component export const Provider = ({ children, options, }) => { activeOptions = Object.assign(Object.assign({}, activeOptions), (options || {})); return (_jsxs(_Fragment, { children: [_jsx(NativeToastContainer, Object.assign({}, activeOptions)), children] })); }; export const ToastContainer = NativeToastContainer; export default alert;