Some apps hide shortcut badges until the user holds a modifier key, keeping buttons clean while still making shortcuts discoverable on demand.
createHotkeyHint returns an object whose value is true while the user holds modifiers belonging to an active hotkey. It's an optional display helper and doesn't register a shortcut itself, so pair it with createHotkey.
import Alpine from 'alpinejs'
import { createHotkeysScope } from '@tanstack/alpine-hotkeys'
const saveHotkey = 'Mod+S'
Alpine.data('saveButton', () => {
const hotkeys = createHotkeysScope()
const formatForDisplay = hotkeys.createFormatForDisplay()
hotkeys.createHotkey(saveHotkey, () => save())
const hint = hotkeys.createHotkeyHint(saveHotkey)
return {
get label() {
return 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>Holding Command on macOS or Control elsewhere reveals Save ⌘ S or Save Ctrl+S. See Formatting & Display for formatting options.
const hint = hotkeys.createHotkeyHint('Alt+Shift+[KeyK]', { exact: true }) // Requires Alt AND ShiftSequences reveal their hint while the user holds the modifiers of their first step:
const hint = hotkeys.createHotkeyHint(['Mod+K', 'Mod+C']) // hint.value is true while Mod is heldPass an app hotkey directly so hints stay synchronized even when users rebind shortcuts. Unassigned app hotkeys never show a hint. A small shared component keeps buttons tidy:
import type { AppHotkeyId } from '@tanstack/alpine-hotkeys'
Alpine.data('hint', (id: AppHotkeyId) => {
const hotkeys = createHotkeysScope()
const formatForDisplay = hotkeys.createFormatForDisplay()
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>Outside Alpine components, such as canvas draw loops, combine matchesHeldModifiers with getKeyStateTracker() and getHotkeyManager().hasActiveRegistration():
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 helperSee Held Keys for more on getKeyStateTracker, and check the Kitchen Sink example to see hints on live buttons.