diff --git a/README.md b/README.md index 1936395..f6a40c7 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Toastify is a lightweight, vanilla JS toast notification library. * Multiple stacked notifications * Customizable * No blocking of execution thread +* Sound Support for Toast Notifications ### Customization options @@ -124,12 +125,31 @@ Toastify({ }).showToast(); ``` -Toast will be pushed 50px from right in x axis and 10px from top in y axis. +### Add Sound + +This feature enhances the Toastify library by providing sound support for toast notifications. Now you can add custom sounds to your toast notifications, making them more engaging and interactive. The sound feature allows you to specify the type of sound (e.g., bell or alert) and even use your own custom sound files. With this addition, you can create a more immersive user experience and capture your users' attention with audio cues. + +```javascript +Toastify({ + text: 'I am a toast', + duration: 3000, + sound: { + default: true, // Use the default sound (set to false if a custom sound file is provided) + type: 'bell', // Specify type [bell,alert,pop] + src: './sounds/alert.wav', // Specify the source file for the custom sound (make sure 'default' is set to false) + } +}).showToast() +``` +To utilize the sound feature in Toastify, follow these steps: **Note:** -If `position` is equals to `left`, it will be pushed from left. -If `gravity` is equals to `bottom`, it will be pushed from bottom. +Set the sound property to an object with the following properties: + - `default`: Set it to `true` to use the default sound provided by Toastify. Set it to false if you want to use a custom sound file. + - `type`: Specify the type of sound you want to use. Available options are 'bell' and 'alert'. + - `src`: If `default` is set to `false`, provide the path to your custom sound file using the src property. + +With this new sound support feature, you can create visually and audibly appealing toast notifications, enriching your users' experience on your website or application. ## API @@ -155,6 +175,7 @@ If `gravity` is equals to `bottom`, it will be pushed from bottom. | style | object | Use the HTML DOM Style properties to add any style directly to toast | | | ariaLive | string | Announce the toast to screen readers, see https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions for options | "polite" | | oldestFirst | boolean | Set the order in which toasts are stacked in page | true | +| soundType | string | For default type use `type` | 'pop', 'alert', 'bell' > Deprecated properties: `backgroundColor` - use `style.background` option instead diff --git a/example/script.css b/example/script.css index 11a46bb..1a40372 100644 --- a/example/script.css +++ b/example/script.css @@ -174,3 +174,9 @@ code p { position: static; } } + +/* Disable carbon ads */ +#carbonads { + display: none !important; +} + \ No newline at end of file diff --git a/example/script.js b/example/script.js index 8d290ff..03f256d 100644 --- a/example/script.js +++ b/example/script.js @@ -1,41 +1,41 @@ var bgColors = [ - "linear-gradient(to right, #00b09b, #96c93d)", - "linear-gradient(to right, #ff5f6d, #ffc371)", - ], - i = 0; + "linear-gradient(to right, #00b09b, #96c93d)", + "linear-gradient(to right, #ff5f6d, #ffc371)", +], +i = 0; Toastify({ - text: "Hi", - duration: 4500, - destination: "https://github.com/apvarun/toastify-js", - newWindow: true, - gravity: "top", - position: 'left', +text: "Hi", +duration: 4500, +destination: "https://github.com/apvarun/toastify-js", +newWindow: true, +gravity: "top", +position: 'left', }).showToast(); setTimeout(function() { - Toastify({ - text: "Simple JavaScript Toasts", - gravity: "top", - position: 'center', - style: { - background: '#0f3443' - } - }).showToast(); +Toastify({ + text: "Simple JavaScript Toasts", + gravity: "top", + position: 'center', + style: { + background: '#0f3443' + } +}).showToast(); }, 1000); // Options for the toast var options = { - text: "Happy toasting!", - duration: 2500, - callback: function() { - console.log("Toast hidden"); - Toastify.reposition(); - }, - close: true, - style: { - background: "linear-gradient(to right, #00b09b, #96c93d)", - } +text: "Happy toasting!", +duration: 2500, +callback: function() { + console.log("Toast hidden"); + Toastify.reposition(); +}, +close: true, +style: { + background: "linear-gradient(to right, #00b09b, #96c93d)", +} }; // Initializing the toast @@ -43,30 +43,48 @@ var myToast = Toastify(options); // Toast after delay setTimeout(function() { - myToast.showToast(); +myToast.showToast(); }, 4500); setTimeout(function() { - Toastify({ - text: "Highly customizable", - gravity: "bottom", - position: 'left', - close: true, - style: { - background: "linear-gradient(to right, #ff5f6d, #ffc371)", - } - }).showToast(); +Toastify({ + text: "Highly customizable", + gravity: "bottom", + position: 'left', + close: true, + style: { + background: "linear-gradient(to right, #ff5f6d, #ffc371)", + } +}).showToast(); }, 3000); // Displaying toast on manual action `Try` document.getElementById("new-toast").addEventListener("click", function() { - Toastify({ - text: "I am a toast", - duration: 3000, - close: i % 3 ? true : false, - style: { - background: bgColors[i % 2], - } - }).showToast(); - i++; +Toastify({ + text: "I am a toast", + duration: 3000, + close: i % 3 ? true : false, + style: { + background: bgColors[i % 2], + } +}).showToast(); +i++; +}); + +// Displaying toast on manual action `Try Sound` +document.getElementById("new-toast-sound").addEventListener("click", function() { +Toastify({ + text: "I am a toast", + duration: 3000, + sound: { + default: true, + type: 'pop', + src: './sounds/alert.wav' // If having custom file + }, + close: i % 3 ? true : false, + style: { + background: bgColors[i % 2], + } +}).showToast(); +i++; }); diff --git a/index.html b/index.html index 49174ee..cc074ee 100644 --- a/index.html +++ b/index.html @@ -6,11 +6,14 @@ Toastify JS - Pure JavaScript Toast Notificaton Library - + + + @@ -21,29 +24,42 @@

