@@ -237,4 +237,77 @@ export class Spinner {
237237 document . body . appendChild ( newSpinner ) ;
238238 }
239239 }
240+ }
241+
242+ /**
243+ * @class Toast
244+ * @description Create a new Neptune Toast
245+ *
246+ * @param {any } config Add your configuration
247+ *
248+ * parent -> class or id of your target element, when null its document.body
249+ *
250+ * icon -> add your icon
251+ *
252+ * text -> add your message text
253+ *
254+ * style -> primary, cta, information, success, warning, error
255+ *
256+ * position -> left-top, left-bottom, right-top, right-bottom
257+ *
258+ * @example
259+ * const myToast = new Toast({
260+ * icon: "YOUR ICON",
261+ * text: "Test Toast",
262+ * style: "primary",
263+ * position: "top-right"
264+ * });
265+ */
266+ export class Toast {
267+ constructor ( config ) {
268+ // Create Toast
269+ const newToast = document . createElement ( 'div' ) ;
270+ newToast . classList . add ( 'toast' ) ;
271+
272+ // Add Icon
273+ if ( config . icon != null ) {
274+ // Create Icon
275+ const newIcon = document . createElement ( 'span' ) ;
276+ newIcon . classList . add ( 'toast-icon' ) ;
277+ newIcon . innerHTML = config . icon ;
278+
279+ // Append icon
280+ newToast . appendChild ( newIcon ) ;
281+ }
282+
283+ // Add Message
284+ if ( config . text != null ) {
285+ // Create Message
286+ const newMessage = document . createElement ( 'p' ) ;
287+ newMessage . classList . add ( 'toast-text' , 'text-l' ) ;
288+ newMessage . innerText = config . text ;
289+
290+ // Append Message
291+ newToast . appendChild ( newMessage ) ;
292+ } else {
293+ console . error ( 'Error! Please ad a Message to your Toast!' ) ;
294+ }
295+
296+ // Setup Style
297+ if ( config . style != null ) {
298+ newToast . classList . add ( 'toast-' + config . style ) ;
299+ }
300+
301+ // Setup Position
302+ if ( config . position != null ) {
303+ newToast . classList . add ( 'toast-' + config . position ) ;
304+ }
305+
306+ // Append to parent element
307+ if ( config . parent != null ) {
308+ document . querySelector ( config . parent ) . appendChild ( newToast ) ;
309+ } else {
310+ document . body . appendChild ( newToast ) ;
311+ }
312+ }
240313}
0 commit comments