﻿# 提示信息 {#alert}

用于显示成功、警告、信息和错误提示，保留原有外观、HTML 标题／描述和三个插槽，支持 Chalk／OEM 组件主题。

<span id="close"></span>

## 主题/图标/关闭/居中 {#types}

### 主题/图标/关闭/居中
```vue
<script setup lang="ts">
import { NfAlert } from '@94ai-plus/common-ui'

const errorTip = `错误数据共<span style='color: red'>${10}</span> 条`
const successTip = `成功数据共<span style='color: green'>${10}</span> 条`
const infoTip = `提示数据共<span style='color: blue'>${10}</span> 条`
const warningTip = `警告数据共<span style='color: orange'>${10}</span> 条`
</script>

<template>
<div>
    <nf-alert
      close-text="查看详情"
      :title="successTip"
      show-icon
      type="success"
    />
    <br />
    <nf-alert
      :title="infoTip"
      close-text="查看详情"
      show-icon
      type="info"
      center
    />
    <br />
    <nf-alert
      :title="warningTip"
      close-text="查看详情"
      show-icon
      type="warning"
    />
    <br />
    <nf-alert
      :title="errorTip"
      show-icon
      type="error"
      center
    />
    <br />
    <nf-alert
      close-text="查看详情"
      :title="successTip"
      show-icon
      effect='dark'
      type="success"
    />
    <br />
    <nf-alert
      :title="infoTip"
      close-text="查看详情"
      show-icon
      effect='dark'
      center
      type="info"
    />
    <br />
    <nf-alert
      :title="warningTip"
      close-text="查看详情"
      show-icon
      effect='dark'
      type="warning"
    />
    <br />
    <nf-alert
      :title="errorTip"
      show-icon
      center
      effect='dark'
      type="error"
    />
  </div>
</template>
```

## 带有辅助性文字介绍 {#description}

### 带有辅助性文字介绍
```vue
<script setup lang="ts">
import { NfAlert } from '@94ai-plus/common-ui'

 const errorTip = `错误数据共<span style='color: red'>${10}</span> 条`
 const successTip = `成功数据共<span style='color: green'>${10}</span> 条`
 const infoTip = `提示数据共<span style='color: blue'>${10}</span> 条`
 const warningTip = `警告数据共<span style='color: orange'>${10}</span> 条`
</script>

<template>
<div class='nf-alert-container'>
  <nf-alert
    close-text="查看详情"
    :description='errorTip'
    title="成功"
    show-icon
    type="success"
  />
  <br />
  <nf-alert
     close-text="查看详情"
     :description='successTip'
     title="成功"
     show-icon
     effect='dark'
     type="success"
  />
  <br />
  <nf-alert
     :description='errorTip'
     title="错误"
     show-icon
     type="error"
  />
  <br />
  <nf-alert
     :description='errorTip'
     title="错误"
     show-icon
     effect='dark'
     type="error"
  />
  <br />
  <nf-alert
    :description='infoTip'
    title="提示"
    close-text="查看详情"
    show-icon
    type="info"
  />
  <br />
 <nf-alert
   :description='infoTip'
    title="提示"
    close-text="查看详情"
    show-icon
    effect='dark'
    type="info"
  />
  <br />
  <nf-alert
    :description='warningTip'
    title="警告"
    close-text="查看详情"
    show-icon
    type="warning"
  />
  <br />
  <nf-alert
    :description='warningTip'
    title="警告"
    close-text="查看详情"
    show-icon
    effect='dark'
    type="warning"
   />
  </div>
</template>
```

## 插槽定制化 {#slots}

### 插槽定制化
```vue
<script setup lang="ts">
import { NfAlert } from '@94ai-plus/common-ui'
import { ElIcon } from 'element-plus'
import { SuccessFilled, MagicStick, Loading, Star } from '@element-plus/icons-vue'

const successTip = `成功数据共<span style='color: green'>${10}</span> 条`
</script>

<template>
<div class="container">
    <nf-alert
      close-text="查看详情"
      show-icon
      type="success"
    >
      <template #title><span  >
        <ElIcon class="nf-alert__icon" style="margin-right: 8px"><SuccessFilled /></ElIcon>
        <ElIcon class="nf-alert__icon" style="margin-right: 8px"><MagicStick /></ElIcon>
        <span v-html="successTip"> </span>
      </span></template>
      <template #close="{ close }"><button type="button" class="nf-alert__closebtn" aria-label="关闭" @click="close"><ElIcon class="custom-close-icon"><Loading /></ElIcon></button></template>
    </nf-alert>
    <br />
    <nf-alert
      close-text="查看详情"
      title="成功"
      show-icon
      type="success"
    >
   <template #title><span  >
      <ElIcon class="nf-alert__icon" style="margin-right: 8px"><SuccessFilled /></ElIcon>
      <ElIcon class="nf-alert__icon" style="margin-right: 8px"><MagicStick /></ElIcon>
      <span>成功</span>
   </span></template>
      <template #close="{ close }"><button type="button" class="nf-alert__closebtn" aria-label="关闭" @click="close"><ElIcon class="custom-close-icon"><Star /></ElIcon></button></template>
      <span v-html="successTip" style='margin-left: 5px;'></span>
    </nf-alert>
  </div>
</template>
```

