Motion kbd
Five keys that answer a press: one tracks the real keyboard, and four react to hover and tap with a pop, a fade, a bounce and a glow. Press listens for a single scoped key and removes its listeners on cleanup, and every effect is gated on reduced motion.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/kbd-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/kbd-motion.tsx"use client"
import * as React from "react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Motion kbd family: press feedback. Press listens to a real keyboard event (scoped to a single key, removed in the effect cleanup); the others react to hover and tap through framer-motion. Colour comes ONLY from tokens, all motion is disabled with useReducedMotion, and no value depends on time or randomness. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
const box: Record<StyledSize, string> = {
sm: "h-5 min-w-5 px-1.5 text-xs",
md: "h-6 min-w-6 px-2 text-xs",
lg: "h-7 min-w-7 px-2.5 text-sm",
xl: "h-8 min-w-8 px-3 text-sm",
}
const base =
"inline-flex w-fit shrink-0 select-none items-center justify-center gap-1 whitespace-nowrap rounded-md border border-border bg-surface-2 font-mono font-medium leading-none text-foreground [&_svg]:size-3 [&_svg]:shrink-0 [&_i]:text-xs [&_i]:leading-none"
const spring = { type: "spring" as const, stiffness: 520, damping: 22 }
type Props = React.ComponentProps<"kbd"> & { size?: StyledSize }
/* Press: it reflects the pressed state from real keydown/keyup. keyName is scoped to
a single key; the listeners are removed in the effect cleanup (no leak). */
export function PressKbd({
className,
size = "md",
children,
keyName = "k",
...props
}: Props & { keyName?: string }) {
const [pressed, setPressed] = React.useState(false)
const reduce = useReducedMotion()
React.useEffect(() => {
const target = keyName.toLowerCase()
const down = (e: KeyboardEvent) => {
if (e.key.toLowerCase() === target) setPressed(true)
}
const up = (e: KeyboardEvent) => {
if (e.key.toLowerCase() === target) setPressed(false)
}
/* Sekme arkaplana giderse keyup gelmeyebilir; durumu sifirla. */
const reset = () => setPressed(false)
window.addEventListener("keydown", down)
window.addEventListener("keyup", up)
window.addEventListener("blur", reset)
return () => {
window.removeEventListener("keydown", down)
window.removeEventListener("keyup", up)
window.removeEventListener("blur", reset)
}
}, [keyName])
return (
<kbd
data-slot="styled-kbd"
data-pressed={pressed ? "true" : undefined}
className={cn(
base,
box[size],
"shadow-[0_2px_0_0_color-mix(in_oklab,var(--color-foreground)_18%,transparent)]",
"transition-all duration-(--motion-fast) ease-(--motion-ease) motion-reduce:transition-none",
/* The pressed state is always readable through colour and shadow; the downward movement is added ONLY when reduced-motion is off, so motion goes away without the feedback disappearing. */
"data-[pressed=true]:bg-secondary data-[pressed=true]:shadow-none",
!reduce && "data-[pressed=true]:translate-y-0.5",
className
)}
{...props}
>
{children ?? keyName.toUpperCase()}
</kbd>
)
}
/* Pop: grows on hover and pulls back on tap (spring). */
export function PopKbd({ className, size = "md", children, ...props }: Props) {
const reduce = useReducedMotion()
return (
<motion.kbd
data-slot="styled-kbd"
className={cn(base, box[size], className)}
whileHover={reduce ? undefined : { scale: 1.1 }}
whileTap={reduce ? undefined : { scale: 0.92 }}
transition={spring}
{...(props as React.ComponentProps<typeof motion.kbd>)}
>
{children ?? "K"}
</motion.kbd>
)
}
/* Fade: the opacity drops softly on hover and tap. */
export function FadeKbd({ className, size = "md", children, ...props }: Props) {
const reduce = useReducedMotion()
return (
<motion.kbd
data-slot="styled-kbd"
className={cn(base, box[size], className)}
whileHover={reduce ? undefined : { opacity: 0.7 }}
whileTap={reduce ? undefined : { opacity: 0.45 }}
transition={{ duration: 0.18, ease: "easeOut" }}
{...(props as React.ComponentProps<typeof motion.kbd>)}
>
{children ?? "K"}
</motion.kbd>
)
}
/* Bounce: bounces down on tap and springs back on release. */
export function BounceKbd({ className, size = "md", children, ...props }: Props) {
const reduce = useReducedMotion()
return (
<motion.kbd
data-slot="styled-kbd"
className={cn(base, box[size], className)}
whileHover={reduce ? undefined : { y: -3 }}
whileTap={reduce ? undefined : { y: 3 }}
transition={{ type: "spring" as const, stiffness: 700, damping: 12 }}
{...(props as React.ComponentProps<typeof motion.kbd>)}
>
{children ?? "K"}
</motion.kbd>
)
}
/* GlowMotion: a token glow layer opens on hover and tap (an opacity animation; the colour is a fixed token color-mix shadow). */
export function GlowMotionKbd({ className, size = "md", children, ...props }: Props) {
const reduce = useReducedMotion()
return (
<motion.kbd
data-slot="styled-kbd"
className={cn(base, box[size], "relative", className)}
initial="rest"
animate="rest"
whileHover={reduce ? undefined : "lit"}
whileTap={reduce ? undefined : "lit"}
{...(props as React.ComponentProps<typeof motion.kbd>)}
>
<motion.span
aria-hidden="true"
className="pointer-events-none absolute inset-0 rounded-md shadow-[0_0_14px_-1px_color-mix(in_oklab,var(--color-info)_75%,transparent)]"
variants={{ rest: { opacity: 0 }, lit: { opacity: 1 } }}
transition={{ duration: 0.2, ease: "easeOut" }}
/>
<span className="relative">{children ?? "K"}</span>
</motion.kbd>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Press
Reflects a real keydown and keyup on the matching key. Press K to see it move.
import { PressKbd } from "@/components/ui/kbd-motion"
<PressKbd />Pop
Grows on hover and springs back in on tap.
import { PopKbd } from "@/components/ui/kbd-motion"
<PopKbd />Fade
Opacity eases down on hover and further on tap.
import { FadeKbd } from "@/components/ui/kbd-motion"
<FadeKbd />Bounce
Drops on tap and springs back when released.
import { BounceKbd } from "@/components/ui/kbd-motion"
<BounceKbd />Glow
A token glow layer fades in on hover and tap.
import { GlowMotionKbd } from "@/components/ui/kbd-motion"
<GlowMotionKbd />ai2 Motion kbd: 5 styled variations on the token system
The ai2 Motion kbd are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around press feedback on a keyboard key. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion drives the pop, fade, bounce and glow on hover and tap, while the press key tracks real keydown and keyup events. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the hover and tap animations are skipped and the keys stay still.
What is in the ai2 Motion kbd?
5 exports in one file: Press, Pop, Fade, Bounce and Glow. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion drives the pop, fade, bounce and glow on hover and tap, while the press key tracks real keydown and keyup events.
- Reduced-motion aware: Under prefers-reduced-motion, the hover and tap animations are skipped and the keys stay still.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Motion kbd sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.