복합 구성(compound composition)을 가진 스타일 없고 접근 가능한 토스트 알림 시스템이에요.
출처: 문서
본문
특징 (Features)
- 복합 API:
Toaster(Root, Portal, Region)와 Toast(Root, Content, Icon, Message, Title, Description, Action, Close)
<Toast.Icon match="...">을 통한 심각도 인지 아이콘 렌더링
- 심각도 단축키(
success, info, warn, error, secondary, contrast)와 update, dismiss, promise를 가진 전역 toast 함수
- 방향 감지와 함께 스와이프로 닫기
- 마우스 호버 시 확장되는 스택형 레이아웃
- 호버, 상호작용, 문서 가시성 시 일시정지되는 자동 타임아웃
사용법 (Usage)
import { Toast } from 'primereact/toast';
import { Toaster, toast } from 'primereact/toaster';
<Toaster.Root>
<Toaster.Portal>
<Toaster.Region>
{({ toaster }) =>
toaster?.toasts?.map((t) => (
<Toast.Root key={t.id} toast={t}>
<Toast.Content>
<Toast.Icon match="success">
<Check />
</Toast.Icon>
<Toast.Icon match="error">
<Times />
</Toast.Icon>
<Toast.Icon match="warn">
<ExclamationTriangle />
</Toast.Icon>
<Toast.Icon match="info">
<InfoCircle />
</Toast.Icon>
<Toast.Message>
<Toast.Title />
<Toast.Description />
<Toast.Action />
</Toast.Message>
<Toast.Close />
</Toast.Content>
</Toast.Root>
))
}
</Toaster.Region>
</Toaster.Portal>
</Toaster.Root>
동작 (Behavior)
아이콘 매칭 (Icon matching)
<Toast.Icon match="severity">는 해당 심각도의 토스트에서만 렌더링돼요. match prop이 없는 <Toast.Icon />은 catch-all로, 토스트에 severity가 없을 때(예: toast.promise의 로딩 토스트) 렌더링돼요. toast({ icon })로 전달된 커스텀 icon은 매칭된 블록의 자식들을 덮어써요.
<Toast.Icon /> {/* catch-all (no severity) */}
<Toast.Icon match="success"><Check /></Toast.Icon>
<Toast.Icon match="error"><Times /></Toast.Icon>
다형 렌더링 (Polymorphic rendering)
어떤 서브 컴포넌트든 as로 렌더링되는 HTML 요소를 바꾸거나 커스텀 React 컴포넌트를 렌더링할 수 있어요.
<Toast.Close as="div">...</Toast.Close>
<Toast.Action as={MyButton}>...</Toast.Action>
기본값: Toaster.Root=div, Toaster.Region=div, Toast.Root=li, Toast.Content=div, Toast.Message=div, Toast.Icon=span, Toast.Title=div, Toast.Description=div, Toast.Action=button, Toast.Close=button.
렌더 함수 자식 (Render function children)
Toaster.Region은 자식으로 렌더 함수를 받아 토스트를 순회하기 위해 toaster 인스턴스를 노출해요.
<Toaster.Region>
{({ toaster }) =>
toaster?.toasts?.map((t) => (
<Toast.Root key={t.id} toast={t}>
...
</Toast.Root>
))
}
</Toaster.Region>
Pass Through
<DocPTViewer name="toast-pt" components={['ToastRoot']} />
API
ToasterRoot
| Attribute |
Value |
data-position |
Current position value |
ToasterRegion
| Attribute |
Value |
data-position |
Current position value |
data-expanded |
Present when region is expanded |
CSS custom properties, 토스트 레이아웃 간격을 위해 Toaster.Region 스타일에 설정돼요.
| Variable |
Description |
--gap |
Vertical gap between toasts in pixels |
--front-toast-height |
Height of the frontmost toast in pixels |
ToasterPortal
ToastRoot
| Attribute |
Value |
data-id |
Toast identifier |
data-index |
Toast index in the stack |
data-mounted |
Present after initial render |
data-removed |
Present during exit animation |
data-visible |
Present when within the visible limit |
data-front |
Present on the frontmost toast |
data-expanded |
Present when toaster is expanded |
data-swiping |
Present during active swipe |
data-swiped |
Present after swipe delta exceeds zero |
data-swipe-out |
Present after swipe threshold exceeded |
data-swipe-direction |
Swipe direction (left, right, up, down) |
data-dismissible |
Present when toast is dismissible |
CSS custom properties, 스와이프 제스처와 스택 포지셔닝을 위해 Toast.Root 스타일에 설정돼요.
| Variable |
Description |
--initial-height |
Initial height of the toast before animation |
--toast-index |
Index of the toast in the stack |
--toast-z-index |
Z-index stacking order for the toast |
--toast-offset |
Vertical offset position in pixels |
--swipe-amount-x |
Horizontal swipe distance in pixels |
--swipe-amount-y |
Vertical swipe distance in pixels |
| Label |
Type |
Description |
| root |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
| body |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the body's DOM element. |
| content |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the content's DOM element. |
| icon |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the icon's DOM element. |
| title |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the title's DOM element. |
| description |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the description's DOM element. |
| action |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the action button's DOM element. |
| close |
ToastRootPassThroughType<HTMLAttributes> |
Used to pass attributes to the close button's DOM element. |
ToastContent
| Label |
Type |
Description |
| root |
ToastContentPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ToastMessage
| Label |
Type |
Description |
| root |
ToastMessagePassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ToastTitle
| Label |
Type |
Description |
| root |
ToastTitlePassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ToastDescription
| Label |
Type |
Description |
| root |
ToastDescriptionPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ToastIcon
match prop은 렌더링을 특정 심각도로 제한해요. catch-all에는 'always'를 사용하거나(또는 match 생략) match를 생략해요.
| Label |
Type |
Description |
| root |
ToastIconPassThroughType<HTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ToastAction
| Label |
Type |
Description |
| root |
ToastActionPassThroughType<ButtonHTMLAttributes> |
Used to pass attributes to the root's DOM element. |
ToastClose
| Label |
Type |
Description |
| root |
ToastClosePassThroughType<ButtonHTMLAttributes> |
Used to pass attributes to the root's DOM element. |
접근성 (Accessibility)
스크린 리더 (Screen reader)
Toast는 role="alert"을 사용하는데, 이는 aria-live를 assertive로, aria-atomic을 true로 암시적으로 설정해 스크린 리더가 토스트 콘텐츠를 즉시 알려주게 해요. toaster 영역은 tabIndex="0"이 있는 role="region"을 사용해 키보드 사용자가 토스트 영역으로 포커스를 이동할 수 있게 해요. 보이지 않는 토스트는 aria-hidden="true"와 inert로 표시해 스크린 리더 상호작용을 막아요.
키보드 지원 (Keyboard support)
| Key |
Function |
tab |
Moves focus into or out of the toast region. |
enter |
Activates the focused toast action or close button. |
space |
Activates the focused toast action or close button. |
더 알아보기 (Learn more)