Installation
Install the dependencies
pnpm add cn class-variance-authorityCopy the component
Copy the following files into src/lib/components/ui/kbd.
<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><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>import type { HTMLAttributes } from 'svelte/elements';
export type KbdProps = HTMLAttributes<HTMLElement>;
export type KbdGroupProps = KbdProps;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>