hotkey-router 0.3.1 → 0.3.2

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
@@ -17,7 +17,8 @@ A tiny, deterministic keyboard routing engine for modern web apps. Not a key uti
17
17
  - Input-safe by default
18
18
  - Testable via `trigger()`
19
19
  - Optional opt-in browser/OS conflict warnings (tree-shakable)
20
- - ~3 kB minified + gzipped (core); ~7.5 kB with conflict warnings
20
+ - Display helpers: `Ctrl+Shift+.` / `⇧⌘.` and accessible keycaps (`hotkey-router/format`)
21
+ - ~4 kB minified + gzipped (core); ~8.5 kB with conflict warnings
21
22
  - Zero dependencies
22
23
 
23
24
  ---
@@ -232,12 +233,14 @@ those bound with `{ capture: true }`, which a second, capture-phase listener
232
233
  handles (v0.3.0+). `capture: true` here runs every binding in the capture
233
234
  phase.
234
235
 
235
- ### `parseHotkey(hotkey)` / `comboFromEvent(event, options?)` (v0.3.0+)
236
+ ### `parseHotkey(hotkey, options?)` / `comboFromEvent(event, options?)` (v0.3.0+)
236
237
 
237
238
  Named exports (also on the default object). `comboFromEvent` turns a
238
239
  keyboard event into a hotkey string for a "press your keys" recorder:
239
240
  physical by default, `{ useMod: true }` writes the platform's primary
240
241
  modifier as `mod`, and it returns `null` while only a modifier is held.
242
+ Both take `{ platform }` (v0.3.2+) to resolve `mod` for a platform other
243
+ than the one detected.
241
244
 
242
245
  ```js
243
246
  import { comboFromEvent } from 'hotkey-router'
@@ -248,6 +251,14 @@ input.addEventListener('keydown', (e) => {
248
251
  })
249
252
  ```
250
253
 
254
+ ### `detectPlatform(navigator?, { chromeos? })` (v0.3.2+)
255
+
256
+ The one platform detector the router, reservations, sites, validate and
257
+ format all use: `'mac'`, `'windows'`, `'linux'` or `null`. ChromeOS counts as
258
+ `'linux'` unless you pass `{ chromeos: true }`. Reads
259
+ `navigator.userAgentData.platform`, then `navigator.platform`, then the user
260
+ agent. Also exported from `hotkey-router/reservations`.
261
+
251
262
  ### `destroy()`
252
263
 
253
264
  Removes all listeners and clears internal state.
@@ -258,9 +269,14 @@ Programmatically trigger a hotkey. Useful for testing.
258
269
 
259
270
  ```js
260
271
  hotkeys.trigger('ctrl+k')
272
+ hotkeys.trigger('ctrl+k', { target: inputElement }) // as if typed in the input
261
273
  ```
262
274
 
263
- Returns `true` if a handler ran.
275
+ Returns `true` if a handler ran. Since v0.3.2 it fires what a real keypress
276
+ would: the synthetic event has both `key` and `code` (US layout), so
277
+ `trigger('ctrl+k')` also fires a `ctrl+code:KeyK` binding and the reverse,
278
+ and the same guards apply (`when`, `ignoreInput`/`allowIn` with `target`,
279
+ priority, `once`, `pause()`).
264
280
 
265
281
  ---
266
282
 
