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.
Usage
import { Kbd, KbdGroup } from "@/components/ui/kbd";<div className="flex flex-col items-center gap-4">
<div className="flex gap-2">
<Kbd>K</Kbd>
<Kbd>⌘</Kbd>
<Kbd>⌃</Kbd>
<Kbd>⇧</Kbd>
</div>
<div className="flex gap-2">
<KbdGroup>
<Kbd>⌘</Kbd>
<span>+</span>
<Kbd>K</Kbd>
</KbdGroup>
</div>
</div>Demos
Button
Input Group
Kbd Group
Tooltip
Variant Outline
API Reference
Kbd
kbd
PropType
AttributeType
KbdGroup
div
PropType
AttributeType