Skip to main content
Clever Ops

Hotkey Button

A React button that binds a real keyboard shortcut, mac-aware kbd chip, aria-keyshortcuts, a typing-context guard, and a remote-press flash when the hotkey fires it.

Preview & code

Loading Hotkey Button preview

Installation

pnpm dlx shadcn@latest add @cleverui/button-hotkey

Requires the @cleverui registry in your components.json, a one-off, two-line setup.

Usage

tsx
import { ButtonHotkey } from '@/components/ui/button-hotkey'
Exampletsx
'use client'

import { useState } from 'react'
import { ButtonHotkey } from '@/components/ui/button-hotkey'

export default function ButtonHotkeyDemo() {
  const [log, setLog] = useState<string[]>([])

  const push = (msg: string) => {
    setLog((prev) => [msg, ...prev].slice(0, 4))
  }

  return (
    <div className="mx-auto flex min-h-[360px] max-w-lg flex-col items-center justify-center gap-10 px-6 py-16">
      <div className="flex w-full flex-col items-center gap-3 text-center">
        <p className="font-mono text-[11px] uppercase tracking-[0.2em] text-muted-foreground">
          Ledger · command palette
        </p>
        <p className="max-w-sm text-sm leading-relaxed text-muted-foreground">
          Open the global search for invoices, vendors, and audit trails, same binding
          as the app chrome.
        </p>
        <ButtonHotkey keys="mod+k" onAction={() => push('Opened search')} variant="outline">
          Search ledger
        </ButtonHotkey>
      </div>

      <div className="flex w-full flex-col items-center gap-3 border-t border-border/60 pt-10 text-center">
        <p className="font-mono text-[11px] uppercase tracking-[0.2em] text-muted-foreground">
          Draft · quick save
        </p>
        <div className="flex flex-wrap items-center justify-center gap-3">
          <ButtonHotkey
            keys="mod+s"
            onAction={() => push('Draft saved')}
            variant="primary"
            size="sm"
          >
            Save draft
          </ButtonHotkey>
          <ButtonHotkey
            keys="shift+d"
            onAction={() => push('Duplicated line')}
            variant="ghost"
            size="sm"
          >
            Duplicate row
          </ButtonHotkey>
        </div>
      </div>

      {log.length > 0 && (
        <ul className="w-full max-w-xs space-y-1.5 border-t border-border/60 pt-8 font-mono text-[11px] text-muted-foreground">
          {log.map((entry, i) => (
            <li key={`${entry}-${i}`} className="tabular-nums">
              {entry}
            </li>
          ))}
        </ul>
      )}
    </div>
  )
}

API reference

ButtonHotkey accepts the following props.

PropTypeDefaultDescription
children*React.ReactNodeaction label, e.g. 'Search docs'
keys*stringshortcut combo like 'mod+k' or 'shift+d', 'mod' resolves to ⌘ on mac and Ctrl elsewhere
onAction*() => voidfires exactly once per activation, click, Enter/Space, or the bound hotkey all converge here
guardTypingbooleantrueignore the hotkey while focus is inside an input, textarea, select, or contenteditable

…plus everything from Omit< React.ButtonHTMLAttributes<HTMLButtonElement>, 'children' | 'onClick' | 'onDrag' | 'onDragStart' | 'onDragEnd' | 'onAnimationStart' >, VariantProps<typeof buttonHotkeyVariants>: className, event handlers, aria attributes and the rest pass straight through.

Frequently asked questions

How do I install the Hotkey Button component?

Register the @cleverui registry in your components.json ("@cleverui": "https://cleverops.com.au/r/{name}.json"), then run `npx shadcn@latest add @cleverui/button-hotkey`. The shadcn CLI copies the source into your project, you own the code from that point. You can also copy the source directly from this page.

What dependencies does Hotkey Button require?

Hotkey Button uses motion and class-variance-authority on top of React and Tailwind CSS. You don't need to install them manually, the shadcn CLI resolves and installs npm dependencies automatically when you add the component.

Can I theme Hotkey Button to match my brand?

Yes. The component is styled entirely with Tailwind utilities over standard design tokens (primary, background, muted, border), so it inherits your existing shadcn/ui theme automatically. Change your CSS variables and the component follows, no source edits needed for palette, radius, or fonts.

Want this built for you?

CleverUI is built by the Clever Ops web team. If you'd rather ship a hand-coded, fast, custom website than assemble one, we build them for Australian businesses every week.

Explore web development services