Alert 消息条

操作之后出现在页面内的消息条。

已发布

文章现在对所有人可见。
<script setup lang="ts">
  import { Alert, Button, Stack } from '@hina-ui/vue'
</script>

<template>
  <Stack class="min-h-48 w-full max-w-xl">
    <Alert tone="success" title="已发布" closable>文章现在对所有人可见。</Alert>
    <Alert tone="danger" title="发布失败">
      服务器暂时无法响应,草稿已保留。
      <template #actions>
        <Button size="sm" variant="soft" tone="danger">重试</Button>
      </template>
    </Alert>
  </Stack>
</template>
vue

用法

import { Alert } from '@hina-ui/vue'
ts

消息条用于告知刚刚发生的事件,例如保存成功、发布失败、需要重新登录。它出现在页面内容中,屏幕阅读器会主动朗读。写在内容里的固定提示使用 Callout,短暂浮出的反馈使用 Toast。

已保存到草稿箱。
<script setup lang="ts">
  import { Alert } from '@hina-ui/vue'
</script>

<template>
  <Alert class="w-full max-w-xl">已保存到草稿箱。</Alert>
</template>
vue

示例

色调

共六种色调,默认为 neutral。图标随色调变化,icon 设为 false 时不显示图标。

已保存到草稿箱。
新版本已经可用,刷新页面即可体验。
本页内容来自用户投稿,尚未经过审核。
文章已发布。
文章已发布。
<script setup lang="ts">
  import { Alert, Stack } from '@hina-ui/vue'
</script>

<template>
  <Stack class="w-full max-w-xl">
    <Alert>已保存到草稿箱。</Alert>
    <Alert tone="accent">新版本已经可用,刷新页面即可体验。</Alert>
    <Alert tone="info">本页内容来自用户投稿,尚未经过审核。</Alert>
    <Alert tone="success">文章已发布。</Alert>
    <Alert tone="warning">登录状态即将过期,请尽快保存。</Alert>
    <Alert tone="danger">发布失败,请稍后重试。</Alert>
    <Alert tone="success" :icon="false">文章已发布。</Alert>
  </Stack>
</template>
vue

自定义图标

icon 插槽替换默认的图标。

新版本已经可用,刷新页面即可体验。
<script setup lang="ts">
  import { Alert } from '@hina-ui/vue'
  import { Sparkles } from '@lucide/vue'
</script>

<template>
  <Alert tone="accent" class="w-full max-w-xl">
    <template #icon><Sparkles class="text-accent-text mt-0.5 size-5 shrink-0" /></template>
    新版本已经可用,刷新页面即可体验。
  </Alert>
</template>
vue

标题

title 显示在正文之前。

<script setup lang="ts">
  import { Alert } from '@hina-ui/vue'
</script>

<template>
  <Alert tone="warning" title="登录状态即将过期" class="w-full max-w-xl">
    请在五分钟内保存正在编辑的内容,否则需要重新登录。
  </Alert>
</template>
vue

可关闭

设置 closable 后右侧显示关闭按钮,关闭时触发 close 事件。v-model:open 可以控制显示与隐藏。

文章已发布。
<script setup lang="ts">
  import { ref } from 'vue'
  import { Alert, Button, Stack } from '@hina-ui/vue'

  const open = ref(true)
</script>

<template>
  <Stack class="min-h-28 w-full max-w-xl">
    <Alert v-model:open="open" tone="success" closable>文章已发布。</Alert>
    <Button variant="soft" tone="neutral" :disabled="open" class="self-start" @click="open = true">
      再次显示
    </Button>
  </Stack>
</template>
vue

操作

actions 插槽位于正文之后,用于放置一到两个按钮。

有新的评论

你的文章收到了 3 条新评论。
<script setup lang="ts">
  import { Alert, Button } from '@hina-ui/vue'
</script>

<template>
  <Alert tone="info" title="有新的评论" class="w-full max-w-xl">
    你的文章收到了 3 条新评论。
    <template #actions>
      <Button size="sm" variant="soft" tone="neutral">稍后</Button>
      <Button size="sm">查看</Button>
    </template>
  </Alert>
</template>
vue

行为

  • 消息条出现时展开并淡入,关闭时收合并淡出,周围内容平滑移动。
  • 首次渲染时不播放出现动画。

无障碍

  • danger 与 warning 的消息条角色为 alert,屏幕阅读器立即朗读;其余色调的角色为 status,在当前朗读结束后朗读。
  • 关闭按钮的名称为“关闭”。

API

Props

属性
类型
默认值
说明
tone
'neutral' | 'accent' | 'info' | 'success' | 'warning' | 'danger'
'neutral'
色调
title
string
—
标题
icon
boolean
true
是否显示图标
closable
boolean
false
是否显示关闭按钮
open
boolean
true
是否显示,支持 v-model:open
class
string
—
追加至消息条的类名

Events

事件
参数
说明
update:open
open: boolean
显示状态变化
close
—
点击了关闭按钮

Slots

插槽
说明
default
正文
icon
替换图标
actions
正文之后的操作区