键盘按键 · Kbd
键盘按键用于内容展示场景。下面的官方示例展示原生用法与状态,属性名保持上游 API。
安装与前置条件
npm install @astryxdesign/core@0.7.1 @astryxdesign/theme-neutral@0.7.1 @stylexjs/stylex@0.19.0
需要 React 19+,按顺序导入基础 CSS 和主题,并设置 Theme Provider。参见开始使用。
示例与使用方式
- 键盘按键 · 综合展示 1:首先体验这个示例,再查看代码理解组件的组合方式。
- 键盘按键 · 交互示例 2:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 键盘按键 · 交互示例 3:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
- 键盘按键 · 交互示例 4:可交互查看这一用法的行为,复制代码时保留它实际使用的属性与导入。
API 与上游详细参考
优先使用 0.7.1 官方中文说明;上游尚未提供中文的条目保留英文。属性名、类型及默认值保持原生 API,不把示例状态名称当成组件属性。
Kbd
Renders a keyboard shortcut as styled key badges. Use Kbd in tooltips, menus, and help text to show key combinations.
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| keys * | string | 键盘快捷键字符串。使用 "+" 分隔各按键。特殊按键:mod(Mac 上为 Cmd)、ctrl、alt、shift、enter、backspace、escape、tab、up、down、left、right、plus。别名:esc 等同于 escape,return 等同于 enter。 | |
| xstyle | StyleXStyles | 用于布局自定义的 StyleX 样式(边距、定位、尺寸)。必须是 stylex.create() 的值,这不能是 style={{}} 这样的内联样式对象。 | |
| className | string | 根元素的 CSS 类名。建议优先使用 xstyle 进行样式设置,className 用于与非 StyleX 系统的集成。 | |
| style | CSSProperties | 根元素的内联样式。建议优先使用 xstyle 进行样式设置,内联样式会绕过 StyleX 优化。 |
使用建议
- Place shortcuts near the action they trigger: in a tooltip, menu item, or inline instruction.
- Use mod instead of ctrl or cmd; it automatically adapts to the user's platform.
- 避免:Use Kbd as the only way to discover an action; shortcuts should supplement visible controls, not replace them.
主题 API
{
"targets": [
{
"className": "astryx-kbd"
}
]
}
组件结构
- Shortcut: Group that presents the complete keyboard shortcut and its accessible name.
- Key badge: Painted key badge rendered once for each key in the shortcut.