Some apps show each button's shortcut only while the user holds a modifier, so the hints are there when the user reaches for the keyboard, and out of the way otherwise. createHotkeyHint returns an object whose value is true while the user holds modifiers that belong to a hotkey that can fire. It's an optional helper, and it doesn't register anything, so use it alongside createHotkey.
import Alpine from 'alpinejs'
import { createHotkeysScope, formatForDisplay } from '@tanstack/alpine-hotkeys'
const saveHotkey = 'Mod+S'
Alpine.data('saveButton', () => {
const hotkeys = createHotkeysScope()
hotkeys.createHotkey(saveHotkey, () => save())
const hint = hotkeys.createHotkeyHint(saveHotkey)
return {
label: formatForDisplay(saveHotkey),
get showHint() {
return hint.value
},
save,
destroy() {
hotkeys.destroy()
},
}
})<button x-data="saveButton" @click="save()">
Save <kbd x-show="showHint" x-text="label"></kbd>
</button>While the user holds Command on macOS, or Control elsewhere, the button reads "Save ⌘ S" or "Save Ctrl+S". Formatting & Display covers labels and keycaps.
To show a hint only while every modifier is held, pass exact:
const hint = hotkeys.createHotkeyHint('Alt+Shift+[KeyK]', { exact: true }) // Needs Alt and ShiftIf the hotkey sets platform, pass the same platform to createHotkeyHint, so Mod resolves the same way. The hotkey and the options can also be getters.
A sequence shows its hint while the user holds the modifiers of its first step:
const hint = hotkeys.createHotkeyHint(['Mod+K', 'Mod+C']) // hint.value is true while Mod is heldPass an app hotkey to show the user's current hotkey, including one they changed. An app hotkey with no hotkey assigned shows no hint. A small component keeps every button's hint the same:
import type { AppHotkeyId } from '@tanstack/alpine-hotkeys'
Alpine.data('hint', (id: AppHotkeyId) => {
const hotkeys = createHotkeysScope()
const appHotkeys = hotkeys.createAppHotkeys()
const hint = hotkeys.createHotkeyHint(() => appHotkeys.value[id])
return {
get label() {
const { hotkey } = appHotkeys.value[id]
return hint.value && hotkey ? formatForDisplay(hotkey) : ''
},
destroy() {
hotkeys.destroy()
},
}
})Alpine.data('toolbar', () => {
const hotkeys = createHotkeysScope()
const appHotkeys = hotkeys.createAppHotkeys()
hotkeys.createHotkey(() => appHotkeys.value['doc.save'], () => save())
return {
save,
destroy() {
hotkeys.destroy()
},
}
})<div x-data="toolbar">
<button @click="save()">
Save <kbd x-data="hint('doc.save')" x-show="label" x-text="label"></kbd>
</button>
</div>To check outside a component, such as while drawing on a canvas, pass the held keys to matchesHeldModifiers. It takes the same options, but it only looks at held keys. To also check that the hotkey can fire, call hasActiveRegistration on the hotkey manager:
import {
getHotkeyManager,
getKeyStateTracker,
matchesHeldModifiers,
} from '@tanstack/alpine-hotkeys'
const tracker = getKeyStateTracker()
matchesHeldModifiers('Mod+S', tracker.getHeldKeys()) // true while Mod is held
const showHint =
matchesHeldModifiers('Mod+S', tracker.getHeldKeys()) &&
getHotkeyManager().hasActiveRegistration('Mod+S') // Same check as the helperHeld Key State Helpers covers the tracker and the other held-key functions.
The Kitchen Sink example shows hints next to its buttons while Alt or Option is held.