앵귤러 팝오버 컴포넌트
앵귤러 팝오버 컴포넌트 (Angular Popover Component)
Popover는 페이지 위에 다른 컴포넌트를 겹쳐 띄워 보여주는 컨테이너 컴포넌트예요. 트리거 요소의 이벤트와 함께 toggle, show, hide 메서드로 표시 여부를 제어하고, 버튼이나 DataTable 같은 다양한 대상에 붙여서 쓸 수 있어요. 템플릿으로 내용을 자유롭게 구성할 수 있고, dialog 역할과 키보드 내비게이션 같은 접근성 기능도 갖추고 있어요. Props, Emits, Methods 같은 API와 디자인 토큰을 활용한 테마 설정 방법까지 이 문서에서 확인할 수 있어요
출처: 문서
본문
Accessibility
스크린 리더 - Popover 컴포넌트는 dialog role을 사용해요. 그리고 모든 속성이 루트 엘리먼트로 전달되기 때문에, 팝업 내용을 설명하려면 aria-label이나 aria-labelledby 같은 속성을 정의할 수 있어요. 또한 포커스가 팝업 안에 유지되기 때문에 aria-modal이 추가돼요. 버튼처럼 키보드로 접근할 수 있는 트리거 컴포넌트를 사용하는 게 권장되고, 그렇지 않으면 tabIndex를 추가해야 해요. Popover는 트리거와 팝업 사이의 관계가 정의되도록 트리거에 aria-expanded 상태 속성과 aria-controls를 추가해요. Popover 키보드 지원 - 팝업이 열리면 첫 번째 포커스 가능한 엘리먼트가 포커스를 받는데, 이건 팝업 안의 엘리먼트에 autofocus를 추가해서 커스터마이즈할 수 있어요. 키 - 기능: tab - 팝업 안에서 다음 포커스 가능한 엘리먼트로 포커스를 이동해요. shift + tab - 팝업 안에서 이전 포커스 가능한 엘리먼트로 포커스를 이동해요. escape - 팝업을 닫고 트리거로 포커스를 이동해요. 닫기 버튼 키보드 지원 - 키 - 기능: enter - 팝업을 닫고 트리거로 포커스를 이동해요. space - 팝업을 닫고 트리거로 포커스를 이동해요.
Basic
Popover는 참조를 통해 접근하고, 타겟의 이벤트와 함께 toggle, show, hide 메서드를 사용해서 가시성을 제어해요.
import { Component } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { InputGroupModule } from 'primeng/inputgroup';
import { PopoverModule } from 'primeng/popover';
import { InputTextModule } from 'primeng/inputtext';
@Component({
template: `
<div class="card flex justify-center">
<p-button (click)="op.toggle($event)" icon="pi pi-share-alt" label="Share" />
<p-popover #op>
<div class="flex flex-col gap-4 w-[25rem]">
<div>
<span class="font-medium text-surface-900 dark:text-surface-0 block mb-2">Share this document</span>
<p-inputgroup>
<input pInputText value="https://primeng.org/12323ff26t2g243g423g234gg52hy25XADXAG3" readonly class="w-[25rem]" />
<p-inputgroup-addon>
<i class="pi pi-copy"></i>
</p-inputgroup-addon>
</p-inputgroup>
</div>
<div>
<span class="font-medium text-surface-900 dark:text-surface-0 block mb-2">Invite Member</span>
<div class="flex">
<p-inputgroup>
<input pInputText disabled />
<button pButton label="Invite" icon="pi pi-users"></button>
</p-inputgroup>
</div>
</div>
<div>
<span class="font-medium text-surface-900 dark:text-surface-0 block mb-2">Team Members</span>
<ul class="list-none p-0 m-0 flex flex-col gap-4">
@for (member of members; track member) {
<li class="flex items-center gap-2">
<img [src]="'https://primefaces.org/cdn/primeng/images/demo/avatar/' + member.image" style="width: 32px" />
<div>
<span class="font-medium">{{ member.name }}</span>
<div class="text-sm text-muted-color">{{ member.email }}</div>
</div>
<div class="flex items-center gap-2 text-muted-color ml-auto text-sm">
<span>{{ member.role }}</span>
<i class="pi pi-angle-down"></i>
</div>
</li>
}
</ul>
</div>
</div>
</p-popover>
</div>
`,
standalone: true,
imports: [ButtonModule, InputGroupModule, PopoverModule, InputTextModule]
})
export class PopoverBasicDemo {
members: any[];
}
DataTable
여러 번 렌더링되는 걸 피하려면 Popover를 데이터 반복 컴포넌트 바깥에 배치하세요.
import { Component, OnInit, inject, signal } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { Popover, PopoverModule } from 'primeng/popover';
import { TableModule } from 'primeng/table';
import { TagModule } from 'primeng/tag';
import { ProductService } from '@/service/productservice';
import { MessageService } from 'primeng/api';
import { Product } from '@/domain/product';
@Component({
template: `
<div class="card">
<p-table [value]="products()" [tableStyle]="{ 'min-width': '50rem' }" [paginator]="true" [rows]="5">
<ng-template #header>
<tr>
<th class="w-1/6">Id</th>
<th class="w-1/6">Code</th>
<th class="w-1/6">Name</th>
<th class="w-1/6">Price</th>
<th class="w-1/6">Image</th>
<th class="w-1/6">Details</th>
</tr>
</ng-template>
<ng-template #body let-product>
<tr>
<td>{{ product.id }}</td>
<td>{{ product.code }}</td>
<td>{{ product.name }}</td>
<td>$ {{ product.price }}</td>
<td>
<img [src]="'https://primefaces.org/cdn/primeng/images/demo/product/' + product.image" [alt]="product.image" class="w-16 shadow-sm" />
</td>
<td>
<p-button (onClick)="displayProduct($event, product)" icon="pi pi-search" severity="secondary" rounded />
</td>
</tr>
</ng-template>
</p-table>
<p-popover #op (onHide)="selectedProduct.set(null)">
<ng-template #content>
<div *ngIf="selectedProduct()" class="rounded flex flex-col">
<div class="flex justify-center rounded">
<div class="relative mx-auto">
<img class="rounded w-44 sm:w-64" [src]="'https://primefaces.org/cdn/primeng/images/demo/product/' + selectedProduct().image" [alt]="selectedProduct().name" />
<p-tag [value]="selectedProduct().inventoryStatus" [severity]="getSeverity(selectedProduct())" class="absolute dark:!bg-surface-900" [style.left.px]="4" [style.top.px]="4" />
</div>
</div>
<div class="pt-4">
<div class="flex flex-row justify-between items-start gap-2 mb-4">
<div>
<span class="font-medium text-surface-500 dark:text-surface-400 text-sm">{{ selectedProduct().category }}</span>
<div class="text-lg font-medium mt-1">{{ selectedProduct().name }}</div>
</div>
<div class="bg-surface-100 p-1" style="border-radius: 30px">
<div class="bg-surface-0 flex items-center gap-2 justify-center py-1 px-2" style="border-radius: 30px; box-shadow: 0px 1px 2px 0px rgba(0, 0, 0, 0.04), 0px 1px 2px 0px rgba(0, 0, 0, 0.06)">
<span class="text-surface-900 font-medium text-sm">{{ selectedProduct().rating }}</span>
<i class="pi pi-star-fill text-yellow-500"></i>
</div>
</div>
</div>
<div class="flex gap-2">
<p-button
icon="pi pi-shopping-cart"
[label]="'Buy Now | $' + selectedProduct().price"
[disabled]="selectedProduct().inventoryStatus === 'OUTOFSTOCK'"
class="flex-auto"
styleClass="w-full whitespace-nowrap"
(onClick)="hidePopover()"
/>
<p-button icon="pi pi-heart" outlined (onClick)="hidePopover()" />
</div>
</div>
</div>
</ng-template>
</p-popover>
</div>
`,
standalone: true,
imports: [ButtonModule, PopoverModule, TableModule, TagModule],
providers: [ProductService, MessageService]
})
export class PopoverDatatableDemo implements OnInit {
private productService = inject(ProductService);
private messageService = inject(MessageService);
products = signal<Product[]>([]);
selectedProduct = signal<Product | null>(null);
ngOnInit() {
this.productService.getProductsSmall().then((products) => {
this.products.set(products);
});
}
displayProduct(event, product) {
if (this.selectedProduct()?.id === product.id) {
this.op.hide();
this.selectedProduct.set(null);
} else {
this.selectedProduct.set(product);
this.op.show(event);
if (this.op.container) {
this.op.align();
}
}
}
hidePopover() {
this.op.hide();
}
getSeverity(product) {
switch (product.inventoryStatus) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
return 'warn';
case 'OUTOFSTOCK':
return 'danger';
default:
return null;
}
}
}
Select Data
이 샘플에서는 데이터를 popover 안쪽 콘텐츠에서 가져와요.
import { Component } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { Popover, PopoverModule } from 'primeng/popover';
@Component({
template: `
<div class="card flex justify-center">
<p-button type="button" [label]="selectedMember ? selectedMember.name : 'Select Member'" (onClick)="toggle($event)" styleClass="min-w-48" />
<p-popover #op>
<div class="flex flex-col gap-4">
<div>
<span class="font-medium block mb-2">Team Members</span>
<ul class="list-none p-0 m-0 flex flex-col">
<li *ngFor="let member of members" class="flex items-center gap-2 px-2 py-3 hover:bg-emphasis cursor-pointer rounded-border" (click)="selectMember(member)">
<img [src]="'https://primefaces.org/cdn/primeng/images/demo/avatar/' + member.image" style="width: 32px" />
<div>
<span class="font-medium">{{ member.name }}</span>
<div class="text-sm text-surface-500 dark:text-surface-400">{{ member.email }}</div>
</div>
</li>
</ul>
</div>
</div>
</p-popover>
</div>
`,
standalone: true,
imports: [ButtonModule, PopoverModule]
})
export class PopoverSelectdataDemo {
selectedMember: any = null;
members: any[];
toggle(event) {
this.op.toggle(event);
}
selectMember(member) {
this.selectedMember = member;
this.op.hide();
}
}
Target
show 메서드는 두 개의 파라미터를 받아요. 첫 번째는 이벤트이고 필수예요. 기본적으로 오버레이를 정렬할 대상 컴포넌트는 이벤트 target이에요. 다른 요소에 정렬하고 싶다면 두 번째 파라미터인 target으로 전달하세요.
import { Component } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { PopoverModule } from 'primeng/popover';
@Component({
template: `
<div class="card flex flex-col items-center gap-4">
<p-button (click)="op.show($event, targetEl)" icon="pi pi-image" label="Show"></p-button>
<div #targetEl class="mt-8 w-40 h-20 border border-surface rounded-border flex items-center justify-center">
<span>Target Element</span>
</div>
<p-popover #op>
<img src="https://primefaces.org/cdn/primeng/images/demo/product/bamboo-watch.jpg" alt="product" />
</p-popover>
</div>
`,
standalone: true,
imports: [ButtonModule, PopoverModule]
})
export class PopoverTargetDemo {}
Template
OverlayPanel의 콘텐츠는 content 템플릿으로 정의돼요.
import { Component } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { PopoverModule } from 'primeng/popover';
import { OverlayPanel } from 'primeng/overlaypanel';
@Component({
template: `
<div class="card flex justify-center">
<p-popover #op>
<ng-template #content>
<h4>Custom Content</h4>
</ng-template>
</p-popover>
<p-button (click)="op.toggle($event)" icon="pi pi-image" label="Show"></p-button>
</div>
`,
standalone: true,
imports: [ButtonModule, PopoverModule]
})
export class PopoverTemplateDemo {}
Popover
Popover는 페이지의 다른 컴포넌트 위에 오버레이할 수 있는 컨테이너 컴포넌트예요.
Props
| Name | Type | Default | Description |
|---|---|---|---|
| dt | InputSignal | undefined | 컴포넌트의 스코프 디자인 토큰을 정의해요. |
| unstyled | InputSignal | undefined | 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요. |
| pt | InputSignal | undefined | 컴포넌트 내부의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| ptOptions | InputSignal | undefined | 컴포넌트의 passthrough(pt) 옵션을 구성하는 데 사용돼요. |
| ariaLabel | string | - | 접근성을 위해 입력 요소를 라벨링하는 문자열을 정의해요. |
| ariaLabelledBy | string | - | 컴포넌트와 라벨 간의 관계를 설정해요. 값은 하나 이상의 요소 ID여야 해요. |
| dismissable | boolean | true | 바깥을 클릭하면 오버레이를 숨길 수 있게 해요. |
| style | { [klass: string]: any } | - | 컴포넌트의 인라인 스타일이에요. |
| styleClass | string | - | 컴포넌트의 스타일 클래스예요. |
| appendTo | InputSignal | 'self' | 오버레이를 붙일 대상 요소예요. 유효한 값은 "body" 또는 다른 요소의 로컬 ng-template 변수예요 (참고: 템플릿 변수는 대괄호로 바인딩하세요. 예를 들어 변수 이름이 #mydiv인 div 요소는 [appendTo]="mydiv"로 지정해요). |
| autoZIndex | boolean | true | 레이어를 자동으로 관리할지 여부예요. |
| ariaCloseLabel | string | - | 닫기 아이콘의 Aria 라벨이에요. |
| baseZIndex | number | 0 | 레이어링에 사용할 기본 zIndex 값이에요. |
| focusOnShow | boolean | true | 활성화하면 표시될 때 첫 번째 버튼이 포커스를 받아요. |
| showTransitionOptions | string | .12s cubic-bezier(0, 0, 0.2, 1) | show 애니메이션의 전환 옵션이에요. (지원 중단됨) |
| hideTransitionOptions | string | .1s linear | hide 애니메이션의 전환 옵션이에요. (지원 중단됨) |
| motionOptions | InputSignal | ... | 모션 옵션이에요. |
Emits
| Name | Parameters | Description |
|---|---|---|
| onShow | value: any | 오버레이가 표시될 때 호출되는 콜백이에요. |
| onHide | value: any | 오버레이가 숨겨질 때 호출되는 콜백이에요. |
Templates
| Name | Type | Description |
|---|---|---|
| content | TemplateRef | 커스텀 콘텐츠 템플릿이에요. |
Methods
| Name | Parameters | Return Type | Description |
|---|---|---|---|
| toggle | event: any, target: any | void | 패널의 표시 여부를 토글해요. |
| show | event: any, target: any | void | 패널을 표시해요. |
| hide | void | 패널을 숨겨요. |
Pass Through Options
| Name | Type | Description |
|---|---|---|
| host | PassThroughOption<HTMLElement, I> | host의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| root | PassThroughOption<HTMLDivElement, I> | root의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| content | PassThroughOption<HTMLDivElement, I> | content의 DOM 요소에 속성을 전달하는 데 사용돼요. |
| motion | MotionOptions | motion 컴포넌트/디렉티브에 옵션을 전달하는 데 사용돼요. |
Theming
Design Tokens
| Token | CSS Variable | Description |
|---|---|---|
| popover.background | --p-popover-background | root의 배경 |
| popover.border.color | --p-popover-border-color | root의 테두리 색상 |
| popover.color | --p-popover-color | root의 색상 |
| popover.border.radius | --p-popover-border-radius | root의 테두리 반경 |
| popover.shadow | --p-popover-shadow | root의 그림자 |
| popover.gutter | --p-popover-gutter | root의 거터 |
| popover.arrow.offset | --p-popover-arrow-offset | root의 화살표 오프셋 |
| popover.content.padding | --p-popover-content-padding | content의 패딩 |
더 알아보기 (Learn more)
이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.