Files
heroui-vue/apps/docs/components/kbd.md
T
2026-10-21 18:35:24 +08:00

143 lines
4.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: Kbd 键盘按键
description: 展示键盘快捷键与按键组合
component: kbd
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { Kbd, KbdAbbr, KbdContent } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="kbd-basic" />
## 组件结构
```vue
<script setup lang="ts">
import { Kbd, KbdAbbr, KbdContent } from "@heroui-vue/components";
</script>
<template>
<Kbd>
<KbdAbbr key-value="command" />
<KbdContent>K</KbdContent>
</Kbd>
</template>
```
`KbdAbbr` 根据 `keyValue` 显示按键符号,并自动设置无障碍 `title`。`KbdContent` 包裹按键字母或文本。
## 示例
### 变体
<ComponentPreview name="kbd-variants" />
### 导航按键
<ComponentPreview name="kbd-navigation-keys" />
### 行内用法
<ComponentPreview name="kbd-inline-usage" />
### 说明文字
<ComponentPreview name="kbd-instructional-text" />
### 特殊按键
<ComponentPreview name="kbd-special-keys" />
## 自定义样式
### Tailwind CSS
<ComponentPreview name="kbd-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 Kbd 组件类。
```css
@layer components {
.hv-kbd {
@apply bg-gray-100 dark:bg-gray-800 border-gray-300;
}
.hv-kbd__abbr {
@apply font-bold;
}
.hv-kbd__content {
@apply text-sm;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
Kbd 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/kbd.css)):
#### 基础类 [!toc]
- `.hv-kbd` - 基础按键样式,含背景、边框与间距
- `.hv-kbd__abbr` - 修饰键的缩写元素
- `.hv-kbd__content` - 按键文本的内容包裹层
## API 参考
### Kbd
| Prop | 类型 | 默认值 | 描述 |
| --------- | --------------------------- | ----------- | ----------------------------- |
| `variant` | `'default' \| 'light'` | `'default'` | 按键视觉变体 |
| 默认插槽 | `Component` | - | 按键内容 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### KbdAbbr
| Prop | 类型 | 默认值 | 描述 |
| ---------- | --------------------------- | ------ | --------------------------------------------------------------------------- |
| `keyValue` | `KbdKey` | - | 要显示的按键;符号与 `title` 由映射表给出(如 `command` → `⌘` / `Command`) |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
### KbdContent
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ----------------------------- |
| 默认插槽 | `Component` | - | 按键文本内容 |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
`class` 遵循 Vue 的 class 绑定,可接收字符串、数组和对象,并与对应 slot 的基础类合并;不要使用 React 的 `className`。
### KbdContent Type
`keyValue` prop 可用的按键值:
| Modifier Keys | Special Keys | Navigation Keys | Function Keys |
| ------------- | ------------ | --------------- | ------------- |
| `command` | `enter` | `up` | `fn` |
| `shift` | `delete` | `down` | |
| `ctrl` | `escape` | `left` | |
| `option` | `tab` | `right` | |
| `alt` | `space` | `pageup` | |
| `win` | `capslock` | `pagedown` | |
| | `help` | `home` | |
| | | `end` | |
上游文档表格曾写作 `Kbd.Key` 与 `title`/`children`;实际实现与全部演示使用 `KbdAbbr` 的 `keyValue` 与 `KbdContent`。本库按实现迁移,并同时导出 `kbdKeysMap`、`kbdKeysLabelMap` 与 `KbdKey` 类型。
## 相关组件
[Button 按钮](./button) · [Link 链接](./link)