Alert(提示)
显示引起用户注意的提示。
loading
安装
bash
npx shadcn@latest add alert1
1. 将以下代码复制到你的项目中。
tsx
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
const alertVariants = cva(
"group/alert relative grid w-full gap-0.5 rounded-lg border px-4 py-3 text-start text-sm has-data-[slot=alert-action]:relative has-data-[slot=alert-action]:pe-18 has-[>svg]:grid-cols-[auto_1fr] has-[>svg]:gap-x-2.5 *:[svg]:row-span-2 *:[svg]:translate-y-0.5 *:[svg]:text-current *:[svg:not([class*='size-'])]:size-4",
{
variants: {
variant: {
default: "bg-card text-card-foreground",
destructive:
"bg-card text-destructive *:data-[slot=alert-description]:text-destructive/90 *:[svg]:text-current",
},
},
defaultVariants: {
variant: "default",
},
}
)
function Alert({
className,
variant,
...props
}: React.ComponentProps<"div"> & VariantProps<typeof alertVariants>) {
return (
<div
data-slot="alert"
role="alert"
className={cn(alertVariants({ variant }), className)}
{...props}
/>
)
}
function AlertTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-title"
className={cn(
"font-medium group-has-[>svg]/alert:col-start-2 [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground",
className
)}
{...props}
/>
)
}
function AlertDescription({
className,
...props
}: React.ComponentProps<"div">) {
return (
<div
data-slot="alert-description"
className={cn(
"text-sm text-balance text-muted-foreground md:text-pretty [&_a]:underline [&_a]:underline-offset-3 [&_a]:hover:text-foreground [&_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.5 end-3", className)}
{...props}
/>
)
}
export { Alert, AlertTitle, AlertDescription, AlertAction }1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2. 根据你的项目配置调整导入路径。
用法
tsx
import {
Alert,
AlertAction,
AlertDescription,
AlertTitle,
} from "@/components/ui/alert"1
2
3
4
5
6
2
3
4
5
6
tsx
<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>1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
结构
按以下结构组合 Alert:
text
Alert
├── Icon
├── AlertTitle
├── AlertDescription
└── AlertAction1
2
3
4
5
2
3
4
5
基础
基础提示,包含图标、标题和说明。
loading
危险操作
使用 variant="destructive" 创建危险操作提示。
loading
操作
使用 AlertAction 为提示添加按钮或其他操作元素。
loading
自定义颜色
可以向 Alert 组件添加 border-chart-4/25 [&>svg]:text-chart-4 等自定义类来调整提示颜色。
loading
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading
API 参考
Alert
Alert 组件显示引起用户注意的提示。
| 属性 | 类型 | 默认值 |
|---|---|---|
variant | "default" | "destructive" | "default" |
AlertTitle
AlertTitle 组件显示提示的标题。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
AlertDescription
AlertDescription 组件显示提示的说明或内容。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |
AlertAction
AlertAction 组件显示操作元素(例如按钮),并将其绝对定位到提示的右上角。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | - |