hotkey-router 0.1.0 โ†’ 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,56 +1,45 @@
1
1
  # hotkey-router
2
2
 
3
- [![npm version](https://img.shields.io/npm/v/hotkey-router.svg)](https://www.npmjs.com/package/hotkey-router)
3
+ [![npm](https://img.shields.io/npm/v/hotkey-router)](https://www.npmjs.com/package/hotkey-router)
4
+ [![downloads](https://img.shields.io/npm/dm/hotkey-router)](https://www.npmjs.com/package/hotkey-router)
4
5
  [![bundle size](https://img.shields.io/bundlephobia/minzip/hotkey-router)](https://bundlephobia.com/package/hotkey-router)
5
- [![GitHub stars](https://img.shields.io/github/stars/iWhatty/hotkey-router-js?style=social)](https://github.com/iWhatty/hotkey-router-js)
6
+ [![license](https://img.shields.io/npm/l/hotkey-router)](https://github.com/iWhatty/hotkey-router-js/blob/main/LICENSE)
7
+ [![stars](https://img.shields.io/github/stars/iWhatty/hotkey-router-js?style=social)](https://github.com/iWhatty/hotkey-router-js)
6
8
 
7
- **A tiny, deterministic keyboard routing engine for modern web apps.**
9
+ A tiny, deterministic keyboard routing engine for modern web apps. Not a key utility, a predictable plugin-first routing layer for keyboard shortcuts.
8
10
 
9
- Hotkey Router is not a key utility.
10
- It is a predictable, plugin-first routing layer for keyboard shortcuts.
11
+ ## Features
11
12
 
12
- * โšก O(1) dispatch
13
- * ๐Ÿงฉ Plugin-safe lifecycle management
14
- * ๐ŸŽฏ Deterministic winner selection (priority + recency)
15
- * ๐Ÿ›‘ Input-safe by default
16
- * ๐Ÿงช Testable via `trigger()`
17
- * ๐Ÿ“ฆ 4.9 kB minified
18
- * ๐Ÿ—œ 2.2 kB minified + gzipped
19
- * ๐Ÿšซ Zero dependencies
20
-
21
- ---
22
-
23
- ## Philosophy
24
-
25
- Hotkey Router follows three core rules:
26
-
27
- 1. **Predictable routing** โ€” Highest priority wins. Ties go to the most recently bound handler.
28
- 2. **Safe composition** โ€” Plugins can register and unregister without affecting others.
29
- 3. **Modern only** โ€” Built for modern browsers using `KeyboardEvent.key`.
30
-
31
- No keycodes. No legacy IE hacks. No hidden global scope state.
13
+ - O(1) dispatch
14
+ - Plugin-safe lifecycle management
15
+ - Deterministic winner selection (priority + recency)
16
+ - Input-safe by default
17
+ - Testable via `trigger()`
18
+ - Optional opt-in browser/OS conflict warnings (tree-shakable)
19
+ - ~3 kB minified + gzipped (core); ~7.5 kB with conflict warnings
20
+ - Zero dependencies
32
21
 
33
22
  ---
34
23
 
35
24
  ## Install
36
25
 
37
- ```bash
38
- npm install hotkey-router
26
+ ```sh
27
+ pnpm add hotkey-router
39
28
  ```
40
29
 
41
- ### ESM
30
+ ESM:
42
31
 
43
32
  ```js
44
33
  import hotkeys from 'hotkey-router'
45
34
  ```
46
35
 
47
- ### CommonJS
36
+ CommonJS:
48
37
 
49
38
  ```js
50
39
  const hotkeys = require('hotkey-router')
51
40
  ```
52
41
 
53
- ### CDN (ESM)
42
+ CDN (ESM):
54
43
 
55
44
  ```js
56
45
  import hotkeys from 'https://cdn.jsdelivr.net/npm/hotkey-router/dist/hotkey-router.min.js'
@@ -58,7 +47,7 @@ import hotkeys from 'https://cdn.jsdelivr.net/npm/hotkey-router/dist/hotkey-rout
58
47
 
59
48
  ---
60
49
 
61
- # Basic Usage
50
+ ## Quick start
62
51
 
63
52
  ```js
64
53
  import hotkeys from 'hotkey-router'
@@ -82,8 +71,8 @@ hotkeys.bind('^k', () => {
82
71
  hotkeys.bind('alt', enterSelectMode) // fires on Alt keydown
83
72
  hotkeys.bind('alt up', exitSelectMode) // fires on Alt keyup
84
73
 
85
- // Layout-stable matching via KeyboardEvent.code (cross-platform Alt+letter
86
- // โ€” works on macOS where Option remaps Alt+X -> โ‰ˆ)
74
+ // Layout-stable matching via KeyboardEvent.code (cross-platform Alt+letter,
75
+ // works on macOS where Option remaps Alt+X to โ‰ˆ)
87
76
  hotkeys.bind('alt+code:KeyX', deleteHovered, null, { preventDefault: true })
88
77
 
89
78
  // Plugin grouping
@@ -93,62 +82,13 @@ hotkeys.registerPlugin('docs', {
93
82
  })
94
83
  ```
95
84
 
96
- ## Bare-modifier bindings (v0.1.0+)
97
-
98
- Some UX patterns are driven by a bare modifier rather than a chord โ€” e.g. "hold Alt to enter select mode, release Alt to exit."
99
-
100
- ```js
101
- hotkeys.bind('alt', onAltDown) // fires on Alt keydown
102
- hotkeys.bind('alt up', onAltUp) // fires on Alt keyup
103
- hotkeys.bind('ctrl', onCtrlDown) // any single modifier supported
104
- ```
105
-
106
- Notes:
107
- - Only **single** bare modifiers are supported. Multi-modifier bare bindings (`'ctrl+alt'`) throw โ€” add a base key for those.
108
- - Default `repeat: false` applies, so a held modifier fires only once on keydown.
109
- - Loose match: a bare-Alt binding fires whenever the Alt key is the one being pressed/released, regardless of which other modifier flags are also set. Add a `when` filter for exact-set semantics.
110
-
111
- ## Code-based matching (v0.1.0+)
112
-
113
- `KeyboardEvent.key` is layout- and modifier-dependent โ€” Alt+X gives `โ‰ˆ` on macOS, `x` on Linux/Windows. For shortcuts that should be stable across platforms, bind to `KeyboardEvent.code` instead:
114
-
115
- ```js
116
- hotkeys.bind('alt+code:KeyX', deleteHovered) // matches the physical X key
117
- hotkeys.bind('ctrl+code:Digit1', goToTab1) // matches digit row, not numpad
118
- hotkeys.bind('!code:KeyX', deleteHovered) // AHK shorthand also works
119
- ```
120
-
121
- The `code:` value is **case-sensitive** (matches the camelCase `KeyboardEvent.code` spec values: `KeyA`, `Digit1`, `ArrowLeft`, etc.). Only one `code:` token per binding is allowed; multiple `code:` tokens throw a parse error.
122
-
123
- Both key-based and code-based bindings can coexist; the standard priority + recency rules pick the winner.
124
-
125
85
  ---
126
86
 
127
- # Routing Model
87
+ ## API
128
88
 
129
- When multiple handlers match the same hotkey:
130
-
131
- 1. Highest `priority` wins
132
- 2. If equal priority โ†’ newest binding wins
89
+ ### `bind(hotkey, handler, plugin?, options?)`
133
90
 
134
- This makes modal overrides simple:
135
-
136
- ```js
137
- hotkeys.bind('escape', closeModal, null, {
138
- priority: 100,
139
- preventDefault: true,
140
- })
141
- ```
142
-
143
- ---
144
-
145
- # API
146
-
147
- ## `bind(hotkey, handler, plugin?, options?)`
148
-
149
- Register a hotkey.
150
-
151
- Returns an `off()` function.
91
+ Register a hotkey. Returns an `off()` function.
152
92
 
153
93
  ```js
154
94
  const off = hotkeys.bind('mod+k', openPalette)
@@ -157,22 +97,23 @@ const off = hotkeys.bind('mod+k', openPalette)
157
97
  off()
158
98
  ```
159
99
 
160
- ### Options
100
+ **Options**
161
101
 
162
102
  ```ts
163
103
  {
164
104
  preventDefault?: boolean
165
105
  stopPropagation?: boolean
166
106
  stopImmediatePropagation?: boolean
167
- repeat?: boolean // default false on keydown
107
+ repeat?: boolean // default false on keydown
168
108
  once?: boolean
169
109
  when?: (event) => boolean
170
110
  allowIn?: (event) => boolean
171
- priority?: number // default 0
111
+ priority?: number // default 0
112
+ warnOnReserved?: boolean // only read when conflict warnings are installed
172
113
  }
173
114
  ```
174
115
 
175
- ### Examples
116
+ **Examples**
176
117
 
177
118
  Ignore repeat keydown (default behavior):
178
119
 
@@ -203,9 +144,7 @@ Run once:
203
144
  hotkeys.bind('ctrl+s', saveDraft, null, { once: true })
204
145
  ```
205
146
 
206
- ---
207
-
208
- ## `unbind(hotkey, handler?)`
147
+ ### `unbind(hotkey, handler?)`
209
148
 
210
149
  Remove bindings.
211
150
 
@@ -214,9 +153,7 @@ hotkeys.unbind('ctrl+k')
214
153
  hotkeys.unbind('ctrl+k', openPalette)
215
154
  ```
216
155
 
217
- ---
218
-
219
- ## `registerPlugin(name, map)`
156
+ ### `registerPlugin(name, map)`
220
157
 
221
158
  Batch register hotkeys under a plugin namespace.
222
159
 
@@ -230,37 +167,42 @@ const unregister = hotkeys.registerPlugin('files', {
230
167
  unregister()
231
168
  ```
232
169
 
233
- Plugin cleanup is isolated โ€” removing one plugin never affects other bindings.
170
+ Plugin cleanup is isolated. Removing one plugin never affects other bindings.
234
171
 
235
- ---
236
-
237
- ## `unregisterPlugin(name)`
172
+ ### `unregisterPlugin(name)`
238
173
 
239
174
  Remove all hotkeys associated with a plugin.
240
175
 
241
- ---
176
+ ### `onBind(hook)`
242
177
 
243
- ## `pause()` / `resume()`
178
+ Subscribe to bind events. The hook receives `{ combo, raw, options, plugin, id }` after every successful `bind()`. Returns an unsubscribe function. Used internally by `installReservationWarnings`; exposed for telemetry, dev panels, and custom validation.
244
179
 
245
- Temporarily disable or re-enable all routing.
180
+ ```js
181
+ const off = hotkeys.onBind(({ raw }) => {
182
+ console.debug('bound:', raw)
183
+ })
184
+ // off() unsubscribes.
185
+ ```
246
186
 
247
- ---
187
+ Hook errors are caught and logged via `console.error`. A buggy hook can't break the bind.
188
+
189
+ ### `pause()` / `resume()`
248
190
 
249
- ## `ignoreInput(boolean = true)`
191
+ Temporarily disable or re-enable all routing.
192
+
193
+ ### `ignoreInput(boolean = true)`
250
194
 
251
195
  By default, hotkeys do **not** fire inside:
252
196
 
253
- * `<input>`
254
- * `<textarea>`
255
- * `<select>`
256
- * `[contenteditable]`
257
- * `role="textbox"`
197
+ - `<input>`
198
+ - `<textarea>`
199
+ - `<select>`
200
+ - `[contenteditable]`
201
+ - `role="textbox"`
258
202
 
259
203
  Override per-binding with `allowIn()`.
260
204
 
261
- ---
262
-
263
- ## `init(options?)`
205
+ ### `init(options?)`
264
206
 
265
207
  Manually attach listeners.
266
208
 
@@ -273,18 +215,13 @@ hotkeys.init({
273
215
 
274
216
  Auto-initializes on `window` by default (browser environments).
275
217
 
276
- ---
277
-
278
- ## `destroy()`
218
+ ### `destroy()`
279
219
 
280
220
  Removes all listeners and clears internal state.
281
221
 
282
- ---
283
-
284
- ## `trigger(hotkey, options?)`
222
+ ### `trigger(hotkey, options?)`
285
223
 
286
- Programmatically trigger a hotkey.
287
- Useful for testing.
224
+ Programmatically trigger a hotkey. Useful for testing.
288
225
 
289
226
  ```js
290
227
  hotkeys.trigger('ctrl+k')
@@ -294,77 +231,210 @@ Returns `true` if a handler ran.
294
231
 
295
232
  ---
296
233
 
297
- # Supported Syntax
234
+ ## Notes
298
235
 
299
- ## Standard
236
+ ### Routing model
300
237
 
301
- * `ctrl+k`
302
- * `shift+a`
303
- * `ctrl+k up`
304
- * `mod+s` (meta on macOS, ctrl elsewhere)
305
- * `ctrl++` or `ctrl+plus`
238
+ When multiple handlers match the same hotkey:
306
239
 
307
- ## AHK-Style Prefix Modifiers
240
+ 1. Highest `priority` wins.
241
+ 2. If equal priority, newest binding wins.
308
242
 
309
- * `^k` โ†’ `ctrl+k`
310
- * `!k` โ†’ `alt+k`
311
- * `+k` โ†’ `shift+k`
312
- * `#k` โ†’ `meta+k`
313
- * `^!k` โ†’ `ctrl+alt+k`
243
+ This makes modal overrides simple:
314
244
 
315
- Modifiers must appear before the base key.
245
+ ```js
246
+ hotkeys.bind('escape', closeModal, null, {
247
+ priority: 100,
248
+ preventDefault: true,
249
+ })
250
+ ```
251
+
252
+ ### Bare-modifier bindings (v0.1.0+)
253
+
254
+ Some UX patterns are driven by a bare modifier rather than a chord. For example, "hold Alt to enter select mode, release Alt to exit."
255
+
256
+ ```js
257
+ hotkeys.bind('alt', onAltDown) // fires on Alt keydown
258
+ hotkeys.bind('alt up', onAltUp) // fires on Alt keyup
259
+ hotkeys.bind('ctrl', onCtrlDown) // any single modifier supported
260
+ ```
316
261
 
317
- ## Aliases Supported
262
+ - Only **single** bare modifiers are supported. Multi-modifier bare bindings (`'ctrl+alt'`) throw, add a base key for those.
263
+ - Default `repeat: false` applies, so a held modifier fires only once on keydown.
264
+ - Loose match: a bare-Alt binding fires whenever the Alt key is the one being pressed/released, regardless of which other modifier flags are also set. Add a `when` filter for exact-set semantics.
265
+
266
+ ### Code-based matching (v0.1.0+)
267
+
268
+ `KeyboardEvent.key` is layout- and modifier-dependent. Alt+X gives `โ‰ˆ` on macOS, `x` on Linux/Windows. For shortcuts that should be stable across platforms, bind to `KeyboardEvent.code` instead:
269
+
270
+ ```js
271
+ hotkeys.bind('alt+code:KeyX', deleteHovered) // matches the physical X key
272
+ hotkeys.bind('ctrl+code:Digit1', goToTab1) // matches digit row, not numpad
273
+ hotkeys.bind('!code:KeyX', deleteHovered) // AHK shorthand also works
274
+ ```
318
275
 
319
- ### Modifiers
276
+ The `code:` value is **case-sensitive** (matches the camelCase `KeyboardEvent.code` spec values: `KeyA`, `Digit1`, `ArrowLeft`, etc.). Only one `code:` token per binding is allowed; multiple `code:` tokens throw a parse error. Both key-based and code-based bindings can coexist; the standard priority + recency rules pick the winner.
320
277
 
321
- * `ctrl`, `control`, `โŒƒ`
322
- * `shift`, `โ‡ง`, `+`
323
- * `alt`, `option`, `โŒฅ`, `!`
324
- * `meta`, `cmd`, `command`, `win`, `โŒ˜`, `#`
325
- * `mod` (meta on macOS, ctrl elsewhere)
278
+ ### Conflict warnings (v0.2.0+)
326
279
 
327
- ### Navigation / Special Keys
280
+ Some combos are reserved by the browser chrome (find bar, devtools, bookmarks) or the OS (Spotlight, window management). They never reach page-world JavaScript, no matter how early you listen or whether you call `preventDefault`. hotkey-router ships an **opt-in** reservation table that emits a soft warning at bind time, so the silent failure becomes a noisy one.
328
281
 
329
- * `escape`, `esc`
330
- * `enter`, `return`
331
- * `space`
332
- * `tab`
333
- * `backspace`
334
- * `delete`, `del`
335
- * `home`, `end`
336
- * `pageup`, `pgup`
337
- * `pagedown`, `pgdn`
338
- * `up`, `down`, `left`, `right`
339
- * `f1`โ€“`f19`
282
+ The feature is opt-in by design: the core router stays ~3 KB gzipped, and the ~5 KB reservation data is tree-shaken out entirely unless you import it.
283
+
284
+ **Two ways to opt in.** One-line ergonomic, use the `auto` entry. Same default export as `hotkey-router`, with warnings pre-installed:
285
+
286
+ ```js
287
+ import hotkeys from 'hotkey-router/auto'
288
+
289
+ hotkeys.bind('meta+shift+f', toggleFullscreen)
290
+ // Firefox on macOS:
291
+ // [hotkey-router] "meta+shift+f" reserved by firefox on macOS:
292
+ // "Toggle fullscreen" [hard], will not fire.
293
+ ```
294
+
295
+ Explicit install, keep using the tiny core, install warnings yourself:
296
+
297
+ ```js
298
+ import hotkeys from 'hotkey-router'
299
+ import { installReservationWarnings } from 'hotkey-router/reservations'
300
+
301
+ installReservationWarnings(hotkeys)
302
+ hotkeys.bind('meta+shift+f', toggleFullscreen)
303
+ ```
304
+
305
+ `installReservationWarnings()` returns an `uninstall` function if you ever need to detach. The warning is **never fatal**, the binding is still registered, in case you're running in a browser/platform where the conflict doesn't apply.
306
+
307
+ **Severity to log level:**
308
+
309
+ | Severity | Log level | Meaning |
310
+ | ----------------- | --------- | ------------------------------------------------------- |
311
+ | `hard` | `warn` | Browser intercepts before page world; combo won't fire. |
312
+ | `os` | `warn` | OS intercepts globally. |
313
+ | `menu-activation` | `warn` | Alt+letter activates the browser menu bar (Win/Linux). |
314
+ | `find-bar-only` | `info` | Reserved only when the find bar is focused. |
315
+ | `compose` | `info` | macOS Option+letter types a special char in inputs. |
316
+ | `system-text` | `info` | Mac Ctrl+letter cursor controls inside text inputs. |
317
+ | `devtools-open` | `info` | Only relevant when DevTools is already open. |
318
+
319
+ **Per-bind opt-out:**
320
+
321
+ ```js
322
+ hotkeys.bind('meta+shift+f', toggleFullscreen, null, { warnOnReserved: false })
323
+ ```
324
+
325
+ To turn warnings off globally, just don't install them (use the bare `hotkey-router` entry instead of `hotkey-router/auto`).
326
+
327
+ **Force a platform/browser (tests, SSR previews):**
328
+
329
+ ```js
330
+ installReservationWarnings(hotkeys, { platform: 'mac', browser: 'firefox' })
331
+ ```
332
+
333
+ Accepted platforms: `'mac' | 'windows' | 'linux'`. Browsers: `'firefox' | 'chrome' | 'safari' | 'edge'`.
334
+
335
+ **Caveats:**
336
+
337
+ - Reservations reflect **default** keybindings. Users with custom shortcuts (Edge 95+ rebinds, Firefox add-ons, OS-level customization) may not match.
338
+ - KDE/XFCE desktop reservations beyond GNOME are not yet catalogued. Linux coverage is conservative.
339
+ - Layout-specific differences (Dvorak, AZERTY) change which physical key produces `event.key === 'f'`. For layout-stable bindings against the physical key, use `code:KeyX` syntax. The reservation lookup normalizes both.
340
+
341
+ **Programmatic lookup.** If you want to query the table yourself (e.g. building a cheatsheet that flags conflicts):
342
+
343
+ ```js
344
+ import { lookupReservation } from 'hotkey-router/reservations'
345
+
346
+ const r = lookupReservation(
347
+ { meta: true, shift: true, key: 'f' },
348
+ { platform: 'mac', browser: 'firefox' }
349
+ )
350
+ // โ†’ { source: 'browser', action: 'Toggle fullscreen', severity: 'hard', ... }
351
+ ```
352
+
353
+ **Extension hook (`onBind`).** Reservation warnings are built on a public `onBind` hook, useful for telemetry, dev panels, or any cross-cutting concern that needs to observe every binding:
354
+
355
+ ```js
356
+ const off = hotkeys.onBind(({ combo, raw, options, plugin, id }) => {
357
+ // combo: parsed combo { ctrl, meta, alt, shift, key, code, bareModifier }
358
+ // raw: original hotkey string
359
+ })
360
+ // off() unsubscribes.
361
+ ```
362
+
363
+ ### Supported syntax
364
+
365
+ **Standard:**
366
+
367
+ - `ctrl+k`
368
+ - `shift+a`
369
+ - `ctrl+k up`
370
+ - `mod+s` (meta on macOS, ctrl elsewhere)
371
+ - `ctrl++` or `ctrl+plus`
372
+
373
+ **AHK-style prefix modifiers:**
374
+
375
+ - `^k` โ†’ `ctrl+k`
376
+ - `!k` โ†’ `alt+k`
377
+ - `+k` โ†’ `shift+k`
378
+ - `#k` โ†’ `meta+k`
379
+ - `^!k` โ†’ `ctrl+alt+k`
380
+
381
+ Modifiers must appear before the base key.
382
+
383
+ **Modifier aliases:**
384
+
385
+ - `ctrl`, `control`, `โŒƒ`
386
+ - `shift`, `โ‡ง`, `+`
387
+ - `alt`, `option`, `โŒฅ`, `!`
388
+ - `meta`, `cmd`, `command`, `win`, `โŒ˜`, `#`
389
+ - `mod` (meta on macOS, ctrl elsewhere)
390
+
391
+ **Navigation / special key aliases:**
392
+
393
+ - `escape`, `esc`
394
+ - `enter`, `return`
395
+ - `space`
396
+ - `tab`
397
+ - `backspace`
398
+ - `delete`, `del`
399
+ - `home`, `end`
400
+ - `pageup`, `pgup`
401
+ - `pagedown`, `pgdn`
402
+ - `up`, `down`, `left`, `right`
403
+ - `f1`โ€“`f19`
340
404
 
341
405
  Keys are case-insensitive.
342
406
 
343
- ---
407
+ ### Philosophy
344
408
 
345
- # What This Is Not
409
+ hotkey-router follows three core rules:
346
410
 
347
- * Not a keycode polyfill
348
- * Not a legacy browser shim
349
- * Not a global scope manager
350
- * Not a VSCode-style sequence engine
411
+ 1. **Predictable routing.** Highest priority wins. Ties go to the most recently bound handler.
412
+ 2. **Safe composition.** Plugins can register and unregister without affecting others.
413
+ 3. **Modern only.** Built for modern browsers using `KeyboardEvent.key`.
351
414
 
352
- This is a small, deterministic routing layer for modern applications.
415
+ No keycodes. No legacy IE hacks. No hidden global scope state.
353
416
 
354
- ---
417
+ ### What this is not
418
+
419
+ - Not a keycode polyfill
420
+ - Not a legacy browser shim
421
+ - Not a global scope manager
422
+ - Not a VSCode-style sequence engine
423
+
424
+ This is a small, deterministic routing layer for modern applications.
355
425
 
356
- # Browser Support
426
+ ### Browser support
357
427
 
358
428
  Modern browsers supporting:
359
429
 
360
- * `KeyboardEvent.key`
361
- * `Map`
362
- * `addEventListener`
430
+ - `KeyboardEvent.key`
431
+ - `Map`
432
+ - `addEventListener`
363
433
 
364
434
  Chrome, Firefox, Safari, Edge.
365
435
 
366
436
  ---
367
437
 
368
- # License
438
+ ## License
369
439
 
370
- See LICENSE file for details.
440
+ Licensed under AGPL-3.0 with WATT3D Additional Terms. See [LICENSE](./LICENSE) and [ADDITIONAL_TERMS.md](./ADDITIONAL_TERMS.md). Commercial AI/model-training use requires compliance with those terms or a separate WATT3D license. ยฉ WATT3D.