shadcn.io is not affiliated with official shadcn/ui
Shadcn Kbd for React and Tailwind
Styled representation of a single key.
K⌘⌃⇧
⌘+K
Installation
bunx --bun shadcn@latest add https://kit.dev/r/kbd.jsonpnpm dlx shadcn@latest add https://kit.dev/r/kbd.jsonnpx shadcn@latest add https://kit.dev/r/kbd.jsonyarn shadcn@latest add https://kit.dev/r/kbd.jsonInstall the following dependencies:
bun add @ark-ui/react tailwind-variantspnpm add @ark-ui/react tailwind-variantsnpm install @ark-ui/react tailwind-variantsyarn add @ark-ui/react tailwind-variantsCopy and paste the following code into your project.
"use client";
import { ark } from "@ark-ui/react/factory";
import type React from "react";
import { tv, type VariantProps } from "tailwind-variants";
import { cn } from "@/lib/utils";
const kbdVariants = tv({
base: [
"h-5 min-w-5",
"px-1",
"inline-flex items-center justify-center gap-1",
"select-none font-medium font-sans text-foreground text-xs",
"rounded-sm border border-transparent",
"pointer-events-none",
"in-data-[slot=tooltip-content]:bg-background/20 in-data-[slot=tooltip-content]:text-background",
"[&_svg:not([class*='size-'])]:size-3",
],
variants: {
variant: {
default: "bg-muted",
outline: "border border-border",
},
},
defaultVariants: {
variant: "default",
},
});
interface KbdProps
extends React.ComponentProps<typeof ark.kbd>,
VariantProps<typeof kbdVariants> {}
export const Kbd = (props: KbdProps) => {
const { variant = "default", className, ...rest } = props;
return (
<ark.kbd
className={cn(kbdVariants({ variant }), className)}
data-slot="kbd"
{...rest}
/>
);
};
export const KbdGroup = (props: React.ComponentProps<typeof ark.div>) => {
const { className, ...rest } = props;
return (
<ark.div
className={cn("inline-flex items-center gap-1", className)}
data-slot="kbd-group"
{...rest}
/>
);
};Update the import paths to match your project setup.
Anatomy
Kbd
KbdGroup
├── Kbd
└── KbdUsage
import {
Kbd,
KbdGroup
} from "@/components/ui";<KbdGroup>
<Kbd>⌘</Kbd>
<Kbd>K</Kbd>
</KbdGroup>Variant
Outline
Examples
Group
Use KbdGroup to group keys into a single shortcut.
Input Group
Use Kbd inside InputGroup addons to show shortcut hints next to inputs.
Button
Add shortcut hints to buttons with Kbd or KbdGroup.
Tooltip
Show keyboard shortcuts in Tooltip content.
Keyboard Table
For a Key / Description list of shortcuts, use Keyboard Table.
API Reference
Kbd
Styled representation of a single key.
| Prop | Type | Default |
|---|---|---|
variant | "default" | "outline" | "default" |
className | string | - |
asChild | boolean | false |
KbdGroup
Groups multiple keys into one shortcut.
| Prop | Type | Default |
|---|---|---|
className | string | - |
asChild | boolean | false |