<script lang="ts">
import {
HotkeysProvider,
createHotkey,
createHotkeys,
getFormatForAria,
getFormatForDisplay,
getHeldKeyCodesMap,
getHeldKeys,
getHotkeyRegistrations,
getIsKeyHeld,
} from '@tanstack/svelte-hotkeys'
import { getContext, onMount, setContext } from 'svelte'
import Editor from './Editor.svelte'
import Formatting from './Formatting.svelte'
import Hint from './Hint.svelte'
import Recording from './Recording.svelte'
import Sequences from './Sequences.svelte'
import Tickets from './Tickets.svelte'
const pages = [
{ to: '/', label: 'Tickets', shortcut: 'Alt+[Digit1]', component: Tickets },
{
to: '/editor',
label: 'Editor',
shortcut: 'Alt+[Digit2]',
component: Editor,
},
{
to: '/sequences',
label: 'Sequences',
shortcut: 'Alt+[Digit3]',
component: Sequences,
},
{
to: '/recording',
label: 'Recording',
shortcut: 'Alt+[Digit4]',
component: Recording,
},
{
to: '/formatting',
label: 'Formatting',
shortcut: 'Alt+[Digit5]',
component: Formatting,
},
] as const
const formatForDisplay = getFormatForDisplay()
const formatForAria = getFormatForAria()
const characterKeys = getContext<{ enabled: boolean }>('characterKeys')
let path = $state(location.hash.slice(1) || '/')
onMount(() => {
const update = () => {
path = location.hash.slice(1) || '/'
}
window.addEventListener('hashchange', update)
return () => window.removeEventListener('hashchange', update)
})
const page = $derived(pages.find((p) => p.to === path) ?? pages[0])
let showShortcuts = $state(false)
let activity = $state<Array<string>>([])
setContext('log', (message: string) => {
activity = [message, ...activity].slice(0, 6)
})
const keys = getHeldKeys()
const codes = getHeldKeyCodesMap()
const shift = getIsKeyHeld('Shift')
const registrations = getHotkeyRegistrations()
const groups = $derived([
...new Set(
registrations.hotkeys.map((reg) => reg.options.meta?.group ?? 'Other'),
),
])
createHotkeys(
pages.map((p) => ({
hotkey: p.shortcut,
callback: () => {
location.hash = p.to
},
options: { meta: { name: `Open ${p.label}`, group: 'Navigation' } },
})),
{ ignoreInputs: true },
)
createHotkey(
'Alt+Shift+[KeyK]',
() => {
showShortcuts = !showShortcuts
},
{ meta: { name: 'Show shortcuts', group: 'Navigation' } },
)
</script>
<header>
<h1>TanStack Hotkeys kitchen sink</h1>
<p>
Hold Alt / Option to see shortcut hints. Change routes to see registrations
mount and unmount.
</p>
<nav aria-label="Examples">
{#each pages as p (p.to)}
<a
href={`#${p.to}`}
class:active={path === p.to}
aria-keyshortcuts={formatForAria(p.shortcut)}
>{p.label} <Hint hotkey={p.shortcut} /></a
>
{/each}
</nav>
<button
onclick={() => (showShortcuts = !showShortcuts)}
aria-expanded={showShortcuts}
aria-keyshortcuts={formatForAria('Alt+Shift+[KeyK]')}
>{showShortcuts ? 'Hide' : 'Show'} shortcuts
<Hint hotkey="Alt+Shift+[KeyK]" /></button
>
<label>
<input type="checkbox" bind:checked={characterKeys.enabled} />
Single-key shortcuts
</label>
<small>
Turn off to suspend shortcuts made only of keys that type a character, such
as the G G sequence (WCAG 2.1.4).
</small>
</header>
{#if showShortcuts}
<section aria-label="Registered shortcuts">
<h2>Registered shortcuts</h2>
<p>
Global and current-route registrations, including disabled handlers. Each
page renders a <code>HotkeysProvider</code> with its own scope. Target shortcuts
run only while focus is inside their target.
</p>
{#each groups as group (group)}
<div>
<h3>{group}</h3>
<table>
<thead>
<tr>
<th>Action</th>
<th>Binding</th>
<th>Scope</th>
<th>Status</th>
</tr>
</thead>
<tbody>
{#each registrations.hotkeys.filter((reg) => (reg.options.meta?.group ?? 'Other') === group) as reg (reg.id)}
<tr>
<td>
{reg.options.meta?.name ?? reg.id}
<br />
<small>{reg.options.meta?.description}</small>
</td>
<td>
{formatForDisplay(reg.hotkey, { sequenceSeparator: ' → ' })}
</td>
<td>{reg.options.scope}</td>
<td>
{reg.options.enabled === false
? 'Disabled'
: `${reg.triggerCount} fired`}
</td>
</tr>
{/each}
</tbody>
</table>
</div>
{/each}
</section>
{/if}
<main>
{#key page.to}
<HotkeysProvider scope={page.label.toLowerCase()}>
<page.component />
</HotkeysProvider>
{/key}
</main>
<section>
<h2>Key state</h2>
<p>
Keys: {keys.keys.join(' + ') || 'None'}
<br />
Codes: {Object.values(codes.codes).join(' + ') || 'None'}
<br />
Shift: {shift.held ? 'held' : 'released'}
</p>
<h2>Activity</h2>
<button onclick={() => (activity = [])}>Clear log</button>
<ul aria-live="polite">
{#each activity as message, i (i)}
<li>{message}</li>
{/each}
</ul>
</section>