1+ /**
2+ * Custom Simple Toast Class
3+ * Developed by: oretnom23
4+ * Please load this file with the CustomToast.css file and Google Icons
5+ */
6+ class CustomToast {
7+ // Toast Box variable
8+ toastBox ;
9+ // Toast Box Duration variable
10+ duration ;
11+ // Valid Toast Icons
12+ toastIcon = {
13+ success : `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><circle cx="12" cy="12" r="10" fill="#4caf50"/><path d="M9 16.2l-4.2-4.2L3 13.8l6 6 12-12-1.4-1.4z" fill="#fff"/></svg>` ,
14+ danger : `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><circle cx="12" cy="12" r="10" fill="#f44336"/><path d="M15 9l-6 6m0-6l6 6" stroke="#fff" stroke-width="2"/></svg>` ,
15+ warning : `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><circle cx="12" cy="12" r="10" fill="#ff9800"/><path d="M11 17h2v-2h-2v2zm0-4h2V7h-2v6z" fill="#fff"/></svg>` ,
16+ info : `<svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 0 24 24" width="24"><circle cx="12" cy="12" r="10" fill="#2196f3"/><path d="M11 17h2v-2h-2v2zm0-4h2V7h-2v6z" fill="#fff"/></svg>` ,
17+ } ;
18+ show ( message = "Sample Message" , toastType = "info" , duration = 5000 ) {
19+ // Check if toast type is valid, otherwise make info toast as the default
20+ if ( ! Object . keys ( this . toastIcon ) . includes ( toastType ) )
21+ toastType = `info` ;
22+ // Creatign the Toast Box Element
23+ this . toastBox = document . createElement ( 'div' )
24+ // Adding .toast class to Toast Box Element
25+ this . toastBox . classList . add ( 'toast' , `toast-${ toastType } ` )
26+ // Toast box content
27+ this . toastBox . innerHTML = `<button class="toast-close-btn"><svg xmlns="http://www.w3.org/2000/svg" height="24" viewBox="0 96 960 960" width="24"><path d="M480 606.923 273.077 813.846q-8.615 8.615-21.231 8.615-12.615 0-21.231-8.615-8.615-8.615-8.615-21.231 0-12.615 8.615-21.231L437.077 585.692 230.154 378.769q-8.615-8.615-8.615-21.231 0-12.615 8.615-21.231 8.615-8.615 21.231-8.615 12.615 0 21.231 8.615L480 564.077l206.923-206.923q8.615-8.615 21.231-8.615 12.615 0 21.231 8.615 8.615 8.615 8.615 21.231 0 12.615-8.615 21.231L522.923 585.692l206.923 206.923q8.615 8.615 8.615 21.231 0 12.615-8.615 21.231-8.615 8.615-21.231 8.615-12.615 0-21.231-8.615L480 606.923Z"/></svg></button>
28+ <div class="toast-content-wrapper">
29+ <div class="toast-icon">
30+ ${ this . toastIcon [ toastType ] }
31+ </div>
32+ <div class="toast-message">${ message } </div>
33+ <div class="toast-progress"></div>
34+ </div>` ;
35+ // Set Toast Duration
36+ this . duration = duration
37+ // Update Toast Duration
38+ this . toastBox . querySelector ( '.toast-progress' ) . style . animationDuration = `${ this . duration / 1000 } s`
39+
40+ // Remove Current Toast Notification if Exists
41+ if ( document . body . querySelector ( '.toast' ) != null )
42+ document . body . querySelector ( '.toast' ) . remove ( ) ;
43+ // Append New Toast Notification to the document
44+ document . body . appendChild ( this . toastBox )
45+ // Trigger closing duration
46+ this . triggerClose ( )
47+ // When Close Icon/Button is clicked event listener
48+ this . toastBox . querySelector ( '.toast-close-btn' ) . addEventListener ( 'click' , e => {
49+ e . preventDefault ( )
50+ // Trigger immediate closing
51+ this . triggerClose ( 0 )
52+ } )
53+ }
54+ triggerClose ( closeDuration = null ) {
55+ // Set toast duration as the closing delay if the closing duration value is null
56+ if ( closeDuration == null ) {
57+ closeDuration = this . duration
58+ }
59+ setTimeout ( ( ) => {
60+ // adding closing class for closing animation
61+ this . toastBox . classList . add ( 'closing' )
62+ // trigger removing the toast notification
63+ this . closeToast ( )
64+ } , closeDuration )
65+ }
66+ closeToast ( ) {
67+ // Set removing toast delay
68+ setTimeout ( ( ) => {
69+ this . toastBox . remove ( ) ;
70+ } , 500 )
71+ }
72+ }
0 commit comments