Add progressbar background to toasts

This commit is contained in:
shamoon 2023-11-09 22:12:13 -08:00
parent 8c67e486c7
commit accd05ee65
6 changed files with 49 additions and 3 deletions

View File

@ -3,7 +3,10 @@
[autohide]="true" [delay]="toast.delay"
[class]="toast.classname"
[class.mb-2]="true"
(shown)="onShow(toast)"
(hidden)="toastService.closeToast(toast)">
<ngb-progressbar class="position-absolute h-100 w-100 top-90 start-0 bottom-0 end-0 pointer-events-none" type="dark" [max]="toast.delay" [value]="toast.delayRemaining"></ngb-progressbar>
<span class="visually-hidden">{{ toast.delayRemaining / 1000 | number: '1.0-0' }} seconds</span>
<div class="d-flex">
<p class="mb-0">{{toast.content}}</p>
<button type="button" class="btn-close ms-auto" data-bs-dismiss="toast" aria-label="Close" (click)="toastService.closeToast(toast);"></button>

View File

@ -10,10 +10,23 @@
display: block; // this corrects an ng-bootstrap bug that prevented animations
}
::ng-deep .toast-body {
position: relative;
}
::ng-deep .toast.error {
border-color: hsla(350, 79%, 40%, 0.4); // bg-danger
}
::ng-deep .toast.error .toast-body {
background-color: hsla(350, 79%, 40%, 0.9); // bg-danger
background-color: hsla(350, 79%, 40%, 0.8); // bg-danger
border-top-left-radius: inherit;
border-top-right-radius: inherit;
border-bottom-left-radius: inherit;
border-bottom-right-radius: inherit;
}
.progress {
background-color: var(--pngx-primary);
opacity: .07;
}

View File

@ -3,6 +3,7 @@ import {
discardPeriodicTasks,
fakeAsync,
flush,
tick,
} from '@angular/core/testing'
import { ToastService } from 'src/app/services/toast.service'
import { ToastsComponent } from './toasts.component'
@ -92,6 +93,17 @@ describe('ToastsComponent', () => {
discardPeriodicTasks()
}))
it('should countdown toast', fakeAsync(() => {
component.ngOnInit()
fixture.detectChanges()
component.onShow(toasts[0])
tick(5000)
expect(component.toasts[0].delayRemaining).toEqual(0)
component.ngOnDestroy()
flush()
discardPeriodicTasks()
}))
it('should show an error if given with toast', fakeAsync(() => {
component.ngOnInit()
fixture.detectChanges()

View File

@ -1,5 +1,5 @@
import { Component, OnDestroy, OnInit } from '@angular/core'
import { Subscription } from 'rxjs'
import { Subscription, interval, take } from 'rxjs'
import { Toast, ToastService } from 'src/app/services/toast.service'
import { Clipboard } from '@angular/cdk/clipboard'
@ -20,6 +20,8 @@ export class ToastsComponent implements OnInit, OnDestroy {
public copied: boolean = false
public seconds: number = 0
ngOnDestroy(): void {
this.subscription?.unsubscribe()
}
@ -37,6 +39,20 @@ export class ToastsComponent implements OnInit, OnDestroy {
})
}
onShow(toast: Toast) {
const refreshInterval = 150
const delay = toast.delay - 500 // for fade animation
interval(refreshInterval)
.pipe(take(delay / refreshInterval))
.subscribe((count) => {
toast.delayRemaining = Math.max(
0,
delay - refreshInterval * (count + 1)
)
})
}
public isDetailedError(error: any): boolean {
return (
typeof error === 'object' &&

View File

@ -6,6 +6,8 @@ export interface Toast {
delay: number
delayRemaining?: number
action?: any
actionName?: string

View File

@ -250,7 +250,7 @@ $form-check-radio-checked-bg-image-dark: url("data:image/svg+xml,<svg xmlns='htt
}
.toast {
background-color: hsla(var(--pngx-primary), calc(var(--pngx-primary-lightness) - 10%), 0.9);
background-color: hsla(var(--pngx-primary), calc(var(--pngx-primary-lightness) - 15%), 0.9);
}
.toast,