@engie-group/fluid-design-system-angular
Version:
Fluid Design System Angular
124 lines (107 loc) • 3.56 kB
text/typescript
import {
ChangeDetectionStrategy,
Component,
ComponentRef,
EventEmitter,
inject,
Output,
TemplateRef,
ViewChild,
ViewContainerRef
} from '@angular/core';
import { take } from 'rxjs';
import { ButtonComponent, ToastComponent, ToastService } from '../../public-api';
export class ToastExComponent {
// Either use ViewChild to gain access to toast events
toast: ToastComponent;
// Or Mirror toast events to be able to listen to them
closed = new EventEmitter<Event>();
dismissed = new EventEmitter<void>();
}
export class ToastServiceStoryComponent extends ToastComponent {
isFullWidth = false;
private viewContainerRef = inject(ViewContainerRef);
private toastService = inject(ToastService);
openToast(toast: TemplateRef<any>) {
this.toastService.open(toast, { viewContainerRef: this.viewContainerRef });
}
openComponentToast() {
const toastRef: ComponentRef<ToastExComponent> = this.toastService.open(ToastExComponent, {
viewContainerRef: this.viewContainerRef
});
// If mirroring events in component
toastRef.instance.closed.pipe(take(1)).subscribe((ev) => {
this.onToastClose(ev);
});
toastRef.instance.dismissed.pipe(take(1)).subscribe(() => {
this.onToastDismiss();
});
// If using viewChild, we need to wrap in setTimeout to make sure Custom component is already rendered,
// and we have access to child ToastComponent
setTimeout(() => {
const toastComponent = toastRef.instance.toast;
if (toastComponent) {
toastComponent.closed.pipe(take(1)).subscribe((ev) => {
this.onToastClose(ev);
});
toastComponent.dismissed.pipe(take(1)).subscribe(() => {
this.onToastDismiss();
});
}
});
}
onToastClose(event: Event) {
console.warn('Do Action on toast close');
}
onToastDismiss() {
console.warn('Do Action on toast dismiss');
}
toggleFullWidth() {
this.isFullWidth = !this.isFullWidth;
this.toastService.setConfig({ isContainerFullWidth: this.isFullWidth });
}
}