Shortcut Confirm

Makes a destructive keyboard shortcut need a deliberate second press, so a slipped finger only shows a prompt.

StopsKeyboard shortcut deleted items

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/shortcut-confirm.json

The files land in your project. They're yours to edit.

Usage

shortcut-confirm-usage.tsx
import { isApplePlatform, ShortcutConfirm } from "@/components/ui/shortcut-confirm"

<ShortcutConfirm
  shortcut="Mod+Backspace"
  enabled={count > 0}
  action={`delete ${count} ${count === 1 ? "item" : "items"}`}
  onConfirm={() => {
    setItems((current) => current.filter((item) => !selected.has(item)))
    setLog(`Deleted ${count} ${count === 1 ? "item" : "items"}.`)
  }}
/>

What it catches

  • A first press, which only arms and explains
  • Key auto-repeat from holding the shortcut
  • An extra modifier (a slipped Shift)
  • Any other key pressed between the two presses
  • Shortcuts typed inside inputs, unless allowInInputs
  • IME composition keystrokes

Props

ShortcutConfirm props
PropTypeDefault
shortcut* (required)

The destructive chord, e.g. "Mod+Backspace" or "Mod+Shift+D".

string—
onConfirm* (required)

Runs on the confirming second press.

() => void—
window

Ms the second press has to land in.

number2000
enabled

Listen only while this is true, e.g. when something is selected.

booleantrue
allowInInputs

Also listen while typing in inputs and contenteditable. Off by default.

booleanfalse
action* (required)

What the shortcut does, used in every message, e.g. "delete 3 items".

string—
idleText

Templates: `{keys}` and `{action}` are filled in.

string"{keys} twice to {action}"
armedText

Prompt after the first press. `{shortcut}` and `{action}` are filled in.

string"Press {keys} again to {action}"
doneText

Shown after the action is confirmed.

string"Done: {action}"

* required. Also accepts the native props of <div> (className, aria-*, data-*…), passed through to it.

Accessibility

  • Armed and done prompts render in a polite live region
  • Shortcut shown as ⌘ on Apple platforms and Ctrl elsewhere
  • Icons are aria-hidden

Only what the source does. Test with your own screen reader and content before shipping.

Limitations

  • Guards the keyboard path only; menu or button paths need their own confirm
  • Letters also match by physical key, so a layout remap could still trigger it

What it stops

Keyboard shortcut deleted items

Also known as

keyboard shortcut · hotkey · cmd+backspace · keybinding · power user · press again · shortcuts

Dependencies

Pairs well with