Updated
Live Kbd
NewCustomize
Installation
npx shadcn@latest add https://uibrik.com/r/live-kbd.jsonInstall the packages
npm install motionAdd the cn helper
Imports
cnfrom@/lib/utils, whichshadcn initcreates. Add it by hand without shadcn.Copy the source into your project
components/uibrik/live-kbd/live-kbd.tsx"use client"; import { motion, useAnimationControls, useReducedMotion } from "motion/react"; import { useEffect, useRef, useState, useSyncExternalStore } from "react"; import { cn } from "@/lib/utils"; const EASE_OUT: [number, number, number, number] = [0.23, 1, 0.32, 1]; // How far a keycap travels when pressed, in px. const TRAVEL = 2; export type LiveKbdProps = { /** The shortcut, e.g. ["mod", "k"]. "mod" is ⌘ on Apple devices and Ctrl elsewhere; also "shift", "alt", "ctrl", "meta" and any key name. */ keys?: string[]; /** Text before the keys. Leave empty for the keys alone. */ label?: string; /** Called when the shortcut is pressed on the keyboard or the button is clicked. */ onTrigger?: () => void; /** Listen for the shortcut on the whole page. */ listen?: boolean; /** Stop the browser's own action for the shortcut, like ⌘K focusing the address bar. */ preventDefault?: boolean; className?: string; };
Adds the files to
components/uibrik/live-kbd and installs motion. With the @uibrik namespace set up: npx shadcn@latest add @uibrik/live-kbd.Usage
import { LiveKbd } from "@/components/uibrik/live-kbd/live-kbd";<LiveKbd keys={["mod", "k"]} label="Search" listen preventDefault onTrigger={() => {}}/>Where to use Live Kbd
Search and command triggers
Holding ⌘ sinks its keycap first, so people see the shortcut working before it fires.
Pairs with Command Menu and Search
Docs and onboarding
Keycaps that react to the real keyboard teach shortcuts better than a static hint.
Pairs with Product Tour
Toolbars and menus
Shows ⌘ on Apple devices and Ctrl elsewhere, so the hint is right for everyone.
Pairs with Dropdown Menu and Context Menu
Props
LiveKbd- keys
- Type string[]
- ["mod","k"]
- The shortcut. "mod" is ⌘ on Apple devices and Ctrl elsewhere.
- label
- Type string
- "Search"
- Text before the keys. Empty shows the keys alone.
- listen
- Type boolean
- true
- Listen for the shortcut on the whole page.
- preventDefault
- Type boolean
- true
- Stop the browser's own action for the shortcut.
- onTrigger
- Type () => void
- –
- Called when the shortcut is pressed or the button is clicked.
- className
- Type string
- –
- Classes for the button.