Composables

Kbd Element

Style keyboard input elements with configurable background, color, font, border radius, and padding using the useKbdElement composable.

Overview

The useKbdElement composable styles the kbd element for displaying keyboard shortcuts and key combinations. It creates a compact, visually distinct key cap appearance with configurable background, color, font, border radius, and padding.

useKbdElement

function useKbdElement(
    s: Styleframe,
    options?: WithThemes<KbdElementConfig>
): KbdElementResult
styleframe.config.ts
import { styleframe } from 'styleframe';
import { useKbdElement } from '@styleframe/theme';

const s = styleframe();

const {
    kbdBackground,
    kbdColor,
    kbdFontFamily,
    kbdFontSize,
    kbdBorderRadius,
    kbdPaddingBlock,
    kbdPaddingInline,
} = useKbdElement(s);

export default s;

The composable also sets display: inline-block on the kbd element. Nested kbd > kbd elements have their padding and font size reset to avoid double-styling compound key combinations like Ctrl+C.

Default Values

OptionDefaultVariableExport
background"#1e293b"--kbd--backgroundkbdBackground
color"#ffffff"--kbd--colorkbdColor
fontFamily"@font-family.mono"--kbd--font-familykbdFontFamily
fontSize"0.875em"--kbd--font-sizekbdFontSize
borderRadius"@border-radius.sm"--kbd--border-radiuskbdBorderRadius
paddingBlock"0.1875rem"--kbd--padding-blockkbdPaddingBlock
paddingInline"0.375rem"--kbd--padding-inlinekbdPaddingInline

The default configuration includes a dark theme that inverts the colors: background: "@color.white" and color: "@color.black".

Custom Values

styleframe.config.ts
import { styleframe } from 'styleframe';
import { useKbdElement } from '@styleframe/theme';

const s = styleframe();

const { kbdBackground, kbdColor } = useKbdElement(s, {
    background: '@color.gray-100',
    color: '@color.gray-800',
    borderRadius: '@border-radius.md',
    paddingBlock: '0.25rem',
    paddingInline: '0.5rem',
});

export default s;

Best Practices

  • Use high-contrast colors: Ensure sufficient contrast between background and text for readability.
  • Use a monospace font: The default monospace font helps users recognize keyboard shortcuts at a glance.
  • Customize per-theme: The default dark theme inverts colors automatically. Override the themes option if your dark mode needs different values.
  • Rely on the nested reset: Compound key combinations using nested kbd elements (e.g., <kbd><kbd>Ctrl</kbd>+<kbd>C</kbd></kbd>) are automatically handled.