import { render } from '@solidjs/web'
import { TanStackDevtools } from '@tanstack/solid-devtools'
import {
HotkeysProvider,
createFormatForDisplay,
createHotkeyRecorder,
createHotkeys,
} from '@tanstack/solid-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/solid-hotkeys-devtools'
import { For, Show, createSignal } from 'solid-js'
import type { Hotkey } from '@tanstack/solid-hotkeys'
import './index.css'
const devtoolsPlugins = [hotkeysDevtoolsPlugin()]
function hotkeyText(hotkey: Hotkey) {
return Array.isArray(hotkey) ? hotkey.join(' ') : hotkey
}
function ShortcutList() {
const formatForDisplay = createFormatForDisplay()
const [status, setStatus] = createSignal('Not recording')
const [editing, setEditing] = createSignal<string | null>(null)
const [shortcuts, setShortcuts] = createSignal<
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 recorder = createHotkeyRecorder({
maxSteps: Infinity,
onRecord: (hotkey) => {
setShortcuts((prev) =>
prev.map((s) => (s.name === editing() ? { ...s, hotkey } : s)),
)
setStatus(`${editing()} is now ${hotkeyText(hotkey)}`)
setEditing(null)
},
onCancel: () => {
setStatus(`${editing()} unchanged`)
setEditing(null)
},
onClear: () => {
setShortcuts((prev) =>
prev.map((s) => (s.name === editing() ? { ...s, hotkey: null } : s)),
)
setStatus(`${editing()} cleared`)
setEditing(null)
},
})
createHotkeys(() =>
shortcuts().map((s) => ({
hotkey: s.hotkey,
callback: () => setStatus(`Ran ${s.name}`),
options: { meta: { name: s.name } },
})),
)
return (
<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">
<For each={shortcuts()}>
{(s) => (
<li>
<span>{s.name}</span>
<Show
when={editing() === s.name && recorder.isRecording()}
fallback={
<Show when={s.hotkey} fallback={<span>No shortcut</span>}>
{(hotkey) => <kbd>{formatForDisplay(hotkey())}</kbd>}
</Show>
}
>
<Show
when={recorder.steps().length > 0}
fallback={<span>Press keys</span>}
>
<kbd>{formatForDisplay(recorder.steps())}</kbd>
</Show>
</Show>
<Show
when={editing() === s.name && recorder.isRecording()}
fallback={
<button
type="button"
onClick={() => {
recorder.startRecording()
setEditing(s.name)
setStatus(`Recording ${s.name}`)
}}
>
Edit
</button>
}
>
<button type="button" onClick={recorder.cancelRecording}>
Cancel
</button>
</Show>
</li>
)}
</For>
</ul>
<p role="status" class="status">
{status()}
</p>
</section>
)
}
function MaxSteps() {
const formatForDisplay = createFormatForDisplay()
const [status, setStatus] = createSignal('Not recording')
const recorder = createHotkeyRecorder({
maxSteps: 2,
onRecord: (hotkey) => setStatus(`Recorded ${hotkeyText(hotkey)}`),
onCancel: () => setStatus('Cancelled'),
})
return (
<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"
onClick={() => {
recorder.startRecording()
setStatus('Recording')
}}
>
Record
</button>
</div>
<p role="status" class="status">
{status()}
</p>
</section>
)
}
function CommitKeys() {
const formatForDisplay = createFormatForDisplay()
const [status, setStatus] = createSignal('Not recording')
const enterSaves = createHotkeyRecorder({
maxSteps: Infinity,
commitKeys: 'enter',
onRecord: (hotkey) => setStatus(`Recorded ${hotkeyText(hotkey)}`),
onCancel: () => setStatus('Cancelled'),
})
const enterRecords = createHotkeyRecorder({
maxSteps: Infinity,
commitKeys: 'none',
onRecord: (hotkey) => setStatus(`Recorded ${hotkeyText(hotkey)}`),
onCancel: () => setStatus('Cancelled'),
})
return (
<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"
onClick={() => {
enterSaves.startRecording()
setStatus('Recording')
}}
>
Record (commitKeys: 'enter')
</button>
<button
type="button"
onClick={() => {
enterRecords.startRecording()
setStatus('Recording')
}}
>
Record (commitKeys: 'none')
</button>
<button
type="button"
disabled={enterRecords.steps().length === 0}
onClick={enterRecords.commitRecording}
>
Done
</button>
</div>
<Show when={enterRecords.steps().length > 0}>
<p>
Steps: <kbd>{formatForDisplay(enterRecords.steps())}</kbd>
</p>
</Show>
<p role="status" class="status">
{status()}
</p>
</section>
)
}
function IdleTimeout() {
const formatForDisplay = createFormatForDisplay()
const [status, setStatus] = createSignal('Not recording')
const recorder = createHotkeyRecorder({
maxSteps: Infinity,
idleTimeoutMs: 1000,
onRecord: (hotkey) => setStatus(`Recorded ${hotkeyText(hotkey)}`),
onCancel: () => setStatus('Cancelled'),
})
return (
<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"
onClick={() => {
recorder.startRecording()
setStatus('Recording')
}}
>
Record
</button>
</div>
<Show when={recorder.steps().length > 0}>
<p>
Steps: <kbd>{formatForDisplay(recorder.steps())}</kbd>
</p>
</Show>
<p role="status" class="status">
{status()}
</p>
</section>
)
}
function Validation() {
const formatForDisplay = createFormatForDisplay()
const [status, setStatus] = createSignal('Not recording')
const recorder = createHotkeyRecorder({
maxSteps: Infinity,
validate: (_hotkey, context) =>
context.kind === 'sequence' ||
context.parsedChord.modifiers.length > 0 ||
'A single chord needs a modifier.',
onReject: ({ reason, message }) =>
setStatus(message ? `Rejected: ${message}` : `Rejected (${reason})`),
onRecord: (hotkey) => setStatus(`Recorded ${hotkeyText(hotkey)}`),
onCancel: () => setStatus('Cancelled'),
})
return (
<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"
onClick={() => {
recorder.startRecording()
setStatus('Recording')
}}
>
Record
</button>
</div>
<Show when={recorder.steps().length > 0}>
<p>
Steps: <kbd>{formatForDisplay(recorder.steps())}</kbd>
</p>
</Show>
<p role="status" class="status">
{status()}
</p>
</section>
)
}
function Conflicts() {
const formatForDisplay = createFormatForDisplay()
const [status, setStatus] = createSignal('Not recording')
const recorder = createHotkeyRecorder({
maxSteps: Infinity,
detectConflicts: true,
onReject: ({ reason, hotkey, conflicts }) =>
setStatus(
reason === 'conflict' && hotkey
? `Rejected: ${hotkeyText(hotkey)} is used by ${conflicts
?.map((c) => c.registration.options.meta?.name)
.join(', ')}`
: `Rejected (${reason})`,
),
onRecord: (hotkey) => setStatus(`Recorded ${hotkeyText(hotkey)}`),
onCancel: () => setStatus('Cancelled'),
})
return (
<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"
onClick={() => {
recorder.startRecording()
setStatus('Recording')
}}
>
Record
</button>
</div>
<Show when={recorder.steps().length > 0}>
<p>
Steps: <kbd>{formatForDisplay(recorder.steps())}</kbd>
</p>
</Show>
<p role="status" class="status">
{status()}
</p>
</section>
)
}
function App() {
return (
<div class="app">
<header>
<h1>Recording Sequences</h1>
<p>
With <code>maxSteps</code> above 1, <code>createHotkeyRecorder</code>{' '}
records several chords in a row, such as G then I. Each section shows
one recorder option.
</p>
</header>
<main>
<ShortcutList />
<MaxSteps />
<CommitKeys />
<IdleTimeout />
<Validation />
<Conflicts />
</main>
<TanStackDevtools plugins={devtoolsPlugins} />
</div>
)
}
render(
() => (
<HotkeysProvider>
<App />
</HotkeysProvider>
),
document.getElementById('root')!,
)