Skip to content

Preact Example: Keyboard Layouts

import { TanStackDevtools } from '@tanstack/preact-devtools'
import {
  HotkeysProvider,
  useFormatForDisplay,
  useHotkey,
} from '@tanstack/preact-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/preact-hotkeys-devtools'
import { render } from 'preact'
import { useState } from 'preact/hooks'
import type { GermanKeys, UsKeys } from '@tanstack/preact-hotkeys'
import './index.css'

// Hotkey strings accept the keys that US and German keyboards type. Without
// this, only US keys type-check, and `Mod+Ö` is an error. This changes types
// only: matching works the same with or without it.
declare module '@tanstack/preact-hotkeys' {
  interface Register {
    keys: UsKeys | GermanKeys
  }
}

function App() {
  const formatForDisplay = useFormatForDisplay()
  const [lastFired, setLastFired] = useState('')

  // These type-check because German keys are registered above.
  useHotkey('Mod+Ö', () => setLastFired('Toggle comment'))
  useHotkey('Mod+Shift+Ä', () => setLastFired('Add annotation'))
  useHotkey('ß', () => setLastFired('Toggle sidebar'))
  useHotkey('§', () => setLastFired('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', () => setLastFired('Open command palette'))
  useHotkey('Alt+[Digit1]', () => setLastFired('Go to the first tab'))
  useHotkey('Escape', () => setLastFired(''))

  return (
    <div className="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" className="note">
          {lastFired ? `Last fired: ${lastFired}` : 'Press a hotkey below.'}
        </p>

        <section className="demo-section">
          <h2>German keys</h2>
          <p className="note">
            Switch your keyboard layout to German to press these.
          </p>
          <div className="hotkey-list">
            <div className="hotkey-row">
              <kbd>{formatForDisplay('Mod+Ö')}</kbd> Toggle comment
            </div>
            <div className="hotkey-row">
              <kbd>{formatForDisplay('Mod+Shift+Ä')}</kbd> Add annotation
            </div>
            <div className="hotkey-row">
              <kbd>{formatForDisplay('ß')}</kbd> Toggle sidebar
            </div>
            <div className="hotkey-row">
              <kbd>{formatForDisplay('§')}</kbd> Show sections
            </div>
          </div>
        </section>

        <section className="demo-section">
          <h2>Keys on every layout</h2>
          <div className="hotkey-list">
            <div className="hotkey-row">
              <kbd>{formatForDisplay('Mod+K')}</kbd> Open command palette
            </div>
            <div className="hotkey-row">
              <kbd>{formatForDisplay('Alt+[Digit1]')}</kbd> Go to the first tab
            </div>
            <div className="hotkey-row">
              <kbd>{formatForDisplay('Escape')}</kbd> Clear
            </div>
          </div>
        </section>

        <section className="demo-section">
          <h2>Usage</h2>
          <pre className="code-block">{`import type { GermanKeys, UsKeys } from '@tanstack/preact-hotkeys'

declare module '@tanstack/preact-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`}</pre>
        </section>
      </main>
    </div>
  )
}

// TanStackDevtools as sibling of App to avoid Preact hook errors when hotkeys update state
const devtoolsPlugins = [hotkeysDevtoolsPlugin()]

render(
  <HotkeysProvider>
    <App />
    <TanStackDevtools plugins={devtoolsPlugins} />
  </HotkeysProvider>,
  document.getElementById('root')!,
)