章节
组件
import { CheckCircle2Icon, InfoIcon } from "lucide-react"
import {警告提示是一块行内提示:一个图标、一个标题、一段描述,再加上可选的操作,用 CSS 网格排布。它是一个零依赖的 role="alert" div——底层没有任何基元,两套后端完全一致——按新粗野主义的配方设计:粗边框、硬阴影、加粗字体,另有一条 status 维度负责错误、成功、警告和信息四种配色。
适合以下场景:
- 表单与请求错误——把校验失败或接口报错直接呈现在出问题的东西旁边。
- 成功与状态确认——「设置已保存」「试用即将到期」「已收到付款」,配色由
status属性决定。 - 带后续动作的提示——附带
AlertAction按钮(「启用」「撤销」「升级」)的警告或升级提醒。
安装#
pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/alert.jsonnpx shadcn@latest add https://neobrutalism.com/r/base/alert.jsonyarn dlx shadcn@latest add https://neobrutalism.com/r/base/alert.jsonbunx --bun shadcn@latest add https://neobrutalism.com/r/base/alert.json
将以下代码复制并粘贴到您的项目中。
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"
// CSS-grid layout (shadcn-style): drop an icon as a direct child and the alert
// auto-arranges into an icon column + a content column — no manual flex wrapper.
// `has-[>svg]` adds the icon column only when an icon is present. NeoBrutalist
// chrome: hard 2px border, square corners, hard offset shadow.
const alertVariants = cva(
cn(
"group/alert relative grid w-full grid-cols-[0_1fr] items-start gap-y-1 rounded border-2 px-4 py-3 text-left text-sm shadow-md",
"has-[>svg]:grid-cols-[1.25rem_1fr] has-[>svg]:gap-x-3",
"has-data-[slot=alert-action]:pr-14",
"[&>svg]:size-5 [&>svg]:translate-y-0.5 [&>svg]:shrink-0 [&>svg]:text-current"
),
{
variants: {
// shadcn's variant axis (kept for API compatibility). `solid` is a Neobrutalism
// addition.
variant: {
default:
"border-border bg-background text-foreground *:data-[slot=alert-description]:text-muted-foreground",
destructive:
"border-red-900 bg-red-300 text-red-900 *:data-[slot=alert-description]:text-red-900/80",
solid:
"border-border bg-foreground text-background *:data-[slot=alert-description]:text-background/80",
},
// Neobrutalism status axis — additive; overrides the variant's colors when set.
status: {
error:
"border-red-900 bg-red-300 text-red-900 *:data-[slot=alert-description]:text-red-900/80",
success:
"border-green-900 bg-green-300 text-green-900 *:data-[slot=alert-description]:text-green-900/80",
warning:
"border-yellow-900 bg-yellow-300 text-yellow-900 *:data-[slot=alert-description]:text-yellow-900/80",
info: "border-blue-900 bg-blue-300 text-blue-900 *:data-[slot=alert-description]:text-blue-900/80",
},
},
defaultVariants: {
variant: "default",
},
}
)
function Alert({
className,
variant,
status,
...props
}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
return (
<div
data-slot="alert"
role="alert"
className={cn(alertVariants({ variant, status }), className)}
{...props}
/>
)
}
function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-title"
className={cn(
"col-start-2 min-h-5 font-head text-base leading-tight tracking-tight [&_a]:underline [&_a]:underline-offset-3",
className
)}
{...props}
/>
)
}
function AlertDescription({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-description"
className={cn(
"col-start-2 grid justify-items-start gap-1 text-sm [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_p]:leading-relaxed [&_p:not(:last-child)]:mb-4",
className
)}
{...props}
/>
)
}
function AlertAction({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-action"
className={cn("absolute top-2 right-2", className)}
{...props}
/>
)
}
export { Alert, AlertTitle, AlertDescription, AlertAction, alertVariants }
更新导入路径以匹配您的项目设置。
用法#
import {
Alert,
AlertAction,
AlertDescription,
AlertTitle,
} from "@/components/ui/alert"<Alert>
<InfoIcon />
<AlertTitle>Heads up!</AlertTitle>
<AlertDescription>
You can add components and dependencies to your app using the cli.
</AlertDescription>
<AlertAction>
<Button variant="outline">Enable</Button>
</AlertAction>
</Alert>组合#
使用以下组合来构建 Alert:
Alert
├── Icon
├── AlertTitle
├── AlertDescription
└── AlertAction示例#
基础#
一个带有图标、标题和描述的基础提示。
import { CheckCircle2Icon } from "lucide-react"
import {危险#
使用 variant="destructive" 创建一个危险提示。
import { AlertCircleIcon } from "lucide-react"
import {操作#
使用 AlertAction 向提示添加按钮或其他操作元素。
import {
Alert,
AlertAction,自定义颜色#
您可以通过向 Alert 组件添加 bg-amber-50 dark:bg-amber-950 等自定义类来自定义提示的颜色。
import { AlertTriangleIcon } from "lucide-react"
import {RTL#
要在 Neobrutalism 中启用 RTL 支持,请参阅 RTL 配置指南。
"use client"
import * as React from "react"无障碍#
警告提示的根元素渲染 role="alert",这是一个隐式的 assertive 实时区域:元素一插入 DOM,屏幕阅读器就会朗读其中的内容。这条播报只在动态插入时触发——页面加载时就已存在的警告提示会被当成普通文本读出,所以需要被听见的提示应当条件式渲染。role="alert" 从不移动焦点;如果用户必须先做出响应才能继续,请改用警告对话框,并把 AlertAction 当作一条捷径,而不是唯一的出路。
API 参考#
Alert#
Alert 组件显示一段引起用户注意的提示。
| Prop | Type | Default |
|---|---|---|
variant | "default" | "destructive" | "default" |
AlertTitle#
AlertTitle 组件显示提示的标题。
| Prop | Type | Default |
|---|---|---|
className | string | - |
AlertDescription#
AlertDescription 组件显示提示的描述或内容。
| Prop | Type | Default |
|---|---|---|
className | string | - |
AlertAction#
AlertAction 组件显示一个绝对定位于提示右上角的操作元素(如按钮)。
| Prop | Type | Default |
|---|---|---|
className | string | - |