Kbd(键盘按键)
用于显示用户通过键盘输入的文本。
loading
安装
bash
npx shadcn@latest add kbd1
1. 将以下代码复制并粘贴到项目中。
tsx
import { cn } from "cn"
function Kbd({ className, ...props }: React.ComponentProps<"kbd">) {
return (
<kbd
data-slot="kbd"
className={cn(
"pointer-events-none inline-flex h-5 w-fit min-w-5 items-center justify-center gap-1 rounded-sm bg-muted px-1 font-sans text-xs font-medium text-muted-foreground select-none in-data-[slot=tooltip-content]:bg-background/20 in-data-[slot=tooltip-content]:text-background dark:in-data-[slot=tooltip-content]:bg-background/10 [&_svg:not([class*='size-'])]:size-3",
className
)}
{...props}
/>
)
}
function KbdGroup({ className, ...props }: React.ComponentProps<"div">) {
return (
<kbd
data-slot="kbd-group"
className={cn("inline-flex items-center gap-1", className)}
{...props}
/>
)
}
export { Kbd, KbdGroup }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
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
2. 根据项目配置调整导入路径。
用法
tsx
import { Kbd } from "@/components/ui/kbd"1
tsx
<Kbd>Ctrl</Kbd>1
组件结构
使用以下结构构建 Kbd 和 KbdGroup:
text
Kbd
KbdGroup
├── Kbd
└── Kbd1
2
3
4
2
3
4
按键组
使用 KbdGroup 组件将键盘按键分组。
loading
按钮
在 Button 组件内使用 Kbd 组件,可在按钮中显示键盘按键。
loading
工具提示
可以在 Tooltip 组件内使用 Kbd 组件,显示带键盘按键的工具提示。
loading
输入组
可以在 InputGroupAddon 组件内使用 Kbd 组件,在输入组中显示键盘按键。
loading
RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
loading
API 参考
Kbd
使用 Kbd 组件显示键盘按键。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | `` |
tsx
<Kbd>Ctrl</Kbd>1
KbdGroup
使用 KbdGroup 组件将 Kbd 组件分组。
| 属性 | 类型 | 默认值 |
|---|---|---|
className | string | `` |
tsx
<KbdGroup>
<Kbd>Ctrl</Kbd>
<Kbd>B</Kbd>
</KbdGroup>1
2
3
4
2
3
4