Alert 消息条

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

已发布

文章现在对所有人可见。
import { Alert, Button, Stack } from '@hina-ui/react'

export default function Demo() {
  return (
    <Stack className="min-h-48 w-full max-w-xl">
      <Alert tone="success" title="已发布" closable>
        文章现在对所有人可见。
      </Alert>
      <Alert
        tone="danger"
        title="发布失败"
        actions={
          <Button size="sm" variant="soft" tone="danger">
            重试
          </Button>
        }
      >
        服务器暂时无法响应,草稿已保留。
      </Alert>
    </Stack>
  )
}
tsx

用法

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

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

已保存到草稿箱。
import { Alert } from '@hina-ui/react'

export default function Demo() {
  return <Alert className="w-full max-w-xl">已保存到草稿箱。</Alert>
}
tsx

示例

色调

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

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

export default function Demo() {
  return (
    <Stack className="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>
  )
}
tsx

自定义图标

icon 属性替换默认的图标。

新版本已经可用,刷新页面即可体验。
import { Sparkles } from 'lucide-react'
import { Alert } from '@hina-ui/react'

export default function Demo() {
  return (
    <Alert
      tone="accent"
      className="w-full max-w-xl"
      icon={<Sparkles className="text-accent-text mt-0.5 size-5 shrink-0" />}
    >
      新版本已经可用,刷新页面即可体验。
    </Alert>
  )
}
tsx

标题

title 显示在正文之前。

import { Alert } from '@hina-ui/react'

export default function Demo() {
  return (
    <Alert tone="warning" title="登录状态即将过期" className="w-full max-w-xl">
      请在五分钟内保存正在编辑的内容,否则需要重新登录。
    </Alert>
  )
}
tsx

可关闭

设置 closable 后右侧显示关闭按钮,关闭时调用 onClose。open / onOpenChange 可以控制显示与隐藏。

文章已发布。
'use client'

import { useState } from 'react'
import { Alert, Button, Stack } from '@hina-ui/react'

export default function Demo() {
  const [open, setOpen] = useState(true)

  return (
    <Stack className="min-h-28 w-full max-w-xl">
      <Alert open={open} onOpenChange={setOpen} tone="success" closable>
        文章已发布。
      </Alert>
      <Button
        variant="soft"
        tone="neutral"
        disabled={open}
        className="self-start"
        onClick={() => setOpen(true)}
      >
        再次显示
      </Button>
    </Stack>
  )
}
tsx

操作

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

有新的评论

你的文章收到了 3 条新评论。
import { Alert, Button } from '@hina-ui/react'

export default function Demo() {
  return (
    <Alert
      tone="info"
      title="有新的评论"
      className="w-full max-w-xl"
      actions={
        <>
          <Button size="sm" variant="soft" tone="neutral">
            稍后
          </Button>
          <Button size="sm">查看</Button>
        </>
      }
    >
      你的文章收到了 3 条新评论。
    </Alert>
  )
}
tsx

行为

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

无障碍

  • 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
是否显示,可受控
className
string
—
追加至消息条的类名

回调

回调
参数
说明
onOpenChange
open: boolean
显示状态变化
onClose
—
点击了关闭按钮

内容属性

属性
说明
children
正文
icon
替换图标
actions
正文之后的操作区