<script lang="ts">
import { TanStackDevtools } from '@tanstack/svelte-devtools'
import { createHotkey, getFormatForDisplay } from '@tanstack/svelte-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/svelte-hotkeys-devtools'
const formatForDisplay = getFormatForDisplay()
let lastFired = $state('')
// These type-check because German keys are registered in main.ts.
createHotkey('Mod+Ö', () => (lastFired = 'Toggle comment'))
createHotkey('Mod+Shift+Ä', () => (lastFired = 'Add annotation'))
createHotkey('ß', () => (lastFired = 'Toggle sidebar'))
createHotkey('§', () => (lastFired = 'Show sections'))
// A to Z, 0 to 9, named keys, and physical codes type-check under every
// registration, and fire on every layout.
createHotkey('Mod+K', () => (lastFired = 'Open command palette'))
createHotkey('Alt+[Digit1]', () => (lastFired = 'Go to the first tab'))
createHotkey('Escape', () => (lastFired = ''))
const devtoolsPlugins = [hotkeysDevtoolsPlugin()]
</script>
<div class="app">
<header>
<h1>Keyboard Layouts</h1>
<p>
Registering a layout lets hotkey strings accept the keys it types, such as
Ö and ß. It changes types only: these hotkeys match the same way without
it.
</p>
</header>
<main>
<p role="status" class="note">
{lastFired ? `Last fired: ${lastFired}` : 'Press a hotkey below.'}
</p>
<section class="demo-section">
<h2>German keys</h2>
<p class="note">Switch your keyboard layout to German to press these.</p>
<div class="hotkey-list">
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+Ö')}</kbd> Toggle comment
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+Shift+Ä')}</kbd> Add annotation
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('ß')}</kbd> Toggle sidebar
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('§')}</kbd> Show sections
</div>
</div>
</section>
<section class="demo-section">
<h2>Keys on every layout</h2>
<div class="hotkey-list">
<div class="hotkey-row">
<kbd>{formatForDisplay('Mod+K')}</kbd> Open command palette
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Alt+[Digit1]')}</kbd> Go to the first tab
</div>
<div class="hotkey-row">
<kbd>{formatForDisplay('Escape')}</kbd> Clear
</div>
</div>
</section>
<section class="demo-section">
<h2>Usage</h2>
<pre
class="code-block">{`import type { GermanKeys, UsKeys } from '@tanstack/svelte-hotkeys'
declare module '@tanstack/svelte-hotkeys' {
interface Register {
keys: UsKeys | GermanKeys
}
}
createHotkey('Mod+Ö', toggleComment) // OK
createHotkey('Mod+[', outdent) // OK, because UsKeys is listed
createHotkey('Mod+Ñ', x) // Type error: Spanish isn't registered`}</pre>
</section>
</main>
</div>
{#if import.meta.env.DEV}
<TanStackDevtools plugins={devtoolsPlugins} />
{/if}