# 提示条

## 使用方法

`Banner` 用于持续展示需要用户关注或处理的消息，例如功能更新、操作结果或需要解决的问题。与会自动消失的 Toast 不同，它会一直留在界面上，直到用户处理或主动关闭：

```vue
<script setup lang="ts">
import { Banner } from '@astryx-vue/core/Banner'
</script>

<template>
  <Banner
    status="success"
    title="Changes saved"
    description="Your settings are up to date."
    is-dismissable
  />
</template>
```

主题在应用入口引入一次：

```ts
import '@astryx-vue/themes/neutral.css'
```

### 结构

- **通告框**: 把状态面和可选的内容面包在一起的外框。整条通告的抬升由它承担；抬升的卡片样式还会带上形成轮廓的圆角。
- **状态面**: 主要着色的那一面。它表达状态，并装着图标、标题、说明、操作和控件。
- **图标**: 按状态自动选择：信息、警告、错误、成功。
- **标题**: 主要信息。必填。
- **说明**: 标题下面的补充。
- **操作按钮**: 让用户针对这条信息做事的按钮，比如「查看」或「重试」。
- **关闭按钮**: 让用户关掉通告。设置 isDismissable 后出现。
- **内容面**: 状态面下面的第二层，放额外细节，比如一列错误。默认藏在展开和收起开关后面；把 collapsible 设为 false 就一直显示。

## 最佳实践

| 指引 | 实践 |
| --- | --- |
| 推荐 | 选择与消息相符的状态：更新用 info，注意事项用 warning，问题用 error，确认用 success。 |
| 推荐 | 页面内容中使用 card 容器。覆盖整页宽度的消息使用 section 容器。 |
| 推荐 | info 和 success 提示条可以关闭。error 提示条必须保留，直到用户解决问题。 |
| 推荐 | 标题保持简短、便于扫读。例如用 “Payment failed”，不用 “There was a problem processing your most recent payment.” |
| 避免 | 用提示条展示会自动消失的短暂消息。应改用 Toast。 |
| 避免 | 叠放同一状态的多条提示条。应把相关消息合并为一条。 |

## 示例

### 可折叠

同一条提示里放操作按钮、关闭按钮，以及默认展开的详情。详情写在默认插槽里。

```vue
<script setup lang="ts">
import { Banner } from '@astryx-vue/core/Banner'
import { Button } from '@astryx-vue/core/Button'
import { VStack } from '@astryx-vue/core/Stack'
import { Text } from '@astryx-vue/core/Text'
</script>

<template>
  <Banner
    status="warning"
    title="Configuration changes detected"
    description="Review the changes before they take effect."
    :default-is-open="true"
    is-dismissable
  >
    <template #end>
      <Button label="Review" variant="secondary" size="sm" />
    </template>
    <VStack :gap="2">
      <Text type="supporting" color="secondary">
        Changed settings:
      </Text>
      <ul class="banner-preview-list">
        <li>Authentication method updated</li>
        <li>Rate limits modified</li>
      </ul>
    </VStack>
  </Banner>
</template>
```

### 关闭

isDismissable 让用户读完后关掉提示。适合维护通知和功能介绍。

```vue
<script setup lang="ts">
import { Banner } from '@astryx-vue/core/Banner'
import { VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <VStack :gap="3">
    <Banner status="success" title="Deployment complete" description="Version 3.2.0 is now live in production." is-dismissable />
    <Banner status="warning" title="Scheduled maintenance tonight" description="The system will be briefly unavailable from 2:00–3:00 AM." is-dismissable />
    <Banner status="info" title="New feature available" description="Try the new dashboard layout in Settings." is-dismissable />
  </VStack>
</template>
```

### 浮动

elevation 设为 med 后，提示条浮在页面之上，读起来像一层悬空通知。

```vue
<script setup lang="ts">
import { Banner } from '@astryx-vue/core/Banner'
</script>

<template>
  <Banner
    status="info"
    title="You have unsaved changes"
    description="A raised banner reads as an overlay floating above the page."
    elevation="med"
  />
</template>
```

### 通栏

container 设为 section 后，提示条铺满整行，没有圆角，适合放在页面顶部。

```vue
<script setup lang="ts">
import { Banner } from '@astryx-vue/core/Banner'
import { Button } from '@astryx-vue/core/Button'
import { VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <VStack :gap="3">
    <Banner
      status="warning"
      title="Scheduled downtime"
      description="All services will be unavailable on Sunday from 2:00–4:00 AM."
      container="section"
      is-dismissable
    />
    <Banner
      status="info"
      title="Welcome to the new dashboard"
      description="We have redesigned the layout based on your feedback."
      container="section"
    >
      <template #end>
        <Button label="Take a tour" variant="secondary" size="sm" />
      </template>
    </Banner>
  </VStack>
</template>
```

### 状态

info、success、warning、error 四种状态，分别用于更新、完成、提醒和出错。

```vue
<script setup lang="ts">
import { Banner } from '@astryx-vue/core/Banner'
import { VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <VStack :gap="3">
    <Banner status="info" title="A new software update is available" description="Version 2.4.1 includes performance improvements and bug fixes." />
    <Banner status="success" title="Changes saved" description="Your profile information has been updated successfully." />
    <Banner status="warning" title="Storage almost full" description="You have used 90% of your available storage." />
    <Banner status="error" title="Build failed" description="3 tests did not pass. Check the logs for details." />
  </VStack>
</template>
```

### 操作

end 插槽放一个按钮，让用户能直接处理这条消息。

