TanStack

Vue Example: Keyboard Layouts

<script setup lang="ts">
import { TanStackDevtools } from '@tanstack/vue-devtools'
import { useFormatForDisplay, useHotkey } from '@tanstack/vue-hotkeys'
import { HotkeysDevtoolsPanel } from '@tanstack/vue-hotkeys-devtools'
import { ref } from 'vue'

const formatForDisplay = useFormatForDisplay()
const lastFired = ref('')

// These type-check because German keys are registered in index.ts.
useHotkey('Mod+Ö', () => (lastFired.value = 'Toggle comment'))
useHotkey('Mod+Shift+Ä', () => (lastFired.value = 'Add annotation'))
useHotkey('ß', () => (lastFired.value = 'Toggle sidebar'))
useHotkey('§', () => (lastFired.value = 'Show sections'))

// A to Z, 0 to 9, named keys, and physical codes type-check under every
// registration, and fire on every layout.
useHotkey('Mod+K', () => (lastFired.value = 'Open command palette'))
useHotkey('Alt+[Digit1]', () => (lastFired.value = 'Go to the first tab'))
useHotkey('Escape', () => (lastFired.value = ''))

const plugins = [{ name: 'TanStack Hotkeys', component: HotkeysDevtoolsPanel }]

const usageCode = `import type { GermanKeys, UsKeys } from '@tanstack/vue-hotkeys'

declare module '@tanstack/vue-hotkeys' {
  interface Register {
    keys: UsKeys | GermanKeys
  }
}

useHotkey('Mod+Ö', toggleComment) // OK
useHotkey('Mod+[', outdent) // OK, because UsKeys is listed
useHotkey('Mod+Ñ', x) // Type error: Spanish isn't registered`
</script>

<template>
  <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">{{ usageCode }}</pre>
      </section>
    </main>

    <TanStackDevtools :plugins="plugins" />
  </div>
</template>