UNPKG

toastvibe

Version:

ToastVibe is a lightweight, customizable, and flexible toast notification library for React.

2 lines 3.83 kB
"use strict";var $=Object.create;var u=Object.defineProperty,F=Object.defineProperties,L=Object.getOwnPropertyDescriptor,D=Object.getOwnPropertyDescriptors,M=Object.getOwnPropertyNames,k=Object.getOwnPropertySymbols,_=Object.getPrototypeOf,N=Object.prototype.hasOwnProperty,q=Object.prototype.propertyIsEnumerable;var R=(t,o,a)=>o in t?u(t,o,{enumerable:!0,configurable:!0,writable:!0,value:a}):t[o]=a,i=(t,o)=>{for(var a in o||(o={}))N.call(o,a)&&R(t,a,o[a]);if(k)for(var a of k(o))q.call(o,a)&&R(t,a,o[a]);return t},y=(t,o)=>F(t,D(o));var H=(t,o)=>{for(var a in o)u(t,a,{get:o[a],enumerable:!0})},S=(t,o,a,n)=>{if(o&&typeof o=="object"||typeof o=="function")for(let s of M(o))!N.call(t,s)&&s!==a&&u(t,s,{get:()=>o[s],enumerable:!(n=L(o,s))||n.enumerable});return t};var J=(t,o,a)=>(a=t!=null?$(_(t)):{},S(o||!t||!t.__esModule?u(a,"default",{value:t,enumerable:!0}):a,t)),K=t=>S(u({},"__esModule",{value:!0}),t);var Q={};H(Q,{ToastContext:()=>x,ToastProvider:()=>h,ToasterContainer:()=>I,toast:()=>E,useToast:()=>X});module.exports=K(Q);var m=require("react");var b=require("react");var c=require("react/jsx-runtime"),O=({message:t,type:o="info",onClose:a,richColors:n,closeButton:s,status:d="pending"})=>{let[p,f]=(0,b.useState)(d);return(0,b.useEffect)(()=>{f(d)},[d]),(0,c.jsxs)("div",{className:`toast toast-${o} ${n?"rich-colors":""}`,children:[(0,c.jsx)("span",{className:"toast-message",children:t}),o==="pending"&&p==="pending"&&(0,c.jsx)("div",{className:"toast-loading",children:(0,c.jsx)("div",{className:"toast-loading-spinner"})}),p==="completed"&&o==="success"&&(0,c.jsx)("span",{className:"toast-icon",children:"\u2705"}),p==="completed"&&o==="error"&&(0,c.jsx)("span",{className:"toast-icon",children:"\u274C"}),s&&(0,c.jsx)("button",{className:"toast-close",onClick:a,children:"\xD7"})]})},z=O;var e=null,P=t=>{e=t},E={info:(t,o)=>{if(!e)throw new Error("ToastProvider not initialized");e.addToast(i({message:t,type:"info"},o))},success:(t,o)=>{if(!e)throw new Error("ToastProvider not initialized");e.addToast(i({message:t,type:"success"},o))},warning:(t,o)=>{if(!e)throw new Error("ToastProvider not initialized");e.addToast(i({message:t,type:"warning"},o))},error:(t,o)=>{if(!e)throw new Error("ToastProvider not initialized");e.addToast(i({message:t,type:"error"},o))},pending:(t,o)=>{if(!e)throw new Error("ToastProvider not initialized");return e.addToast(i({message:t,type:"pending",status:"pending"},o))},updateStatus:(t,o)=>{if(!e)throw new Error("ToastProvider not initialized");e.updateToastStatus(t,o)}};var T=require("react/jsx-runtime"),x=(0,m.createContext)(null),h=({children:t,position:o="top-right",richColors:a=!1,closeButton:n=!0,className:s,duration:d=3e3})=>{let[p,f]=(0,m.useState)([]),w=r=>{f(g=>[...g,y(i({},r),{position:r.position||o})]);let l=r.duration||d;setTimeout(()=>v(p.length),l)},v=r=>{f(p.filter((l,g)=>g!==r))},Y=(r,l)=>{f(g=>g.map((C,G)=>G===r?y(i({},C),{status:l}):C))};return(0,m.useEffect)(()=>(P({addToast:w}),()=>P(null)),[]),(0,T.jsxs)(x.Provider,{value:{toasts:p,addToast:w,removeToast:v,updateToastStatus:Y,position:o,richColors:a,closeButton:n,className:s,duration:d},children:[t,(0,T.jsx)("div",{className:`toast-container ${o} ${s||""}`,children:p.map((r,l)=>(0,T.jsx)(z,{message:r.message,type:r.type,onClose:()=>v(l),richColors:a,closeButton:n,status:r.status},l))})]})};var B=require("react/jsx-runtime"),I=({position:t="top-right",richColors:o=!1,closeButton:a=!0,className:n,duration:s,children:d})=>(0,B.jsx)(h,{position:t,richColors:o,closeButton:a,className:n,duration:s,children:d});var A=J(require("react"));var X=()=>{let t=A.default.useContext(x);if(!t)throw new Error("\u274C useToast must be used within a <ToastProvider>.");return t};0&&(module.exports={ToastContext,ToastProvider,ToasterContainer,toast,useToast}); //# sourceMappingURL=index.js.map