<script setup lang="ts">
import { TanStackDevtools } from '@tanstack/vue-devtools'
import {
useFormatForDisplay,
useHotkeyRecorder,
useHotkeys,
} from '@tanstack/vue-hotkeys'
import { HotkeysDevtoolsPanel } from '@tanstack/vue-hotkeys-devtools'
import { ref } from 'vue'
import type { Hotkey } from '@tanstack/vue-hotkeys'
const plugins = [{ name: 'TanStack Hotkeys', component: HotkeysDevtoolsPanel }]
const formatForDisplay = useFormatForDisplay()
function hotkeyText(hotkey: Hotkey) {
return Array.isArray(hotkey) ? hotkey.join(' ') : hotkey
}
const listStatus = ref('Not recording')
const editing = ref<string | null>(null)
const shortcuts = ref<Array<{ name: string; hotkey: Hotkey | null }>>([
{ name: 'Save', hotkey: 'Mod+S' },
{ name: 'Go to inbox', hotkey: ['G', 'I'] },
{ name: 'Go to settings', hotkey: ['G', 'S'] },
])
const listRecorder = useHotkeyRecorder({
maxSteps: Infinity,
onRecord: (hotkey) => {
shortcuts.value = shortcuts.value.map((s) =>
s.name === editing.value ? { ...s, hotkey } : s,
)
listStatus.value = `${editing.value} is now ${hotkeyText(hotkey)}`
editing.value = null
},
onCancel: () => {
listStatus.value = `${editing.value} unchanged`
editing.value = null
},
onClear: () => {
shortcuts.value = shortcuts.value.map((s) =>
s.name === editing.value ? { ...s, hotkey: null } : s,
)
listStatus.value = `${editing.value} cleared`
editing.value = null
},
})
useHotkeys(() =>
shortcuts.value.map((s) => ({
hotkey: s.hotkey,
callback: () => (listStatus.value = `Ran ${s.name}`),
options: { meta: { name: s.name } },
})),
)
function editShortcut(name: string) {
listRecorder.startRecording()
editing.value = name
listStatus.value = `Recording ${name}`
}
const maxStepsStatus = ref('Not recording')
const maxStepsRecorder = useHotkeyRecorder({
maxSteps: 2,
onRecord: (hotkey) =>
(maxStepsStatus.value = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (maxStepsStatus.value = 'Cancelled'),
})
const commitStatus = ref('Not recording')
const enterSaves = useHotkeyRecorder({
maxSteps: Infinity,
commitKeys: 'enter',
onRecord: (hotkey) => (commitStatus.value = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (commitStatus.value = 'Cancelled'),
})
const enterRecords = useHotkeyRecorder({
maxSteps: Infinity,
commitKeys: 'none',
onRecord: (hotkey) => (commitStatus.value = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (commitStatus.value = 'Cancelled'),
})
const idleStatus = ref('Not recording')
const idleRecorder = useHotkeyRecorder({
maxSteps: Infinity,
idleTimeoutMs: 1000,
onRecord: (hotkey) => (idleStatus.value = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (idleStatus.value = 'Cancelled'),
})
const validationStatus = ref('Not recording')
const validationRecorder = useHotkeyRecorder({
maxSteps: Infinity,
validate: (_hotkey, context) =>
context.kind === 'sequence' ||
context.parsedChord.modifiers.length > 0 ||
'A single chord needs a modifier.',
onReject: ({ reason, message }) =>
(validationStatus.value = message
? `Rejected: ${message}`
: `Rejected (${reason})`),
onRecord: (hotkey) =>
(validationStatus.value = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (validationStatus.value = 'Cancelled'),
})
const conflictsStatus = ref('Not recording')
const conflictsRecorder = useHotkeyRecorder({
maxSteps: Infinity,
detectConflicts: true,
onReject: ({ reason, hotkey, conflicts }) =>
(conflictsStatus.value =
reason === 'conflict' && hotkey
? `Rejected: ${hotkeyText(hotkey)} is used by ${conflicts
?.map((c) => c.registration.options.meta?.name)
.join(', ')}`
: `Rejected (${reason})`),
onRecord: (hotkey) =>
(conflictsStatus.value = `Recorded ${hotkeyText(hotkey)}`),
onCancel: () => (conflictsStatus.value = 'Cancelled'),
})
</script>
<template>
<div class="app">
<header>
<h1>Recording Sequences</h1>
<p>
With <code>maxSteps</code> above 1, <code>useHotkeyRecorder</code>
records several chords in a row, such as G then I. Each section shows
one recorder option.
</p>
</header>
<main>
<section class="demo-section">
<h2>Shortcut list</h2>
<p>
Click Edit, press each chord in order, then press Enter. Backspace
removes the last chord, or clears the shortcut when none are left.
Escape cancels.
</p>
<ul class="shortcut-list">
<li v-for="s in shortcuts" :key="s.name">
<span>{{ s.name }}</span>
<template
v-if="editing === s.name && listRecorder.isRecording.value"
>
<kbd v-if="listRecorder.steps.value.length > 0">{{
formatForDisplay(listRecorder.steps.value)
}}</kbd>
<span v-else>Press keys</span>
<button @click="listRecorder.cancelRecording()">Cancel</button>
</template>
<template v-else>
<kbd v-if="s.hotkey">{{ formatForDisplay(s.hotkey) }}</kbd>
<span v-else>No shortcut</span>
<button @click="editShortcut(s.name)">Edit</button>
</template>
</li>
</ul>
<p role="status" class="status">{{ listStatus }}</p>
</section>
<section class="demo-section">
<h2>Max steps</h2>
<p>
Press <kbd>{{ formatForDisplay('G') }}</kbd>, then
<kbd>{{ formatForDisplay('H') }}</kbd>. Recording ends at the second
chord without Enter.
</p>
<div class="buttons">
<button
type="button"
@click="
maxStepsRecorder.startRecording();
maxStepsStatus = 'Recording'
"
>
Record
</button>
</div>
<p role="status" class="status">{{ maxStepsStatus }}</p>
</section>
<section class="demo-section">
<h2>Commit keys</h2>
<p>
Press <kbd>{{ formatForDisplay('G') }}</kbd>, then
<kbd>{{ formatForDisplay('Enter') }}</kbd> with each button. With
commitKeys: 'none', click Done to save.
</p>
<div class="buttons">
<button
type="button"
@click="
enterSaves.startRecording();
commitStatus = 'Recording'
"
>
Record (commitKeys: 'enter')
</button>
<button
type="button"
@click="
enterRecords.startRecording();
commitStatus = 'Recording'
"
>
Record (commitKeys: 'none')
</button>
<button
type="button"
:disabled="enterRecords.steps.value.length === 0"
@click="enterRecords.commitRecording()"
>
Done
</button>
</div>
<p v-if="enterRecords.steps.value.length > 0">
Steps: <kbd>{{ formatForDisplay(enterRecords.steps.value) }}</kbd>
</p>
<p role="status" class="status">{{ commitStatus }}</p>
</section>
<section class="demo-section">
<h2>Idle timeout</h2>
<p>
Press <kbd>{{ formatForDisplay('G') }}</kbd>, then
<kbd>{{ formatForDisplay('H') }}</kbd>, and wait one second.
</p>
<div class="buttons">
<button
type="button"
@click="
idleRecorder.startRecording();
idleStatus = 'Recording'
"
>
Record
</button>
</div>
<p v-if="idleRecorder.steps.value.length > 0">
Steps: <kbd>{{ formatForDisplay(idleRecorder.steps.value) }}</kbd>
</p>
<p role="status" class="status">{{ idleStatus }}</p>
</section>
<section class="demo-section">
<h2>Validation</h2>
<p>
Press <kbd>{{ formatForDisplay('G') }}</kbd>, then
<kbd>{{ formatForDisplay('Enter') }}</kbd> to see the rejection. Then
press <kbd>{{ formatForDisplay('H') }}</kbd> and
<kbd>{{ formatForDisplay('Enter') }}</kbd>.
</p>
<div class="buttons">
<button
type="button"
@click="
validationRecorder.startRecording();
validationStatus = 'Recording'
"
>
Record
</button>
</div>
<p v-if="validationRecorder.steps.value.length > 0">
Steps:
<kbd>{{ formatForDisplay(validationRecorder.steps.value) }}</kbd>
</p>
<p role="status" class="status">{{ validationStatus }}</p>
</section>
<section class="demo-section">
<h2>Conflicts</h2>
<p>
Record <kbd>{{ formatForDisplay('G') }}</kbd> alone, or
<kbd>{{ formatForDisplay(['G', 'I']) }}</kbd>, to conflict with the
list above. Then record <kbd>{{ formatForDisplay(['G', 'H']) }}</kbd>.
</p>
<div class="buttons">
<button
type="button"
@click="
conflictsRecorder.startRecording();
conflictsStatus = 'Recording'
"
>
Record
</button>
</div>
<p v-if="conflictsRecorder.steps.value.length > 0">
Steps:
<kbd>{{ formatForDisplay(conflictsRecorder.steps.value) }}</kbd>
</p>
<p role="status" class="status">{{ conflictsStatus }}</p>
</section>
</main>
<TanStackDevtools :plugins="plugins" />
</div>
</template>