import { useState } from 'react'; import { Toast, toastService } from '~/components/ui/Toast'; import type { ToastType } from '~/components/ui/Toast'; import { LinksFunction } from '@remix-run/node'; import toastStyles from '~/styles/components/toast.css?url'; export const links: LinksFunction = () => [ { rel: "stylesheet", href: toastStyles }, ]; export default function ToastExample() { const [isToastOpen, setIsToastOpen] = useState(false); const [toastType, setToastType] = useState('info'); const [toastMessage, setToastMessage] = useState(''); const [autoClose, setAutoClose] = useState(true); // 打开普通通知 const openToast = (type: ToastType, message: string) => { setToastType(type); setToastMessage(message); setIsToastOpen(true); }; // 使用服务显示不同类型通知 const showSuccessToast = () => { toastService.success('操作成功完成!'); }; const showErrorToast = () => { toastService.error('操作过程中发生错误,请重试。'); }; const showWarningToast = () => { toastService.warning('此操作可能产生不可逆转的结果。'); }; const showInfoToast = () => { toastService.info('系统将于今晚10点进行升级维护。'); }; // 显示多行文本的长通知 const showLongToast = () => { toastService.info('这是一个具有很长内容的通知,将自动换行以适应容器宽度,并且最多显示三行,超出部分会被截断。系统会自动处理长文本的换行和截断,确保显示效果一致。'); }; // 短时间内显示多个通知 const showMultipleToasts = () => { toastService.success('第一条通知'); setTimeout(() => { toastService.info('第二条通知'); }, 300); setTimeout(() => { toastService.warning('第三条通知'); }, 600); setTimeout(() => { toastService.error('第四条通知'); }, 900); }; return (

轻量级顶部通知示例

基本用法

您可以直接使用Toast组件来控制通知的显示和隐藏。

setIsToastOpen(false)} message={toastMessage} type={toastType} autoClose={autoClose} />

全局通知服务

使用toastService可以在任何组件中方便地显示通知,而无需手动管理状态。

); }