Skip to content

Alpine Example: CreateHotkey Alternates

import {
  createHotkeysScope,
  defineAppHotkeys,
  detectPlatform,
  hotkeysPlugin,
  provideAppHotkeys,
  resolveHotkeyAlternates,
} from '@tanstack/alpine-hotkeys'
import Alpine from 'alpinejs'
import './index.css'
import type { Hotkey } from '@tanstack/alpine-hotkeys'

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

const appHotkeys = defineAppHotkeys({
  'help.open': {
    hotkey: { default: '?', alternates: ['F1'] },
    meta: { name: 'Open help' },
  },
})

// Alpine has no provider, so publish the app hotkeys before Alpine starts.
provideAppHotkeys(appHotkeys)

class App {
  // The scope reads its default options again on every use. A reactive object
  // makes each hotkey and label follow the platform buttons.
  options = Alpine.reactive({ platform: detectPlatform() })
  private hotkeysScope = createHotkeysScope(this.options)

  // Formats for the scope's platform.
  formatForDisplay = this.hotkeysScope.createFormatForDisplay()
  resolveHotkeyAlternates = resolveHotkeyAlternates
  appHotkeys = this.hotkeysScope.createAppHotkeys()
  customHotkeys = this.hotkeysScope.createCustomHotkeys()
  lastFired: string | null = null

  // `context.hotkey` is the hotkey that fired: the first one or an alternate.
  fire(action: string, hotkey: Hotkey) {
    this.lastFired = `${action} fired from ${this.formatForDisplay(hotkey, { sequenceSeparator: ' then ' })}`
  }

  init() {
    // A platform replaces `default`.
    this.hotkeysScope.createHotkey(
      { default: 'Delete', mac: 'Mod+Backspace' },
      (_event, context) => this.fire('Delete item', context.hotkey),
    )
    this.hotkeysScope.createHotkey(
      { default: 'F3', mac: 'Mod+G' },
      (_event, context) => this.fire('Find next', context.hotkey),
    )

    // `null` leaves a platform without a hotkey.
    this.hotkeysScope.createHotkey(
      { default: null, mac: 'Mod+,' },
      (_event, context) => this.fire('Preferences', context.hotkey),
    )
    this.hotkeysScope.createHotkey(
      { default: 'Alt+Enter', mac: null },
      (_event, context) => this.fire('Properties', context.hotkey),
    )

    // `alternates` adds hotkeys. All of them fire the callback.
    this.hotkeysScope.createHotkey(
      { default: 'Space', alternates: ['K', 'MediaPlayPause'] },
      (_event, context) => this.fire('Play or pause', context.hotkey),
    )

    // An alternate can be a chord string, a chord object, or a sequence. The
    // hotkeys mix chords and a sequence, so both kinds' options are accepted.
    this.hotkeysScope.createHotkey(
      {
        default: 'Mod+K',
        alternates: ['/', { key: 'F', mod: true, shift: true }, ['G', 'S']],
      },
      (_event, context) => this.fire('Open search', context.hotkey),
      { timeout: 800, requireReset: true },
    )

    // An alternate can differ by platform. Windows and Linux take either
    // hotkey, and macOS takes only Mod+Shift+Z.
    this.hotkeysScope.createHotkey(
      {
        default: 'Mod+Y',
        mac: 'Mod+Shift+Z',
        alternates: [{ default: 'Mod+Shift+Z', mac: null }],
      },
      (_event, context) => this.fire('Redo', context.hotkey),
    )

    // An array in `alternates` is a sequence, so these two differ.
    this.hotkeysScope.createHotkey(
      { default: 'X', alternates: ['Q', 'W'] },
      (_event, context) => this.fire('Two alternates', context.hotkey),
    )
    this.hotkeysScope.createHotkey(
      { default: 'Z', alternates: [['Q', 'E']] },
      (_event, context) => this.fire('One sequence', context.hotkey),
    )

    // `createHotkeys` takes the same objects.
    this.hotkeysScope.createHotkeys([
      {
        hotkey: { default: 'Mod+=', alternates: ['Mod+[NumpadAdd]'] },
        callback: (_event, context) => this.fire('Zoom in', context.hotkey),
      },
      {
        hotkey: { default: 'Mod+-', alternates: ['Mod+[NumpadSubtract]'] },
        callback: (_event, context) => this.fire('Zoom out', context.hotkey),
      },
    ])

    // App hotkeys take them too. A custom hotkey replaces the whole object.
    // The getter makes the registration follow a custom hotkey.
    this.hotkeysScope.createHotkey(
      () => this.appHotkeys.value['help.open'],
      (_event, context) => this.fire('Open help', context.hotkey),
    )
  }
  destroy() {
    this.hotkeysScope.destroy()
  }
}
Alpine.data('app', () => new App())
Alpine.start()