캐러셀
캐러셀 (Carousel)
캐러셀은 다양한 커스터마이징 옵션을 갖춘 콘텐츠 슬라이더 컴포넌트예요. 항목 컬렉션을 값으로 전달하고 템플릿으로 각 항목을 렌더링하는 기본 사용법부터 순환 스크롤, 반응형, 세로 방향 구성까지 차근차근 살펴봐요. 스크린 리더와 키보드 접근성 지원 방법도 함께 안내하고, props·emits·템플릿·패스 스루 옵션·테마(디자인 토큰, CSS 클래스) 설정까지 이 문서에서 모두 확인할 수 있어요
출처: 문서
본문
접근성
스크린 리더 Carousel은 region role을 사용해요. 모든 속성이 메인 컨테이너 요소에 전달되기 때문에 aria-label과 aria-roledescription 같은 속성도 함께 사용할 수 있어요. 슬라이드 컨테이너에는 aria-live 속성이 있는데, 캐러셀이 자동 재생 모드가 아니면 "polite"로 설정되고, 자동 재생 중일 때는 "off"가 값으로 돼요. 각 슬라이드는 group role을 가지며, aria-label은 locale API의 aria.slideNumber 속성을 참조해요. 마찬가지로 aria.slide가 항목의 aria-roledescription으로 사용돼요. 활성 상태가 아닌 슬라이드는 aria-hidden으로 스크린 리더에게 숨겨져요. 다음/이전 내비게이터는 button 요소로서, 기본적으로 각각 locale API의 aria.nextPageLabel과 aria.firstPageLabel 속성을 참조하는 aria-label 속성을 가져요. nextButtonProps와 prevButtonProps를 사용하면 유효한 속성이 button 요소에 암묵적으로 전달되기 때문에, 여전히 자신만의 aria role과 속성을 사용할 수도 있어요. 빠른 내비게이션 요소는 locale API의 aria.pageLabel을 참조하는 aria-label 속성을 가진 button 요소예요. 현재 페이지는 aria-current로 표시돼요. Next/Prev 키보드 지원 키 기능 tab 캐러셀 내의 대화형 요소들 사이에서 포커스를 이동해요. enter 내비게이션을 활성화해요. space 내비게이션을 활성화해요. 빠른 내비게이션 키보드 지원 키 기능 tab 활성 슬라이드 링크 사이에서 포커스를 이동해요. enter 포커스가 있는 슬라이드 링크를 활성화해요. space 포커스가 있는 슬라이드 링크를 활성화해요. right arrow 다음 슬라이드 링크로 포커스를 이동해요. left arrow 이전 슬라이드 링크로 포커스를 이동해요. home 첫 번째 슬라이드 링크로 포커스를 이동해요. end 마지막 슬라이드 링크로 포커스를 이동해요.
기본
Carousel은 값으로 항목 컬렉션이 필요하고, 각 항목을 렌더링할 템플릿도 함께 필요해요.
import { Component, OnInit, inject, signal } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { CarouselModule } from 'primeng/carousel';
import { TagModule } from 'primeng/tag';
import { ProductService } from '@/service/productservice';
import { Product } from '@/domain/product';
@Component({
template: `
<div class="card">
<p-carousel [value]="products()" [numVisible]="3" [numScroll]="3" [circular]="false" [responsiveOptions]="responsiveOptions">
<ng-template let-product #item>
<div class="border border-surface rounded-border m-2 p-4">
<div class="mb-4">
<div class="relative mx-auto">
<img src="https://primefaces.org/cdn/primeng/images/demo/product/{{ product.image }}" [alt]="product.name" class="w-full rounded-border" />
<p-tag [value]="product.inventoryStatus" [severity]="getSeverity(product.inventoryStatus)" class="absolute dark:!bg-surface-900" [ngStyle]="{ 'left.px': 5, 'top.px': 5 }" />
</div>
</div>
<div class="mb-4 font-medium">{{ product.name }}</div>
<div class="flex justify-between items-center">
<div class="mt-0 font-semibold text-xl">{{ '$' + product.price }}</div>
<span>
<p-button icon="pi pi-heart" severity="secondary" [outlined]="true" />
<p-button icon="pi pi-shopping-cart" styleClass="ml-2" />
</span>
</div>
</div>
</ng-template>
</p-carousel>
</div>
`,
standalone: true,
imports: [ButtonModule, CarouselModule, TagModule],
providers: [ProductService]
})
export class CarouselBasicDemo implements OnInit {
private productService = inject(ProductService);
products = signal<Product[]>([]);
responsiveOptions: any[] | undefined;
ngOnInit() {
this.productService.getProductsSmall().then((data) => {
this.products.set(data.slice(0, 9));
});
this.responsiveOptions = [
{
breakpoint: '1400px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '1199px',
numVisible: 3,
numScroll: 1
},
{
breakpoint: '767px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '575px',
numVisible: 1,
numScroll: 1
}
];
}
getSeverity(status: string) {
switch (status) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
return 'warn';
case 'OUTOFSTOCK':
return 'danger';
}
}
}
순환
autoplayInterval이 밀리초 단위로 정의되면 항목들이 자동으로 스크롤돼요. 게다가 무한 스크롤을 위해서는 circular 속성을 추가해야 하는데, 이 속성은 자동 재생 모드에서 자동으로 활성화돼요.
import { Component, OnInit, inject, signal } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { CarouselModule } from 'primeng/carousel';
import { TagModule } from 'primeng/tag';
import { ProductService } from '@/service/productservice';
import { Product } from '@/domain/product';
@Component({
template: `
<div class="card">
<p-carousel [value]="products()" [numVisible]="3" [numScroll]="1" [circular]="true" [responsiveOptions]="responsiveOptions" autoplayInterval="3000">
<ng-template let-product #item>
<div class="border border-surface-200 dark:border-surface-700 rounded m-2 p-4">
<div class="mb-4">
<div class="relative mx-auto">
<img src="https://primefaces.org/cdn/primeng/images/demo/product/{{ product.image }}" [alt]="product.name" class="w-full rounded-border" />
<p-tag [value]="product.inventoryStatus" [severity]="getSeverity(product.inventoryStatus)" class="absolute dark:!bg-surface-900" [ngStyle]="{ 'left.px': 5, 'top.px': 5 }" />
</div>
</div>
<div class="mb-4 font-medium">{{ product.name }}</div>
<div class="flex justify-between items-center">
<div class="mt-0 font-semibold text-xl">{{ '$' + product.price }}</div>
<span>
<p-button icon="pi pi-heart" severity="secondary" [outlined]="true" />
<p-button icon="pi pi-shopping-cart" styleClass="ml-2" />
</span>
</div>
</div>
</ng-template>
</p-carousel>
</div>
`,
standalone: true,
imports: [ButtonModule, CarouselModule, TagModule],
providers: [ProductService]
})
export class CarouselCircularDemo implements OnInit {
private productService = inject(ProductService);
products = signal<Product[]>([]);
responsiveOptions: any[] | undefined;
ngOnInit() {
this.productService.getProductsSmall().then((data) => {
this.products.set(data.slice(0, 9));
});
this.responsiveOptions = [
{
breakpoint: '1400px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '1199px',
numVisible: 3,
numScroll: 1
},
{
breakpoint: '767px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '575px',
numVisible: 1,
numScroll: 1
}
];
}
getSeverity(status: string) {
switch (status) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
return 'warn';
case 'OUTOFSTOCK':
return 'danger';
}
}
}
반응형
Carousel은 responsiveOptions 속성을 통해 화면 크기별 개별 설정을 지원해요. 이 속성은 객체 배열을 받으며, 각 객체는 max-width 브레이크포인트와 페이지당 항목 수를 나타내는 numVisible, 스크롤할 항목 수를 나타내는 numScroll을 정의해요. responsiveOptions가 정의되어 있으면 적용되는 브레이크포인트가 있을 때 Carousel의 numScroll과 numVisible 속성이 기본값으로 사용돼요.
import { Component, OnInit, inject, signal } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { CarouselModule } from 'primeng/carousel';
import { TagModule } from 'primeng/tag';
import { ProductService } from '@/service/productservice';
import { Product } from '@/domain/product';
@Component({
template: `
<div class="card">
<p-carousel [value]="products()" [numVisible]="3" [numScroll]="1" [responsiveOptions]="responsiveOptions">
<ng-template let-product #item>
<div class="border border-surface-200 dark:border-surface-700 rounded m-2 p-4">
<div class="mb-4">
<div class="relative mx-auto">
<img src="https://primefaces.org/cdn/primeng/images/demo/product/{{ product.image }}" [alt]="product.name" class="w-full rounded-border" />
<p-tag [value]="product.inventoryStatus" [severity]="getSeverity(product.inventoryStatus)" class="absolute dark:!bg-surface-900" [ngStyle]="{ 'left.px': 5, 'top.px': 5 }" />
</div>
</div>
<div class="mb-4 font-medium">{{ product.name }}</div>
<div class="flex justify-between items-center">
<div class="mt-0 font-semibold text-xl">{{ '$' + product.price }}</div>
<span>
<p-button icon="pi pi-heart" severity="secondary" [outlined]="true" />
<p-button icon="pi pi-shopping-cart" styleClass="ml-2" />
</span>
</div>
</div>
</ng-template>
</p-carousel>
</div>
`,
standalone: true,
imports: [ButtonModule, CarouselModule, TagModule],
providers: [ProductService]
})
export class CarouselResponsiveDemo implements OnInit {
private productService = inject(ProductService);
products = signal<Product[]>([]);
responsiveOptions: any[] | undefined;
ngOnInit() {
this.productService.getProductsSmall().then((products) => {
this.products.set(products);
});
this.responsiveOptions = [
{
breakpoint: '1400px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '1199px',
numVisible: 3,
numScroll: 1
},
{
breakpoint: '767px',
numVisible: 2,
numScroll: 1
},
{
breakpoint: '575px',
numVisible: 1,
numScroll: 1
}
];
}
getSeverity(status: string) {
switch (status) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
return 'warn';
case 'OUTOFSTOCK':
return 'danger';
}
}
}
Vertical
세로 방향 Carousel을 만들려면 orientation을 vertical로 설정하고 여기에 verticalViewPortHeight도 함께 지정해야 해요.
import { Component, OnInit, inject, signal } from '@angular/core';
import { ButtonModule } from 'primeng/button';
import { CarouselModule } from 'primeng/carousel';
import { TagModule } from 'primeng/tag';
import { ProductService } from '@/service/productservice';
import { Product } from '@/domain/product';
@Component({
template: `
<div class="card">
<p-carousel [value]="products()" [numVisible]="1" [numScroll]="1" orientation="vertical" verticalViewPortHeight="330px" contentClass="flex items-center">
<ng-template let-product #item>
<div class="border border-surface-200 dark:border-surface-700 rounded m-2 p-4">
<div class="mb-4">
<div class="relative mx-auto">
<img src="https://primefaces.org/cdn/primeng/images/demo/product/{{ product.image }}" [alt]="product.name" class="w-full rounded" />
<p-tag [value]="product.inventoryStatus" [severity]="getSeverity(product.inventoryStatus)" class="absolute dark:!bg-surface-900" [ngStyle]="{ 'left.px': 5, 'top.px': 5 }" />
</div>
</div>
<div class="mb-4 font-medium">{{ product.name }}</div>
<div class="flex justify-between items-center">
<div class="mt-0 font-semibold text-xl">{{ '$' + product.price }}</div>
<span>
<p-button icon="pi pi-heart" severity="secondary" [outlined]="true" />
<p-button icon="pi pi-shopping-cart" styleClass="ml-2" />
</span>
</div>
</div>
</ng-template>
</p-carousel>
</div>
`,
standalone: true,
imports: [ButtonModule, CarouselModule, TagModule],
providers: [ProductService]
})
export class CarouselVerticalDemo implements OnInit {
private productService = inject(ProductService);
products = signal<Product[]>([]);
ngOnInit() {
this.productService.getProductsSmall().then((products) => {
this.products.set(products);
});
}
getSeverity(status: string) {
switch (status) {
case 'INSTOCK':
return 'success';
case 'LOWSTOCK':
return 'warn';
case 'OUTOFSTOCK':
return 'danger';
}
}
}
Carousel
Carousel은 다양한 커스터마이징 옵션을 갖춘 콘텐츠 슬라이더예요.
Props
| Name | Type | Default | Description |
|---|---|---|---|
| dt | InputSignal | undefined | 컴포넌트의 스코프 디자인 토큰을 정의해요. |
| unstyled | InputSignal | undefined | 컴포넌트를 스타일 없이 렌더링할지 여부를 나타내요. |
| pt | InputSignal | undefined | 컴포넌트 내부의 DOM 요소에 속성을 전달할 때 사용해요. |
| ptOptions | InputSignal | undefined | 컴포넌트의 passthrough(pt) 옵션을 구성할 때 사용해요. |
| page | number | - | 첫 번째 항목의 인덱스예요. |
| numVisible | number | - | 페이지당 항목 수예요. |
| numScroll | number | - | 스크롤할 항목 수예요. |
| responsiveOptions | CarouselResponsiveOptions[] | - | 반응형 디자인을 위한 옵션 배열이에요. |
| orientation | "vertical" | "horizontal" | horizontal | 컴포넌트의 레이아웃을 지정해요. |
| verticalViewPortHeight | string | 300px | 세로 레이아웃에서 뷰포트의 높이예요. |
| contentClass | string | - | 메인 콘텐츠의 스타일 클래스예요. |
| indicatorsContentClass | string | - | 인디케이터 항목의 스타일 클래스예요. |
| indicatorsContentStyle | { [klass: string]: any } | - | 인디케이터 항목의 인라인 스타일이에요. |
| indicatorStyleClass | string | - | 인디케이터의 스타일 클래스예요. |
| indicatorStyle | { [klass: string]: any } | - | 인디케이터의 스타일이에요. |
| value | any[] | - | 표시할 객체 배열이에요. |
| circular | boolean | false | 스크롤이 무한으로 이어질지 정의해요. |
| showIndicators | boolean | true | 인디케이터 컨테이너를 표시할지 여부예요. |
| showNavigators | boolean | true | 컨테이너에 내비게이션 버튼을 표시할지 여부예요. |
| autoplayInterval | number | 0 | 항목을 자동으로 스크롤하는 시간(밀리초)이에요. |
| styleClass | string | - | 뷰포트 컨테이너의 스타일 클래스예요. (지원 중단됨) |
| prevButtonProps | ButtonProps | ... | ButtonProps의 모든 속성을 Button 컴포넌트에 전달할 때 사용해요. |
| nextButtonProps | ButtonProps | ... | ButtonProps의 모든 속성을 Button 컴포넌트에 전달할 때 사용해요. |
Emits
| Name | Parameters | Description |
|---|---|---|
| onPage | event: CarouselPageEvent | 스크롤 후에 호출되는 콜백이에요. |
Templates
| Name | Type | Description |
|---|---|---|
| item | TemplateRef<CarouselItemTemplateContext> | 커스텀 항목 템플릿이에요. |
| header | TemplateRef | 커스텀 헤더 템플릿이에요. |
| footer | TemplateRef | 커스텀 푸터 템플릿이에요. |
| previousicon | TemplateRef | 커스텀 이전 아이콘 템플릿이에요. |
| nexticon | TemplateRef | 커스텀 다음 아이콘 템플릿이에요. |
Pass Through Options
| Name | Type | Description |
|---|---|---|
| root | PassThroughOption<HTMLDivElement, I> | 루트의 DOM 요소에 속성을 전달할 때 사용해요. |
| header | PassThroughOption<HTMLDivElement, I> | 헤더의 DOM 요소에 속성을 전달할 때 사용해요. |
| contentContainer | PassThroughOption<HTMLDivElement, I> | 콘텐츠 컨테이너의 DOM 요소에 속성을 전달할 때 사용해요. |
| content | PassThroughOption<HTMLDivElement, I> | 콘텐츠의 DOM 요소에 속성을 전달할 때 사용해요. |
| pcPrevButton | ButtonPassThroughOptions | 이전 버튼의 DOM 요소에 속성을 전달할 때 사용해요. |
| viewport | PassThroughOption<HTMLDivElement, I> | 뷰포트의 DOM 요소에 속성을 전달할 때 사용해요. |
| itemList | PassThroughOption<HTMLDivElement, I> | 항목 목록의 DOM 요소에 속성을 전달할 때 사용해요. |
| item | PassThroughOption<HTMLDivElement, I> | 항목의 DOM 요소에 속성을 전달할 때 사용해요. |
| itemClone | PassThroughOption<HTMLDivElement, I> | 항목 복제본의 DOM 요소에 속성을 전달할 때 사용해요. |
| pcNextButton | ButtonPassThroughOptions | 다음 버튼의 DOM 요소에 속성을 전달할 때 사용해요. |
| indicatorList | PassThroughOption<HTMLUListElement, I> | 인디케이터 목록의 DOM 요소에 속성을 전달할 때 사용해요. |
| indicator | PassThroughOption<HTMLLIElement, I> | 인디케이터의 DOM 요소에 속성을 전달할 때 사용해요. |
| indicatorButton | PassThroughOption<HTMLButtonElement, I> | 인디케이터 버튼의 DOM 요소에 속성을 전달할 때 사용해요. |
| footer | PassThroughOption<HTMLDivElement, I> | 푸터의 DOM 요소에 속성을 전달할 때 사용해요. |
Theming
CSS Classes
| Class | Description |
|---|---|
| p-carousel | 루트 요소의 클래스 이름이에요. |
| p-carousel-header | 헤더 요소의 클래스 이름이에요. |
| p-carousel-content-container | 콘텐츠 컨테이너 요소의 클래스 이름이에요. |
| p-carousel-content | 콘텐츠 요소의 클래스 이름이에요. |
| p-carousel-prev-button | 이전 버튼 요소의 클래스 이름이에요. |
| p-carousel-viewport | 뷰포트 요소의 클래스 이름이에요. |
| p-carousel-item-list | 항목 목록 요소의 클래스 이름이에요. |
| p-carousel-item-clone | 항목 복제본 요소의 클래스 이름이에요. |
| p-carousel-item | 항목 요소의 클래스 이름이에요. |
| p-carousel-next-button | 다음 버튼 요소의 클래스 이름이에요. |
| p-carousel-indicator-list | 인디케이터 목록 요소의 클래스 이름이에요. |
| p-carousel-indicator | 인디케이터 요소의 클래스 이름이에요. |
| p-carousel-indicator-button | 인디케이터 버튼 요소의 클래스 이름이에요. |
| p-carousel-footer | 푸터 요소의 클래스 이름이에요. |
Design Tokens
| Token | CSS Variable | Description |
|---|---|---|
| carousel.transition.duration | --p-carousel-transition-duration | 루트의 트랜지션 지속 시간이에요. |
| carousel.content.gap | --p-carousel-content-gap | 콘텐츠의 간격이에요. |
| carousel.indicator.list.padding | --p-carousel-indicator-list-padding | 인디케이터 목록의 패딩이에요. |
| carousel.indicator.list.gap | --p-carousel-indicator-list-gap | 인디케이터 목록의 간격이에요. |
| carousel.indicator.width | --p-carousel-indicator-width | 인디케이터의 너비예요. |
| carousel.indicator.height | --p-carousel-indicator-height | 인디케이터의 높이예요. |
| carousel.indicator.border.radius | --p-carousel-indicator-border-radius | 인디케이터의 테두리 반경이에요. |
| carousel.indicator.focus.ring.width | --p-carousel-indicator-focus-ring-width | 인디케이터의 포커스 링 너비예요. |
| carousel.indicator.focus.ring.style | --p-carousel-indicator-focus-ring-style | 인디케이터의 포커스 링 스타일이에요. |
| carousel.indicator.focus.ring.color | --p-carousel-indicator-focus-ring-color | 인디케이터의 포커스 링 색상이에요. |
| carousel.indicator.focus.ring.offset | --p-carousel-indicator-focus-ring-offset | 인디케이터의 포커스 링 오프셋이에요. |
| carousel.indicator.focus.ring.shadow | --p-carousel-indicator-focus-ring-shadow | 인디케이터의 포커스 링 그림자예요. |
| carousel.indicator.background | --p-carousel-indicator-background | 인디케이터의 배경이에요. |
| carousel.indicator.hover.background | --p-carousel-indicator-hover-background | 인디케이터의 호버 배경이에요. |
| carousel.indicator.active.background | --p-carousel-indicator-active-background | 인디케이터의 활성 배경이에요. |
더 알아보기 (Learn more)
이 문서에는 원문에서 추출한 별도의 관련 문서 링크가 없어요.