﻿# Alert {#alert}

Display success, warning, information and error alerts with the original appearance, HTML title/description and three slots. Supports Chalk/OEM component themes.

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

## Themes / icons / closing / centering {#types}

### Themes / icons / closing / centering
```vue
<script setup lang="ts">
import { NfAlert } from '@94ai-plus/common-ui'

const errorTip = `Error data in total<span style='color: red'>${10}</span> Article`
const successTip = `A total of successful data<span style='color: green'>${10}</span> Article`
const infoTip = `Prompt data total<span style='color: blue'>${10}</span> Article`
const warningTip = `Total warning data<span style='color: orange'>${10}</span> Article`
</script>

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

## Supporting descriptions {#description}

### Supporting descriptions
```vue
<script setup lang="ts">
import { NfAlert } from '@94ai-plus/common-ui'

 const errorTip = `Error data in total<span style='color: red'>${10}</span> Article`
 const successTip = `A total of successful data<span style='color: green'>${10}</span> Article`
 const infoTip = `Prompt data total<span style='color: blue'>${10}</span> Article`
 const warningTip = `Total warning data<span style='color: orange'>${10}</span> Article`
</script>

<template>
<div class='nf-alert-container'>
  <nf-alert
    close-text="View details"
    :description='errorTip'
    title="success"
    show-icon
    type="success"
  />
  <br />
  <nf-alert
     close-text="View details"
     :description='successTip'
     title="success"
     show-icon
     effect='dark'
     type="success"
  />
  <br />
  <nf-alert
     :description='errorTip'
     title="Error"
     show-icon
     type="error"
  />
  <br />
  <nf-alert
     :description='errorTip'
     title="Error"
     show-icon
     effect='dark'
     type="error"
  />
  <br />
  <nf-alert
    :description='infoTip'
    title="Tips"
    close-text="View details"
    show-icon
    type="info"
  />
  <br />
 <nf-alert
   :description='infoTip'
    title="Tips"
    close-text="View details"
    show-icon
    effect='dark'
    type="info"
  />
  <br />
  <nf-alert
    :description='warningTip'
    title="warning"
    close-text="View details"
    show-icon
    type="warning"
  />
  <br />
  <nf-alert
    :description='warningTip'
    title="warning"
    close-text="View details"
    show-icon
    effect='dark'
    type="warning"
   />
  </div>
</template>
```

## Custom slots {#slots}

### Custom 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 = `A total of successful data<span style='color: green'>${10}</span> Article`
</script>

<template>
<div class="container">
    <nf-alert
      close-text="View details"
      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="close" @click="close"><ElIcon class="custom-close-icon"><Loading /></ElIcon></button></template>
    </nf-alert>
    <br />
    <nf-alert
      close-text="View details"
      title="success"
      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>success</span>
   </span></template>
      <template #close="{ close }"><button type="button" class="nf-alert__closebtn" aria-label="close" @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 example {#interactive}


See expanded example example-4 (AlertExamples).


## On-demand import {#installation}

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

app.use(NfAlert)
```

## Types {#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}

| parameters | Description | Type | Optional value | Default value |
| --- | --- | --- | --- | --- |
| title | Title, supports trusted HTML; can also be passed in via the title slot. | string | — | '' |
| description | Supporting text. It can also be passed in through the default slot, and the default slot takes priority. | string | — | '' |
| type | Alert type | string | success/warning/info/error | 'info' |
| closable | Can it be closed | boolean | — | true |
| close-text | Close button custom text | string | — | '' |
| showIcon | Whether to display the icon | boolean | — | true |
| center | Is the text centered? | boolean | — | false |
| effect | Visual theme | string | light/dark | 'light' |

## Event {#events}

| event name | Description | callback parameters |
| --- | --- | --- |
| close | Event triggered when alert is turned off | — |

## Slot {#slot-api}

| Name | Description | Slot parameters |
| --- | --- | --- |
| default | Description | — |
| title | Title content | — |
| close | Custom close control | { close: () =&gt; void } |

## Migration instructions {#migration}

Reserves 8 properties, no-argument close event, and three slots. Fixed the problem of the description disappearing when the default slot and description are passed in at the same time, and changed it to slot priority; fixed the problem of repeatedly closing the trigger event after being hidden. After closing, updating properties will not automatically reopen. Please remount via key.

The close button is changed to a native button, which supports keyboard operations, accessibility labels in eight languages, and does not submit the form. The icons are changed to built-in SVG and do not rely on Element UI fonts. The close slot has a new close callback, and the instance has a new close() method. The effect controls the appearance of the prompt box and is independent of the white and black theme of the document/drill field.

## 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 }
}
```
