UNPKG

haptic-sound-toast

Version:

A customizable toast notification library for Next.js and React

3 lines (2 loc) 8.41 kB
"use strict";var t=require("react"),e=require("lucide-react"),n=require("framer-motion");function o(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,o=Array(e);n<e;n++)o[n]=t[n];return o}function r(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var o,r,i,s,a=[],c=!0,u=!1;try{if(i=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;c=!1}else for(;!(c=(o=i.call(n)).done)&&(a.push(o.value),a.length!==e);c=!0);}catch(t){u=!0,r=t}finally{try{if(!c&&null!=n.return&&(s=n.return(),Object(s)!==s))return}finally{if(u)throw r}}return a}}(t,e)||s(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function i(t){return function(t){if(Array.isArray(t))return o(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||s(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function s(t,e){if(t){if("string"==typeof t)return o(t,e);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?o(t,e):void 0}}var a={success:{icon:e.CheckCircle,className:"toast-success",buttonClass:"toast-success-button",sound:"/sounds/success.mp3",vibration:100},error:{icon:e.AlertCircle,className:"toast-error",buttonClass:"toast-error-button",sound:"/sounds/error.mp3",vibration:[100,50,200]},info:{icon:e.Info,className:"toast-info",buttonClass:"toast-info-button",sound:"/sounds/info.mp3",vibration:50},warning:{icon:e.AlertTriangle,className:"toast-warning",buttonClass:"toast-warning-button",sound:"/sounds/warning.mp3",vibration:[50,25,50,25,50]}},c=function(o){var i=o.message,s=void 0===i?"An error occurred":i,c=o.isVisible,u=void 0!==c&&c,l=o.onDismiss,d=void 0===l?function(){}:l,m=o.duration,f=void 0===m?2e3:m,v=o.type,p=void 0===v?"error":v;o.position;var b=o.sound,g=void 0===b||b,y=o.customSound,h=void 0===y?null:y,w=o.customIcon,E=void 0===w?null:w,N=o.description,x=void 0===N?"":N,C=o.showProgress,O=void 0!==C&&C,S=o.className,A=void 0===S?"":S,j=o.vibration,T=void 0===j||j,I=o.customVibration,P=void 0===I?null:I,D=o.action,V=void 0===D?null:D,H=o.pauseOnHover,M=void 0===H||H,k=o.swipeToClose,q=void 0===k||k,z=o.role,L=void 0===z?"status":z;t.useEffect((function(){if(u&&f){var t=setTimeout((function(){d()}),f);return function(){return clearTimeout(t)}}}),[u,f,d]),t.useEffect((function(){if(u&&g){var t=new Audio;if(h)t.src=h;else{var e=a[p]||a.error;t.src=e.sound}t.play().catch((function(t){console.error("Error playing toast sound:",t)}))}}),[u,g,h,p]),t.useEffect((function(){if(u&&T&&"undefined"!=typeof navigator&&navigator.vibrate){var t=P||(a[p]||a.error).vibration||100;return navigator.vibrate(t),function(){return navigator.vibrate(0)}}}),[u,T,P,p]);var Y=a[p]||a.error,R=E||Y.icon,U=t.useRef(null),W=r(t.useState("100%"),2),X=W[0],$=W[1],B=r(t.useState(!1),2),F=B[0],G=B[1],J=r(t.useState(Date.now()),1)[0],K=r(t.useState(0),2);K[0];var Q=K[1],Z=r(t.useState(f),2),_=Z[0],tt=Z[1];t.useEffect((function(){if(u&&f&&!F){var t=setInterval((function(){var t=Date.now()-J;Q(t);var e=Math.max(0,f-t);tt(e),e<=0&&d()}),10);return function(){return clearInterval(t)}}}),[u,f,d,F,J]),t.useEffect((function(){U.current&&f>0&&$("".concat(_/f*100,"%"))}),[_,f]);return t.createElement("div",{className:"w-full",onMouseEnter:function(){return M&&G(!0)},onMouseLeave:function(){return M&&G(!1)},role:L,"aria-live":"polite"},t.createElement(n.motion.div,{drag:!!q&&"x",dragConstraints:{left:0,right:0},onDragEnd:function(t,e){if(q){var n=Math.abs(e.velocity.x);(Math.abs(e.offset.x)>100||n>500)&&d()}},className:"toast-container"},t.createElement("div",{className:"toast-content ".concat(Y.className," ").concat(A)},O&&f>0&&t.createElement(n.motion.div,{ref:U,initial:{width:"100%"},animate:{width:F?X:"0%"},transition:{duration:f/1e3,ease:"linear",pause:F},className:"toast-progress",style:{zIndex:1}}),t.createElement(R,{className:"toast-icon"}),t.createElement("div",{className:"toast-message"},t.createElement("div",{className:"toast-title"},s),x&&t.createElement("div",{className:"toast-description"},x),V&&t.createElement("div",{className:"toast-action"},V)),t.createElement("button",{onClick:function(t){t.stopPropagation(),d()},className:"toast-close-button ".concat(Y.buttonClass),"aria-label":"Dismiss",type:"button"},t.createElement(e.X,{className:"toast-close-icon"})))))},u=t.createContext(null),l=0;exports.Toast=c,exports.ToastProvider=function(e){var o=e.children,s=r(t.useState([]),2),a=s[0],d=s[1],m=function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=e.id||"toast-".concat(l++),o={id:n,message:t,type:e.type||"info",duration:void 0!==e.duration?e.duration:5e3,position:e.position||"top-center",sound:void 0===e.sound||e.sound,customSound:void 0===e.customSound?void 0:null,customIcon:void 0===e.customIcon?void 0:null,description:e.description||"",showProgress:void 0!==e.showProgress&&e.showProgress,className:e.className||"",vibration:void 0===e.vibration||e.vibration,customVibration:void 0===e.customVibration?void 0:null,action:void 0===e.action?void 0:null,pauseOnHover:void 0===e.pauseOnHover||e.pauseOnHover,swipeToClose:void 0===e.swipeToClose||e.swipeToClose,role:e.role||"status",createdAt:Date.now()};return d((function(t){return[].concat(i(t),[o])})),n},f=function(t){d((function(e){return e.filter((function(e){return e.id!==t}))}))},v={info:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return m(t,Object.assign(Object.assign({},e),{type:"info"}))},success:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return m(t,Object.assign(Object.assign({},e),{type:"success"}))},error:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return m(t,Object.assign(Object.assign({},e),{type:"error"}))},warning:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};return m(t,Object.assign(Object.assign({},e),{type:"warning"}))},custom:function(t){return m(t,arguments.length>1&&void 0!==arguments[1]?arguments[1]:{})},dismiss:function(t){t?f(t):d([])}},p=a.reduce((function(t,e){var n=e.position;return t[n]||(t[n]=[]),t[n].push(e),t}),{});return Object.keys(p).forEach((function(t){p[t].sort((function(t,e){return t.createdAt-e.createdAt}))})),t.createElement(u.Provider,{value:v},o,Object.entries(p).map((function(e){var o=r(e,2),i=o[0],s=o[1];return t.createElement("div",{key:i,className:"fixed z-50 ".concat(i.includes("top")?"top-0":"bottom-0"," ").concat(i.includes("left")?"left-0":i.includes("right")?"right-0":"left-1/2 -translate-x-1/2"," ").concat(i.includes("center")?"w-full flex justify-center":"w-auto"," p-4 pointer-events-none"),style:{maxWidth:i.includes("center")?"100%":"auto",transform:i.includes("bottom")?"translateY(-100%)":"none"}},t.createElement("div",{className:"flex flex-col gap-3 ".concat(i.includes("left")?"items-start":i.includes("right")?"items-end":"items-center"," w-full max-w-sm sm:max-w-md")},t.createElement(n.AnimatePresence,{mode:"popLayout"},s.map((function(e,o){return t.createElement(n.motion.div,{key:e.id,className:"pointer-events-auto w-full max-w-sm sm:max-w-md",initial:{opacity:0,y:i.includes("top")?-20:20,scale:.8},animate:{opacity:1,y:0,scale:1,transition:{type:"spring",stiffness:300,damping:30,delay:.05*o}},exit:{opacity:0,scale:.8,transition:{duration:.2}},layoutId:e.id,style:{transform:"translateY(".concat(80*o,"px)")}},t.createElement(c,{message:e.message,isVisible:!0,type:e.type,duration:e.duration,position:e.position,sound:e.sound,customSound:e.customSound,customIcon:e.customIcon,description:e.description,showProgress:e.showProgress,className:e.className,vibration:e.vibration,customVibration:e.customVibration,onDismiss:function(){return f(e.id)},action:e.action,pauseOnHover:e.pauseOnHover,swipeToClose:e.swipeToClose,role:e.role}))})))))})))},exports.useToast=function(){var e=t.useContext(u);if(!e)throw new Error("useToast must be used within a ToastProvider");return e}; //# sourceMappingURL=index.js.map