Toast — 사용자에게 메시지를 표시하는 컴포넌트
Toast는 사용자에게 짧은 메시지를 화면에 띄워 알려 주는 컴포넌트예요. toast(...) 함수로 어디서든 토스트를 띄우고, severity·약속(promise)·업데이트·고정(sticky)·커스텀 렌더링 등을 폭넓게 지원해요.
출처: 문서
본문
Toast는 사용자에게 메시지를 표시하는 컴포넌트예요.
사용법 (Usage)
import { Toast } from '@primereact/ui/toast';
import { Toaster, toast } from '@primereact/ui/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>
toast(...) 또는 severity 단축 메서드(toast.success, toast.info, toast.warn, toast.error, toast.secondary, toast.contrast)로 어디서든 토스트를 띄울 수 있어요.
예시
심각도 (Severity)
내장된 6가지 severity: info, success, warn, error, secondary, contrast. severity를 직접 넘기거나 단축 메서드를 사용해요.
toast({ severity: 'success', title: 'Saved' });
toast.success({ title: 'Saved' });
약속 (Promise)
toast.promise는 토스트를 promise에 연결해, 로딩 상태를 보여 주다가 결과에 따라 success 또는 error로 전환해요.
업데이트 (Update)
toast.update(id, options)는 기존 토스트에 새 옵션을 병합해요. 로딩 토스트를 결과 토스트로 바꿀 때 유용해요.
고정 (Sticky)
duration: Infinity를 넘기면 토스트를 수동으로 닫을 때까지 유지해요. 모든 토스트를 기본적으로 고정하려면 토스트마다 지정하는 대신 Toaster의 timeout prop을 Infinity로 설정해요.
커스텀 (Custom)
render를 넘기면 기본 본문을 커스텀 JSX로 대체해요. 루트 래퍼(위치 지정, 애니메이션, 스와이프)는 유지돼요.
위치 (Position)
<Toaster.Root>의 position prop으로 토스트가 나타날 위치를 제어해요.
펼침 모드 (Expanded Mode)
mode="expanded"는 스택을 항상 펼쳐서 렌더링해요. 기본적으로 토스트는 접혀서 쌓이고 호버 시 펼쳐져요.
동작 버튼 (Action)
action 필드로 토스트에 대화형 버튼을 추가할 수 있어요.
toast 함수 (toast function)
toast(options)는 토스트를 큐에 넣고 그 id를 반환해요.
const id = toast({ severity: 'success', title: 'Saved' });
심각도 단축 메서드 (Severity shortcuts)
각 severity에는 severity를 자동으로 설정하는 단축 메서드가 있어요:
toast.success({ title: 'Saved' });
toast.info({ title: 'Heads up' });
toast.warn({ title: 'Check this' });
toast.error({ title: 'Failed' });
toast.secondary({ title: '...' });
toast.contrast({ title: '...' });
메서드 (Methods)
toast.dismiss(id); // dismiss specific toast
toast.dismiss(); // dismiss all
toast.update(id, options); // merge options into existing toast
toast.promise(p, { loading, success, error });
Toast 옵션 (Toast options)
이 필드들은 toast(...) 또는 그 단축 메서드에 넘길 수 있어요.
| Field | Type | Description |
|---|---|---|
severity |
'info' | 'success' | 'warn' | 'error' | 'secondary' | 'contrast' |
Visual severity. Defaults to info. |
title |
ReactNode |
Main heading. |
description |
ReactNode |
Secondary text. |
icon |
ReactNode |
Custom icon, overrides the matched <Toast.Icon match="..." /> for this toast. |
action |
ButtonProps |
Props forwarded to <Toast.Action> (e.g. children, onClick). |
render |
ReactElement |
Replaces the default body with custom JSX. |
duration |
number |
Auto-dismiss delay in ms. Pass Infinity to keep open. |
loading |
boolean |
Marks the toast as loading and disables auto-dismiss. |
dismissible |
boolean |
If false, hides the close button and disables swipe-to-dismiss. |
group |
string |
Scopes the toast to a specific <Toaster.Root group="..." />. |
data |
Record<string, unknown> |
Arbitrary metadata, available on the toast object and in callbacks. |
onDismiss |
(toast) => void |
Called when dismissed by user (close button, swipe, or dismiss(id)). |
onTimeout |
(toast) => void |
Called when duration elapses. |
관련 자료
하위 컴포넌트 (Sub-Components)
전체 하위 컴포넌트 API 레퍼런스는 Toast Primitive API에서 확인할 수 있어요.
훅 (Hooks)
훅 API 레퍼런스는 Toast Headless API에서 확인할 수 있어요.
접근성 (Accessibility)
WAI-ARIA 준수 사항과 키보드 지원에 대한 자세한 내용은 Toast Primitive에서 볼 수 있어요.
테마 (Theming)
CSS 클래스 (CSS Classes)
styled 모드에서 사용하는 클래스 이름 목록이에요.
| ClassName | Description |
|---|---|
| p-toast p-component | Class name of the root element |
| p-toast-icon | Class name of the icon element |
| p-toast-title | Class name of the title element |
| p-toast-description | Class name of the description element |
| p-toast-action | Class name of the action element |
| p-toast-close | Class name of the close element |
| p-toast-progress | Class name of the progress element |
디자인 토큰 (Design Tokens)
디자인 토큰 목록이에요.
| Token | CSS Variable | Description |
|---|---|---|
| toast.width | --p-toast-width | Width of root |
| toast.border.radius | --p-toast-border-radius | Border radius of root |
| toast.border.width | --p-toast-border-width | Border width of root |
| toast.transition.duration | --p-toast-transition-duration | Transition duration of root |
| toast.blur | --p-toast-blur | Used to pass tokens of the blur section |
| toast.focus.ring.width | --p-toast-focus-ring-width | Focus ring width of root |
| toast.focus.ring.style | --p-toast-focus-ring-style | Focus ring style of root |
| toast.focus.ring.color | --p-toast-focus-ring-color | Focus ring color of root |
| toast.focus.ring.offset | --p-toast-focus-ring-offset | Focus ring offset of root |
| toast.focus.ring.shadow | --p-toast-focus-ring-shadow | Focus ring shadow of root |
| toast.icon.size | --p-toast-icon-size | Size of icon |
| toast.icon.margin | --p-toast-icon-margin | Margin of icon |
| toast.content.padding | --p-toast-content-padding | Padding of content |
| toast.content.gap | --p-toast-content-gap | Gap of content |
| toast.text.gap | --p-toast-text-gap | Gap of text |
| toast.summary.font.weight | --p-toast-summary-font-weight | Font weight of summary |
| toast.summary.font.size | --p-toast-summary-font-size | Font size of summary |
| toast.detail.font.weight | --p-toast-detail-font-weight | Font weight of detail |
| toast.detail.font.size | --p-toast-detail-font-size | Font size of detail |
| toast.close.button.width | --p-toast-close-button-width | Width of close button |
| toast.close.button.height | --p-toast-close-button-height | Height of close button |
| toast.close.button.border.radius | --p-toast-close-button-border-radius | Border radius of close button |
| toast.close.button.focus.ring.width | --p-toast-close-button-focus-ring-width | Focus ring width of close button |
| toast.close.button.focus.ring.style | --p-toast-close-button-focus-ring-style | Focus ring style of close button |
| toast.close.button.focus.ring.offset | --p-toast-close-button-focus-ring-offset | Focus ring offset of close button |
| toast.close.icon.size | --p-toast-close-icon-size | Size of close icon |
| toast.normal.background | --p-toast-normal-background | Background of normal |
| toast.normal.border.color | --p-toast-normal-border-color | Border color of normal |
| toast.normal.color | --p-toast-normal-color | Color of normal |
| toast.normal.detail.color | --p-toast-normal-detail-color | Detail color of normal |
| toast.normal.shadow | --p-toast-normal-shadow | Shadow of normal |
| toast.normal.close.button.hover.background | --p-toast-normal-close-button-hover-background | Close button hover background of normal |
| toast.normal.close.button.focus.ring.color | --p-toast-normal-close-button-focus-ring-color | Close button focus ring color of normal |
| toast.normal.close.button.focus.ring.shadow | --p-toast-normal-close-button-focus-ring-shadow | Close button focus ring shadow of normal |
| toast.info.background | --p-toast-info-background | Background of info |
| toast.info.border.color | --p-toast-info-border-color | Border color of info |
| toast.info.color | --p-toast-info-color | Color of info |
| toast.info.detail.color | --p-toast-info-detail-color | Detail color of info |
| toast.info.shadow | --p-toast-info-shadow | Shadow of info |
| toast.info.close.button.hover.background | --p-toast-info-close-button-hover-background | Close button hover background of info |
| toast.info.close.button.focus.ring.color | --p-toast-info-close-button-focus-ring-color | Close button focus ring color of info |
| toast.info.close.button.focus.ring.shadow | --p-toast-info-close-button-focus-ring-shadow | Close button focus ring shadow of info |
| toast.success.background | --p-toast-success-background | Background of success |
| toast.success.border.color | --p-toast-success-border-color | Border color of success |
| toast.success.color | --p-toast-success-color | Color of success |
| toast.success.detail.color | --p-toast-success-detail-color | Detail color of success |
| toast.success.shadow | --p-toast-success-shadow | Shadow of success |
| toast.success.close.button.hover.background | --p-toast-success-close-button-hover-background | Close button hover background of success |
| toast.success.close.button.focus.ring.color | --p-toast-success-close-button-focus-ring-color | Close button focus ring color of success |
| toast.success.close.button.focus.ring.shadow | --p-toast-success-close-button-focus-ring-shadow | Close button focus ring shadow of success |
| toast.warn.background | --p-toast-warn-background | Background of warn |
| toast.warn.border.color | --p-toast-warn-border-color | Border color of warn |
| toast.warn.color | --p-toast-warn-color | Color of warn |
| toast.warn.detail.color | --p-toast-warn-detail-color | Detail color of warn |
| toast.warn.shadow | --p-toast-warn-shadow | Shadow of warn |
| toast.warn.close.button.hover.background | --p-toast-warn-close-button-hover-background | Close button hover background of warn |
| toast.warn.close.button.focus.ring.color | --p-toast-warn-close-button-focus-ring-color | Close button focus ring color of warn |
| toast.warn.close.button.focus.ring.shadow | --p-toast-warn-close-button-focus-ring-shadow | Close button focus ring shadow of warn |
| toast.error.background | --p-toast-error-background | Background of error |
| toast.error.border.color | --p-toast-error-border-color | Border color of error |
| toast.error.color | --p-toast-error-color | Color of error |
| toast.error.detail.color | --p-toast-error-detail-color | Detail color of error |
| toast.error.shadow | --p-toast-error-shadow | Shadow of error |
| toast.error.close.button.hover.background | --p-toast-error-close-button-hover-background | Close button hover background of error |
| toast.error.close.button.focus.ring.color | --p-toast-error-close-button-focus-ring-color | Close button focus ring color of error |
| toast.error.close.button.focus.ring.shadow | --p-toast-error-close-button-focus-ring-shadow | Close button focus ring shadow of error |
| toast.secondary.background | --p-toast-secondary-background | Background of secondary |
| toast.secondary.border.color | --p-toast-secondary-border-color | Border color of secondary |
| toast.secondary.color | --p-toast-secondary-color | Color of secondary |
| toast.secondary.detail.color | --p-toast-secondary-detail-color | Detail color of secondary |
| toast.secondary.shadow | --p-toast-secondary-shadow | Shadow of secondary |
| toast.secondary.close.button.hover.background | --p-toast-secondary-close-button-hover-background | Close button hover background of secondary |
| toast.secondary.close.button.focus.ring.color | --p-toast-secondary-close-button-focus-ring-color | Close button focus ring color of secondary |
| toast.secondary.close.button.focus.ring.shadow | --p-toast-secondary-close-button-focus-ring-shadow | Close button focus ring shadow of secondary |
| toast.contrast.background | --p-toast-contrast-background | Background of contrast |
| toast.contrast.border.color | --p-toast-contrast-border-color | Border color of contrast |
| toast.contrast.color | --p-toast-contrast-color | Color of contrast |
| toast.contrast.detail.color | --p-toast-contrast-detail-color | Detail color of contrast |
| toast.contrast.shadow | --p-toast-contrast-shadow | Shadow of contrast |
| toast.contrast.close.button.hover.background | --p-toast-contrast-close-button-hover-background | Close button hover background of contrast |
| toast.contrast.close.button.focus.ring.color | --p-toast-contrast-close-button-focus-ring-color | Close button focus ring color of contrast |
| toast.contrast.close.button.focus.ring.shadow | --p-toast-contrast-close-button-focus-ring-shadow | Close button focus ring shadow of contrast |