Kbd

updated

Ctrl+C, Ctrl+V. That's the whole career. A keyboard key, and a group of keys for a shortcut.

Source
Demo
⌘⇧⌥⌃Ctrl+B

Press / to search, or Esc to close.

Open Props in the corner of the example to try size on the modifier keys.

xs to xl, md by default. Match the key to the text or control around it: xs in small print, lg and xl beside large buttons and inputs.

Installation

Usage

<script setup lang="ts">
import { Kbd, KbdGroup } from '@/components/ui/kbd'
</script>

<template>
  <KbdGroup>
    <Kbd>Ctrl</Kbd>
    <Kbd>B</Kbd>
  </KbdGroup>
</template>

Kbd is one key, and KbdGroup holds a whole shortcut. Separators like + or then are up to you: the group only spaces the keys.

Examples

Sequences

For a sequence, just list the keys. You know this one:

<template>
  <KbdGroup>
    <Kbd>↑</Kbd>
    <Kbd>↑</Kbd>
    <Kbd>↓</Kbd>
    <Kbd>↓</Kbd>
    <Kbd>←</Kbd>
    <Kbd>→</Kbd>
    <Kbd>←</Kbd>
    <Kbd>→</Kbd>
    <Kbd>B</Kbd>
    <Kbd>A</Kbd>
  </KbdGroup>
</template>

In a button

In a button

Inside a Button, or any other <button>, keys take the button's colours, so they fit every variant without extra classes.

A screen reader would read the keys as part of the label: "Save Ctrl S". Hide them and put the shortcut in aria-keyshortcuts instead:

<template>
  <Button aria-keyshortcuts="Control+S">
    Save
    <KbdGroup aria-hidden="true">
      <Kbd>Ctrl</Kbd>
      <Kbd>S</Kbd>
    </KbdGroup>
  </Button>
</template>

aria-keyshortcuts only describes the shortcut. Listening for the keys is up to your app.

API Reference

Both parts take a class that merges with their own. Icons inside a key are 12px unless you size them.

Data attributes

data-slotAttributesDescription
kbd—One key.
kbd-group—The keys of one shortcut.

Changelog

  1. registry 0.22.0

    4712efa — A Kbd inside a Button wrapped in TooltipTrigger as-child (or any other as-child trigger) keeps the button's colours, and an InputGroupButton wrapped the same way in a block addon keeps the addon's inset. The trigger's data-slot replaces the child's own, as in shadcn, so Kbd now keys on the <button> element (any button tints it, Toggle and Toolbar items included) and the block addon sets the inset on its children.

  2. registry 0.14.0

    2c0edc3 — Kbd takes a size, xs to xl (16 to 28px tall), with the label and icons scaled to match. md is the size it had before.

  3. registry 0.5.0

    59f9254 — Add Tooltip, from shadcn-vue, with its behaviour in @kappa-ui/core/tooltip: it opens once the pointer rests on the trigger (moving restarts the wait), on keyboard focus only, and on a long press on touch screens; tooltips under a TooltipProvider warm up together; update:open reports why it opened or closed; role="label" suits icon buttons; followCursor keeps it at the pointer. v-tooltip gives the same tooltip from a string or an options object, with the side as its argument and .label for icon buttons. Button with aria-disabled="true" now looks disabled while keeping focus and pointer events, and swallows its click. A Kbd inside a tooltip takes the tooltip's colours.

  4. core 0.4.0

    59f9254 — Add Tooltip, from shadcn-vue, with its behaviour in @kappa-ui/core/tooltip: it opens once the pointer rests on the trigger (moving restarts the wait), on keyboard focus only, and on a long press on touch screens; tooltips under a TooltipProvider warm up together; update:open reports why it opened or closed; role="label" suits icon buttons; followCursor keeps it at the pointer. v-tooltip gives the same tooltip from a string or an options object, with the side as its argument and .label for icon buttons. Button with aria-disabled="true" now looks disabled while keeping focus and pointer events, and swallows its click. A Kbd inside a tooltip takes the tooltip's colours.