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