Toast
支持定时自动关闭、悬停或聚焦暂停以及操作按钮的 Duostrap 实时通知组件。
Toast 以经典 Duostrap .alert 视觉样式呈现简短的非阻塞通知,支持自动倒计时关闭、鼠标悬停或焦点进入时暂停计时,以及经典的右上角 × 关闭与链接操作按钮。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-duostrap-button id="bs-toast-trigger">
Show Toast
</proto-ui-duostrap-button>
<proto-ui-duostrap-toast-viewport>
<proto-ui-duostrap-toast-root id="bs-toast-root">
<proto-ui-duostrap-toast-close>×</proto-ui-duostrap-toast-close>
<proto-ui-duostrap-toast-title>
Event has been created
</proto-ui-duostrap-toast-title>
<proto-ui-duostrap-toast-description>
Sunday, December 03, 2023 at 9:00 AM
</proto-ui-duostrap-toast-description>
<proto-ui-duostrap-toast-action>
Undo
</proto-ui-duostrap-toast-action>
</proto-ui-duostrap-toast-root>
</proto-ui-duostrap-toast-viewport>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#bs-toast-root');
setElementProps(root, { defaultOpen: false, duration: 5000 });
document
.querySelector('#bs-toast-trigger')
.addEventListener('click', () => root.getExposes().openToast());
</script>将于 0.3.0-rc.1 及后续版本中提供。可直接从 @proto.ui/prototypes-duostrap/toast 导入组件原型并使用对应宿主 Adapter 适配。
import { toastViewport, toastRoot, toastTitle, toastDescription, toastAction, toastClose,} from '@proto.ui/prototypes-duostrap/toast';沿用 Duostrap .alert 标记约定,将 Close(×)放在 Root 内的第一位,以对齐右侧浮动偏移:
<DuostrapToastViewport a11yLabel="Notifications"> <DuostrapToastRoot defaultOpen={false} duration={5000}> <DuostrapToastClose>×</DuostrapToastClose> <DuostrapToastTitle>Event has been created</DuostrapToastTitle> <DuostrapToastDescription> Sunday, December 03, 2023 at 9:00 AM </DuostrapToastDescription> <DuostrapToastAction a11yLabel="Undo event creation">Undo</DuostrapToastAction> </DuostrapToastRoot></DuostrapToastViewport>- 定时与暂停:
Root默认显示5000ms后请求关闭;设置duration={0}可保持常驻显示。鼠标悬停在通知上,或焦点位于Action/Close上时,自动暂停剩余倒计时。 - 操作与关闭:点击
Close、聚焦通知时按Escape或点击Action都会请求关闭通知;Action对应的业务逻辑由应用监听处理。受控open模式下,超时后仅发出一次关闭请求并等待父组件更新。 - 快捷聚焦:
Viewport支持按F8快速将焦点移至通知区域。全局队列编排由应用层自行管理。
Alert 变体
Section titled “Alert 变体”通过 variant 切换经典的 .alert(default)、.alert-info(info)、.alert-success(success)与 .alert-error(error)外观。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-duostrap-button id="bs-info-btn">
Info
</proto-ui-duostrap-button>
<proto-ui-duostrap-toast-viewport>
<proto-ui-duostrap-toast-root id="bs-info-toast">
<proto-ui-duostrap-toast-close>×</proto-ui-duostrap-toast-close>
<proto-ui-duostrap-toast-title>
Heads up!
</proto-ui-duostrap-toast-title>
<proto-ui-duostrap-toast-description>
Arrive 10 minutes before the session starts.
</proto-ui-duostrap-toast-description>
</proto-ui-duostrap-toast-root>
</proto-ui-duostrap-toast-viewport>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const toast = document.querySelector('#bs-info-toast');
setElementProps(toast, { defaultOpen: false, variant: 'info' });
document
.querySelector('#bs-info-btn')
.addEventListener('click', () =>
toast.getExposes().openToast()
);
</script>通过 Action 触发撤销或跟进操作,保留 Duostrap 经典的链接交互样式。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-duostrap-button id="bs-schedule">
Schedule Meeting
</proto-ui-duostrap-button>
<proto-ui-duostrap-toast-viewport>
<proto-ui-duostrap-toast-root id="bs-scheduled">
<proto-ui-duostrap-toast-close>×</proto-ui-duostrap-toast-close>
<proto-ui-duostrap-toast-title>
Meeting scheduled
</proto-ui-duostrap-toast-title>
<proto-ui-duostrap-toast-description>
Design review set for Tuesday at 2:00 PM.
</proto-ui-duostrap-toast-description>
<proto-ui-duostrap-toast-action>
Undo
</proto-ui-duostrap-toast-action>
</proto-ui-duostrap-toast-root>
</proto-ui-duostrap-toast-viewport>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const toast = document.querySelector('#bs-scheduled');
setElementProps(toast, {
defaultOpen: false,
duration: 6000,
variant: 'info',
});
document
.querySelector('#bs-schedule')
.addEventListener('click', () =>
toast.getExposes().openToast()
);
</script>传入 open 与 duration={0} 将错误提示保持常驻,直至用户手动关闭或连接恢复。
Web Components React Vue Vue 2
正在加载交互示例… 正文和源码已可阅读,交互控件就绪后会显示在这里。
<proto-ui-duostrap-button id="bs-open">
Show Persistent Toast
</proto-ui-duostrap-button>
<proto-ui-duostrap-toast-viewport>
<proto-ui-duostrap-toast-root id="bs-persistent">
<proto-ui-duostrap-toast-close>×</proto-ui-duostrap-toast-close>
<proto-ui-duostrap-toast-title>
Connection lost
</proto-ui-duostrap-toast-title>
<proto-ui-duostrap-toast-description>
Changes are paused until you reconnect or dismiss this notice.
</proto-ui-duostrap-toast-description>
</proto-ui-duostrap-toast-root>
</proto-ui-duostrap-toast-viewport>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const toast = document.querySelector('#bs-persistent');
setElementProps(toast, {
open: false,
duration: 0,
politeness: 'assertive',
variant: 'error',
});
document
.querySelector('#bs-open')
.addEventListener('click', () =>
setElementProps(toast, { open: true })
);
toast.addEventListener('openChange', (e) =>
setElementProps(toast, { open: e.detail.open })
);
</script>| 属性 | 类型 | 默认值 |
|---|---|---|
open | boolean | — |
defaultOpen | boolean | true |
duration | number | 5000 |
paused | boolean | false |
politeness | 'polite' 'assertive' | 'polite' |
variant | 'default' 'info' 'success' 'error' | 'default' |
appear | boolean | false |
enterDuration | number | 0 |
leaveDuration | number | 0 |
interrupt | 'reverse' 'wait' 'immediate' | 'reverse' |
Viewport、Action 与 Close
Section titled “Viewport、Action 与 Close”| 属性 | 类型 | 默认值 |
|---|---|---|
Viewport.a11yLabel | string | 'Notifications' |
Viewport.hotkey | string | 'F8' |
Action.disabled | boolean | false |
Action.a11yLabel | string | — |
Close.disabled | boolean | false |
事件与暴露方法:
- 事件:
Root暴露openChange({ open: boolean; reason: string });Action暴露action(void)。 - 方法:
Root暴露openToast()、close(reason?)、pause()、resume();Viewport暴露focusFirst()。