import { ChangeDetectionStrategy, Component, ElementRef, effect, inject, signal, viewChild } from '@angular/core'; import { A11yModule } from '@angular/cdk/a11y'; import { FormsModule } from '@angular/forms'; import { MatIconModule } from '@angular/material/icon'; import { DialogService } from './dialog.service'; import { ToastService } from './toast.service'; /** Toasts and the dialog — placed once in the app root. */ @Component({ selector: 'app-overlays', imports: [A11yModule, FormsModule, MatIconModule], changeDetection: ChangeDetectionStrategy.OnPush, template: `
@for (toast of toasts.toasts(); track toast.id) {
{{ toast.kind === 'error' ? 'error' : 'check_circle' }} {{ toast.text }}
}
@if (dialogs.current(); as d) {
} ` }) export class OverlaysComponent { readonly toasts = inject(ToastService); readonly dialogs = inject(DialogService); readonly value = signal(''); private input = viewChild>('input'); constructor() { effect(() => { const d = this.dialogs.current(); this.value.set(d?.input ?? ''); }); effect(() => { const el = this.input()?.nativeElement; if (el) setTimeout(() => el.select()); }); } confirm(): void { const d = this.dialogs.current(); if (!d || (d.required && !this.value().trim())) return; d.resolve(d.input !== undefined ? this.value().trim() : ''); } cancel(): void { this.dialogs.current()?.resolve(null); } }