Notification 通知
在視窗的一角彈出帶標題的通知卡片,用來告訴使用者後臺任務完成、收到新訊息等不需要立即處理的資訊,幾秒後自動消失。用函式呼叫,不用在模板裡寫元件。
引入
import { HuiNotification } from '@nscode/handy-ui'
基礎用法
傳配置物件,title 是標題,message 是內容。預設出現在右上角,info 類型,4.5 秒後自動關閉,右上角有關閉按鈕。
只傳一段文字時,這段文字作為內容,沒有標題。
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const show = () => {
HuiNotification({ title: '導出完成', message: '報表已經生成,可以在下載中心查看。' })
}
const showText = () => {
HuiNotification('這是一條通知')
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="show">顯示通知</HuiButton>
<HuiButton @click="showText">只傳文字(沒有標題)</HuiButton>
</HuiSpace>
</template>
不同類型
有 success、warning、info、error 四種類型,左側圖示不同。可以用對應的快捷方法,也可以在配置裡寫 type。快捷方法也能只傳文字。
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" align="start">
<HuiSpace wrap>
<span class="row-label">快捷方法</span>
<HuiButton
type="success"
@click="HuiNotification.success({ title: '成功', message: '資料已同步' })"
>
成功(success)
</HuiButton>
<HuiButton
type="warning"
@click="HuiNotification.warning({ title: '警告', message: '存儲空間不足 10%' })"
>
警告(warning)
</HuiButton>
<HuiButton
type="info"
@click="HuiNotification.info({ title: '提示', message: '有新版本可用' })"
>
資訊(info)
</HuiButton>
<HuiButton
type="danger"
@click="HuiNotification.error({ title: '錯誤', message: '同步失敗,請重試' })"
>
錯誤(error)
</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">配置 type</span>
<HuiButton
@click="HuiNotification({ title: 'success', message: '資料已同步', type: 'success' })"
>
success
</HuiButton>
<HuiButton
@click="HuiNotification({ title: 'warning', message: '存儲空間不足 10%', type: 'warning' })"
>
warning
</HuiButton>
<HuiButton @click="HuiNotification({ title: 'info', message: '有新版本可用', type: 'info' })">
info
</HuiButton>
<HuiButton
@click="HuiNotification({ title: 'error', message: '同步失敗,請重試', type: 'error' })"
>
error
</HuiButton>
</HuiSpace>
<HuiSpace wrap>
<span class="row-label">只傳文字</span>
<HuiButton @click="HuiNotification.success('資料已同步')">success</HuiButton>
<HuiButton @click="HuiNotification.warning('儲存空間不足 10%')">warning</HuiButton>
<HuiButton @click="HuiNotification.info('有新版本可用')">info</HuiButton>
<HuiButton @click="HuiNotification.error('同步失敗,請重試')">error</HuiButton>
</HuiSpace>
</HuiSpace>
</template>
<style scoped>
.row-label {
display: inline-block;
width: 72px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
快捷方法傳配置物件時,type 以方法名為準,配置裡的 type 不生效。
位置
position 有 top-right(預設)、top-left、bottom-right、bottom-left 四種。每個位置的通知各自排列,互不影響。
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
import type { NotificationPosition } from '@nscode/handy-ui'
const show = (position: NotificationPosition) => {
HuiNotification({ title: position, message: `出現在窗口的 ${position}`, position })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="show('top-right')">右上(top-right,預設)</HuiButton>
<HuiButton @click="show('top-left')">左上(top-left)</HuiButton>
<HuiButton @click="show('bottom-right')">右下(bottom-right)</HuiButton>
<HuiButton @click="show('bottom-left')">左下(bottom-left)</HuiButton>
</HuiSpace>
</template>
多條通知和 offset
同一位置連續彈出多條時:頂部的位置從上往下排,後彈出的在下面;底部的位置從下往上排,後彈出的在上面。offset 是這條通知和同一位置上一條之間的距離,排在第一條時是和視窗上邊(top-*)或下邊(bottom-*)的距離,預設 16(像素)。有一條關閉後,其餘的依次移動補位。通知和視窗左右邊緣的距離固定是 16 像素。
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const showTop = () => {
HuiNotification({ title: '第 1 條', message: '離窗口頂部 16px' })
HuiNotification({ title: '第 2 條', message: '離上一條 60px', offset: 60 })
}
const showBottom = () => {
HuiNotification({ title: '第 1 條', message: '先彈出,在最下面', position: 'bottom-right' })
HuiNotification({ title: '第 2 條', message: '後彈出,排在上面', position: 'bottom-right' })
HuiNotification({
title: '第 3 條',
message: '離上一條 40px',
position: 'bottom-right',
offset: 40
})
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="showTop">右上角兩條(第二條 offset 60)</HuiButton>
<HuiButton @click="showBottom">右下角三條(從下往上排)</HuiButton>
</HuiSpace>
</template>
顯示時長和手動關閉
duration 是顯示多少毫秒後自動關閉,預設 4500,為 0 時不自動關閉。滑鼠移到通知上時暫停計時,移開後重新計時。showClose 預設是 true,設為 false 時不顯示關閉按鈕。
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="HuiNotification({ title: '2 秒', message: '2 秒後關閉', duration: 2000 })">
2 秒
</HuiButton>
<HuiButton @click="HuiNotification({ title: '4.5 秒', message: '4.5 秒後關閉' })">
4.5 秒(預設)
</HuiButton>
<HuiButton
@click="HuiNotification({ title: '不自動關閉', message: '點右上角的 × 關閉', duration: 0 })"
>
不自動關閉(0)
</HuiButton>
<HuiButton
@click="
HuiNotification({
title: '沒有關閉按鈕',
message: '3 秒後自動關閉',
showClose: false,
duration: 3000
})
"
>
不顯示關閉按鈕
</HuiButton>
</HuiSpace>
</template>
每次呼叫都回傳一個 NotificationInstance,呼叫它的 close() 可以隨時關閉這條通知。
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
import type { NotificationInstance } from '@nscode/handy-ui'
let instance: NotificationInstance | null = null
const open = () => {
instance = HuiNotification({
title: '正在上傳',
message: '上傳完成前不會自動關閉',
duration: 0,
showClose: false
})
}
const close = () => {
instance?.close()
instance = null
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="open">打開</HuiButton>
<HuiButton @click="close">關閉</HuiButton>
</HuiSpace>
</template>
點擊和關閉回呼
onClick在點擊通知時呼叫,點關閉按鈕不會呼叫它。onClose在通知關閉、淡出動畫結束後呼叫一次。自動關閉、點關閉按鈕、呼叫close()或closeAll()都會呼叫。
點擊通知本身不會關閉通知,需要時在 onClick 裡關閉。
<script setup lang="ts">
import { ref } from 'vue'
import { HuiButton, HuiMessage, HuiNotification } from '@nscode/handy-ui'
import type { NotificationInstance } from '@nscode/handy-ui'
const status = ref('還沒有打開通知')
const open = () => {
status.value = '通知顯示中,點一下通知試試'
const instance: NotificationInstance = HuiNotification({
title: '新訊息',
message: '點這裡查看',
duration: 0,
onClick: () => {
HuiMessage.success('onClick:打開訊息中心')
instance.close()
},
onClose: () => {
status.value = `onClose 已呼叫(${new Date().toLocaleTimeString()})`
}
})
}
</script>
<template>
<HuiButton @click="open">打開通知</HuiButton>
<p style="margin-top: 12px">{{ status }}</p>
</template>
內容傳 VNode
message 除了字串,還可以傳用 h() 建立的 VNode。字串按純文字顯示,不會解析 HTML,寫在字串裡的標籤會原樣顯示出來。title 只能是字串。
<script setup lang="ts">
import { h } from 'vue'
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const showVNode = () => {
HuiNotification({
title: '審批通過',
message: h('span', ['申請單 ', h('b', 'SQ-0012'), ' 已通過審批'])
})
}
const showString = () => {
HuiNotification({ title: '審批通過', message: '申請單 <b>SQ-0012</b> 已通過審批' })
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="showVNode">VNode:加粗單號</HuiButton>
<HuiButton @click="showString">字串:標籤原樣顯示</HuiButton>
</HuiSpace>
</template>
關閉全部
HuiNotification.closeAll() 關閉所有位置上的通知,每條都有正常的淡出動畫,也會呼叫各自的 onClose。
<script setup lang="ts">
import { HuiButton, HuiNotification, HuiSpace } from '@nscode/handy-ui'
const openMany = () => {
HuiNotification.success({ title: '右上', message: '不會自動關閉', duration: 0 })
HuiNotification.warning({
title: '左上',
message: '不會自動關閉',
duration: 0,
position: 'top-left'
})
HuiNotification.info({
title: '右下',
message: '不會自動關閉',
duration: 0,
position: 'bottom-right'
})
HuiNotification.error({
title: '左下',
message: '不會自動關閉',
duration: 0,
position: 'bottom-left'
})
}
</script>
<template>
<HuiSpace wrap>
<HuiButton @click="openMany">四個角各彈一條</HuiButton>
<HuiButton type="primary" @click="HuiNotification.closeAll()">關閉全部</HuiButton>
</HuiSpace>
</template>
響應式呈現內容
傳入 () => VNode 後,顯示內容可以跟隨響應式資料更新。
<script setup lang="ts">
import { h, onBeforeUnmount, ref } from 'vue'
import { HuiNotification, HuiButton, HuiSpace } from '@nscode/handy-ui'
const count = ref(1)
let instance: { close: () => void } | undefined
const open = () => {
instance?.close()
instance = HuiNotification({
message: () => h('strong', '已處理 ' + count.value + ' 項'),
duration: 0,
showClose: true
})
}
onBeforeUnmount(() => instance?.close())
</script>
<template>
<HuiSpace
><HuiButton @click="open">顯示進度</HuiButton><HuiButton @click="count++">增加進度</HuiButton
><span>{{ count }}</span></HuiSpace
>
</template>
呼叫方式
| 寫法 | 說明 |
|---|---|
HuiNotification(options: NotificationOptions) | 按配置彈出一條通知 |
HuiNotification(message: string) | 彈出一條只有內容、沒有標題的 info 類型通知 |
HuiNotification.success(message | options) | 彈出 success 類型的通知 |
HuiNotification.warning(message | options) | 彈出 warning 類型的通知 |
HuiNotification.info(message | options) | 彈出 info 類型的通知 |
HuiNotification.error(message | options) | 彈出 error 類型的通知 |
HuiNotification.closeAll() | 關閉所有位置上的通知,沒有回傳值 |
除了 closeAll,其他寫法都回傳這條通知的 NotificationInstance。
在內容裡用路由、全域元件
message 傳的 VNode 或元件,用的是應用的上下文:能用全域註冊的元件、路由(<RouterLink>、useRouter())、Pinia、i18n 這類外掛,以及 app.provide 的值。
- 全量引入(
app.use(HandyUI))時自動設定好,不用另外寫; - 按需引入時,在入口檔案裡調一次
setAppContext(app):
// main.ts
import { createApp } from 'vue'
import { setAppContext } from '@nscode/handy-ui'
import App from './App.vue'
import router from './router'
const app = createApp(App)
app.use(router)
setAppContext(app)
app.mount('#app')
import { h } from 'vue'
import { RouterLink } from 'vue-router'
import { HuiNotification } from '@nscode/handy-ui'
HuiNotification({
title: '新訂單',
message: h(RouterLink, { to: '/orders' }, () => '去處理')
})
一個頁面裡有多個 Vue 應用(例如微前端)時,可以在這次呼叫的配置裡傳 appContext 指定用哪個應用,優先於預設的:HuiNotification({ message: '...', appContext: otherApp._context })。
HuiConfigProvider 是寫在元件樹裡的,函式呼叫讀不到它的 size、zIndex,和設沒設應用上下文無關。
配置項
| 配置項 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| title | string | '' | 標題,為空時不顯示標題行 |
| message | string | VNode | (() => VNode) | '' | 內容。字串按純文字顯示 |
| type | 'success' | 'warning' | 'info' | 'error' | 'info' | 類型,決定圖示 |
| duration | number | 4500 | 顯示多少毫秒後自動關閉,為 0 時不自動關閉 |
| showClose | boolean | true | 是否顯示關閉按鈕 |
| position | 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left' | 'top-right' | 出現在視窗的哪個角 |
| offset | number | 16 | 和同一位置上一條通知的距離(像素),排在第一條時是和視窗上邊或下邊的距離 |
| onClick | () => void | — | 點擊通知時呼叫,點關閉按鈕不呼叫 |
| onClose | () => void | — | 關閉的淡出動畫結束後呼叫 |
| appContext | AppContext | null | — | 呈現用的應用上下文,見“在內容裡用路由、全域元件”。不傳時用 app.use(HandyUI) 或 setAppContext(app) 設定的應用 |
回傳值
NotificationInstance:
| 屬性 | 類型 | 說明 |
|---|---|---|
| id | string | 這條通知的編號 |
| vm | HTMLElement | null | 通知的DOM元素;伺服端空句柄為null |
| props | NotificationProps | 這條通知最終使用的配置(沒傳的項是預設值) |
| close | () => void | 關閉這條通知,有淡出動畫。已經在關閉的通知再呼叫不做任何事 |
類型
import type {
NotificationType,
NotificationPosition,
NotificationProps,
NotificationOptions,
NotificationInstance,
NotificationFn
} from '@nscode/handy-ui'
import type { AppContext, VNode } from 'vue'
type NotificationType = 'success' | 'warning' | 'info' | 'error'
type NotificationPosition = 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
interface NotificationProps {
title?: string
message: string | VNode | (() => VNode)
type?: NotificationType
duration?: number
showClose?: boolean
position?: NotificationPosition
offset?: number
onClose?: () => void
onClick?: () => void
}
interface NotificationOptions extends Omit<NotificationProps, 'message'> {
title?: string
message?: string | VNode | (() => VNode)
/** 呈現用的應用上下文,不傳時用 app.use(HandyUI) 或 setAppContext(app) 設定的應用 */
appContext?: AppContext | null
}
interface NotificationInstance {
id: string
vm: HTMLElement | null
props: NotificationProps
close: () => void
}
interface NotificationFn {
(options: NotificationOptions): NotificationInstance
(message: string): NotificationInstance
success: (message: string | NotificationOptions) => NotificationInstance
warning: (message: string | NotificationOptions) => NotificationInstance
info: (message: string | NotificationOptions) => NotificationInstance
error: (message: string | NotificationOptions) => NotificationInstance
closeAll: () => void
}
另外導出了通知本身的元件 HuiNotificationComponent,HuiNotification 內部用它呈現,一般不需要直接用。