Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | 30x 30x 30x 30x 30x 26x 27x 30x 27x 30x 30x 30x 30x 30x 28x 2x 2x 28x 30x 35x 35x 35x 1x 34x 1x 33x 1x 32x 32x 32x 3x 32x 7x 32x 2x 2x 32x 33x 33x 33x 7x 26x 26x 30x 5x 5x 26x 21x 1x 21x 21x 21x 21x 21x 21x 30x 30x 30x 28x 28x | import { useCallback, useEffect, useMemo, useRef } from "react";
import { useEventListener } from "@usefy/use-event-listener";
import { useLatest } from "@usefy/use-latest";
import type {
Hotkey,
HotkeyHandler,
HotkeyTarget,
ParsedChord,
ParsedHotkey,
UseHotkeysOptions,
} from "./types";
import {
MODIFIER_KEY_NAMES,
chordMatches,
eventToChord,
isEditableTarget,
isMacPlatform,
parseHotkey,
} from "./utils";
/**
* A React hook for high-level keyboard shortcuts, built on top of a single
* managed keyboard listener (via `@usefy/use-event-listener`).
*
* It is a purpose-built handler API rather than a thin wrapper over
* `@usefy/use-key-press`: that sibling hook returns the *held* state of a key
* as a boolean and has no notion of chord sequences, so `useHotkeys` owns its
* own listener plus a sequence buffer to deliver a callback-style API with
* multi-combo sequences, a cross-platform `mod` alias, scoping, and an
* input-field guard.
*
* Features:
* - **Combos** — `"mod+k"`, `"ctrl+shift+p"`, `"shift+?"`, `"Escape"`, `"ArrowUp"`.
* - **Sequences** — space-separated combos such as `"g i"` or `"g g"`, with a
* configurable reset timeout ({@link UseHotkeysOptions.sequenceTimeoutMs}).
* - **Multiple bindings** — pass an array to bind several hotkeys to one handler.
* - **`mod` alias** — Cmd on macOS, Ctrl on Windows/Linux (SSR-safe detection,
* overridable via {@link UseHotkeysOptions.mac}).
* - **Exact modifiers** — `"a"` does not fire on `Ctrl+A`; `"ctrl+a"` does not
* fire on a plain `a`.
* - **Input-field guard** — ignores editable targets by default
* ({@link UseHotkeysOptions.enableOnFormTags} opts out).
* - **Scoping** — bind to `document` (default), `window`, an element, or a ref.
* - **SSR-safe & StrictMode-safe** — no listeners on the server, no duplicate
* listeners or leaked sequence timers, full cleanup on unmount and on
* `enabled`/`target`/`keys` change.
*
* The `handler` is stored in a ref, so it never needs to be memoized by the
* caller and the listener is not re-subscribed when only the handler changes.
*
* @param keys - A hotkey string or an array of hotkey strings (all bound to `handler`).
* @param handler - Called with the {@link KeyboardEvent} and match details when a hotkey fires.
* @param options - Configuration. See {@link UseHotkeysOptions}.
*
* @example
* ```tsx
* // Command palette (mod = Ctrl on Win/Linux, Cmd on macOS)
* function App() {
* const [open, setOpen] = useState(false);
* useHotkeys("mod+k", () => setOpen((o) => !o), { preventDefault: true });
* return open ? <CommandPalette /> : null;
* }
* ```
*
* @example
* ```tsx
* // Multiple bindings for one handler
* useHotkeys(["mod+s", "ctrl+p"], (event) => {
* event.preventDefault();
* save();
* });
* ```
*
* @example
* ```tsx
* // A sequence: press "g" then "i" to go to the inbox
* useHotkeys("g i", () => navigate("/inbox"));
* ```
*
* @example
* ```tsx
* // Scoped to an element, allowed inside form fields, only while enabled
* const ref = useRef<HTMLDivElement>(null);
* useHotkeys("Escape", close, {
* target: ref,
* enableOnFormTags: true,
* enabled: isOpen,
* });
* ```
*/
export function useHotkeys(
keys: Hotkey | Hotkey[],
handler: HotkeyHandler,
options: UseHotkeysOptions = {}
): void {
const {
enabled = true,
target,
eventType = "keydown",
enableOnFormTags = false,
preventDefault = false,
sequenceTimeoutMs = 1000,
mac,
} = options;
// Keep the latest handler in a ref so callers don't need to memoize it and
// the listener is never re-subscribed when only the handler changes.
const handlerRef = useLatest(handler);
// Serialized identity of the bindings, used as a stable memo/effect key.
const keysKey = Array.isArray(keys) ? keys.join("␟") : keys;
const isMac = mac ?? isMacPlatform();
const bindings = useMemo<ParsedHotkey[]>(() => {
const list = Array.isArray(keys) ? keys : [keys];
return list.map((binding) => parseHotkey(binding, { mac: isMac }));
// `keysKey` captures the serialized identity of `keys`.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [keysKey, isMac]);
// Longest sequence — the sequence buffer never needs to hold more than this.
const maxChords = useMemo(
() => bindings.reduce((max, binding) => Math.max(max, binding.chords.length), 1),
[bindings]
);
// Rolling buffer of recently pressed chords (for sequence matching), plus the
// reset timer. Both live in refs so the stable listener can read/mutate them.
const bufferRef = useRef<ParsedChord[]>([]);
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
// Latest reactive config, mirrored into a ref so the stable listener never
// goes stale and never needs re-registering when only options change.
const configRef = useRef({
bindings,
maxChords,
enableOnFormTags,
preventDefault,
sequenceTimeoutMs,
});
configRef.current = {
bindings,
maxChords,
enableOnFormTags,
preventDefault,
sequenceTimeoutMs,
};
const clearSequence = useCallback(() => {
if (timerRef.current !== null) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
bufferRef.current = [];
}, []);
const listener = useCallback((event: Event) => {
const kbd = event as KeyboardEvent;
const config = configRef.current;
// Do not fire while the user is typing in a form field, unless opted in.
if (!config.enableOnFormTags && isEditableTarget(kbd.target)) {
return;
}
// A lone modifier press neither starts a combo nor breaks a sequence.
if (MODIFIER_KEY_NAMES.has(kbd.key)) {
return;
}
// Ignore auto-repeat (held keys) so a held combo fires once and does not
// flood the sequence buffer. (keyup events never repeat.)
if (kbd.repeat) {
return;
}
// Append the current chord to the rolling buffer, capped to the longest
// sequence so single-combo bindings never accumulate history.
const buffer = bufferRef.current;
buffer.push(eventToChord(kbd));
if (buffer.length > config.maxChords) {
buffer.splice(0, buffer.length - config.maxChords);
}
// (Re)start the sequence reset timer.
if (timerRef.current !== null) {
clearTimeout(timerRef.current);
}
timerRef.current = setTimeout(() => {
bufferRef.current = [];
timerRef.current = null;
}, config.sequenceTimeoutMs);
// Match against each binding: the last N buffered chords must equal the
// binding's N combos in order. First binding wins.
for (let i = 0; i < config.bindings.length; i++) {
const binding = config.bindings[i];
const n = binding.chords.length;
if (n === 0 || buffer.length < n) {
continue;
}
let matched = true;
for (let j = 0; j < n; j++) {
if (!chordMatches(buffer[buffer.length - n + j], binding.chords[j])) {
matched = false;
break;
}
}
if (matched) {
if (config.preventDefault) {
kbd.preventDefault();
}
// Consume the buffer so a completed hotkey does not retrigger or bleed
// into the next one.
bufferRef.current = [];
Eif (timerRef.current !== null) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
handlerRef.current(kbd, {
hotkey: binding.raw,
index: i,
sequence: n > 1,
});
return;
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
// Default to `document`; pass refs/elements straight through so
// `useEventListener` resolves them (including `.current`) inside its effect.
const listenerTarget: HotkeyTarget =
target !== undefined
? target
: typeof document !== "undefined"
? document
: null;
useEventListener(eventType, listener, listenerTarget, { enabled });
// Reset sequence tracking whenever the matching config changes, and always
// clear the pending timer on unmount (StrictMode-safe).
useEffect(() => {
return () => {
clearSequence();
};
}, [
clearSequence,
enabled,
eventType,
keysKey,
listenerTarget,
sequenceTimeoutMs,
]);
}
|