Skip to main content
HotkeyPad is framework-agnostic and works seamlessly with any JavaScript framework. This guide shows you how to integrate it with popular frameworks.

React

Basic setup

import { useEffect } from 'react'
import HotKeyPad from 'hotkeypad'
import 'hotkeypad/index.css'

function App() {
  useEffect(() => {
    const hotkeypad = new HotKeyPad({
      placeholder: 'Search commands...',
      activationLetter: 'K'
    })

    hotkeypad.setCommands([
      {
        id: 'home',
        title: 'Go to Home',
        icon: 'home',
        hotkey: `${hotkeypad.activationKey} + H`,
        section: 'Navigation',
        handler: () => {
          window.location.href = '/'
        }
      },
      {
        id: 'about',
        title: 'Go to About',
        icon: 'aboutdotme',
        hotkey: `${hotkeypad.activationKey} + A`,
        section: 'Navigation',
        handler: () => {
          window.location.href = '/about'
        }
      }
    ])
  }, [])

  return (
    <>
      <div id="hotkeypad"></div>
      <div className="app">
        <h1>My React App</h1>
        <p>Press Cmd+K or Ctrl+K to open commands</p>
      </div>
    </>
  )
}

export default App

Custom React hook

Create a reusable hook for better integration:
import { useEffect, useRef } from 'react'
import HotKeyPad from 'hotkeypad'

export function useHotKeyPad(commands, options = {}) {
  const hotkeypadRef = useRef(null)

  useEffect(() => {
    if (!hotkeypadRef.current) {
      hotkeypadRef.current = new HotKeyPad({
        placeholder: 'Search commands...',
        activationLetter: 'K',
        ...options
      })
    }

    if (commands && commands.length > 0) {
      hotkeypadRef.current.setCommands(commands)
    }

    return () => {
      // Cleanup if needed
    }
  }, [commands, options])

  return hotkeypadRef.current
}

Vue

Vue 3 Composition API

<template>
  <div id="hotkeypad"></div>
  <div class="app">
    <h1>My Vue App</h1>
    <p>Press Cmd+K or Ctrl+K to open commands</p>
  </div>
</template>

<script setup>
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'
import HotKeyPad from 'hotkeypad'
import 'hotkeypad/index.css'

const router = useRouter()

onMounted(() => {
  const hotkeypad = new HotKeyPad({
    placeholder: 'Search commands...',
    activationLetter: 'K'
  })

  hotkeypad.setCommands([
    {
      id: 'home',
      title: 'Go to Home',
      icon: 'home',
      hotkey: `${hotkeypad.activationKey} + H`,
      section: 'Navigation',
      handler: () => {
        router.push('/')
      }
    },
    {
      id: 'about',
      title: 'Go to About',
      icon: 'aboutdotme',
      hotkey: `${hotkeypad.activationKey} + A`,
      section: 'Navigation',
      handler: () => {
        router.push('/about')
      }
    },
    {
      id: 'theme',
      title: 'Toggle Dark Mode',
      icon: 'darkreader',
      hotkey: `${hotkeypad.activationKey} + D`,
      section: 'Settings',
      handler: () => {
        document.documentElement.classList.toggle('dark')
      }
    }
  ])
})
</script>

Vue 3 composable

Create a composable for reusability:
import { onMounted, onBeforeUnmount, ref } from 'vue'
import HotKeyPad from 'hotkeypad'

export function useHotKeyPad(commands, options = {}) {
  const hotkeypad = ref(null)

  onMounted(() => {
    hotkeypad.value = new HotKeyPad({
      placeholder: 'Search commands...',
      activationLetter: 'K',
      ...options
    })

    if (commands && commands.length > 0) {
      hotkeypad.value.setCommands(commands)
    }
  })

  onBeforeUnmount(() => {
    // Cleanup if needed
  })

  return { hotkeypad }
}

Svelte

Basic setup

<script>
  import { onMount } from 'svelte'
  import { goto } from '$app/navigation'
  import HotKeyPad from 'hotkeypad'
  import 'hotkeypad/index.css'

  onMount(() => {
    const hotkeypad = new HotKeyPad({
      placeholder: 'Search commands...',
      activationLetter: 'K'
    })

    hotkeypad.setCommands([
      {
        id: 'home',
        title: 'Go to Home',
        icon: 'home',
        hotkey: `${hotkeypad.activationKey} + H`,
        section: 'Navigation',
        handler: () => {
          goto('/')
        }
      },
      {
        id: 'about',
        title: 'Go to About',
        icon: 'aboutdotme',
        hotkey: `${hotkeypad.activationKey} + A`,
        section: 'Navigation',
        handler: () => {
          goto('/about')
        }
      },
      {
        id: 'theme',
        title: 'Toggle Dark Mode',
        icon: 'darkreader',
        hotkey: `${hotkeypad.activationKey} + D`,
        section: 'Settings',
        handler: () => {
          document.documentElement.classList.toggle('dark')
        }
      }
    ])
  })
