UNPKG

ngx-toastr-notifier

Version:

Lightweight, customizable toast notifications for Angular 20+. A modern replacement for toastr with Angular Material design and flexible APIs.

1 lines 13.4 kB
{"version":3,"file":"ngx-toastr-notifier.mjs","sources":["../../../projects/ngx-toastr-notifier/src/lib/progress-bar.ts","../../../projects/ngx-toastr-notifier/src/lib/progress-bar.html","../../../projects/ngx-toastr-notifier/src/lib/toast.ts","../../../projects/ngx-toastr-notifier/src/lib/toast.html","../../../projects/ngx-toastr-notifier/src/lib/toast.service.ts","../../../projects/ngx-toastr-notifier/src/public-api.ts","../../../projects/ngx-toastr-notifier/src/ngx-toastr-notifier.ts"],"sourcesContent":["import { Component, OnInit, OnDestroy, ChangeDetectorRef, ChangeDetectionStrategy, Input, inject } from '@angular/core';\nimport { interval, Subscription } from 'rxjs';\n\n\n@Component({\n selector: 'lib-progress-bar',\n templateUrl: './progress-bar.html',\n styleUrl: './_progress-bar.scss',\n changeDetection: ChangeDetectionStrategy.OnPush,\n})\nexport class ProgressBar implements OnInit, OnDestroy {\n private cdr = inject(ChangeDetectorRef);\n\n\n @Input() duration = 2000; // ms, default value\n progressValue = 100;\n\n\n private sub!: Subscription;\n\n ngOnInit() {\n const stepTime = 100; // ms\n const totalSteps = (this.duration) / stepTime;\n let currentStep = 0;\n this.sub = interval(stepTime).subscribe(() => {\n currentStep++;\n this.progressValue = 100 - (currentStep / totalSteps) * 100;\n\n this.cdr.markForCheck();\n if (this.progressValue <= 0) {\n this.progressValue = 0;\n this.sub.unsubscribe();\n }\n });\n }\n\n\n ngOnDestroy() {\n if (this.sub) {\n this.sub.unsubscribe();\n }\n\n }\n}\n","<div [style.width.%]=\"progressValue\" class=\"progress-inner\"></div>","\nimport { ChangeDetectionStrategy, Component, inject } from '@angular/core';\nimport { MatSnackBarRef, MAT_SNACK_BAR_DATA, MatSnackBarAction } from '@angular/material/snack-bar';\nimport { ViewEncapsulation } from '@angular/core';\nimport { ProgressBar } from \"./progress-bar\";\n\nexport interface ToastData {\n type: 'success' | 'info' | 'warning' | 'error';\n panelClass?: string;\n title?: string;\n message: string;\n icon?: string;\n duration: number; // in milliseconds\n showClose?: boolean;\n progressBar?: boolean;\n}\n\n\n@Component({\n selector: 'lib-toast',\n imports: [MatSnackBarAction, ProgressBar],\n templateUrl: './toast.html',\n styleUrl: './toast.scss',\n changeDetection: ChangeDetectionStrategy.OnPush,\n encapsulation: ViewEncapsulation.None,\n})\nexport class Toast {\n snackRef = inject<MatSnackBarRef<Toast>>(MatSnackBarRef);\n data = inject<ToastData>(MAT_SNACK_BAR_DATA);\n\n dismiss() {\n this.snackRef.dismiss();\n }\n}\n","<div class=\"toast\">\n <span class=\"toast-icon\">\n\n @switch (data.type) {\n @case ('success') {\n <svg class=\"w-8 h-8\" aria-hidden=\"true\" xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\"\n viewBox=\"0 0 20 20\">\n <path\n d=\"M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 8.207-4 4a1 1 0 0 1-1.414 0l-2-2a1 1 0 0 1 1.414-1.414L9 10.586l3.293-3.293a1 1 0 0 1 1.414 1.414Z\" />\n </svg>\n }\n @case ('error') {\n <svg class=\"w-5 h-5\" aria-hidden=\"true\" xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\"\n viewBox=\"0 0 20 20\">\n <path\n d=\"M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5Zm3.707 11.793a1 1 0 1 1-1.414 1.414L10 11.414l-2.293 2.293a1 1 0 0 1-1.414-1.414L8.586 10 6.293 7.707a1 1 0 0 1 1.414-1.414L10 8.586l2.293-2.293a1 1 0 0 1 1.414 1.414L11.414 10l2.293 2.293Z\" />\n </svg>\n }\n @case ('warning') {\n <svg class=\"w-5 h-5\" aria-hidden=\"true\" xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\"\n viewBox=\"0 0 16 16\">\n <path\n d=\"M8.982 1.566a1.13 1.13 0 0 0-1.96 0L.165 13.233c-.457.778.091 1.767.98 1.767h13.713c.889 0 1.438-.99.98-1.767zM8 5c.535 0 .954.462.9.995l-.35 3.507a.552.552 0 0 1-1.1 0L7.1 5.995A.905.905 0 0 1 8 5m.002 6a1 1 0 1 1 0 2 1 1 0 0 1 0-2\" />\n </svg>\n }\n @default {\n <svg class=\"w-5 h-5\" aria-hidden=\"true\" xmlns=\"http://www.w3.org/2000/svg\" fill=\"currentColor\"\n viewBox=\"0 0 20 20\">\n <path\n d=\"M10 .5a9.5 9.5 0 1 0 9.5 9.5A9.51 9.51 0 0 0 10 .5ZM10 15a1 1 0 1 1 0-2 1 1 0 0 1 0 2Zm1-4a1 1 0 0 1-2 0V6a1 1 0 0 1 2 0v5Z\" />\n </svg>\n }\n }\n\n </span>\n <div class=\"toast-message\">\n @if(data.title){\n <p style=\"font-size: 18px; margin: 0px 0 5px 0;\"> <strong>{{data.title}}</strong></p>\n }\n @if (data.message){\n <p style=\"font-size: 16px; margin: 0;\">{{data.message}}</p>\n }\n </div>\n @if (data.showClose){\n <button matSnackBarAction (click)=\"snackRef.dismissWithAction()\" type=\"button\" class=\"toast-close\"\n aria-label=\"Close\">\n <span aria-hidden=\"true\">&times;</span>\n </button>\n }\n</div>\n@if(data.progressBar){\n<span style=\"display: block; width: 100%; position: absolute; bottom: 0; left: 0;\">\n <lib-progress-bar [duration]=\"data.duration\" />\n</span>\n}","import { Injectable, inject } from '@angular/core';\nimport { MatSnackBar, MatSnackBarConfig } from '@angular/material/snack-bar';\nimport { Toast, ToastData } from './toast';\n\ntype ToastParameter = Partial<Pick<MatSnackBarConfig<ToastData>, 'direction' | 'horizontalPosition' | 'verticalPosition' | 'duration'>>\n & { showClose?: boolean } & { progressBar?: boolean };\n\n@Injectable({ providedIn: 'root' })\nexport class ToastService {\n private snackBar = inject(MatSnackBar);\n\n\n private readonly commonConfig: Partial<MatSnackBarConfig<ToastData>> = {\n duration: 2000,\n horizontalPosition: 'right',\n verticalPosition: 'top',\n direction: 'ltr',\n }\n\n // default = info\n private readonly defaultConfig: MatSnackBarConfig<ToastData> = {\n ...this.commonConfig,\n data: { type: 'info', message: '', showClose: true, duration: this.commonConfig.duration!, progressBar: false },\n };\n\n private readonly sucessConfig: MatSnackBarConfig<ToastData> = {\n ...this.commonConfig,\n data: { type: 'success', message: '', showClose: true, duration: this.commonConfig.duration!, progressBar: false }\n }\n\n private readonly warningConfig: MatSnackBarConfig<ToastData> = {\n ...this.commonConfig,\n data: { type: 'warning', message: '', showClose: true, duration: this.commonConfig.duration!, progressBar: false }\n }\n\n private readonly errorConfig: MatSnackBarConfig<ToastData> = {\n ...this.commonConfig,\n data: { type: 'error', message: '', showClose: true, duration: this.commonConfig.duration!, progressBar: false }\n }\n\n show(\n type: ToastData['type'],\n message: string,\n title?: string,\n opts?: ToastParameter\n ) {\n\n const data: ToastData = {\n type,\n message,\n title,\n showClose: opts?.showClose ?? true,\n duration: opts?.duration ?? this.commonConfig.duration!,\n progressBar: opts?.progressBar ?? false,\n };\n const config = { ...this.defaultConfig, ...(opts ?? {}), data }; // override of default\n config.panelClass = type;\n\n return this.snackBar.openFromComponent(Toast, config);\n }\n\n\n info(message: string, title?: string, opts?: ToastParameter) {\n const infoCfg = { ...this.defaultConfig, ...(opts) ?? {} };\n return this.show('info', message, title, infoCfg);\n }\n\n success(message: string, title?: string, opts?: ToastParameter) {\n const successCfg = { ...this.sucessConfig, ...(opts) ?? {} };\n return this.show('success', message, title, successCfg);\n }\n\n warning(message: string, title?: string, opts?: ToastParameter) {\n const warningCfg = { ...this.warningConfig, ...(opts) ?? {} };\n return this.show('warning', message, title, warningCfg);\n }\n\n error(message: string, title?: string, opts?: ToastParameter) {\n const errorCfg = { ...this.errorConfig, ...(opts) ?? {} };\n return this.show('error', message, title, errorCfg);\n }\n}\n","/*\n * Public API Surface of ngx-toastr-notifier\n */\n\nexport * from './lib/toast.service';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mapp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