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

4.4 KiB
Raw Blame History

title, description, component, group
title description component group
Kbd 键盘按键 展示键盘快捷键与按键组合 kbd 组件

用法

<script setup lang="ts">
import { Kbd, KbdAbbr, KbdContent } from "@heroui-vue/components";
</script>

组件结构

<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 包裹按键字母或文本。

示例

变体

导航按键

行内用法

说明文字

特殊按键

自定义样式

Tailwind CSS

全局 CSS

可使用 @layer components 指令自定义 Kbd 组件类。

@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 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 hv-。

CSS 类

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 按钮 · Link 链接