@tanstack/react-hotkeys
Version:
React adapter for TanStack Hotkeys
39 lines (36 loc) • 1.12 kB
JavaScript
'use client';
import { useSelector } from "./_useSelector.js";
import { getHotkeyManager } from "@tanstack/hotkeys";
import { getHotkeyRegistrationViews } from "@tanstack/hotkeys/adapter";
// src/useHotkeyRegistrations.ts
/**
* React hook that reactively reads every hotkey registration, chords and
* sequences, from the singleton HotkeyManager. Narrow each view with `kind`.
*
* This hook does NOT require the HotkeysProvider. It re-renders when
* registrations change, including trigger counts and sequence progress.
*
* @returns All registrations, without callbacks
*
* @example
* ```tsx
* function ShortcutPalette() {
* const registrations = useHotkeyRegistrations()
* const formatForDisplay = useFormatForDisplay()
*
* return (
* <ul>
* {registrations.map((reg) => (
* <li key={reg.id}>
* {reg.options.meta?.name}: {formatForDisplay(reg.hotkey)}
* </li>
* ))}
* </ul>
* )
* }
* ```
*/
function useHotkeyRegistrations() {
return useSelector(getHotkeyManager().registrations, (state) => getHotkeyRegistrationViews(state));
}
export { useHotkeyRegistrations };