Alert 警告提示
在頁面裡固定顯示一條提示,例如操作說明、風險提醒、處理結果。不會自動消失,可以加關閉按鈕。
引入
import { HuiAlert } from '@nscode/handy-ui'
基礎用法
title 是標題,type 預設是 'info'。
<script setup lang="ts">
import { HuiAlert } from '@nscode/handy-ui'
</script>
<template>
<HuiAlert title="這是一條提示" />
</template>
類型
type 有 success、warning、info、error、help 五種,顏色和左側圖示跟著變。
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="成功提示(success)" type="success" />
<HuiAlert title="警告提示(warning)" type="warning" />
<HuiAlert title="消息提示(info)" type="info" />
<HuiAlert title="錯誤提示(error)" type="error" />
<HuiAlert title="幫助提示(help)" type="help" />
</HuiSpace>
</template>
深色效果
effect 預設是 'light'(淺色底、彩色圖示)。改成 'dark' 後是實色背景、白色文字,沒有邊框。五種類型都有深色效果:
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="深色的成功提示(success)" type="success" effect="dark" />
<HuiAlert title="深色的警告提示(warning)" type="warning" effect="dark" />
<HuiAlert title="深色的消息提示(info)" type="info" effect="dark" />
<HuiAlert title="深色的錯誤提示(error)" type="error" effect="dark" />
<HuiAlert title="深色的幫助提示(help)" type="help" effect="dark" />
<HuiAlert
title="深色效果也能帶描述"
description="描述文字同樣是白色。"
type="success"
effect="dark"
closable
/>
</HuiSpace>
</template>
描述文字
description 顯示在標題下面,字級小一號。只有描述、沒有標題時,圖示和文字垂直居中。
描述內容也可以寫在預設插槽或 #description 插槽裡,優先級是:#description 插槽 > 預設插槽 > description 屬性。也就是說,預設插槽是描述內容的後備,寫了 #description 時預設插槽不顯示。
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="帶描述的提示" description="這裡是更詳細的說明文字。" type="success" />
<HuiAlert title="帶描述的警告" description="這裡是更詳細的說明文字。" type="warning" />
<HuiAlert title="帶描述的錯誤" description="這裡是更詳細的說明文字。" type="error" />
<HuiAlert description="只有描述、沒有標題" />
<HuiAlert title="用預設插槽寫描述">
<span>描述裡可以放<a href="https://example.com" target="_blank">鏈接</a>等內容。</span>
</HuiAlert>
<HuiAlert>
<template #title>
<strong>用插槽寫的標題</strong>
</template>
<template #description>用插槽寫的描述</template>
</HuiAlert>
<HuiAlert title="兩個插槽都寫" description="description 屬性不顯示">
<template #description>#description 插槽優先</template>
預設插槽也不顯示
</HuiAlert>
</HuiSpace>
</template>
可關閉
closable 預設是 false,加上後右側顯示關閉按鈕。close-text 把關閉圖示換成文字。
點關閉按鈕時立即觸發 close 事件,提示開始淡出;0.3 秒的淡出結束、元素移除後觸發 after-close。淡出期間再點關閉按鈕不會重複觸發。
關掉以後提示不會再出現,下面用“全部重新顯示”換一個 key 重新呈現:
<script setup lang="ts">
import { ref } from 'vue'
import { HuiAlert, HuiButton, HuiSpace } from '@nscode/handy-ui'
const log = ref<string[]>([])
const key = ref(0)
const handleClose = (event: MouseEvent) => {
log.value.push(`close(${event.type})`)
}
const handleAfterClose = () => {
log.value.push('after-close:已經從頁面上移除')
}
const reset = () => {
key.value++
log.value = []
}
</script>
<template>
<HuiSpace :key="key" direction="vertical" fill>
<HuiAlert
title="可以關閉的提示"
closable
@close="handleClose"
@after-close="handleAfterClose"
/>
<HuiAlert title="關閉按鈕顯示文字" type="success" closable close-text="知道了" />
<HuiAlert
title="帶描述的可關閉提示"
description="關閉按鈕和標題對齊。"
type="warning"
closable
/>
<HuiAlert title="深色的可關閉提示" type="error" effect="dark" closable />
</HuiSpace>
<HuiSpace wrap style="margin-top: 12px">
<HuiButton size="small" @click="reset">全部重新顯示</HuiButton>
<span>{{ log.length ? log.join(' → ') : '點第一條的關閉按鈕看事件' }}</span>
</HuiSpace>
</template>
對齊方式
align 設定圖示、標題和描述的對齊方式,有 left(預設)、center、right 三種。center 屬性是早期的寫法,等同於 align="center",兩個同時寫時以 center 為準。居中時關閉按鈕固定在右上角。
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="左對齊(left)" description="預設左對齊" />
<HuiAlert title="居中(center)" description="align 為 center" align="center" />
<HuiAlert title="右對齊(right)" description="align 為 right" align="right" />
<HuiAlert title="center 屬性" description="等同於 align 為 center" type="success" center />
<HuiAlert title="居中且可關閉" description="關閉按鈕在右上角" type="warning" center closable />
</HuiSpace>
</template>
圖示
show-icon預設是true,設為false不顯示圖示。icon-position是圖示的位置,'left'(預設)在內容左側,'right'在內容右側。#icon插槽替換預設圖示,位置同樣由icon-position決定;show-icon為false時插槽也不顯示。
<script setup lang="ts">
import { HuiAlert, HuiIcon, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="不顯示圖示" :show-icon="false" />
<HuiAlert title="圖示在左邊(left)" type="success" />
<HuiAlert title="圖示在右邊(right)" type="success" icon-position="right" />
<HuiAlert title="自訂圖示" type="warning">
<template #icon>
<HuiIcon name="star" size="md" />
</template>
</HuiAlert>
<HuiAlert title="自訂圖示放在右邊" type="help" icon-position="right">
<template #icon>
<HuiIcon name="bell" size="md" />
</template>
</HuiAlert>
</HuiSpace>
</template>
無邊框
border 預設是 true,設為 false 去掉邊框。淺色效果的五種類型都可以去掉邊框(深色效果本來就沒有邊框)。
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert title="有邊框(預設)" type="info" />
<HuiAlert title="沒有邊框的成功提示" type="success" :border="false" />
<HuiAlert title="沒有邊框的警告提示" type="warning" :border="false" />
<HuiAlert title="沒有邊框的消息提示" type="info" :border="false" />
<HuiAlert title="沒有邊框的錯誤提示" type="error" :border="false" />
<HuiAlert title="沒有邊框的幫助提示" type="help" :border="false" />
</HuiSpace>
</template>
跑馬燈
marquee 為 true 時,描述文字排成一行,從右向左循環捲動,每輪 10 秒,長文字也能完整滾過去。標題不捲動。
<script setup lang="ts">
import { HuiAlert, HuiSpace } from '@nscode/handy-ui'
</script>
<template>
<HuiSpace direction="vertical" fill>
<HuiAlert
type="warning"
marquee
description="系統將于今晚 22:00 至 24:00 停機維護,請提前保存工作內容。"
/>
<HuiAlert
title="公告"
type="info"
effect="dark"
marquee
description="帶標題時只有描述捲動,標題固定在上面。"
/>
</HuiSpace>
</template>
Props
| 屬性 | 類型 | 預設值 | 說明 |
|---|---|---|---|
| title | string | '' | 標題。也可以用 #title 插槽 |
| description | string | '' | 描述文字。#description 插槽和預設插槽都會覆蓋它 |
| type | 'success' | 'warning' | 'info' | 'error' | 'help' | 'info' | 類型,決定顏色和圖示 |
| effect | 'light' | 'dark' | 'light' | 淺色或深色效果 |
| closable | boolean | false | 是否顯示關閉按鈕 |
| closeText | string | '' | 關閉按鈕的文字,不為空時代替關閉圖示 |
| align | 'left' | 'center' | 'right' | 'left' | 對齊方式 |
| center | boolean | false | 是否居中,等同於 align="center",同時寫時以它為準 |
| showIcon | boolean | true | 是否顯示圖示 |
| iconPosition | 'left' | 'right' | 'left' | 圖示在內容的左邊還是右邊 |
| border | boolean | true | 是否顯示邊框 |
| marquee | boolean | false | 描述文字是否單行循環捲動 |
Events
| 事件 | 回呼參數 | 說明 |
|---|---|---|
| close | (event: MouseEvent) | 點關閉按鈕時立即觸發,參數是點擊事件 |
| after-close | — | 關閉的淡出動畫結束、提示從頁面上移除後觸發 |
Slots
| 插槽 | 說明 |
|---|---|
| default | 描述內容的後備。寫了 #description 插槽時不顯示 |
| title | 標題,覆蓋 title 屬性 |
| description | 描述內容,覆蓋預設插槽和 description 屬性 |
| icon | 替換預設圖示,位置由 iconPosition 決定。showIcon 為 false 時不顯示 |
類型
import type {
AlertType,
AlertEffect,
AlertAlign,
AlertIconPosition,
AlertProps,
AlertEmits
} from '@nscode/handy-ui'
type AlertType = 'success' | 'warning' | 'info' | 'error' | 'help'
type AlertEffect = 'dark' | 'light'
type AlertAlign = 'left' | 'center' | 'right'
type AlertIconPosition = 'left' | 'right'
interface AlertProps {
title?: string
description?: string
type?: AlertType
closable?: boolean
center?: boolean
showIcon?: boolean
effect?: AlertEffect
closeText?: string
align?: AlertAlign
border?: boolean
marquee?: boolean
iconPosition?: AlertIconPosition
}
interface AlertEmits {
close: [event: MouseEvent]
afterClose: []
}