Skip to content

Toast

短暂提示,会叠在一起。外层放 ToastProvider,用 Reka UI 的 useToastManager() 添加。status 会换上对应图标和配色。指针悬停或焦点在视口里时展开。side 决定位置和划走方向。

Installation

pnpm dlx @weme-ui/weme-ui add toast

Usage

Vueexamples/usage.vueExpand codeHide code
<script lang="ts" setup>
import type { ToastStatus } from 'reka-ui'
import type { ToastData } from '../toast.props'
import { useToastManager } from 'reka-ui'
import { defineComponent, h } from 'vue'
import Button from '../../button/button.vue'
import ToastProvider from '../toast-provider.vue'

const ToastButtons = defineComponent({
  setup() {
    const toast = useToastManager<ToastData>()

    function showToast(status?: ToastStatus) {
      toast.add({
        title: 'Hello',
        description: 'This is a toast',
        status,
        data: {
          dismissible: false,
          actions: [
            {
              label: 'Action',
              onClick: () => {
                // eslint-disable-next-line no-alert
                alert('Action clicked')
              },
            },
          ],
        },
      })
    }

    function showLoadingToast() {
      toast.promise(
        new Promise((resolve) => {
          setTimeout(() => {
            resolve('Hello')
          }, 4000)
        }),
        {
          loading: {
            title: 'Loading',
            description: 'This is a loading toast',
            duration: 0,
            data: {
              loadingIcon: 'ri:loader-3-line',
            },
          },
          success: () => ({
            title: 'Success',
            description: 'This is a success toast',
          }),
          error: () => ({
            title: 'Error',
            description: 'This is an error toast',
          }),
        },
      )
    }

    return () =>
      h('div', { class: 'flex flex-wrap gap-6 items-center' }, [
        h(
          Button,
          {
            onClick: () => showToast(),
          },
          () => 'Default',
        ),
        h(
          Button,
          {
            onClick: () => showToast('info'),
          },
          () => 'Info',
        ),
        h(
          Button,
          {
            onClick: () => showToast('success'),
          },
          () => 'Success',
        ),
        h(
          Button,
          {
            onClick: () => showToast('warning'),
          },
          () => 'Warning',
        ),
        h(
          Button,
          {
            onClick: () => showToast('error'),
          },
          () => 'Error',
        ),
        h(Button, { onClick: showLoadingToast }, () => 'Promise'),
      ])
  },
})
</script>

<template>
  <ToastProvider dismissible>
    <ToastButtons />
  </ToastProvider>
</template>

Props

PropTypeDefault
side'top-left' | 'top' | 'top-right' | 'bottom-right' | 'bottom' | 'bottom-left''bottom-right'
ToastProvider。视口位置。上沿划走为 up,下沿为 down,左侧为 left,右侧为 right。类型上的 swipeDirection 不会转发
'top-left' | 'top' | 'top-right' | 'bottom-right' | 'bottom' | 'bottom-left'
'bottom-right'
gapnumber16
ToastProvider。两条之间的间距(像素),并写入视口 --gap
number
16
orientation'horizontal' | 'vertical''horizontal'
ToastProvider 传给 Toaster 里的 Toast;写在 Toast 上则管这一条。vertical 时操作区落到文案下面
'horizontal' | 'vertical'
'horizontal'
closeIconIconifyIconProps['icon']'ri:close-line'
关闭图标(Iconify)。ToastProvider 未设时由 Toast 补默认值
IconifyIconProps['icon']
'ri:close-line'
loadingIconIconifyIconProps['icon']'ri:loader-line'
status 为 loading 时的图标(Iconify),并带旋转
IconifyIconProps['icon']
'ri:loader-line'
dismissibleboolean-
是否显示关闭按钮。Toaster 优先用 data.dismissible
boolean
-
labelstring'Notification'
ToastProvider。读屏用来称呼每条提示的文案(Reka UI)
string
'Notification'
durationnumber5000
自动关闭的毫秒数。Toast 上的值覆盖 ToastProvider。0 或 Infinity 不自动关;记录的 status 为 loading 时 Reka UI 也不自动关
number
5000
disableSwipebooleanfalse
ToastProvider。是否禁止划动关闭
boolean
false
swipeThresholdnumber50
ToastProvider。划动超过多少像素才关闭
number
50
limitnumber-
ToastProvider。同时可见的条数。超出的旧条目带 data-limited 和 inert,不会被删掉。未设则不限制
number
-
toastManagerGlobalToastManager-
ToastProvider。createToastManager() 的实例,方便在组件外添加
GlobalToastManager
-
asAsTag | Component'li'
Toast 根节点渲染为的元素或组件(Reka UI Primitive)
AsTag | Component
'li'
asChildbooleanfalse
Toast。合并到子元素上渲染(Reka UI composition)
boolean
false
openboolean-
Toast 受控开关;可用 v-model:open。传入 toast 时以记录的 open 为准
boolean
-
defaultOpenbooleantrue
Toast 非受控时的初始开关
boolean
true
forceMountboolean-
Toast。强制挂载,便于自己做动画
boolean
-
type'foreground' | 'background''foreground'
Toast 的读屏敏感度。foreground 立刻播报,background 礼貌播报。传入 toast 时以记录为准
'foreground' | 'background'
'foreground'
toastToastObject-
useToastManager() 的一条记录。Toaster 会传入。open、duration、type 以这条记录为准
ToastObject
-
iconIconifyIconProps['icon']-
图标(Iconify)。图标区域只在 loading 或有状态图标时渲染,这条属性目前到不了界面
IconifyIconProps['icon']
-
titlestring-
标题。无 title 插槽时作为内容;没有它时标题不渲染
string
-
descriptionstring-
描述。无 description 插槽时作为内容;没有它时描述不渲染
string
-
status'loading' | 'info' | 'success' | 'warning' | 'error' | (string & {})-
语义状态,覆盖 variant 并决定图标。info 为 ri:information-fill,success 为 ri:checkbox-circle-fill,warning 为 ri:alert-fill,error 为 ri:indeterminate-circle-fill。其它字符串不改配色
'loading' | 'info' | 'success' | 'warning' | 'error' | (string & {})
-
variant'default' | 'accent' | 'neutral' | 'info' | 'success' | 'warning' | 'error' | 'loading''default'
外观配色。有 status 时被状态配色盖掉
'default' | 'accent' | 'neutral' | 'info' | 'success' | 'warning' | 'error' | 'loading'
'default'
actionsToastActionProps[]-
操作按钮。每项是去掉 as、asChild、variant 的 ButtonProps,另有 Reka altText、closeOnClick。渲染时固定 variant="soft"、size="sm"。color 未传时,loading 用 'default',否则用当前配色。alt-text 用 label,没有则是 'Action'
ToastActionProps[]
-
expandedboolean-
Toaster 会传入视口是否展开。Toast 模板目前不读
boolean
-
classany-
Toast 根节点额外 class。Toaster 会合并自己的堆叠 class 和 data.class
any
-
uiPartial<ToastStyleSlots>-
覆盖 Toast slots 样式(root / container / icon / wrapper / content / title / description / actions / action / close)。Toaster 不传 data.ui
Partial<ToastStyleSlots>
-

Events

EventType
update:openvalue: boolean
Toast 开关变化时触发(含 v-model:open)。Toaster 在关闭时收起该条
value: boolean
escapeKeyDownevent: KeyboardEvent
按下 Escape 时触发。preventDefault 可以取消关闭
event: KeyboardEvent
pause-
自动关闭计时暂停(指针在视口上、视口聚焦或窗口失焦)
-
resume-
自动关闭计时恢复
-
swipeStartevent: SwipeEvent
开始划动。preventDefault 可以取消这次手势
event: SwipeEvent
swipeMoveevent: SwipeEvent
划动中。preventDefault 可以取消
event: SwipeEvent
swipeCancelevent: SwipeEvent
划动取消。preventDefault 可以取消
event: SwipeEvent
swipeEndevent: SwipeEvent
划动结束。未取消时会关闭
event: SwipeEvent

Slots

SlotDescription
defaultToastProvider 的子树。Toaster 和视口由组件自己渲染
ToastProvider 的子树。Toaster 和视口由组件自己渲染
iconToast 图标。作用域参数 { status }。仅 loading 或有状态图标时渲染;未传时用对应图标
Toast 图标。作用域参数 { status }。仅 loading 或有状态图标时渲染;未传时用对应图标
title标题内容。仅 title 有值时渲染;未传时回退到 title
标题内容。仅 title 有值时渲染;未传时回退到 title
description描述内容。仅 description 有值时渲染;未传时回退到 description
描述内容。仅 description 有值时渲染;未传时回退到 description
close关闭按钮内容。仅 dismissible 时渲染;未传时为 closeIcon
关闭按钮内容。仅 dismissible 时渲染;未传时为 closeIcon

CSS Variables

可在 *.style.ts 或 class 中使用,例如 bg-toast-width。

CSS VariableValue
--toast-width20rem
20rem

error.9 / success.7 等为语义色别名(色板阶),详见 Tokens。

Accessibility

基于 Reka UI ToastProvider、ToastViewport、ToastRoot。视口是 ol,外包一层 role="region",aria-label 默认是 Notifications (F8)。每条是 li,tabindex="0",带 data-state、data-swipe-direction。传入的 toast.status 会写到 data-status(单独的 status prop 不写这个属性)。播报节点是 role="alert":type 为 foreground 时 aria-live="assertive",background 时为 polite。关闭按钮是 type="button"。操作按钮是 ToastAction(asChild)里的 Button。

Keyboard Interactions

KeyDescription
F8把焦点移到通知视口(Reka UI ToastViewport 默认热键)
把焦点移到通知视口(Reka UI ToastViewport 默认热键)
Tab / Shift+Tab在视口内按从新到旧的顺序移动焦点
在视口内按从新到旧的顺序移动焦点
Escape关闭 toast;在 escapeKeyDown 里 preventDefault 可取消
关闭 toast;在 escapeKeyDown 里 preventDefault 可取消

Files

  • src/components/toast/toast.vue
  • src/components/toast/toast-provider.vue
  • src/components/toast/toaster.vue
  • src/components/toast/toast.props.ts
  • src/components/toast/toast.style.ts
  • src/components/toast/toast-provider.props.ts
  • src/components/toast/toast-provider.style.ts
  • src/components/toast/toaster.props.ts
  • src/components/toast/toast.uno.ts