hotkey-router 0.3.2 → 0.3.3

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
@@ -126,6 +126,28 @@ a character (Windows reports AltGr as Ctrl+Alt, so `ctrl+alt+code:Comma`
126
126
  would otherwise swallow the `<` a Polish or Czech user typed) and while an
127
127
  IME is composing.
128
128
 
129
+ `when` and `allowIn` receive the `KeyboardEvent`, not an element. To check
130
+ where the key was typed, read `event.composedPath()[0]` (the element inside an
131
+ open shadow root) or `event.target`.
132
+
133
+ **Shifted punctuation.** With Shift held, `event.key` is the shifted
134
+ character, so a key-based `ctrl+shift+,` waits for a `,` that a US keyboard
135
+ reports as `<`, and never fires. Bind the physical key instead:
136
+ `ctrl+shift+code:Comma`. Since v0.3.3, `bind()` warns once in the console for
137
+ Shift with `` , . / ; ' [ ] \ - = ` ``, and `validateCombo` rejects it with the
138
+ reason `shifted-punctuation`.
139
+
140
+ **Non-Latin layouts (v0.3.3+).** On a Russian layout Ctrl+K reports
141
+ `event.key === 'л'` with `event.code === 'KeyK'`. Like the browser's own
142
+ shortcuts, a key-based `ctrl+k` binding still fires there: when Ctrl or Cmd is
143
+ held (Shift allowed, Alt not), the key types a letter from a non-Latin script
144
+ (Cyrillic, Greek, Hebrew, Arabic, ...), and `code` is `KeyA`..`KeyZ`, the
145
+ router also looks up the Latin letter for that key. It does this only when no
146
+ exact binding is eligible, so a `ctrl+л` or `ctrl+code:KeyK` binding always
147
+ wins over it. Plain and Shift-only letters (typing), Alt and AltGr chords, IME
148
+ composition, digits and Latin layouts such as AZERTY are never affected. To
149
+ require the exact character, gate the binding: `when: (e) => e.key === 'k'`.
150
+
129
151
  **Examples**
130
152
 
131
153
  Ignore repeat keydown (default behavior):
@@ -141,6 +163,11 @@ hotkeys.bind('mod+k', openPalette, null, {
141
163
  allowIn: () => true,
142
164
  preventDefault: true,
143
165
  })
166
+
167
+ // Only in one field (the event, not the element, is passed in)
168
+ hotkeys.bind('enter', submitFollowUp, null, {
169
+ allowIn: (e) => e.composedPath()[0] === followUpInput,
170
+ })
144
171
  ```
145
172
 
146
173
  Conditional binding:
@@ -181,6 +208,8 @@ unregister()
181
208
  ```
182
209
 
183
210
  Plugin cleanup is isolated. Removing one plugin never affects other bindings.
211
+ If any key string fails to parse, `registerPlugin` throws and binds nothing
212
+ (v0.3.3+).
184
213
 
185
214
  ### `unregisterPlugin(name)`
186
215
 
@@ -213,7 +242,24 @@ By default, hotkeys do **not** fire inside:
213
242
  - `[contenteditable]`
214
243
  - `role="textbox"`
215
244
 
216
- Override per-binding with `allowIn()`.
245
+ This includes those elements inside an open shadow root (v0.3.3+; the router
246
+ reads `event.composedPath()[0]`). A closed shadow root hides its inside from
247
+ the page, so only its host element is checked there.
248
+
249
+ Every `<input>` counts, whatever its `type` and whatever modifiers are held:
250
+ a checkbox, radio, range slider, color or file input blocks `ctrl+k` as well
251
+ as Space. A native `<button>` is **not** on the list, so a plain `space` or
252
+ `enter` binding fires while a button has focus; gate such bindings with
253
+ `when` if that matters.
254
+
255
+ Override per-binding with `allowIn()`:
256
+
257
+ ```js
258
+ // Let the palette open from a focused checkbox or slider, but not from text fields
259
+ hotkeys.bind('mod+k', openPalette, null, {
260
+ allowIn: (e) => ['checkbox', 'radio', 'range'].includes(e.composedPath()[0].type),
261
+ })
262
+ ```
217
263
 
218
264
  ### `init(options?)`
219
265
 
@@ -226,7 +272,12 @@ hotkeys.init({
226
272
  })
227
273
  ```
228
274
 
229
- Auto-initializes on `window` by default (browser environments).
275
+ Auto-initializes on `window` by default (browser environments): importing
276
+ `hotkey-router` attaches `keydown` and `keyup` listeners to `window` straight
277
+ away, and `hotkey-router/auto` also installs the reservation warnings. The
278
+ package's `sideEffects` list names those files so bundlers keep that work;
279
+ `/reservations`, `/sites`, `/validate` and `/format` do nothing at import and
280
+ tree-shake away when unused.
230
281
 
231
282
  With `capture: false` (the default), bindings run in the bubble phase except
232
283
  those bound with `{ capture: true }`, which a second, capture-phase listener
@@ -328,9 +379,9 @@ The `code:` value is **case-sensitive** (matches the camelCase `KeyboardEvent.co
328
379
 
329
380
  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.
330
381
 
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.
382
+ The feature is opt-in by design: the core router is ~10 KB minified (~4.5 KB gzipped), and the reservation module (~23 KB minified, ~5.5 KB gzipped, mostly data) is tree-shaken out entirely unless you import it.
332
383
 
333
- **Two ways to opt in.** One-line ergonomic, use the `auto` entry. Same default export as `hotkey-router`, with warnings pre-installed:
384
+ **Two ways to opt in.** One-line ergonomic, use the `auto` entry. Same default export as `hotkey-router`, with warnings pre-installed. It is the same router object, so other files can keep importing `hotkey-router` and their bindings are warned about too (v0.3.3+; earlier versions shipped a second copy of the router in `auto`). The CDN file `dist/auto.min.js` is a standalone build with its own router; don't mix it with other hotkey-router imports.
334
385
 
335
386
  ```js
336
387
  import hotkeys from 'hotkey-router/auto'