Skip to content

Alpine Example: CreateHotkeys Sequences

import {
  createHotkeysScope,
  defineHotkey,
  hotkeysPlugin,
} from '@tanstack/alpine-hotkeys'
import Alpine from 'alpinejs'
import './index.css'

// Gives every hotkeys scope the app's Alpine instance.
Alpine.plugin(hotkeysPlugin)

class App {
  // Owns this component's hotkeys and subscriptions until `destroy()`.
  private hotkeysScope = createHotkeysScope()

  formatForDisplay = this.hotkeysScope.createFormatForDisplay()
  // Every registration on the page, in `.hotkeys`. Sequence views also
  // carry matchedStepCount, the steps pressed so far in the current attempt.
  registrations = this.hotkeysScope.createHotkeyRegistrations()

  lastCommand = 'none'
  vimEnabled = true
  arrows = { konami: 0, side: 0, steps: 0 }

  init() {
    // Arrays are sequences, and chords can share the list. The second
    // argument holds options for every item: `timeout` reaches only the
    // sequences, so the Shift+G chord ignores it. A sequence of letters
    // counts as a character-key shortcut, so the checkbox gives users a way
    // to turn the list off. A getter keeps `enabled` in step with it.
    this.hotkeysScope.createHotkeys(
      [
        {
          hotkey: ['G', 'G'],
          callback: () => {
            this.lastCommand = 'Go to top'
          },
          options: { meta: { name: 'Go to top' } },
        },
        {
          hotkey: 'Shift+G',
          callback: () => {
            this.lastCommand = 'Go to bottom'
          },
          options: { meta: { name: 'Go to bottom' } },
        },
        {
          hotkey: ['D', 'D'],
          callback: () => {
            this.lastCommand = 'Delete line'
          },
          options: { meta: { name: 'Delete line' } },
        },
        {
          hotkey: ['D', 'W'],
          callback: () => {
            this.lastCommand = 'Delete word'
          },
          options: { meta: { name: 'Delete word' } },
        },
        {
          hotkey: ['C', 'I', 'W'],
          callback: () => {
            this.lastCommand = 'Change inner word'
          },
          options: { meta: { name: 'Change inner word' } },
        },
      ],
      () => ({ timeout: 1500, enabled: this.vimEnabled }),
    )

    // createHotkeys gives every array item one type that fits any hotkey, so
    // an item's callback `context` can be a chord or a sequence. defineHotkey
    // types one definition by its hotkey and returns it unchanged. This
    // hotkey is a sequence, so `context.parsedSequence` needs no kind check,
    // and `requireReset` would be a type error because it applies only to
    // chords.
    const konami = defineHotkey({
      hotkey: ['ArrowUp', 'ArrowUp', 'ArrowDown', 'ArrowDown'],
      callback: (_event, context) => {
        this.arrows.konami++
        this.arrows.steps = context.parsedSequence.length
      },
      // Allow 2 seconds between steps instead of the default 1 second.
      options: { timeout: 2000, meta: { name: 'Konami code' } },
    })

    // A defined hotkey and an inline item can share one array.
    this.hotkeysScope.createHotkeys([
      konami,
      {
        hotkey: ['ArrowLeft', 'ArrowRight', 'ArrowLeft', 'ArrowRight'],
        callback: () => {
          this.arrows.side++
        },
        options: { meta: { name: 'Side to side' } },
      },
    ])
  }

  destroy() {
    this.hotkeysScope.destroy()
  }
}

Alpine.data('app', () => new App())
Alpine.start()