## 交互体验 {#interactive}


See expanded example example-4 (AlertExamples).


## 按需引入 {#installation}

```ts
import NfAlert from '@94ai-plus/nf-alert'
import '@94ai-plus/nf-theme-chalk/nf-alert.css'

app.use(NfAlert)
```

## 类型 {#typings}

```ts
import type { AlertProps, AlertType, AlertEffect, AlertInstance } from '@94ai-plus/nf-alert'
// AlertType: 'success' | 'warning' | 'info' | 'error'
// AlertEffect: 'light' | 'dark'
// AlertInstance: { close(): void }
```

## Attribute {#props}

| 参数 | 说明 | 类型 | 可选值 | 默认值 |
| --- | --- | --- | --- | --- |
| title | 标题，支持可信 HTML；也可通过 title 插槽传入。 | string | — | '' |
| description | 辅助性文字。也可通过默认 slot 传入，默认插槽优先。 | string | — | '' |
| type | 主题 | string | success/warning/info/error | 'info' |
| closable | 是否可关闭 | boolean | — | true |
| close-text | 关闭按钮自定义文本 | string | — | '' |
| showIcon | 是否显示图标 | boolean | — | true |
| center | 文字是否居中 | boolean | — | false |
| effect | 选择提供的主题 | string | light/dark | 'light' |

## Event {#events}

| 事件名称 | 说明 | 回调参数 |
| --- | --- | --- |
| close | 关闭alert时触发的事件 | — |

## Slot {#slot-api}

| 名称 | 说明 | 插槽参数 |
| --- | --- | --- |
| default | 描述 | — |
| title | 标题的内容 | — |
| close | 关闭的内容 | { close: () =&gt; void } |

## 迁移说明 {#migration}

保留 8 个属性、无参数 close 事件及三个插槽。修复默认插槽与 description 同时传入时描述消失的问题，改为插槽优先；修复隐藏后重复关闭触发事件的问题。关闭后更新属性不会自动重新打开，请通过 key 重新挂载。

关闭按钮改为原生 button，支持键盘操作、八种语言的无障碍标签，且不会提交表单。图标改为内置 SVG，不依赖 Element UI 字体。close 插槽新增 close 回调，实例新增 close() 方法。effect 控制提示框外观，与文档／演练场的白黑主题相互独立。

## Expanded component examples

### AlertExamples

Source: docs/.vitepress/theme/components/AlertExamples.vue

```vue
<script setup lang="ts">
import { ref } from 'vue'
import { NfAlert, type AlertEffect, type AlertInstance, type AlertType } from '@94ai-plus/common-ui'
import { useDocsLanguage } from '../locale'

const { english } = useDocsLanguage()
const type = ref<AlertType>('info')
const effect = ref<AlertEffect>('light')
const title = ref('Alert <strong>title</strong>')
const description = ref('Description <strong>HTML</strong>')
const closable = ref(true)
const closeText = ref('')
const showIcon = ref(true)
const center = ref(false)
const titleSlot = ref(false)
const descriptionSlot = ref(false)
const closeSlot = ref(false)
const instance = ref<AlertInstance>()
const version = ref(0)
const closes = ref(0)
const submits = ref(0)
</script>

<template>
  <section class="alert-examples">
    <div class="alert-controls">
      <label>type <select v-model="type" data-testid="alert-type"><option>info</option><option>success</option><option>warning</option><option>error</option></select></label>
      <label>effect <select v-model="effect" data-testid="alert-effect"><option>light</option><option>dark</option></select></label>
      <label>title <input v-model="title" data-testid="alert-title"></label>
      <label>description <input v-model="description" data-testid="alert-description"></label>
      <label>closeText <input v-model="closeText" data-testid="alert-close-text"></label>
      <label><input v-model="closable" type="checkbox" data-testid="alert-closable">closable</label>
      <label><input v-model="showIcon" type="checkbox" data-testid="alert-icon">showIcon</label>
      <label><input v-model="center" type="checkbox" data-testid="alert-center">center</label>
      <label><input v-model="titleSlot" type="checkbox" data-testid="alert-title-slot">title slot</label>
      <label><input v-model="descriptionSlot" type="checkbox" data-testid="alert-description-slot">default slot</label>
      <label><input v-model="closeSlot" type="checkbox" data-testid="alert-close-slot">close slot</label>
    </div>
    <form @submit.prevent="submits++">
      <NfAlert :key="version" ref="instance" data-testid="interactive-alert" :title="title" :description="description" :type="type" :effect="effect" :closable="closable" :close-text="closeText" :show-icon="showIcon" :center="center" @close="closes++">
        <template v-if="titleSlot" #title><strong data-testid="alert-custom-title">{{ english ? 'Custom title' : '自定义标题' }}</strong></template>
        <template v-if="descriptionSlot" #default><span data-testid="alert-custom-description">{{ english ? 'Slot description' : '插槽描述' }}</span></template>
        <template v-if="closeSlot" #close="{ close }"><button type="button" class="nf-alert__closebtn" data-testid="alert-custom-close" @click="close">{{ english ? 'Dismiss' : '收起' }}</button></template>
      </NfAlert>
    </form>
    <output data-testid="alert-result">close: {{ closes }}; submit: {{ submits }}</output>
    <button type="button" data-testid="alert-reset" @click="version++">{{ english ? 'Remount' : '重新显示' }}</button>
    <button type="button" data-testid="alert-expose-close" @click="instance?.close()">close()</button>
  </section>
</template>

<style scoped>
.alert-controls { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 20px; }
.alert-controls label { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; }
.alert-controls input:not([type='checkbox']) { width: 130px; }
.alert-controls input, .alert-controls select { border: 1px solid var(--site-border); border-radius: 4px; padding: 4px; background: var(--site-background); color: var(--site-text); }
.alert-examples output { display: block; margin-top: 12px; font-size: 12px; }
.alert-examples > button { font-size: 12px; margin: 8px 12px 0 0; cursor: pointer; }
</style>
```

