Toast
완료한 작업을 닫을 수 있는 알림으로 확인합니다.
사용법
알림을 쌓으려면 제어형 Toast를 ToastViewport에 배치하세요. 남은 시간이 표시되는 자동 만료 알림은 ToastProvider를 사용하고 필요하면 duration을 설정하세요. 마우스를 올리거나 포커스하면 만료가 일시 중지됩니다.
접근성
일반 알림에는 neutral, success, info, warning, loading을, 긴급 오류에는 danger 또는 error를 사용하세요. 닫기 버튼과 포커스 복원을 제공하세요. 사용자 지정 아이콘은 장식용이며 null로 숨길 수 있습니다.
| 속성 | 타입 | 기본값 |
|---|---|---|
| open | boolean | Required |
| title | string | Required |
| onDismiss | () => void | Required |
| dismissLabel | string | Dismiss notification |
| tone | neutral | success | info | warning | danger | error | loading | neutral |
| icon | ReactNode | Tone icon; null hides it |
| progress | number (0–1) | None |
import { useRef, useState } from 'react';
const toastMessages = {
neutral: { label: 'Notice', title: 'Project updated', description: 'The project has a new update.' },
loading: { label: 'Loading', title: 'Saving project', description: 'Your changes are being synchronized.' },
success: { label: 'Success', title: 'Project saved', description: 'Your changes are ready.' },
info: { label: 'Information', title: 'Export ready', description: 'Your export is available in the project history.' },
warning: { label: 'Warning', title: 'Connection interrupted', description: 'Your changes are preserved locally. Reconnect before continuing.' },
error: { label: 'Error', title: 'Save failed', description: 'Your changes could not be synchronized. Check your network connection and retry when you are ready.' },
} as const;
type PreviewTone = keyof typeof toastMessages;
const NoticeControls = () => {
const trigger = useRef<HTMLButtonElement>(null);
const { showToast } = useToast();
const [tone, setTone] = useState<PreviewTone>('success');
const options = (Object.keys(toastMessages) as PreviewTone[]).map(value => ({ value, label: toastMessages[value].label }));
const show = () => showToast({
title: toastMessages[tone].title,
description: toastMessages[tone].description,
tone,
duration: tone === 'neutral' || tone === 'success' || tone === 'info' ? 10000 : null,
restoreFocusTo: trigger.current,
});
return (
<div className="preview-toast-controls">
<Select aria-label={'Notification tone'} options={options} value={tone} onValueChange={value => {
if (Object.hasOwn(toastMessages, value)) setTone(value as PreviewTone);
}} />
<Button ref={trigger} variant="secondary" onClick={show}>
{'Show toast'}
</Button>
</div>
);
};
export const SaveNotice = () => (
<div className="preview-toast">
<ToastProvider className="preview-toast-viewport" maxVisible={3}>
<NoticeControls />
</ToastProvider>
</div>
);