Used to display textual user input from keyboard.

Basic
Ctrl ⌘K Ctrl + B
Modifier Keys
⌘ C
KbdGroup
Ctrl Shift P
Arrow Keys
↑ ↓ ← →
With Icons
With Icons and Text
Left Voice Enabled
InputGroup
Space
Tooltip
With samp
File

Installation

Install the dependencies

pnpm add cn class-variance-authority

Copy the component

Copy the following files into src/lib/components/ui/kbd.

src/lib/components/ui/kbd/Kbd.svelte
<script lang="ts">
  import type { KbdProps } from './types.js';
  import { cn } from 'cn';
  let { children, class: classProp, ...props }: KbdProps = $props();
</script>
<kbd data-slot="kbd" {...props} class={cn("bg-muted text-muted-foreground in-data-[slot=tooltip-content]:bg-background/20 in-data-[slot=tooltip-content]:text-background dark:in-data-[slot=tooltip-content]:bg-background/10 h-5 w-fit min-w-5 gap-1 rounded-sm px-1 font-sans text-xs font-medium [&_svg:not([class*='size-'])]:size-3 pointer-events-none inline-flex items-center justify-center select-none", classProp)}>
  {@render children?.()}
</kbd>
src/lib/components/ui/kbd/KbdGroup.svelte
<script lang="ts">
  import type { KbdGroupProps } from './types.js';
  import { cn } from 'cn';
  let { children, class: classProp, ...props }: KbdGroupProps = $props();
</script>
<kbd data-slot="kbd-group" {...props} class={cn('gap-1 inline-flex items-center', classProp)}>
  {@render children?.()}
</kbd>
src/lib/components/ui/kbd/types.ts
import type { HTMLAttributes } from 'svelte/elements';

export type KbdProps = HTMLAttributes<HTMLElement>;
export type KbdGroupProps = KbdProps;
src/lib/components/ui/kbd/index.ts
export { default as Kbd } from './Kbd.svelte';
export { default as KbdGroup } from './KbdGroup.svelte';
export type { KbdProps, KbdGroupProps } from './types.js';

Update the import paths

Change the imports to match your project setup.

Usage

<script lang="ts">
  import { Kbd, KbdGroup } from '$lib/components/ui/kbd';
</script>
<KbdGroup>
  <Kbd>Ctrl</Kbd>
  <Kbd>K</Kbd>
</KbdGroup>