Oddmash UI
所有组件

Toast

以可关闭的通知确认操作完成。

用法

在 ToastViewport 中放置受控 Toast 以堆叠通知。对于显示剩余时间并自动关闭的通知,请使用 ToastProvider,并按需设置 duration。悬停或聚焦时暂停倒计时。

无障碍

普通通知使用 neutral、success、info、warning 或 loading,紧急错误使用 danger 或 error。提供关闭按钮和焦点恢复。自定义图标为装饰用途,使用 null 隐藏。

属性类型默认值
openbooleanRequired
titlestringRequired
onDismiss() => voidRequired
dismissLabelstringDismiss notification
toneneutral | success | info | warning | danger | error | loadingneutral
iconReactNodeTone icon; null hides it
progressnumber (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>
);

交互式文档