@@ -312,7 +328,7 @@ The `code:` value is **case-sensitive** (matches the camelCase `KeyboardEvent.co
312
328
 
313
329
  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.
314
330
 
315
- 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.
331
+ The feature is opt-in by design: the core router is ~9 KB minified (~4 KB gzipped), and the reservation module (~23 KB minified, ~5.5 KB gzipped, mostly data) is tree-shaken out entirely unless you import it.
316
332
 
317
333
  **Two ways to opt in.** One-line ergonomic, use the `auto` entry. Same default export as `hotkey-router`, with warnings pre-installed:
318
334
 
@@ -428,8 +444,8 @@ shortcut runs.
428
444
  Also exported: `lookupSiteConflicts(combo, { url, platform, verifiedOnly })`,
429
445
  `matchSites(url)`, `siteLookupKey(combo, platform)`, `detectSitePlatform()`
430
446
  and `listSites()`. Platforms: `windows` (also used for Linux), `mac`,
431
- `chromeos`. The runtime table is ~7 KB gzipped and imports nothing from the
432
- core, so it never creates a second router.
447
+ `chromeos`. The module is ~38 KB minified (~9 KB gzipped, mostly the table)
448
+ and imports nothing from the core, so it never creates a second router.
433
449
 
434
450
  ### Validating a recorded combo (v0.3.0+)
435
451
 
@@ -461,6 +477,56 @@ already in `taken`, or reserved by a browser/OS (never reaches the page).
461
477
  Warnings (`ok: true`): reserved only in some situations, Ctrl+Alt acting as
462
478
  AltGr on some Windows layouts, and sites that use the same keys.
463
479
 
480
+ It loads both data tables: with them, about 62 KB minified (~15 KB
481
+ gzipped). Import it in the settings page that records keys, not in code
482
+ that runs on every page.
483
+
484
+ ### Showing a combo to people (v0.3.2+)
485
+
486
+ `hotkey-router/format` turns a hotkey string (or a parsed combo) into what
487
+ people read. It loads no data tables (~9 KB minified, ~3.5 KB gzipped).
488
+
489
+ ```js
490
+ import { formatCombo, comboTokens, modifierLabel } from 'hotkey-router/format'
491
+
492
+ formatCombo('mod+shift+code:Period') // 'Ctrl+Shift+.' (Windows/Linux)
493
+ formatCombo('mod+shift+code:Period', { platform: 'mac' }) // 'Shift+Cmd+.'
494
+ formatCombo('mod+shift+code:Period', { platform: 'mac', style: 'glyphs' }) // '⇧⌘.'
495
+
496
+ comboTokens('mod+shift+code:Period', { platform: 'mac' })
497
+ // [{ label: '⇧', name: 'Shift', modifier: 'shift' },
498
+ // { label: '⌘', name: 'Command', modifier: 'meta' },
499
+ // { label: '.', name: 'Period', key: 'Period' }]
500
+
501
+ modifierLabel('alt', { platform: 'mac', style: 'both' }) // '⌥ Option'
502
+ modifierLabel('mod') // 'Cmd' or 'Ctrl'
503
+ ```
504
+
505
+ - `formatCombo(combo, { platform, style, order, separator })`: one string.
506
+ `style: 'text'` (default) joins words with `+`; `'glyphs'` uses the macOS
507
+ symbols (⌃ ⌥ ⇧ ⌘ ↩ ⌫ ⎋ ← …) with no separator.
508
+ - `comboTokens(combo, { platform, style, order })`: one keycap per key,
509
+ `'glyphs'` by default. `name` is what a screen reader should say; put it in
510
+ visually hidden text or `aria-label` next to a glyph.
511
+ - `modifierLabel(name, { platform, style })` (`'text'`, `'glyphs'` or
512
+ `'both'`), `modifierName(name, { platform })` (spoken), `keyLabel(key,
513
+ { platform, style })`, `listModifiers(names, { platform, conjunction })`
514
+ (`'Ctrl, Alt or Shift'`), `platformLabel('mac')` (`'macOS'`),
515
+ `browserLabel('edge')`.
516
+ - `KEY_LABELS`: label and spoken name for `KeyboardEvent.code` values
517
+ (`Period` → `.` / "Period", `Enter` → Enter, Return on macOS, ↩).
518
+
519
+ Words per platform: macOS Cmd, Option, Control, Shift; Windows Ctrl, Alt,
520
+ Shift, Win; Linux the same with Super; ChromeOS with Search. `platform`
521
+ defaults to `detectPlatform()`. On Windows, Linux and ChromeOS `'glyphs'`
522
+ gives the same words as `'text'` (they have no modifier symbols).
523
+
524
+ **Modifier order.** Modifiers come in the platform's own order whatever order
525
+ the string is written in: on macOS Apple's ⌃⌥⇧⌘ (Control, Option, Shift,
526
+ Command), the order every Mac menu shows, so `mod+shift+.` is `⇧⌘.`; on
527
+ Windows, Linux and ChromeOS Win/Super/Search, Ctrl, Alt, Shift. Pass
528
+ `{ order: 'combo' }` to keep the written order (`⌘⇧.`).
529
+
464
530
  ### Supported syntax
465
531
 
466
532
  **Standard:**
@@ -469,7 +535,8 @@ AltGr on some Windows layouts, and sites that use the same keys.
469
535
  - `shift+a`
470
536
  - `ctrl+k up`
471
537
  - `mod+s` (meta on macOS, ctrl elsewhere)
472
- - `ctrl++` or `ctrl+plus`
538
+ - `ctrl++` or `ctrl+plus` (the `+` key; reservation and site lookups treat
539
+ it as `=`, since `+` is Shift+= on most layouts)
473
540
 
474
541
  **AHK-style prefix modifiers:**
475
542
 
@@ -485,7 +552,7 @@ Modifiers must appear before the base key.
485
552
 
486
553
  - `ctrl`, `control`, `⌃`
487
554
  - `shift`, `⇧`, `+`
488
- - `alt`, `option`, `⌥`, `!`
555
+ - `alt`, `option`, `opt`, `⌥`, `!`
489
556
  - `meta`, `cmd`, `command`, `win`, `⌘`, `#`
490
557
  - `mod` (meta on macOS, ctrl elsewhere)
491
558