Source: docs/.vitepress/theme/locale.ts

```ts
import { computed, inject, type InjectionKey, type Ref } from 'vue'
import { withBase } from 'vitepress'
import { localizePath, type LocaleName } from '../../../packages/common-locale/src/languages'
import { additionalMessages } from '../locales/ui'
export { languageDefinitions, localeFromPath, isLocaleName } from '../../../packages/common-locale/src/languages'

export type DocsLanguage = LocaleName
export const languageStorageKey = 'common-ui-plus:docs-language'
export const docsLanguageKey: InjectionKey<Ref<DocsLanguage>> = Symbol('docs-language')

const zh = {
  brand: '94智能通用组件库', home: '94智能公共组件库首页', skip: '跳至正文',
  toggleSidebar: '切换侧边导航', closeNav: '关闭导航', navigation: '顶部导航', sidebar: '文档导航', outline: '当前页面目录',
  guide: '指南', packages: '分包集合', theme: '主题', language: '语言', links: '边缘库',
  chalk: 'Chalk 默认主题', oem: 'OEM 定制主题', playground: '演练场',
  dark: '切换为暗色模式', light: '切换为亮色模式',
  search: '搜索文档', searchPlaceholder: '搜索文档，索引至标题，定位至组件', noResults: '没有匹配结果',
  example: '示例', loadingDemo: '正在加载示例…', preview: '实时预览', actions: '示例操作',
  edit: '在线编辑', editTitle: '在演练场中编辑', copy: '复制代码', copied: '代码已复制',
  expand: '展开代码', collapse: '收起代码', switchJs: '切换为 JS', switchTs: '切换为 TS',
  editor: '示例代码编辑器', switchFailed: '语言切换失败，代码未更改：',
  manualCopy: '请展开代码后手动复制。', copyFailed: '无法访问剪贴板，请在展开的编辑器中手动复制。',
}
const en: typeof zh = {
  brand: '94AI Common UI', home: '94AI Common UI home', skip: 'Skip to content',
  toggleSidebar: 'Toggle sidebar', closeNav: 'Close navigation', navigation: 'Top navigation', sidebar: 'Documentation navigation', outline: 'On this page',
  guide: 'Guide', packages: 'Packages', theme: 'Theme', language: 'Language', links: 'Links',
  chalk: 'Chalk default theme', oem: 'OEM custom theme', playground: 'Playground',
  dark: 'Switch to dark mode', light: 'Switch to light mode',
  search: 'Search docs', searchPlaceholder: 'Search docs, headings and components', noResults: 'No matching results',
  example: 'Example', loadingDemo: 'Loading example…', preview: ' live preview', actions: 'Example actions',
  edit: 'Edit online', editTitle: 'Edit in playground', copy: 'Copy code', copied: 'Code copied',
  expand: 'Expand code', collapse: 'Collapse code', switchJs: 'Switch to JS', switchTs: 'Switch to TS',
  editor: 'Example code editor', switchFailed: 'Language switch failed; code unchanged: ',
  manualCopy: 'Expand the code and copy it manually.', copyFailed: 'Clipboard unavailable. Copy the code manually from the editor.',
}
export const docsMessages: Record<DocsLanguage, typeof zh> = { 'zh-CN': zh, 'en-US': en, ...additionalMessages }

export function localizedPath(path: string, locale: DocsLanguage): string {
  return localizePath(path, locale)
}

export function useDocsLanguage() {
  const locale = inject(docsLanguageKey)!
  const english = computed(() => locale.value !== 'zh-CN')
  const ui = computed(() => docsMessages[locale.value])
  const docLink = (path: string) => withBase(localizedPath(path, locale.value))
  return { locale, english, ui, docLink }
}
```
