File

src/lib/error-dialog/error-dialog.component.ts

Implements

AfterViewInit

Metadata

Index

Properties
Methods
Outputs

Outputs

closeDialog
Type : EventEmitter

Methods

close
close()
Returns : void

Properties

Public Readonly activeIndex
Default value : signal(0)
Public Readonly componentPortal
Default value : computed(() => { const index = this.activeIndex(); const data = this.data(); const injector = Injector.create({ parent: this.injector, providers: [ { provide: RXAP_ERROR_DIALOG_ERROR, useValue: data[index], }, ], }); return new ComponentPortal(this.component, null, injector); })
Public Readonly data
Type : Signal<any[]>
Default value : inject(RXAP_ERROR_DIALOG_DATA)
Public dialog
Type : ElementRef<HTMLDialogElement>
Decorators :
@ViewChild('dialog', {static: true})
Public Readonly displayedButtons
Default value : computed(() => { const start = Math.max(0, this.activeIndex() - 2); const end = Math.min(this.data().length, start + 5); return Array.from({ length: end - start }, (_, i) => start + i); })
import {
  ComponentPortal,
  PortalModule,
} from '@angular/cdk/portal';
import {
  NgClass,
  NgForOf,
  NgIf,
} from '@angular/common';
import {
  AfterViewInit,
  ChangeDetectionStrategy,
  Component,
  computed,
  ElementRef,
  EventEmitter,
  inject,
  INJECTOR,
  Injector,
  Output,
  signal,
  Signal,
  ViewChild,
} from '@angular/core';
import {
  RXAP_ERROR_DIALOG_COMPONENT,
  RXAP_ERROR_DIALOG_DATA,
  RXAP_ERROR_DIALOG_ERROR,
} from '../tokens';

export interface IErrorDialogComponent<Error = any> {
  error: Error;
}

@Component({
    selector: 'rxap-error-dialog',
    templateUrl: './error-dialog.component.html',
    styleUrls: ['./error-dialog.component.scss'],
    changeDetection: ChangeDetectionStrategy.Default,
    imports: [
        NgIf,
        NgClass,
        NgForOf,
        PortalModule,
    ]
})
export class ErrorDialogComponent implements AfterViewInit {

  @ViewChild('dialog', { static: true })
  public dialog!: ElementRef<HTMLDialogElement>;

  public readonly data: Signal<any[]> = inject(RXAP_ERROR_DIALOG_DATA);

  public readonly activeIndex = signal(0);
  public readonly displayedButtons = computed(() => {
    const start = Math.max(0, this.activeIndex() - 2);
    const end = Math.min(this.data().length, start + 5);
    return Array.from({ length: end - start }, (_, i) => start + i);
  });
  @Output()
  public closeDialog = new EventEmitter<void>();
  private readonly component = inject(RXAP_ERROR_DIALOG_COMPONENT);
  private readonly injector = inject(INJECTOR);
  public readonly componentPortal = computed(() => {
    const index = this.activeIndex();
    const data = this.data();
    const injector = Injector.create({
      parent: this.injector,
      providers: [
        {
          provide: RXAP_ERROR_DIALOG_ERROR,
          useValue: data[index],
        },
      ],
    });
    return new ComponentPortal(this.component, null, injector);
  });

  ngAfterViewInit() {
    this.dialog.nativeElement.showModal();
  }

  close() {
    this.dialog.nativeElement.close();
    this.closeDialog.emit();
  }

}
<dialog #dialog
        class="rounded overflow-auto drop-shadow-2xl bg-white text-black dark:bg-black dark:text-white flex flex-col gap-3 p-6"
        style="max-width: 80vw; max-height: 80vh">
  <div *ngIf="data().length > 1" class="controls flex justify-center space-x-2 mt-4">
    <button
      (click)="activeIndex.set(activeIndex() - 1)"
      *ngIf="activeIndex() > 0" class="px-4 py-2 bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-900 hover:bg-gray-300 transition-colors duration-150 rounded">Previous
    </button>
    <button (click)="activeIndex.set(item)"
            *ngFor="let item of displayedButtons()"
            [ngClass]="{
            'bg-primary-500': item === activeIndex(),
            'bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-900 hover:bg-gray-300 transition-colors duration-150': item !== activeIndex()
            }"
            class="px-4 py-2 rounded">
      {{ item + 1 }}
    </button>
    <button
      (click)="activeIndex.set(activeIndex() + 1)"
      *ngIf="activeIndex() < data().length - 1" class="px-4 py-2 bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-900 hover:bg-gray-300 transition-colors duration-150 rounded">Next
    </button>
  </div>
  <div class="flex flex-col overflow-auto">
    <ng-template [cdkPortalOutlet]="componentPortal()"></ng-template>
  </div>
  <button (click)="close()" class="px-4 py-2 rounded bg-primary-500" i18n>close</button>
</dialog>

./error-dialog.component.scss

dialog::backdrop {
  background: rgba(0, 0, 0, 0.7);
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""