Skip to content

Octane Example: Keyboard Layouts

import { createRoot, useState } from 'octane'

import {
  HotkeysProvider,
  useFormatForDisplay,
  useHotkey,
} from '@tanstack/octane-hotkeys'

import type { GermanKeys, UsKeys } from '@tanstack/octane-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/octane-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(''))

  <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/octane-hotkeys'

declare module '@tanstack/octane-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>
}

function Root() @{
  <HotkeysProvider>
    <App />
  </HotkeysProvider>
}

const root = createRoot(document.getElementById('root')!)
root.render(Root)
import.meta.hot?.dispose(() => root.unmount())