Toastify JS

Better notification messages

Try - Docs + Try with sound + Docs Tweet

Usage

- -

Toastify({

-

text: "This is a toast",

-

duration: 3000

-

}).showToast();

-
+

Normal Toast

+ + +

Sound Toast

+
- +
- + + + - \ No newline at end of file + diff --git a/sounds/alert.wav b/sounds/alert.wav new file mode 100644 index 0000000..ef2645e Binary files /dev/null and b/sounds/alert.wav differ diff --git a/sounds/bell.wav b/sounds/bell.wav new file mode 100644 index 0000000..7e3d6ad Binary files /dev/null and b/sounds/bell.wav differ diff --git a/sounds/pop.wav b/sounds/pop.wav new file mode 100644 index 0000000..4bed79a Binary files /dev/null and b/sounds/pop.wav differ diff --git a/src/plugin/prism/prism.css b/src/plugin/prism/prism.css new file mode 100644 index 0000000..4bf58db --- /dev/null +++ b/src/plugin/prism/prism.css @@ -0,0 +1,4 @@ +/* PrismJS 1.29.0 +https://prismjs.com/download.html#themes=prism-tomorrow&languages=clike+javascript&plugins=toolbar+copy-to-clipboard */ +code[class*=language-],pre[class*=language-]{color:#ccc;background:0 0;font-family:Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace;font-size:1em;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto}:not(pre)>code[class*=language-],pre[class*=language-]{background:#2d2d2d}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal}.token.block-comment,.token.cdata,.token.comment,.token.doctype,.token.prolog{color:#999}.token.punctuation{color:#ccc}.token.attr-name,.token.deleted,.token.namespace,.token.tag{color:#e2777a}.token.function-name{color:#6196cc}.token.boolean,.token.function,.token.number{color:#f08d49}.token.class-name,.token.constant,.token.property,.token.symbol{color:#f8c555}.token.atrule,.token.builtin,.token.important,.token.keyword,.token.selector{color:#cc99cd}.token.attr-value,.token.char,.token.regex,.token.string,.token.variable{color:#7ec699}.token.entity,.token.operator,.token.url{color:#67cdcc}.token.bold,.token.important{font-weight:700}.token.italic{font-style:italic}.token.entity{cursor:help}.token.inserted{color:green} +div.code-toolbar{position:relative}div.code-toolbar>.toolbar{position:absolute;z-index:10;top:.3em;right:.2em;transition:opacity .3s ease-in-out;opacity:0}div.code-toolbar:hover>.toolbar{opacity:1}div.code-toolbar:focus-within>.toolbar{opacity:1}div.code-toolbar>.toolbar>.toolbar-item{display:inline-block}div.code-toolbar>.toolbar>.toolbar-item>a{cursor:pointer}div.code-toolbar>.toolbar>.toolbar-item>button{background:0 0;border:0;color:inherit;font:inherit;line-height:normal;overflow:visible;padding:0;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none}div.code-toolbar>.toolbar>.toolbar-item>a,div.code-toolbar>.toolbar>.toolbar-item>button,div.code-toolbar>.toolbar>.toolbar-item>span{color:#bbb;font-size:.8em;padding:0 .5em;background:#f5f2f0;background:rgba(224,224,224,.2);box-shadow:0 2px 0 0 rgba(0,0,0,.2);border-radius:.5em}div.code-toolbar>.toolbar>.toolbar-item>a:focus,div.code-toolbar>.toolbar>.toolbar-item>a:hover,div.code-toolbar>.toolbar>.toolbar-item>button:focus,div.code-toolbar>.toolbar>.toolbar-item>button:hover,div.code-toolbar>.toolbar>.toolbar-item>span:focus,div.code-toolbar>.toolbar>.toolbar-item>span:hover{color:inherit;text-decoration:none} diff --git a/src/plugin/prism/prism.js b/src/plugin/prism/prism.js new file mode 100644 index 0000000..ce9b2bc --- /dev/null +++ b/src/plugin/prism/prism.js @@ -0,0 +1,7 @@ +/* PrismJS 1.29.0 +https://prismjs.com/download.html#themes=prism-tomorrow&languages=clike+javascript&plugins=toolbar+copy-to-clipboard */ +var _self="undefined"!=typeof window?window:"undefined"!=typeof WorkerGlobalScope&&self instanceof WorkerGlobalScope?self:{},Prism=function(e){var n=/(?:^|\s)lang(?:uage)?-([\w-]+)(?=\s|$)/i,t=0,r={},a={manual:e.Prism&&e.Prism.manual,disableWorkerMessageHandler:e.Prism&&e.Prism.disableWorkerMessageHandler,util:{encode:function e(n){return n instanceof i?new i(n.type,e(n.content),n.alias):Array.isArray(n)?n.map(e):n.replace(/&/g,"&").replace(/=g.reach);A+=w.value.length,w=w.next){var E=w.value;if(n.length>e.length)return;if(!(E instanceof i)){var P,L=1;if(y){if(!(P=l(b,A,e,m))||P.index>=e.length)break;var S=P.index,O=P.index+P[0].length,j=A;for(j+=w.value.length;S>=j;)j+=(w=w.next).value.length;if(A=j-=w.value.length,w.value instanceof i)continue;for(var C=w;C!==n.tail&&(jg.reach&&(g.reach=W);var z=w.prev;if(_&&(z=u(n,z,_),A+=_.length),c(n,z,L),w=u(n,z,new i(f,p?a.tokenize(N,p):N,k,N)),M&&u(n,w,M),L>1){var I={cause:f+","+d,reach:W};o(e,n,t,w.prev,A,I),g&&I.reach>g.reach&&(g.reach=I.reach)}}}}}}function s(){var e={value:null,prev:null,next:null},n={value:null,prev:e,next:null};e.next=n,this.head=e,this.tail=n,this.length=0}function u(e,n,t){var r=n.next,a={value:t,prev:n,next:r};return n.next=a,r.prev=a,e.length++,a}function c(e,n,t){for(var r=n.next,a=0;a"+i.content+""},!e.document)return e.addEventListener?(a.disableWorkerMessageHandler||e.addEventListener("message",(function(n){var t=JSON.parse(n.data),r=t.language,i=t.code,l=t.immediateClose;e.postMessage(a.highlight(i,a.languages[r],r)),l&&e.close()}),!1),a):a;var g=a.util.currentScript();function f(){a.manual||a.highlightAll()}if(g&&(a.filename=g.src,g.hasAttribute("data-manual")&&(a.manual=!0)),!a.manual){var h=document.readyState;"loading"===h||"interactive"===h&&g&&g.defer?document.addEventListener("DOMContentLoaded",f):window.requestAnimationFrame?window.requestAnimationFrame(f):window.setTimeout(f,16)}return a}(_self);"undefined"!=typeof module&&module.exports&&(module.exports=Prism),"undefined"!=typeof global&&(global.Prism=Prism); +Prism.languages.clike={comment:[{pattern:/(^|[^\\])\/\*[\s\S]*?(?:\*\/|$)/,lookbehind:!0,greedy:!0},{pattern:/(^|[^\\:])\/\/.*/,lookbehind:!0,greedy:!0}],string:{pattern:/(["'])(?:\\(?:\r\n|[\s\S])|(?!\1)[^\\\r\n])*\1/,greedy:!0},"class-name":{pattern:/(\b(?:class|extends|implements|instanceof|interface|new|trait)\s+|\bcatch\s+\()[\w.\\]+/i,lookbehind:!0,inside:{punctuation:/[.\\]/}},keyword:/\b(?:break|catch|continue|do|else|finally|for|function|if|in|instanceof|new|null|return|throw|try|while)\b/,boolean:/\b(?:false|true)\b/,function:/\b\w+(?=\()/,number:/\b0x[\da-f]+\b|(?:\b\d+(?:\.\d*)?|\B\.\d+)(?:e[+-]?\d+)?/i,operator:/[<>]=?|[!=]=?=?|--?|\+\+?|&&?|\|\|?|[?*/~^%]/,punctuation:/[{}[\];(),.:]/}; +Prism.languages.javascript=Prism.languages.extend("clike",{"class-name":[Prism.languages.clike["class-name"],{pattern:/(^|[^$\w\xA0-\uFFFF])(?!\s)[_$A-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\.(?:constructor|prototype))/,lookbehind:!0}],keyword:[{pattern:/((?:^|\})\s*)catch\b/,lookbehind:!0},{pattern:/(^|[^.]|\.\.\.\s*)\b(?:as|assert(?=\s*\{)|async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|await|break|case|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally(?=\s*(?:\{|$))|for|from(?=\s*(?:['"]|$))|function|(?:get|set)(?=\s*(?:[#\[$\w\xA0-\uFFFF]|$))|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)\b/,lookbehind:!0}],function:/#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/,number:{pattern:RegExp("(^|[^\\w$])(?:NaN|Infinity|0[bB][01]+(?:_[01]+)*n?|0[oO][0-7]+(?:_[0-7]+)*n?|0[xX][\\dA-Fa-f]+(?:_[\\dA-Fa-f]+)*n?|\\d+(?:_\\d+)*n|(?:\\d+(?:_\\d+)*(?:\\.(?:\\d+(?:_\\d+)*)?)?|\\.\\d+(?:_\\d+)*)(?:[Ee][+-]?\\d+(?:_\\d+)*)?)(?![\\w$])"),lookbehind:!0},operator:/--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/}),Prism.languages.javascript["class-name"][0].pattern=/(\b(?:class|extends|implements|instanceof|interface|new)\s+)[\w.\\]+/,Prism.languages.insertBefore("javascript","keyword",{regex:{pattern:RegExp("((?:^|[^$\\w\\xA0-\\uFFFF.\"'\\])\\s]|\\b(?:return|yield))\\s*)/(?:(?:\\[(?:[^\\]\\\\\r\n]|\\\\.)*\\]|\\\\.|[^/\\\\\\[\r\n])+/[dgimyus]{0,7}|(?:\\[(?:[^[\\]\\\\\r\n]|\\\\.|\\[(?:[^[\\]\\\\\r\n]|\\\\.|\\[(?:[^[\\]\\\\\r\n]|\\\\.)*\\])*\\])*\\]|\\\\.|[^/\\\\\\[\r\n])+/[dgimyus]{0,7}v[dgimyus]{0,7})(?=(?:\\s|/\\*(?:[^*]|\\*(?!/))*\\*/)*(?:$|[\r\n,.;:})\\]]|//))"),lookbehind:!0,greedy:!0,inside:{"regex-source":{pattern:/^(\/)[\s\S]+(?=\/[a-z]*$)/,lookbehind:!0,alias:"language-regex",inside:Prism.languages.regex},"regex-delimiter":/^\/|\/$/,"regex-flags":/^[a-z]+$/}},"function-variable":{pattern:/#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)\s*=>))/,alias:"function"},parameter:[{pattern:/(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/,lookbehind:!0,inside:Prism.languages.javascript},{pattern:/(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i,lookbehind:!0,inside:Prism.languages.javascript},{pattern:/(\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*=>)/,lookbehind:!0,inside:Prism.languages.javascript},{pattern:/((?:\b|\s|^)(?!(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*\s*)\(\s*|\]\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*\{)/,lookbehind:!0,inside:Prism.languages.javascript}],constant:/\b[A-Z](?:[A-Z_]|\dx?)*\b/}),Prism.languages.insertBefore("javascript","string",{hashbang:{pattern:/^#!.*/,greedy:!0,alias:"comment"},"template-string":{pattern:/`(?:\\[\s\S]|\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}|(?!\$\{)[^\\`])*`/,greedy:!0,inside:{"template-punctuation":{pattern:/^`|`$/,alias:"string"},interpolation:{pattern:/((?:^|[^\\])(?:\\{2})*)\$\{(?:[^{}]|\{(?:[^{}]|\{[^}]*\})*\})+\}/,lookbehind:!0,inside:{"interpolation-punctuation":{pattern:/^\$\{|\}$/,alias:"punctuation"},rest:Prism.languages.javascript}},string:/[\s\S]+/}},"string-property":{pattern:/((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m,lookbehind:!0,greedy:!0,alias:"property"}}),Prism.languages.insertBefore("javascript","operator",{"literal-property":{pattern:/((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m,lookbehind:!0,alias:"property"}}),Prism.languages.markup&&(Prism.languages.markup.tag.addInlined("script","javascript"),Prism.languages.markup.tag.addAttribute("on(?:abort|blur|change|click|composition(?:end|start|update)|dblclick|error|focus(?:in|out)?|key(?:down|up)|load|mouse(?:down|enter|leave|move|out|over|up)|reset|resize|scroll|select|slotchange|submit|unload|wheel)","javascript")),Prism.languages.js=Prism.languages.javascript; +!function(){if("undefined"!=typeof Prism&&"undefined"!=typeof document){var e=[],t={},n=function(){};Prism.plugins.toolbar={};var a=Prism.plugins.toolbar.registerButton=function(n,a){var r;r="function"==typeof a?a:function(e){var t;return"function"==typeof a.onClick?((t=document.createElement("button")).type="button",t.addEventListener("click",(function(){a.onClick.call(this,e)}))):"string"==typeof a.url?(t=document.createElement("a")).href=a.url:t=document.createElement("span"),a.className&&t.classList.add(a.className),t.textContent=a.text,t},n in t?console.warn('There is a button with the key "'+n+'" registered already.'):e.push(t[n]=r)},r=Prism.plugins.toolbar.hook=function(a){var r=a.element.parentNode;if(r&&/pre/i.test(r.nodeName)&&!r.parentNode.classList.contains("code-toolbar")){var o=document.createElement("div");o.classList.add("code-toolbar"),r.parentNode.insertBefore(o,r),o.appendChild(r);var i=document.createElement("div");i.classList.add("toolbar");var l=e,d=function(e){for(;e;){var t=e.getAttribute("data-toolbar-order");if(null!=t)return(t=t.trim()).length?t.split(/\s*,\s*/g):[];e=e.parentElement}}(a.element);d&&(l=d.map((function(e){return t[e]||n}))),l.forEach((function(e){var t=e(a);if(t){var n=document.createElement("div");n.classList.add("toolbar-item"),n.appendChild(t),i.appendChild(n)}})),o.appendChild(i)}};a("label",(function(e){var t=e.element.parentNode;if(t&&/pre/i.test(t.nodeName)&&t.hasAttribute("data-label")){var n,a,r=t.getAttribute("data-label");try{a=document.querySelector("template#"+r)}catch(e){}return a?n=a.content:(t.hasAttribute("data-url")?(n=document.createElement("a")).href=t.getAttribute("data-url"):n=document.createElement("span"),n.textContent=r),n}})),Prism.hooks.add("complete",r)}}(); +!function(){function t(t){var e=document.createElement("textarea");e.value=t.getText(),e.style.top="0",e.style.left="0",e.style.position="fixed",document.body.appendChild(e),e.focus(),e.select();try{var o=document.execCommand("copy");setTimeout((function(){o?t.success():t.error()}),1)}catch(e){setTimeout((function(){t.error(e)}),1)}document.body.removeChild(e)}"undefined"!=typeof Prism&&"undefined"!=typeof document&&(Prism.plugins.toolbar?Prism.plugins.toolbar.registerButton("copy-to-clipboard",(function(e){var o=e.element,n=function(t){var e={copy:"Copy","copy-error":"Press Ctrl+C to copy","copy-success":"Copied!","copy-timeout":5e3};for(var o in e){for(var n="data-prismjs-"+o,c=t;c&&!c.hasAttribute(n);)c=c.parentElement;c&&(e[o]=c.getAttribute(n))}return e}(o),c=document.createElement("button");c.className="copy-to-clipboard-button",c.setAttribute("type","button");var r=document.createElement("span");return c.appendChild(r),u("copy"),function(e,o){e.addEventListener("click",(function(){!function(e){navigator.clipboard?navigator.clipboard.writeText(e.getText()).then(e.success,(function(){t(e)})):t(e)}(o)}))}(c,{getText:function(){return o.textContent},success:function(){u("copy-success"),i()},error:function(){u("copy-error"),setTimeout((function(){!function(t){window.getSelection().selectAllChildren(t)}(o)}),1),i()}}),c;function i(){setTimeout((function(){u("copy")}),n["copy-timeout"])}function u(t){r.textContent=n[t],c.setAttribute("data-copy-state",t)}})):console.warn("Copy to Clipboard plugin loaded before Toolbar plugin."))}(); diff --git a/src/toastify.js b/src/toastify.js index 5d9659c..32e45cf 100644 --- a/src/toastify.js +++ b/src/toastify.js @@ -44,7 +44,8 @@ offset: {x: 0, y: 0}, escapeMarkup: true, ariaLive: 'polite', - style: {background: ''} + style: {background: ''}, + sound: {enable: false} }; // Defining the prototype of the object @@ -89,7 +90,9 @@ if(options.backgroundColor) { this.options.style.background = options.backgroundColor; } - + if(options.sound) { + this.options.sound = options.sound || false; // To enable disable sounds + } // Returning the current object for chaining functions return this; }, @@ -300,6 +303,12 @@ this.options.duration ); // Binding `this` for function invocation } + // // If toast with sound + if(this.options.sound && typeof this.options.sound === 'object'){ + console.log('this.options.sound.type',this.options.sound, typeof this.options.sound === 'object' ) + // Check is default or not + makeSound({default: this.options.sound.default, type:this.options.sound.type, src: !this.options.sound.default ? this.options.sound.src : ''}); + } // Supporting function chaining return this; @@ -437,6 +446,25 @@ } } + // Function to trigger sound + function makeSound(obj){ + if(obj.default){ + const availableSound = ['bell','alert', 'pop']; + if(!availableSound.includes(obj.type)){ + throw "Please select default sound type ('bell','alert','pop')"; + } + const path = `${'./sounds/'+obj.type+'.wav'}` + var audio = new Audio(path); + // Play the bell sound + audio.play(); + } else if(!obj.default) { + const path = obj.src + var audio = new Audio(path); + // Play the bell sound + audio.play(); + } + } + // Setting up the prototype for the init object Toastify.lib.init.prototype = Toastify.lib;