Skip to content

Ember Example: Keyboard Layouts

import Component from '@glimmer/component'
import { tracked } from '@glimmer/tracking'

import { useFormatForDisplay, useHotkey } from '@tanstack/ember-hotkeys'
import type { GermanKeys, UsKeys } from '@tanstack/ember-hotkeys'

// 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/ember-hotkeys' {
  interface Register {
    keys: UsKeys | GermanKeys
  }
}

class App extends Component {
  formatForDisplay = useFormatForDisplay()
  usage =
    "import type { GermanKeys, UsKeys } from '@tanstack/ember-hotkeys'\n\ndeclare module '@tanstack/ember-hotkeys' {\n  interface Register {\n    keys: UsKeys | GermanKeys\n  }\n}\n\n{{useHotkey 'Mod+Ö' this.toggleComment}} {{! OK }}\n{{useHotkey 'Mod+[' this.outdent}} {{! OK, because UsKeys is listed }}\n{{useHotkey 'Mod+Ñ' this.search}} {{! Type error: Spanish isn't registered }}"
  @tracked lastFired = ''

  toggleComment = () => (this.lastFired = 'Toggle comment')
  addAnnotation = () => (this.lastFired = 'Add annotation')
  toggleSidebar = () => (this.lastFired = 'Toggle sidebar')
  showSections = () => (this.lastFired = 'Show sections')
  openCommandPalette = () => (this.lastFired = 'Open command palette')
  goToFirstTab = () => (this.lastFired = 'Go to the first tab')
  clear = () => (this.lastFired = '')

  <template>
    {{! These type-check because German keys are registered above. }}
    {{useHotkey 'Mod+Ö' this.toggleComment}}
    {{useHotkey 'Mod+Shift+Ä' this.addAnnotation}}
    {{useHotkey 'ß' this.toggleSidebar}}
    {{useHotkey '§' this.showSections}}

    {{! A to Z, 0 to 9, named keys, and physical codes type-check under every
        registration, and fire on every layout. }}
    {{useHotkey 'Mod+K' this.openCommandPalette}}
    {{useHotkey 'Alt+[Digit1]' this.goToFirstTab}}
    {{useHotkey 'Escape' this.clear}}

    <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'>
          {{#if this.lastFired}}
            Last fired:
            {{this.lastFired}}
          {{else}}
            Press a hotkey below.
          {{/if}}
        </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>{{this.formatForDisplay 'Mod+Ö'}}</kbd>
              Toggle comment
            </div>
            <div class='hotkey-row'>
              <kbd>{{this.formatForDisplay 'Mod+Shift+Ä'}}</kbd>
              Add annotation
            </div>
            <div class='hotkey-row'>
              <kbd>{{this.formatForDisplay 'ß'}}</kbd>
              Toggle sidebar
            </div>
            <div class='hotkey-row'>
              <kbd>{{this.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>{{this.formatForDisplay 'Mod+K'}}</kbd>
              Open command palette
            </div>
            <div class='hotkey-row'>
              <kbd>{{this.formatForDisplay 'Alt+[Digit1]'}}</kbd>
              Go to the first tab
            </div>
            <div class='hotkey-row'>
              <kbd>{{this.formatForDisplay 'Escape'}}</kbd>
              Clear
            </div>
          </div>
        </section>

        <section class='demo-section'>
          <h2>Usage</h2>
          <pre class='code-block'>{{this.usage}}</pre>
        </section>
      </main>
    </div>
  </template>
}
export default App