Toast
支持定时自动消失、悬停暂停与快捷操作按钮的实时通知组件。
Toast 用于以非打断方式展示限时实时通知,支持悬停或聚焦时暂停倒计时、键盘快捷键定位以及可选操作按钮。本页示例使用 Shadcn Rhea 风格。
<proto-ui-shadcn-style-scope id="toast-scope">
<proto-ui-shadcn-button id="toast-trigger">
Show Toast
</proto-ui-shadcn-button>
<proto-ui-shadcn-toast-viewport id="toast-viewport">
<proto-ui-shadcn-toast-root id="toast-root">
<div class="flex items-start justify-between gap-4">
<div class="grid gap-1">
<proto-ui-shadcn-toast-title>
Event has been created
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Sunday, December 03, 2023 at 9:00 AM
</proto-ui-shadcn-toast-description>
</div>
<div class="flex items-center gap-2 shrink-0">
<proto-ui-shadcn-toast-action id="toast-action">
Undo
</proto-ui-shadcn-toast-action>
<proto-ui-shadcn-toast-close>
Dismiss
</proto-ui-shadcn-toast-close>
</div>
</div>
</proto-ui-shadcn-toast-root>
</proto-ui-shadcn-toast-viewport>
</proto-ui-shadcn-style-scope>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#toast-scope'), {
styleProfile: 'rhea',
});
setElementProps(document.querySelector('#toast-trigger'), {
variant: 'outline',
});
setElementProps(document.querySelector('#toast-viewport'), {
a11yLabel: 'Notifications',
});
setElementProps(document.querySelector('#toast-root'), {
defaultOpen: false,
duration: 5000,
});
setElementProps(document.querySelector('#toast-action'), {
a11yLabel: 'Undo event creation',
});
document
.querySelector('#toast-trigger')
.addEventListener('click', () => {
document
.querySelector('#toast-root')
.getExposes()
.openToast();
});
</script>将于 0.3.0-rc.1 及后续版本中提供。可直接从 @proto.ui/prototypes-shadcn/toast 导入组件原型并使用对应宿主 Adapter 适配。
import { toastViewport, toastRoot, toastTitle, toastDescription, toastAction, toastClose,} from '@proto.ui/prototypes-shadcn/toast';<ShadcnToastViewport a11yLabel="Notifications"> <ShadcnToastRoot defaultOpen={false} duration={5000}> <ShadcnToastTitle>Event has been created</ShadcnToastTitle> <ShadcnToastDescription>Sunday, December 03, 2023 at 9:00 AM</ShadcnToastDescription> <ShadcnToastAction a11yLabel="Undo event creation">Undo</ShadcnToastAction> <ShadcnToastClose>Dismiss</ShadcnToastClose> </ShadcnToastRoot></ShadcnToastViewport>Toast 由 Root、Viewport、Title、Description、Action 和 Close 六个部件组成:
Root默认持续显示5000ms后自动请求关闭;将duration设为0可保持常驻不自动关闭。- 指针悬停在通知上,或键盘焦点位于
Action/Close上时,剩余倒计时会自动暂停,离开后继续计时。 - 点击
Close、在获得焦点时按下Escape或激活Action都会发出关闭请求,实际业务副作用由应用事件回调处理。 - 受控模式下(传入
open),超时后只会发出一次关闭请求并等待外部状态更新。 Viewport提供无障碍区域命名(默认Notifications)以及F8快捷键快速聚焦通知区域。多条通知的排队管理由应用层控制。
使用 type 指定通知类型(default、success、info、warning、error、loading),并可搭配 politeness="assertive" 提升关键错误播报优先级。
<proto-ui-shadcn-style-scope id="toast-types-scope">
<div class="flex flex-wrap gap-2">
<proto-ui-shadcn-button data-toast-open="default">
Default
</proto-ui-shadcn-button>
<proto-ui-shadcn-button data-toast-open="success">
Success
</proto-ui-shadcn-button>
<proto-ui-shadcn-button data-toast-open="info">
Info
</proto-ui-shadcn-button>
<proto-ui-shadcn-button data-toast-open="warning">
Warning
</proto-ui-shadcn-button>
<proto-ui-shadcn-button data-toast-open="error">
Error
</proto-ui-shadcn-button>
</div>
<proto-ui-shadcn-toast-viewport id="toast-types-viewport">
<proto-ui-shadcn-toast-root id="toast-default">
<proto-ui-shadcn-toast-title>
Event has been created
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Sunday, December 03, 2023 at 9:00 AM
</proto-ui-shadcn-toast-description>
</proto-ui-shadcn-toast-root>
<proto-ui-shadcn-toast-root id="toast-error">
<proto-ui-shadcn-toast-title>
Request failed
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Event could not be created.
</proto-ui-shadcn-toast-description>
</proto-ui-shadcn-toast-root>
</proto-ui-shadcn-toast-viewport>
</proto-ui-shadcn-style-scope>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#toast-types-scope'), {
styleProfile: 'rhea',
});
setElementProps(document.querySelector('#toast-default'), {
defaultOpen: false,
type: 'default',
});
setElementProps(document.querySelector('#toast-error'), {
defaultOpen: false,
type: 'error',
politeness: 'assertive',
});
</script>通过 Action 触发应用层撤销或重试回调,激活后会先派发 action 事件,再请求关闭当前通知。
<proto-ui-shadcn-style-scope id="toast-action-scope">
<proto-ui-shadcn-button id="schedule-btn">
Schedule Meeting
</proto-ui-shadcn-button>
<proto-ui-shadcn-toast-viewport>
<proto-ui-shadcn-toast-root id="scheduled-toast">
<div class="flex items-start justify-between gap-4">
<div class="grid gap-1">
<proto-ui-shadcn-toast-title>
Meeting scheduled
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Design review set for Tuesday at 2:00 PM.
</proto-ui-shadcn-toast-description>
</div>
<proto-ui-shadcn-toast-action id="undo-action">
Undo
</proto-ui-shadcn-toast-action>
</div>
</proto-ui-shadcn-toast-root>
</proto-ui-shadcn-toast-viewport>
</proto-ui-shadcn-style-scope>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
setElementProps(document.querySelector('#toast-action-scope'), {
styleProfile: 'rhea',
});
setElementProps(document.querySelector('#schedule-btn'), {
variant: 'outline',
});
setElementProps(document.querySelector('#scheduled-toast'), {
defaultOpen: false,
duration: 6000,
});
document
.querySelector('#schedule-btn')
.addEventListener('click', () => {
document
.querySelector('#scheduled-toast')
.getExposes()
.openToast();
});
</script>在异步任务执行期间保持 type="loading" 且 duration={0},任务完成后切换为结果通知。
<proto-ui-shadcn-style-scope id="toast-promise-scope">
<proto-ui-shadcn-button id="create-event-btn">
Create Event
</proto-ui-shadcn-button>
<proto-ui-shadcn-toast-viewport>
<proto-ui-shadcn-toast-root id="loading-toast">
<proto-ui-shadcn-toast-title>
Creating event…
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Syncing calendar details with participants.
</proto-ui-shadcn-toast-description>
</proto-ui-shadcn-toast-root>
<proto-ui-shadcn-toast-root id="done-toast">
<proto-ui-shadcn-toast-title>
Event created
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Invitations have been sent to all attendees.
</proto-ui-shadcn-toast-description>
</proto-ui-shadcn-toast-root>
</proto-ui-shadcn-toast-viewport>
</proto-ui-shadcn-style-scope>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const loading = document.querySelector('#loading-toast');
const done = document.querySelector('#done-toast');
setElementProps(document.querySelector('#toast-promise-scope'), {
styleProfile: 'rhea',
});
setElementProps(loading, {
defaultOpen: false,
duration: 0,
type: 'loading',
});
setElementProps(done, {
defaultOpen: false,
duration: 4000,
type: 'success',
});
document
.querySelector('#create-event-btn')
.addEventListener('click', () => {
loading.getExposes().openToast();
setTimeout(() => {
loading.getExposes().close('programmatic');
done.getExposes().openToast();
}, 1500);
});
</script>通过 Viewport 的 position(top-left、top-center、top-right、bottom-left、bottom-center、bottom-right)将通知栈锚定到任意视口角落或边缘,并可通过 dismissAll() 一键清空当前通知。
<proto-ui-shadcn-toast-viewport
position="top-center"
a11y-label="Positioned notifications"
>
<proto-ui-shadcn-toast-root type="info" duration="4000">
<proto-ui-shadcn-toast-title>
Toast (Top Center)
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Viewport position set to "top-center".
</proto-ui-shadcn-toast-description>
<proto-ui-shadcn-toast-close>
Dismiss
</proto-ui-shadcn-toast-close>
</proto-ui-shadcn-toast-root>
</proto-ui-shadcn-toast-viewport>语义着色与状态图标
Section titled “语义着色与状态图标”在 Root 上开启 richColors 可将内置状态图标(success、info、warning、error、loading)与语义表面配色结合。type="loading" 默认常驻直到状态切换或显式超时,type="error" 默认升级为 politeness="assertive"。
<proto-ui-shadcn-toast-viewport position="bottom-right" expand>
<proto-ui-shadcn-toast-root type="success" rich-colors>
<proto-ui-shadcn-toast-title>
Deployment succeeded
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Release 0.3.0 is live in production.
</proto-ui-shadcn-toast-description>
<proto-ui-shadcn-toast-close>
Dismiss
</proto-ui-shadcn-toast-close>
</proto-ui-shadcn-toast-root>
</proto-ui-shadcn-toast-viewport>传入 open 与 duration={0} 可由应用状态完全接管通知的打开与关闭。
<proto-ui-shadcn-style-scope id="toast-persistent-scope">
<proto-ui-shadcn-button id="open-persistent">
Show Persistent Toast
</proto-ui-shadcn-button>
<proto-ui-shadcn-toast-viewport>
<proto-ui-shadcn-toast-root id="persistent-toast">
<div class="flex items-start justify-between gap-4">
<div class="grid gap-1">
<proto-ui-shadcn-toast-title>
Connection lost
</proto-ui-shadcn-toast-title>
<proto-ui-shadcn-toast-description>
Changes are paused until you reconnect or dismiss this
notice.
</proto-ui-shadcn-toast-description>
</div>
<proto-ui-shadcn-toast-close>
Dismiss
</proto-ui-shadcn-toast-close>
</div>
</proto-ui-shadcn-toast-root>
</proto-ui-shadcn-toast-viewport>
</proto-ui-shadcn-style-scope>
<script type="module">
import { setElementProps } from '@proto.ui/adapter-web-component';
const root = document.querySelector('#persistent-toast');
setElementProps(
document.querySelector('#toast-persistent-scope'),
{ styleProfile: 'rhea' }
);
setElementProps(root, {
open: false,
duration: 0,
politeness: 'assertive',
type: 'warning',
});
document
.querySelector('#open-persistent')
.addEventListener('click', () =>
setElementProps(root, { open: true })
);
root.addEventListener('openChange', (event) =>
setElementProps(root, { open: event.detail.open })
);
</script>| 属性 | 类型 | 默认值 |
|---|---|---|
open | boolean | — |
defaultOpen | boolean | true |
duration | number | 5000(loading 默认 0) |
paused | boolean | false |
politeness | 'polite' 'assertive' | 'polite'(error 默认 'assertive') |
type | 'default' 'success' 'info' 'warning' 'error' 'loading' | 'default' |
dismissible | boolean | true |
richColors | boolean | false |
position | 'top-left' 'top-center' 'top-right' 'bottom-left' 'bottom-center' 'bottom-right' | 继承 Viewport |
appear | boolean | false |
enterDuration | number | 0 |
leaveDuration | number | 0 |
interrupt | 'reverse' 'wait' 'immediate' | 'reverse' |
styleProfile | ShadcnStyleId | 继承样式作用域 |
Viewport、Action 与 Close
Section titled “Viewport、Action 与 Close”| 属性 | 类型 | 默认值 |
|---|---|---|
Viewport.a11yLabel | string | 'Notifications' |
Viewport.hotkey | string | 'F8' |
Viewport.position | 'top-left' 'top-center' 'top-right' 'bottom-left' 'bottom-center' 'bottom-right' | 'bottom-right' |
Viewport.expand | boolean | false |
Viewport.visibleToasts | number | 3 |
Viewport.duration | number | — |
Viewport.swipeDirection | 'right' 'left' 'up' 'down' 'none' | 'right' |
Action.disabled | boolean | false |
Action.a11yLabel | string | — |
Action.keepOpen | boolean | false |
Close.disabled | boolean | false |
事件与暴露方法:
- 事件:
Root暴露openChange({ open: boolean; reason: string });Action暴露action(void)。 - 方法:
Root暴露openToast()、close(reason?)、pause()、resume();Viewport暴露focusFirst()、pauseAll()、resumeAll()、dismissAll(reason?)。