Empty(空状态)
使用 Empty 组件显示空状态。
loading
安装
bash
npx shadcn@latest add empty1
1. 将以下代码复制并粘贴到项目中。
tsx
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"
function Empty({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty"
className={cn(
"flex w-full min-w-0 flex-1 flex-col items-center justify-center gap-4 rounded-lg border-dashed p-12 text-center text-balance",
className
)}
{...props}
/>
)
}
function EmptyHeader({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty-header"
className={cn("flex max-w-sm flex-col items-center gap-2", className)}
{...props}
/>
)
}
const emptyMediaVariants = cva(
"mb-2 flex shrink-0 items-center justify-center [&_svg]:pointer-events-none [&_svg]:shrink-0",
{
variants: {
variant: {
default: "bg-transparent",
icon: "flex size-10 shrink-0 items-center justify-center rounded-lg bg-muted text-foreground [&_svg:not([class*='size-'])]:size-6",
},
},
defaultVariants: {
variant: "default",
},
}
)
function EmptyMedia({
className,
variant = "default",
...props
}: React.ComponentProps<"div"> & VariantProps<typeof emptyMediaVariants>) {
return (
<div
data-slot="empty-icon"
data-variant={variant}
className={cn(emptyMediaVariants({ variant, className }))}
{...props}
/>
)
}
function EmptyTitle({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty-title"
className={cn(
"font-heading text-lg font-medium tracking-tight",
className
)}
{...props}
/>
)
}
function EmptyDescription({ className, ...props }: React.ComponentProps<"p">) {
return (
<div
data-slot="empty-description"
className={cn(
"text-sm/relaxed text-muted-foreground [&>a]:underline [&>a]:underline-offset-4 [&>a:hover]:text-primary",
className
)}
{...props}
/>
)
}
function EmptyContent({ className, ...props }: React.ComponentProps<"div">) {
return (
<div
data-slot="empty-content"
className={cn(
"flex w-full max-w-sm min-w-0 flex-col items-center gap-4 text-sm text-balance",
className
)}
{...props}
/>
)
}
export {
Empty,
EmptyHeader,
EmptyTitle,
EmptyDescription,
EmptyContent,
EmptyMedia,
}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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
2. 根据项目配置调整导入路径。
用法
tsx
import {
Empty,
EmptyContent,
EmptyDescription,
EmptyHeader,
EmptyMedia,
EmptyTitle,
} from "@/components/ui/empty"1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
tsx
<Empty>
<EmptyHeader>
<EmptyMedia variant="icon">
<Icon />
</EmptyMedia>
<EmptyTitle>No data</EmptyTitle>
<EmptyDescription>No data found</EmptyDescription>
</EmptyHeader>
<EmptyContent>
<Button>Add data</Button>
</EmptyContent>
</Empty>1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
组件结构
使用以下结构构建 Empty 空状态:
text
Empty
├── EmptyHeader
│ ├── EmptyMedia
│ ├── EmptyTitle
│ └── EmptyDescription
└── EmptyContent1
2
3
4
5
6
2
3
4
5
6
边框
使用 border 工具类创建带边框的空状态。
loading
背景
使用 bg-* 和 bg-gradient-* 工具类为空状态添加背景。
loading
头像
使用 EmptyMedia 组件在空状态中显示头像。
loading
头像组
使用 EmptyMedia 组件在空状态中显示头像组。
loading
InputGroup
可以在 EmptyContent 组件中添加 InputGroup 组件。
loading
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading
API 参考
Empty
空状态的主组件,包裹 EmptyHeader 和 EmptyContent 组件。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
tsx
<Empty>
<EmptyHeader />
<EmptyContent />
</Empty>1
2
3
4
2
3
4
EmptyHeader
EmptyHeader 组件包裹空状态中的媒体、标题和描述。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
tsx
<EmptyHeader>
<EmptyMedia />
<EmptyTitle />
<EmptyDescription />
</EmptyHeader>1
2
3
4
5
2
3
4
5
EmptyMedia
使用 EmptyMedia 组件在空状态中展示图标、图片等媒体,也可以用它展示头像等其他组件。
| 属性 | 类型 | 默认值 |
|---|---|---|
variant | "default" | "icon" | default |
className | string |
tsx
<EmptyMedia variant="icon">
<Icon />
</EmptyMedia>1
2
3
2
3
tsx
<EmptyMedia>
<Avatar>
<AvatarImage src="..." />
<AvatarFallback>CN</AvatarFallback>
</Avatar>
</EmptyMedia>1
2
3
4
5
6
2
3
4
5
6
EmptyTitle
使用 EmptyTitle 组件显示空状态的标题。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
tsx
<EmptyTitle>No data</EmptyTitle>1
EmptyDescription
使用 EmptyDescription 组件显示空状态的描述。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
tsx
<EmptyDescription>You do not have any notifications.</EmptyDescription>1
EmptyContent
使用 EmptyContent 组件显示空状态的内容,例如按钮、输入框或链接。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string |
tsx
<EmptyContent>
<Button>Add Project</Button>
</EmptyContent>1
2
3
2
3