Skip to content

Notify 消息通知

通知类组件,用于在页面顶部展示通知信息。

组件状态

基础用法

需要在页面中引入该组件,作为挂载点。

html
<wd-notify />
ts
import { useNotify } from '@/uni_modules/wot-ui'

const { showNotify, closeNotify } = useNotify()

// 3 秒后自动关闭
showNotify('通知内容')

// 主动关闭
closeNotify()

自定义配置

支持自定义颜色、弹出位置、展示时长以及是否显示关闭按钮。

ts
// 自定义颜色
showNotify({
  message: '自定义颜色',
  color: '#ad0000',
  background: '#ffe1e1'
})

// 自定义位置
showNotify({
  message: '自定义位置',
  position: 'bottom'
})

// 自定义时长
showNotify({
  message: '自定义时长',
  duration: 1000
})

// 显示关闭按钮
showNotify({
  message: '通知内容',
  closable: true,
  duration: 0
})

组件类型

通知类型

支持 primarysuccesswarningdanger 四种通知类型,默认为 danger

ts
// 主要通知
showNotify({ type: 'primary', message: '通知内容' })

// 成功通知
showNotify({ type: 'success', message: '通知内容' })

// 危险通知
showNotify({ type: 'danger', message: '通知内容' })

// 警告通知
showNotify({ type: 'warning', message: '通知内容' })

组件样式

悬浮通知

通过 variant 属性设置为 floating 可展示悬浮卡片式通知。悬浮通知具有独立的外边距、圆角与阴影,并且其前缀图标会自动适配当前 type 的状态主色。

ts
showNotify({
  type: 'primary',
  message: '通知内容',
  variant: 'floating',
  closable: true
})

内容形态

使用 Notify 组件

如果需要在 Notify 内嵌入组件或其他自定义内容,可以直接使用 Notify 组件,并使用默认插槽进行定制。

html
<wd-button type="primary" @click="showNotify">使用 Notify 组件调用</wd-button>
<wd-notify type="success" :safe-height="safeHeight" v-model:visible="visible">
  <wd-icon name="check-outline" size="inherit" color="inherit" />
  成功通知
</wd-notify>
ts
import { ref, onMounted } from 'vue'

let timer: ReturnType<typeof setTimeout>
export default {
  setup() {
    const visible = ref(false)
    const safeHeight = ref(0)

    const showNotify = () => {
      visible.value = true
      if (timer) clearTimeout(timer)
      timer = setTimeout(() => {
        visible.value = false
      }, 3000)
    }

    onMounted(() => {
      // #ifdef H5
      safeHeight.value = 44
      // #endif
    })

    return {
      visible,
      showNotify,
      safeHeight
    }
  }
}

Attributes

参数说明类型默认值
type类型,可选值为 primary success warning dangerNotifyTypedanger
message展示文案,支持通过\n换行string | number-
duration展示时长(ms),值为 0 时,notify 不会消失number3000
visible显示状态(支持 v-model)booleanfalse
position弹出位置,可选值为 top bottomNotifyPositiontop
color字体颜色string-
background背景颜色string-
z-index将组件的 z-index 层级设置为一个固定值number99
safe-height顶部安全高度number | string-
selector指定唯一标识string-
root-portal是否从页面中脱离出来,用于解决各种 fixed 失效问题booleanfalse
closable是否显示关闭按钮booleanfalse
variant展示形态,可选值为 filled floatingNotifyVariantfilled
custom-class根节点样式类名string-
custom-style根节点样式string-

Events

事件名说明参数
click点击时的回调函数(event: MouseEvent) => void
closed关闭时的回调函数() => void
opened展示后的回调函数() => void

Slots

name说明
default自定义通知内容

Methods

方法名称说明参数
showNotify展示提示NotifyOptions / string
closeNotify关闭提示-
setNotifyDefaultOptions修改默认配置,影响所有的 showNotify 调用NotifyOptions
resetNotifyDefaultOptions重置默认配置,影响所有的 showNotify 调用-

Options

调用 showNotifysetNotifyDefaultOptions 等方法时,支持传入以下选项:

参数说明类型默认值
type类型,可选值为 primary success warning dangerNotifyTypedanger
message展示文案,支持通过\n换行string | number-
duration展示时长(ms),值为 0 时,notify 不会消失number3000
position弹出位置,可选值为 top bottomNotifyPositiontop
color字体颜色string-
background背景颜色string-
zIndex将组件的 z-index 层级设置为一个固定值number99
safeHeight顶部安全高度number | string-
rootPortal是否从页面中脱离出来,用于解决各种 fixed 失效问题booleanfalse
closable是否显示关闭按钮booleanfalse
variant展示形态,可选值为 filled floatingNotifyVariantfilled
customClass根节点样式类名string-
customStyle根节点样式string-
onClick点击时的回调函数(event: MouseEvent) => void-
onClosed关闭时的回调函数() => void-
onOpened展示后的回调函数() => void-

主题定制

CSS 变量

组件提供了下列 CSS 变量,可用于自定义样式,使用方法请参考 ConfigProvider 全局配置

名称默认值描述
--wot-notify-text-color$text-white通知文字颜色
--wot-notify-padding$padding-tight $padding-loose通知内边距
--wot-notify-font-size$typography-body-size-large通知文字字号
--wot-notify-line-height$typography-body-line--height-size-large通知文字行高
--wot-notify-primary-bg$primary-1主色态背景色
--wot-notify-primary-color$primary-6主色态文字颜色
--wot-notify-success-bg$success-surface成功态背景色
--wot-notify-success-color$success-main成功态文字颜色
--wot-notify-danger-bg$danger-surface危险态背景色
--wot-notify-danger-color$danger-main危险态文字颜色
--wot-notify-warning-bg$warning-surface警告态背景色
--wot-notify-warning-color$warning-main警告态文字颜色
--wot-notify-icon-size$n-18图标尺寸
--wot-notify-icon-spacing$spacing-tight图标间距
--wot-notify-floating-bg$base-whitefloating 变体背景色
--wot-notify-floating-color$text-mainfloating 变体文字颜色
--wot-notify-floating-radius$radius-largefloating 变体圆角
--wot-notify-floating-margin-horizontal$spacing-loosefloating 变体左右间距
--wot-notify-floating-margin-vertical$spacing-loosefloating 变体上下间距
--wot-notify-floating-shadow0 12px 48px 16px rgba(39, 43, 59, 0.03), 0 9px 28px 8px rgba(39, 43, 59, 0.05), 0 5px 12px 4px rgba(39, 43, 59, 0.06)floating 变体阴影

源代码

文档
组件

Released under the MIT License.

Released under the MIT License.