```vue
<script setup lang="ts">
import { Banner } from '@astryx-vue/core/Banner'
import { Button } from '@astryx-vue/core/Button'
import { VStack } from '@astryx-vue/core/Stack'
</script>

<template>
  <VStack :gap="3">
    <Banner status="info" title="Your trial expires in 3 days" description="Upgrade now to keep access to all features.">
      <template #end>
        <Button label="Upgrade" variant="secondary" size="sm" />
      </template>
    </Banner>
    <Banner status="warning" title="API key expires soon" description="Generate a new key before December 1 to avoid service interruption.">
      <template #end>
        <Button label="Renew key" variant="secondary" size="sm" />
      </template>
    </Banner>
    <Banner status="error" title="Payment failed" description="We could not process your last payment.">
      <template #end>
        <Button label="Retry" variant="secondary" size="sm" />
      </template>
    </Banner>
  </VStack>
</template>
```

## 使用建议

提示条的语气由 `status` 决定，选错状态会让用户误判消息的紧迫程度。下面按消息的性质说明该选哪个状态，以及两个容易忽略的用法。

- 一般的信息和更新使用 `info`。
- 操作成功的结果使用 `success`。
- 需要提醒用户注意的情况使用 `warning`。
- 阻止用户继续操作的问题使用 `error`。
- 用户必须读到的详情传入 `:collapsible="false"`，这样详情会始终显示，不需要再点开。详情很长、会把提示本身盖住时，保持默认可折叠。
- 用户操作之后才出现的结果，在这次操作的处理函数里用 `useAnnounce` 播报。

`status` 只决定颜色和图标，不会自己变成实时区域。需要一直被读屏当成状态时，显式写上 `role="status"`。只有真正紧急、必须打断当前朗读的内容才写 `role="alert"`。页面初次渲染时就已经在的内容，读屏不一定会再念一遍。

如果只是单个字段的校验消息，应改用 [FieldStatus](/components/field-status)。

## 属性

| 属性 | 类型 | 默认值 | 说明 |
| --- | --- | --- | --- |
| `status` | `'info' \| 'success' \| 'warning' \| 'error'` | — | 必需。消息状态。 |
| `title` | `string` | — | 必需。消息标题。 |
| `description` | `string` | — | 标题下方的说明。 |
| `icon` | `Component` | — | 自定义装饰图标。 |
| `isDismissable` | `boolean` | false | 显示关闭按钮。关闭后组件自行隐藏。 |
| `dismissLabel` | `string` | 跟随语言：关闭“标题” | 关闭按钮的无障碍名称。不写时，英文是 Dismiss 加标题，简体中文是关闭“标题”。提示文字只写关闭。 |
| `container` | `'card' \| 'section'` | card | card 使用圆角。section 使用直角。 |
| `elevation` | `'none' \| 'low' \| 'med' \| 'high'` | none | 整体阴影层级。 |
| `collapsible` | `boolean` | true | 默认插槽内容是否可折叠。 |
| `defaultIsOpen` | `boolean` | false | 未绑定 isOpen 时的初始展开状态。 |
| `v-model:isOpen` | `boolean` | — | 由父组件控制展开状态。 |
| `expandLabel` | `string` | 跟随语言：展开 | 展开按钮的无障碍名称和提示。英文默认 Expand。 |
| `collapseLabel` | `string` | 跟随语言：收起 | 收起按钮的无障碍名称和提示。英文默认 Collapse。 |

## 插槽

- 默认插槽：标题区域下方的详情。
- `title`：替换标题的视觉内容。仍需提供 `title` 属性。
- `description`：替换说明文本。
- `icon`：替换状态图标。图标仅作装饰。
- `end`：标题区域末尾的操作按钮。

标题、说明和详情插槽可包含块级内容。

## 展开与收起

- 默认插槽有内容时，组件显示展开按钮。
- 默认从收起状态开始。
- `defaultIsOpen` 只设置初始状态。
- `v-model:isOpen` 控制当前状态。
- `collapsible` 为 `false` 时，详情始终显示。
- 收起时，详情内容卸载。
- 父组件收起详情时，若焦点在详情内，焦点返回展开按钮。

```vue
<Banner v-model:is-open="open" status="info" title="Configuration changes">
  <p>Review the updated settings before continuing.</p>
</Banner>
```

## 关闭

- `isDismissable` 显示关闭按钮。
- 点击关闭后，组件自行隐藏。
- 组件触发 `dismiss` 事件。
- 关闭时，组件尝试将焦点返回进入提示前的元素。
- 需要再次显示时，重新挂载组件或改变 `key`。

## 事件与模板引用

- `update:isOpen(value)`：请求更新展开状态。
- `dismiss()`：关闭提示。
- `element`：根元素的 `HTMLDivElement | null`。关闭后为 `null`。

## 本地化

关闭、展开、收起跟着文档或应用当前的语言。英文是 Dismiss、Expand、Collapse；简体中文是关闭、展开、收起。需要改成别的说法时，再传 `dismissLabel`、`expandLabel`、`collapseLabel`。

## 主题

```css
.my-banner {
  --astryx-banner-info-background: #ecfdf5;
  --astryx-banner-info-foreground: #065f46;
}
```

- `--astryx-banner-info-background`：信息背景。
- `--astryx-banner-info-foreground`：信息文字和图标。
- `--astryx-banner-success-background`：成功背景。
- `--astryx-banner-success-foreground`：成功文字和图标。
- `--astryx-banner-warning-background`：警告背景。
- `--astryx-banner-warning-foreground`：警告文字和图标。
- `--astryx-banner-error-background`：错误背景。
- `--astryx-banner-error-foreground`：错误文字和图标。
- `--astryx-banner-content-background`：详情区域背景。
- `--astryx-banner-border`：详情区域边框。
- `--astryx-radius-container`：card 容器圆角。

Neutral 自动适配明暗模式。
