UNPKG

react-toaster-service

Version:

A simple React toast notification service with auto CSS injection

23 lines (22 loc) 8.27 kB
(function(){ try {var elementStyle = document.createElement('style'); elementStyle.appendChild(document.createTextNode(".toast-container{position:fixed;top:20px;right:20px;display:flex;flex-direction:column;gap:12px;z-index:9999}.toast{position:relative;padding:12px 16px;border-radius:6px;color:#fff;font-weight:700;min-width:200px;box-shadow:0 2px 8px #0003;animation:fadein .3s ease-in-out}.toast-success{background:#28a745}.toast-error{background:#dc3545}.toast-info{background:#17a2b8}.toast-warning{background:#ffc107;color:#333}.toast-close{background:transparent;border:none;color:inherit;font-size:14px;cursor:pointer;position:absolute;top:4px;right:8px;padding:0}.toast-progress{position:absolute;bottom:0;left:0;height:4px;background:#ffffffb3;animation:progress linear forwards}@keyframes fadein{0%{opacity:0;transform:translateY(-10px)}to{opacity:1;transform:translateY(0)}}@keyframes progress{0%{width:100%}to{width:0%}}")); document.head.appendChild(elementStyle);} catch(e) {console.error('vite-plugin-css-injected-by-js', e);} })();"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const p=require("react");var O={exports:{}},R={};/** * @license React * react-jsx-runtime.production.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var L;function ne(){if(L)return R;L=1;var u=Symbol.for("react.transitional.element"),f=Symbol.for("react.fragment");function l(d,n,i){var o=null;if(i!==void 0&&(o=""+i),n.key!==void 0&&(o=""+n.key),"key"in n){i={};for(var a in n)a!=="key"&&(i[a]=n[a])}else i=n;return n=i.ref,{$$typeof:u,type:d,key:o,ref:n!==void 0?n:null,props:i}}return R.Fragment=f,R.jsx=l,R.jsxs=l,R}var _={};/** * @license React * react-jsx-runtime.development.js * * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */var W;function oe(){return W||(W=1,process.env.NODE_ENV!=="production"&&function(){function u(e){if(e==null)return null;if(typeof e=="function")return e.$$typeof===ee?null:e.displayName||e.name||null;if(typeof e=="string")return e;switch(e){case P:return"Fragment";case V:return"Profiler";case z:return"StrictMode";case H:return"Suspense";case Z:return"SuspenseList";case K:return"Activity"}if(typeof e=="object")switch(typeof e.tag=="number"&&console.error("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),e.$$typeof){case J:return"Portal";case X:return(e.displayName||"Context")+".Provider";case G:return(e._context.displayName||"Context")+".Consumer";case B:var r=e.render;return e=e.displayName,e||(e=r.displayName||r.name||"",e=e!==""?"ForwardRef("+e+")":"ForwardRef"),e;case Q:return r=e.displayName||null,r!==null?r:u(e.type)||"Memo";case C:r=e._payload,e=e._init;try{return u(e(r))}catch{}}return null}function f(e){return""+e}function l(e){try{f(e);var r=!1}catch{r=!0}if(r){r=console;var t=r.error,s=typeof Symbol=="function"&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object";return t.call(r,"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",s),f(e)}}function d(e){if(e===P)return"<>";if(typeof e=="object"&&e!==null&&e.$$typeof===C)return"<...>";try{var r=u(e);return r?"<"+r+">":"<...>"}catch{return"<...>"}}function n(){var e=S.A;return e===null?null:e.getOwner()}function i(){return Error("react-stack-top-frame")}function o(e){if(Y.call(e,"key")){var r=Object.getOwnPropertyDescriptor(e,"key").get;if(r&&r.isReactWarning)return!1}return e.key!==void 0}function a(e,r){function t(){$||($=!0,console.error("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",r))}t.isReactWarning=!0,Object.defineProperty(e,"key",{get:t,configurable:!0})}function b(){var e=u(this.type);return I[e]||(I[e]=!0,console.error("Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release.")),e=this.props.ref,e!==void 0?e:null}function k(e,r,t,s,E,m,g,w){return t=m.ref,e={$$typeof:N,type:e,key:r,props:m,_owner:E},(t!==void 0?t:null)!==null?Object.defineProperty(e,"ref",{enumerable:!1,get:b}):Object.defineProperty(e,"ref",{enumerable:!1,value:null}),e._store={},Object.defineProperty(e._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:0}),Object.defineProperty(e,"_debugInfo",{configurable:!1,enumerable:!1,writable:!0,value:null}),Object.defineProperty(e,"_debugStack",{configurable:!1,enumerable:!1,writable:!0,value:g}),Object.defineProperty(e,"_debugTask",{configurable:!1,enumerable:!1,writable:!0,value:w}),Object.freeze&&(Object.freeze(e.props),Object.freeze(e)),e}function x(e,r,t,s,E,m,g,w){var c=r.children;if(c!==void 0)if(s)if(re(c)){for(s=0;s<c.length;s++)y(c[s]);Object.freeze&&Object.freeze(c)}else console.error("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else y(c);if(Y.call(r,"key")){c=u(e);var v=Object.keys(r).filter(function(te){return te!=="key"});s=0<v.length?"{key: someKey, "+v.join(": ..., ")+": ...}":"{key: someKey}",M[c+s]||(v=0<v.length?"{"+v.join(": ..., ")+": ...}":"{}",console.error(`A props object containing a "key" prop is being spread into JSX: let props = %s; <%s {...props} /> React keys must be passed directly to JSX without using spread: let props = %s; <%s key={someKey} {...props} />`,s,c,v,c),M[c+s]=!0)}if(c=null,t!==void 0&&(l(t),c=""+t),o(r)&&(l(r.key),c=""+r.key),"key"in r){t={};for(var h in r)h!=="key"&&(t[h]=r[h])}else t=r;return c&&a(t,typeof e=="function"?e.displayName||e.name||"Unknown":e),k(e,c,m,E,n(),t,g,w)}function y(e){typeof e=="object"&&e!==null&&e.$$typeof===N&&e._store&&(e._store.validated=1)}var j=p,N=Symbol.for("react.transitional.element"),J=Symbol.for("react.portal"),P=Symbol.for("react.fragment"),z=Symbol.for("react.strict_mode"),V=Symbol.for("react.profiler"),G=Symbol.for("react.consumer"),X=Symbol.for("react.context"),B=Symbol.for("react.forward_ref"),H=Symbol.for("react.suspense"),Z=Symbol.for("react.suspense_list"),Q=Symbol.for("react.memo"),C=Symbol.for("react.lazy"),K=Symbol.for("react.activity"),ee=Symbol.for("react.client.reference"),S=j.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE,Y=Object.prototype.hasOwnProperty,re=Array.isArray,A=console.createTask?console.createTask:function(){return null};j={"react-stack-bottom-frame":function(e){return e()}};var $,I={},D=j["react-stack-bottom-frame"].bind(j,i)(),F=A(d(i)),M={};_.Fragment=P,_.jsx=function(e,r,t,s,E){var m=1e4>S.recentlyCreatedOwnerStacks++;return x(e,r,t,!1,s,E,m?Error("react-stack-top-frame"):D,m?A(d(e)):F)},_.jsxs=function(e,r,t,s,E){var m=1e4>S.recentlyCreatedOwnerStacks++;return x(e,r,t,!0,s,E,m?Error("react-stack-top-frame"):D,m?A(d(e)):F)}}()),_}var U;function ae(){return U||(U=1,process.env.NODE_ENV==="production"?O.exports=ne():O.exports=oe()),O.exports}var T=ae();const se=({id:u,type:f,message:l,duration:d,onClose:n})=>T.jsxs("div",{className:`toast toast-${f}`,children:[T.jsx("span",{children:l}),T.jsx("button",{className:"toast-close",onClick:()=>n(u),children:"✖"}),T.jsx("div",{className:"toast-progress",style:{animationDuration:`${d}ms`}})]}),ce=({toasts:u,removeToast:f})=>T.jsx("div",{className:"toast-container",children:u.map(l=>T.jsx(se,{...l,onClose:f},l.id))}),q=p.createContext(),ue=()=>p.useContext(q);let le=0;const ie=({children:u})=>{const[f,l]=p.useState([]),d=o=>{l(a=>a.filter(b=>b.id!==o))},n=p.useCallback((o,a,b=3e3)=>{const k=le++;l(x=>[...x,{id:k,type:o,message:a,duration:b}]),setTimeout(()=>d(k),b)},[]),i={success:(o,a)=>n("success",o,a),error:(o,a)=>n("error",o,a),info:(o,a)=>n("info",o,a),warning:(o,a)=>n("warning",o,a)};return T.jsxs(q.Provider,{value:i,children:[u,T.jsx(ce,{toasts:f,removeToast:d})]})};exports.ToastProvider=ie;exports.useToast=ue;