</script>

<div id="hotkeypad"></div>
<div class="app">
  <h1>My Svelte App</h1>
  <p>Press Cmd+K or Ctrl+K to open commands</p>
</div>

Next.js

App Router (Next.js 13+)

'use client'

import { useEffect } from 'react'
import { useRouter } from 'next/navigation'
import HotKeyPad from 'hotkeypad'
import 'hotkeypad/index.css'

export default function HotKeyPadProvider() {
  const router = useRouter()

  useEffect(() => {
    const hotkeypad = new HotKeyPad({
      placeholder: 'Search commands...',
      activationLetter: 'K'
    })

    hotkeypad.setCommands([
      {
        id: 'home',
        title: 'Go to Home',
        icon: 'home',
        hotkey: `${hotkeypad.activationKey} + H`,
        section: 'Navigation',
        handler: () => router.push('/')
      },
      {
        id: 'blog',
        title: 'Go to Blog',
        icon: 'blogger',
        hotkey: `${hotkeypad.activationKey} + B`,
        section: 'Navigation',
        handler: () => router.push('/blog')
      },
      {
        id: 'docs',
        title: 'Go to Docs',
        icon: 'readthedocs',
        hotkey: `${hotkeypad.activationKey} + D`,
        section: 'Navigation',
        handler: () => router.push('/docs')
      },
      {
        id: 'theme',
        title: 'Toggle Dark Mode',
        icon: 'darkreader',
        hotkey: `${hotkeypad.activationKey} + T`,
        section: 'Settings',
        handler: () => {
          document.documentElement.classList.toggle('dark')
        }
      }
    ])
  }, [])

  return <div id="hotkeypad"></div>
}

Nuxt

Nuxt 3 plugin

import HotKeyPad from 'hotkeypad'
import 'hotkeypad/index.css'

export default defineNuxtPlugin(() => {
  const router = useRouter()

  onMounted(() => {
    const hotkeypad = new HotKeyPad({
      placeholder: 'Search commands...',
      activationLetter: 'K'
    })

    hotkeypad.setCommands([
      {
        id: 'home',
        title: 'Go to Home',
        icon: 'home',
        hotkey: `${hotkeypad.activationKey} + H`,
        section: 'Navigation',
        handler: () => {
          router.push('/')
        }
      },
      {
        id: 'about',
        title: 'Go to About',
        icon: 'aboutdotme',
        hotkey: `${hotkeypad.activationKey} + A`,
        section: 'Navigation',
        handler: () => {
          router.push('/about')
        }
      },
      {
        id: 'theme',
        title: 'Toggle Dark Mode',
        icon: 'darkreader',
        hotkey: `${hotkeypad.activationKey} + D`,
        section: 'Settings',
        handler: () => {
          document.documentElement.classList.toggle('dark')
        }
      }
    ])
  })
})

SvelteKit

<script>
  import { onMount } from 'svelte'
  import { goto } from '$app/navigation'
  import HotKeyPad from 'hotkeypad'
  import 'hotkeypad/index.css'

  onMount(() => {
    const hotkeypad = new HotKeyPad({
      placeholder: 'Search commands...',
      activationLetter: 'K'
    })

    hotkeypad.setCommands([
      {
        id: 'home',
        title: 'Go to Home',
        icon: 'home',
        hotkey: `${hotkeypad.activationKey} + H`,
        section: 'Navigation',
        handler: () => goto('/')
      },
      {
        id: 'about',
        title: 'Go to About',
        icon: 'aboutdotme',
        hotkey: `${hotkeypad.activationKey} + A`,
        section: 'Navigation',
        handler: () => goto('/about')
      },
      {
        id: 'blog',
        title: 'Go to Blog',
        icon: 'blogger',
        hotkey: `${hotkeypad.activationKey} + B`,
        section: 'Navigation',
        handler: () => goto('/blog')
      }
    ])
  })
</script>

<div id="hotkeypad"></div>
<slot />

Build docs developers (and LLMs) love