확인 대화상자
확인 대화상자 (ConfirmDialog)
ConfirmDialog는 사용자 동작을 확인하는 확인 창을 간편하게 띄워 주는 컴포넌트예요. 내부적으로 Observable을 활용하는 ConfirmationService로 구동되기 때문에, 같은 컴포넌트 안의 여러 동작이 하나의 확인 창을 공유할 수 있어요. 이 문서에서는 기본 사용법부터 위치 지정, 템플릿 커스터마이징, 접근성, 테마 설정까지 확인 대화상자의 모든 기능을 다루고 있어요
출처: 문서
본문
접근성
스크린 리더 ConfirmDialog 컴포넌트는 header 요소를 참조하는 aria-labelledby와 함께 alertdialog role을 사용해요. 하지만 모든 속성은 루트 요소로 전달되기 때문에 aria-labelledby를 사용해서 이 기본 동작을 재정의할 수 있어요. 또한 포커스가 팝업 내부에 유지되기 때문에 aria-modal이 추가돼요. 버튼처럼 키보드로 접근할 수 있는 트리거 컴포넌트를 사용하는 게 권장되고, 그렇지 않으면 tabIndex를 추가해야 해요. confirm 함수를 사용할 때 트리거가 파라미터로 전달되면, ConfirmDialog는 트리거와 팝업 사이의 관계가 정의되도록 트리거에 aria-expanded 상태 속성과 aria-controls를 추가해요. 다이얼로그를 visible 프로퍼티로 제어한다면 aria-expanded와 aria-controls를 명시적으로 처리해야 해요. 오버레이 키보드 지원 키 기능 tab 팝업 내에서 포커스 가능한 다음 요소로 포커스를 이동해요. shift + tab 팝업 내에서 포커스 가능한 이전 요소로 포커스를 이동해요. escape 팝업을 닫고 트리거로 포커스를 이동해요. 버튼 키보드 지원 키 기능 enter 액션을 트리거하고, 팝업을 닫고, 트리거로 포커스를 이동해요. space 액션을 트리거하고, 팝업을 닫고, 트리거로 포커스를 이동해요.
기본
ConfirmDialog는 p-confirmdialog 태그로 정의하고, confirm 메서드를 호출해서 표시하려면 ConfirmationService의 인스턴스가 필요해요.
import { Component, inject } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { ToastModule } from 'primeng/toast';
import { MessageService, ConfirmationService } from 'primeng/api';
@Component({
template: `
<div class="card flex justify-center gap-2">
<p-toast />
<p-confirmdialog />
<p-button (click)="confirm1($event)" label="Save" [outlined]="true" />
<p-button (click)="confirm2($event)" label="Delete" severity="danger" [outlined]="true" />
</div>
`,
standalone: true,
imports: [ButtonModule, ConfirmDialogModule, ToastModule],
providers: [ConfirmationService, MessageService]
})
export class ConfirmdialogBasicDemo {
private confirmationService = inject(ConfirmationService);
private messageService = inject(MessageService);
confirm2(event: Event) {
this.confirmationService.confirm({
target: event.target as EventTarget,
message: 'Do you want to delete this record?',
header: 'Danger Zone',
icon: 'pi pi-info-circle',
rejectLabel: 'Cancel',
rejectButtonProps: {
label: 'Cancel',
severity: 'secondary',
outlined: true
},
acceptButtonProps: {
label: 'Delete',
severity: 'danger'
},
accept: () => {
this.messageService.add({ severity: 'info', summary: 'Confirmed', detail: 'Record deleted' });
},
reject: () => {
this.messageService.add({ severity: 'error', summary: 'Rejected', detail: 'You have rejected' });
}
});
}
}
헤드리스
헤드리스 모드를 사용하면 기본 엘리먼트 대신 전체 사용자 인터페이스를 커스터마이즈할 수 있어요.
import { Component, inject } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { ToastModule } from 'primeng/toast';
import { MessageService, ConfirmationService } from 'primeng/api';
@Component({
template: `
<div class="card flex justify-center">
<p-toast />
<p-confirmdialog #cd>
<ng-template #headless let-message let-onAccept="onAccept" let-onReject="onReject">
@if (message) {
<div class="flex flex-col items-center p-8 bg-surface-0 dark:bg-surface-900 rounded">
<div class="rounded-full bg-primary text-primary-contrast inline-flex justify-center items-center h-24 w-24 -mt-20">
<i class="pi pi-question !text-5xl"></i>
</div>
<span class="font-bold text-2xl block mb-2 mt-6">{{ message.header }}</span>
<p class="mb-0">{{ message.message }}</p>
<div class="flex items-center gap-2 mt-6">
<p-button label="Save" (onClick)="onAccept()" styleClass="w-32"></p-button>
<p-button label="Cancel" [outlined]="true" (onClick)="onReject()" styleClass="w-32"></p-button>
</div>
</div>
}
</ng-template>
</p-confirmdialog>
<p-button (click)="confirm()" label="Save" />
</div>
`,
standalone: true,
imports: [ButtonModule, ConfirmDialogModule, ToastModule],
providers: [ConfirmationService, MessageService]
})
export class ConfirmdialogHeadlessDemo {
private confirmationService = inject(ConfirmationService);
private messageService = inject(MessageService);
}
위치
confirm 옵션의 position 프로퍼티는 화면의 모든 가장자리와 모서리에 Dialog를 표시하는 데 사용돼요.
import { Component, inject } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { ToastModule } from 'primeng/toast';
import { MessageService, ConfirmationService } from 'primeng/api';
import { Dialog } from 'primeng/dialog';
@Component({
template: `
<div class="card">
<p-toast />
<p-confirmdialog key="positionDialog" [position]="position" />
<div class="flex flex-wrap justify-center gap-2 mb-4">
<p-button (click)="confirmPosition('left')" icon="pi pi-arrow-right" label="Left" severity="secondary" styleClass="min-w-40" />
<p-button (click)="confirmPosition('right')" icon="pi pi-arrow-left" label="Right" severity="secondary" styleClass="min-w-40" />
</div>
<div class="flex flex-wrap justify-center gap-2 mb-4">
<p-button (click)="confirmPosition('topleft')" icon="pi pi-arrow-down" label="TopLeft" severity="secondary" styleClass="min-w-40" />
<p-button (click)="confirmPosition('top')" icon="pi pi-arrow-down" label="Top" severity="secondary" styleClass="min-w-40" />
<p-button (click)="confirmPosition('topright')" icon="pi pi-arrow-down" label="TopRight" severity="secondary" styleClass="min-w-40" />
</div>
<div class="flex flex-wrap justify-center gap-2">
<p-button (click)="confirmPosition('bottomleft')" icon="pi pi-arrow-up" label="BottomLeft" severity="secondary" styleClass="min-w-40" />
<p-button (click)="confirmPosition('bottom')" icon="pi pi-arrow-up" label="Bottom" severity="secondary" styleClass="min-w-40" />
<p-button (click)="confirmPosition('bottomright')" icon="pi pi-arrow-up" label="BottomRight" severity="secondary" styleClass="min-w-40" />
</div>
</div>
`,
standalone: true,
imports: [ButtonModule, ConfirmDialogModule, ToastModule],
providers: [ConfirmationService, MessageService]
})
export class ConfirmdialogPositionDemo {
private confirmationService = inject(ConfirmationService);
private messageService = inject(MessageService);
}
템플릿
다이얼로그의 프로퍼티는 두 가지 방식으로 정의할 수 있어요. message, icon, header 프로퍼티는 confirm 메서드로 정의하거나 p-confirmDialog의 ng-template에서 header, message, icon, footer 템플릿으로 선언적으로 정의할 수 있어요. 이 값들이 바뀔 일이 거의 없다면 선언적인 방식이 유용한데, 그래도 ng-template에 정의된 프로퍼티는 confirm 메서드 호출로 재정의할 수 있어요. 게다가 footer 섹션의 버튼은 직접 만든 UI를 전달해서 커스터마이즈할 수 있어요. 커스텀 UI에서 confirmation이 동작하게 만드는 중요한 포인트는 다이얼로그를 위한 지역 ng-template 변수를 정의하고 자체 버튼에 accept()-reject() 메서드를 할당하는 거예요.
import { Component, inject } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { ConfirmDialogModule } from 'primeng/confirmdialog';
import { ToastModule } from 'primeng/toast';
import { MessageService, ConfirmationService } from 'primeng/api';
@Component({
template: `
<div class="card flex justify-center">
<p-toast />
<p-confirmdialog>
<ng-template #message let-message>
@if (message) {
<div class="flex flex-col items-center w-full gap-4 border-b border-surface-200 dark:border-surface-700">
<i [ngClass]="message.icon" class="!text-6xl text-primary-500"></i>
<p>{{ message.message }}</p>
</div>
}
</ng-template>
</p-confirmdialog>
<p-button (click)="confirm()" label="Save" />
</div>
`,
standalone: true,
imports: [ButtonModule, ConfirmDialogModule, ToastModule],
providers: [ConfirmationService, MessageService]
})
export class ConfirmdialogTemplateDemo {
private confirmationService = inject(ConfirmationService);
private messageService = inject(MessageService);
}
Confirm Dialog
ConfirmDialog는 Confirmation API와 통합된 Dialog UI를 사용해요.
Props
| Name | Type | Default | Description |
|---|---|---|---|
| dt | InputSignal | undefined | 컴포넌트의 스코프 디자인 토큰을 정의해요. |
| unstyled | InputSignal | undefined | 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요. |
| pt | InputSignal | undefined | 컴포넌트 내부의 DOM 요소에 속성을 전달할 때 사용해요. |
| ptOptions | InputSignal | undefined | 컴포넌트의 passthrough(pt) 옵션을 구성할 때 사용해요. |
| header | string | - | 다이얼로그의 제목 텍스트예요. |
| icon | string | - | 메시지 옆에 표시할 아이콘이에요. |
| message | string | - | 확인(confirm)의 메시지예요. |
| style | { [klass: string]: any } | - | 요소의 인라인 스타일이에요. |
| styleClass | string | - | 요소의 클래스예요. |
| maskStyleClass | string | - | 마스크 요소를 스타일링할 CSS 클래스를 지정해요. |
| acceptIcon | string | - | 수락 버튼의 아이콘이에요. |
| acceptLabel | string | - | 수락 버튼의 라벨이에요. |
| closeAriaLabel | string | - | 접근성을 위해 닫기 버튼의 라벨이 되는 문자열을 정의해요. |
| acceptAriaLabel | string | - | 접근성을 위해 수락 버튼의 라벨이 되는 문자열을 정의해요. |
| acceptVisible | boolean | true | 수락 버튼의 표시 여부예요. |
| rejectIcon | string | - | 거절 버튼의 아이콘이에요. |
| rejectLabel | string | - | 거절 버튼의 라벨이에요. |
| rejectAriaLabel | string | - | 접근성을 위해 거절 버튼의 라벨이 되는 문자열을 정의해요. |
| rejectVisible | boolean | true | 거절 버튼의 표시 여부예요. |
| acceptButtonStyleClass | string | - | 수락 버튼의 스타일 클래스예요. |
| rejectButtonStyleClass | string | - | 거절 버튼의 스타일 클래스예요. |
| closeOnEscape | boolean | true | Escape 키를 눌렀을 때 다이얼로그를 숨길지 여부를 지정해요. |
| dismissableMask | boolean | false | 모달 배경을 클릭했을 때 다이얼로그를 숨길지 여부를 지정해요. |
| blockScroll | boolean | true | 컴포넌트 내에서 스크롤 동작을 차단할지 여부를 결정해요. |
| rtl | boolean | false | 활성화하면 다이얼로그가 RTL 방향으로 표시돼요. |
| closable | boolean | true | 다이얼로그를 숨길 수 있도록 헤더에 닫기 아이콘을 추가해요. |
| appendTo | InputSignal | 'body' | 오버레이를 붙일 대상 요소예요. 유효한 값은 "body" 또는 다른 요소의 로컬 ng-template 변수예요(참고: 템플릿 변수에는 대괄호를 사용한 바인딩을 사용하세요. 예를 들어 #mydiv를 변수 이름으로 가진 div 요소라면 [appendTo]="mydiv"로 사용해요). |
| key | string | - | confirm 객체의 키와 일치시킬 선택적 키예요. 컴포넌트 트리에 여러 확인 다이얼로그가 있을 때 필요해요. |
| autoZIndex | boolean | true | 레이어링을 자동으로 관리할지 여부예요. |
| baseZIndex | number | 0 | 레이어링에 사용할 기본 zIndex 값이에요. |
| transitionOptions | string | 150ms cubic-bezier(0, 0, 0.2, 1) | 애니메이션의 트랜지션 옵션이에요. |
| focusTrap | boolean | true | 활성화하면 확인 다이얼로그 내부의 요소에만 포커스를 둘 수 있어요. |
| defaultFocus | "accept" | "reject" | "none" | "close" | accept | 다이얼로그가 표시될 때 포커스를 받을 요소예요. |
| breakpoints | any | - | 화면 크기별 너비를 정의하는 객체 리터럴이에요. |
| modal | boolean | true | 다이얼로그가 표시될 때 배경을 차단할지 여부를 정의해요. |
| visible | any | - | 불리언으로 나타낸 현재 표시 상태예요. |
| position | "right" | "left" | "top" | "bottom" | "center" | "topleft" | "bottomleft" | "topright" | "bottomright" | center | 컴포넌트의 위치를 가져올 수 있게 해줘요. |
| draggable | boolean | true | 헤더를 이용해 위치를 변경하는 드래그를 활성화해요. |
Emits
| Name | Parameters | Description |
|---|---|---|
| onHide | event: ConfirmEventType | 다이얼로그가 숨겨질 때 호출되는 콜백이에요. |
Templates
| Name | Type | Description |
|---|---|---|
| header | TemplateRef | 커스텀 헤더 템플릿이에요. |
| footer | TemplateRef | 커스텀 푸터 템플릿이에요. |
| rejecticon | TemplateRef | 커스텀 거절 아이콘 템플릿이에요. |
| accepticon | TemplateRef | 커스텀 수락 아이콘 템플릿이에요. |
| message | TemplateRef | 커스텀 메시지 템플릿이에요. |
| icon | TemplateRef | 커스텀 아이콘 템플릿이에요. |
| headless | TemplateRef | 커스텀 헤드리스 템플릿이에요. |
Pass Through Options
| Name | Type | Description |
|---|---|---|
| host | PassThroughOption<HTMLElement, I> | 호스트의 DOM 요소에 속성을 전달할 때 사용해요. |
| root | DialogPassThrough | 루트의 DOM 요소에 속성을 전달할 때 사용해요. |
| mask | PassThroughOption<HTMLDivElement, I> | 마스크의 DOM 요소에 속성을 전달할 때 사용해요. |
| icon | PassThroughOption<HTMLElement, I> | 아이콘의 DOM 요소에 속성을 전달할 때 사용해요. |
| message | PassThroughOption<HTMLSpanElement, I> | 메시지의 DOM 요소에 속성을 전달할 때 사용해요. |
| resizeHandle | PassThroughOption<HTMLDivElement, I> | 리사이즈 핸들의 DOM 요소에 속성을 전달할 때 사용해요. |
| header | PassThroughOption<HTMLDivElement, I> | 헤더의 DOM 요소에 속성을 전달할 때 사용해요. |
| title | PassThroughOption<HTMLSpanElement, I> | 제목의 DOM 요소에 속성을 전달할 때 사용해요. |
| headerActions | PassThroughOption<HTMLDivElement, I> | 헤더 액션의 DOM 요소에 속성을 전달할 때 사용해요. |
| pcCloseButton | ButtonPassThrough | 닫기 Button 컴포넌트에 속성을 전달할 때 사용해요. |
| content | PassThroughOption<HTMLDivElement, I> | 콘텐츠의 DOM 요소에 속성을 전달할 때 사용해요. |
| footer | PassThroughOption<HTMLDivElement, I> | 푸터의 DOM 요소에 속성을 전달할 때 사용해요. |
| pcAcceptButton | ButtonPassThrough | 수락 Button 컴포넌트에 속성을 전달할 때 사용해요. |
| pcRejectButton | ButtonPassThrough | 거절 Button 컴포넌트에 속성을 전달할 때 사용해요. |
Theming
CSS Classes
| Class | Description |
|---|---|
| p-confirmdialog | 루트 요소의 클래스 이름이에요. |
| p-confirmdialog-icon | 아이콘 요소의 클래스 이름이에요. |
| p-confirmdialog-message | 메시지 요소의 클래스 이름이에요. |
| p-confirmdialog-reject-button | 거절 버튼 요소의 클래스 이름이에요. |
| p-confirmdialog-accept-button | 수락 버튼 요소의 클래스 이름이에요. |
Design Tokens
| Token | CSS Variable | Description |
|---|---|---|
| confirmdialog.icon.size | --p-confirmdialog-icon-size | 아이콘의 크기예요. |
| confirmdialog.icon.color | --p-confirmdialog-icon-color | 아이콘의 색상이에요. |
| confirmdialog.content.gap | --p-confirmdialog-content-gap | 콘텐츠의 간격이에요. |
더 알아보기 (Learn more)
이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.