Utilities
Kbd
Shows a key. With live, it squashes when you press that key.
Examples and variants
Live
Press ⌘ or K.
function KbdLive() {
return (
<KbdGroup>
<Kbd live keyName="Meta">⌘</Kbd>
<Kbd live>K</Kbd>
</KbdGroup>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/kbd.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cnCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/kbd.tsxkbd
components/ui/kbd.tsx"use client" import * as React from "react" import { cn } from "cn" /* Kbd: shows a key. With `live`, it squashes like the real key when that key is pressed on the page. Pass `keyName` for the KeyboardEvent.key to match ("k", "Enter", "Meta"); it defaults to the text when that's one character. */ function Kbd({ className, live = false, keyName, children, ...props }: React.ComponentProps<"kbd"> & { live?: boolean; keyName?: string }) { const [down, setDown] = React.useState(false) const match = (keyName ?? (typeof children === "string" && children.length === 1 ? children : "")).toLowerCase() React.useEffect(() => { if (!live || !match) return const onDown = (event: KeyboardEvent) => { if (event.key.toLowerCase() === match) setDown(true) } const onUp = (event: KeyboardEvent) => { if (event.key.toLowerCase() === match) setDown(false) } const reset = () => setDown(false) window.addEventListener("keydown", onDown) window.addEventListener("keyup", onUp) window.addEventListener("blur", reset) return () => { window.removeEventListener("keydown", onDown) window.removeEventListener("keyup", onUp) window.removeEventListener("blur", reset) } }, [live, match]) return ( <kbd data-slot="kbd" data-down={down || undefined} className={cn( "kbd inline-flex h-(--kbd-height) min-w-(--kbd-height) items-center justify-center rounded-(--kbd-corner) bg-(--kbd-container) px-1 font-sans text-xs font-medium text-muted-foreground shadow-1 select-none", className )} {...props} > {children} </kbd> ) } function KbdGroup({ className, ...props }: React.ComponentProps<"span">) { return <span data-slot="kbd-group" className={cn("inline-flex items-center gap-1", className)} {...props} /> } export { Kbd, KbdGroup }
Imports
What the examples on this page import:
import { InfoIcon } from "lucide-react"
import { Alert, AlertTitle } from "@/components/ui/alert"
import { AspectRatio } from "@/components/ui/aspect-ratio"
import { Button } from "@/components/ui/button"
import { Collapsible, CollapsibleContent, CollapsibleLabel, CollapsibleTrigger } from "@/components/ui/collapsible"
import { DirectionProvider } from "@/components/ui/direction"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { ScrollArea } from "@/components/ui/scroll-area"
import { Heading, Text } from "@/components/ui/typography"Anatomy
- 1Key · --kbd-height, --kbd-corner, --kbd-container.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
live | boolean | — | Reacts to the real key. |
keyName | string | — | KeyboardEvent.key to match. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--kbd-height | var(--space-5) | 20px | same |
--kbd-corner | var(--shape-xs) | 6px | same |
--kbd-container | var(--surface-container) | oklch(0.965 0.005 95) | oklch(0.12 0 0) |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Key press | Squashes down and back. | --duration-micro |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Uses <kbd>; spell out modifiers nearby for screen readers.
Code
"use client"
import * as React from "react"
import { cn } from "cn"
/*
Kbd: shows a key. With `live`, it squashes like the real key when that key
is pressed on the page. Pass `keyName` for the KeyboardEvent.key to match
("k", "Enter", "Meta"); it defaults to the text when that's one character.
*/
function Kbd({
className,
live = false,
keyName,
children,
...props
}: React.ComponentProps<"kbd"> & { live?: boolean; keyName?: string }) {
const [down, setDown] = React.useState(false)
const match = (keyName ?? (typeof children === "string" && children.length === 1 ? children : "")).toLowerCase()
React.useEffect(() => {
if (!live || !match) return
const onDown = (event: KeyboardEvent) => {
if (event.key.toLowerCase() === match) setDown(true)
}
const onUp = (event: KeyboardEvent) => {
if (event.key.toLowerCase() === match) setDown(false)
}
const reset = () => setDown(false)
window.addEventListener("keydown", onDown)
window.addEventListener("keyup", onUp)
window.addEventListener("blur", reset)
return () => {
window.removeEventListener("keydown", onDown)
window.removeEventListener("keyup", onUp)
window.removeEventListener("blur", reset)
}
}, [live, match])
return (
<kbd
data-slot="kbd"
data-down={down || undefined}
className={cn(
"kbd inline-flex h-(--kbd-height) min-w-(--kbd-height) items-center justify-center rounded-(--kbd-corner) bg-(--kbd-container) px-1 font-sans text-xs font-medium text-muted-foreground shadow-1 select-none",
className
)}
{...props}
>
{children}
</kbd>
)
}
function KbdGroup({ className, ...props }: React.ComponentProps<"span">) {
return <span data-slot="kbd-group" className={cn("inline-flex items-center gap-1", className)} {...props} />
}
export { Kbd, KbdGroup }