Toast

Toast 用于在覆盖层中显示消息。


import { Toast } from 'primereact/toast';
         

通过调用组件 ref 提供的 show 方法来显示消息。单个消息由 Message 接口指定,该接口定义了各种属性,例如 severitysummary 详情.


<Toast ref={toast} />
<Button onClick={show} label="Basic" />
         

severity 选项指定消息的类型。


<Toast ref={toast} />
<Button label="Success" severity="success" onClick={showSuccess} />
<Button label="Info" severity="info" onClick={showInfo} />
<Button label="Warn" severity="warning" onClick={showWarn} />
<Button label="Error" severity="danger" onClick={showError} />
<Button label="Secondary" severity="secondary" onClick={showSecondary} />
<Button label="Contrast" severity="contrast" onClick={showContrast} />
         

消息的位置通过 position 属性进行自定义。


<Toast ref={toastTL} position="top-left" />
<Toast ref={toastBL} position="bottom-left" />
<Toast ref={toastBR} position="bottom-right" />
<Button label="Top Left" className="mr-2" onClick={showTopLeft} />
<Button label="Bottom Left" className="p-button-warning" onClick={showBottomLeft} />
<Button label="Bottom Right" className="p-button-success" onClick={showBottomRight} />
         

通过将数组传递给 show 方法来显示多条消息。


<Toast ref={toast} />
<Button onClick={showMultiple} label="Multiple" className="p-button-warning" />
         

消息将在 3000 毫秒后消失,这是为 life 选项定义的默认值。要显示保持可见且不会自动隐藏的消息,请将 sticky 选项设置为 "true"。


<Toast ref={toast} />
<Button onClick={showSticky} label="Sticky" severity="success" />
<Button onClick={clear} label="Clear" />
         

消息中的自定义内容使用 content 选项定义。


toastBC.current.show({
    severity: 'success',
    summary: 'Can you send me the report?',
    sticky: true,
    content: (props) => (
        <div className="flex flex-column align-items-left" style={{ flex: '1' }}>
            <div className="flex align-items-center gap-2">
                <Avatar image="/images/avatar/amyelsner.png" shape="circle" />
                <span className="font-bold text-900">Amy Elsner</span>
            </div>
            <div className="font-medium text-lg my-3 text-900">{props.message.summary}</div>
            <Button className="p-button-sm flex" label="Reply" severity="success" onClick={clear}></Button>
        </div>
    )
}); 

通过定义 content 属性启用无头模式,让你可以实现整个对话框 UI,而不是默认元素。


<Toast
    ref={toast}
    content={({ message }) => (
        <section className="flex p-3 gap-3 w-full bg-black-alpha-90 shadow-2 fadeindown" style={{ borderRadius: '10px' }}>
            <i className="pi pi-cloud-upload text-primary-500 text-2xl"></i>
            <div className="flex flex-column gap-3 w-full">
                <p className="m-0 font-semibold text-base text-white">{message.summary}</p>
                <p className="m-0 text-base text-700">{message.detail}</p>
                <div className="flex flex-column gap-2">
                    <ProgressBar value={progress} showValue="false"></ProgressBar>
                    <label className="text-right text-xs text-white">{progress}% uploaded...</label>
                </div>
                <div className="flex gap-3 mb-3">
                    <Button label="Another Upload?" text className="p-0" onClick={clear}></Button>
                    <Button label="Cancel" text className="text-white p-0" onClick={clear}></Button>
                </div>
            </div>
        </section>
    )}
></Toast>
<Button onClick={show} label="View" />
             

屏幕阅读器

Toast 组件使用 alert 角色,它隐式地将 aria-live 定义为 "assertive",将 aria-atomic 定义为 "true"。

关闭元素是一个 button,其 aria-label 默认引用 locale API 的 aria.close 属性,你可以使用 closeButtonProps 来自定义元素并覆盖默认的 aria-label

关闭按钮键盘支持

按键功能
回车键关闭消息。
空格键关闭消息。