@tanstack/lit-hotkeys 0.16.1 → 0.17.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/lit-hotkeys",
3
- "version": "0.16.1",
3
+ "version": "0.17.0",
4
4
  "description": "Lit adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -17,7 +17,8 @@
17
17
  "keywords": [
18
18
  "lit",
19
19
  "tanstack",
20
- "keys"
20
+ "keys",
21
+ "tanstack-intent"
21
22
  ],
22
23
  "type": "module",
23
24
  "types": "./dist/index.d.ts",
@@ -30,10 +31,11 @@
30
31
  "node": ">=20"
31
32
  },
32
33
  "files": [
33
- "dist"
34
+ "dist",
35
+ "skills"
34
36
  ],
35
37
  "dependencies": {
36
- "@tanstack/hotkeys": "0.16.1"
38
+ "@tanstack/hotkeys": "0.17.0"
37
39
  },
38
40
  "devDependencies": {
39
41
  "lit": "^3.3.3"
@@ -0,0 +1,93 @@
1
+ ---
2
+ name: lit-hotkeys
3
+ description: >-
4
+ Use when adding or fixing keyboard shortcuts in Lit web components with
5
+ @tanstack/lit-hotkeys: the @hotkey decorator, HotkeyController with an
6
+ options function, element targets, scopes activated from the hotkey manager,
7
+ app hotkey and recorder controllers, held-key controllers, or replacing
8
+ keydown listeners in connectedCallback.
9
+ metadata:
10
+ purpose: >-
11
+ Lit bindings for TanStack Hotkeys: the decorator, reactive controllers,
12
+ manager-activated scopes, and the Lit-specific mistakes to avoid.
13
+ type: framework
14
+ framework: lit
15
+ library: '@tanstack/lit-hotkeys'
16
+ library_version: '0.16.0'
17
+ sources:
18
+ - src/decorators/hotkey.ts
19
+ - src/controllers/hotkey.ts
20
+ - src/controllers/app-hotkeys.ts
21
+ - src/controllers/hotkey-recorder.ts
22
+ - TanStack/hotkeys:docs/framework/lit/quick-start.md
23
+ - TanStack/hotkeys:docs/framework/lit/guides/app-hotkeys.md
24
+ requires:
25
+ - '@tanstack/hotkeys#hotkeys-core'
26
+ ---
27
+
28
+ # TanStack Hotkeys in Lit
29
+
30
+ Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack/intent@latest load @tanstack/hotkeys#hotkeys-core`).
31
+
32
+ ```ts no-check
33
+ import { LitElement, html } from 'lit'
34
+ import { customElement, state } from 'lit/decorators.js'
35
+ import { HotkeyController, hotkey } from '@tanstack/lit-hotkeys'
36
+
37
+ @customElement('my-editor')
38
+ export class MyEditor extends LitElement {
39
+ @state() private dirty = false
40
+
41
+ @hotkey('Mod+K')
42
+ openSearch() {}
43
+
44
+ constructor() {
45
+ super()
46
+ this.addController(
47
+ new HotkeyController(
48
+ this,
49
+ 'Mod+S',
50
+ () => this.save(),
51
+ () => ({ enabled: this.dirty }),
52
+ ),
53
+ )
54
+ }
55
+
56
+ save() {}
57
+
58
+ render() {
59
+ return html`<textarea @input=${() => (this.dirty = true)}></textarea>`
60
+ }
61
+ }
62
+ ```
63
+
64
+ ## API
65
+
66
+ | API | What it's for |
67
+ | -------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
68
+ | `@hotkey(hotkey, options?)` | Run the decorated method when the hotkey fires, with options fixed when the class is defined. |
69
+ | `new HotkeyController(host, hotkey, callback, options?)` | Register a hotkey whose options can change; add it with `this.addController(...)`. |
70
+ | `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys once at startup, since Lit has no provider. |
71
+ | `getHotkeyManager().activateScope(scope, options?)` | Turn on a scope, such as an exclusive one for a dialog; it returns the function that turns the scope off. |
72
+ | `new AppHotkeysController(host)` | Read `appHotkeys`, the cheat sheet `list`, and `customHotkeys`, and change custom hotkeys with `setCustomHotkey`, `resetCustomHotkey`, and `findConflicts`. |
73
+ | `new HotkeyRecorderController(host, options)` | Record a chord or sequence the user presses, for a settings screen. |
74
+ | `new HotkeyRegistrationsController(host)` | Read `.hotkeys`, every registered hotkey with its options and `meta`. |
75
+ | `new HotkeyHintController(host, hotkey, options?)` | Read `.value`, whether held modifiers should reveal a shortcut's hint. |
76
+ | `new KeyHoldController(host, key)` | Read `.value`, whether one key is held. |
77
+ | `new HeldKeysController(host)` | Read `.value`, every key that's held right now. |
78
+ | `new HeldKeyCodesController(host)` | Read `.value`, which maps each held key to its physical `event.code`. |
79
+
80
+ The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
81
+
82
+ ## Pitfalls
83
+
84
+ - **Add `HotkeyController` to its host.** It is the only controller that doesn't add itself, so without `this.addController(...)` it never registers.
85
+ - **`@hotkey` is a legacy decorator.** It needs `"experimentalDecorators": true` and `"useDefineForClassFields": false`; with standard decorators it fails with TS1241. Use `HotkeyController` in that case.
86
+ - **Decorator options are fixed when the class is defined.** For options that read instance state, or a `target` from a `ref`, use `HotkeyController` with an options function, which reruns after each update.
87
+ - **Release every scope you activate.** Lit has no provider, so a dialog activates its scope in `connectedCallback` with `this.release = getHotkeyManager().activateScope('dialog-1', { exclusive: true })` and calls `this.release()` in `disconnectedCallback`. A forgotten release keeps every other hotkey suspended. Give each open dialog its own scope name.
88
+
89
+ When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Lit](references/app-hotkeys.md).
90
+
91
+ ## API discovery and docs
92
+
93
+ Signatures: `node_modules/@tanstack/lit-hotkeys/dist/controllers/*.d.ts` and `dist/decorators/*.d.ts`. Guides: `https://tanstack.com/hotkeys/latest/docs/framework/lit/guides/<page>.md`, such as `hotkeys` or `app-hotkeys`.
@@ -0,0 +1,71 @@
1
+ # App hotkeys in Lit
2
+
3
+ The `@tanstack/hotkeys#app-hotkeys` skill owns the rules. This is the Lit wiring.
4
+
5
+ ```ts
6
+ // app-hotkeys.ts
7
+ import { defineAppHotkeys } from '@tanstack/lit-hotkeys'
8
+
9
+ export const appHotkeys = defineAppHotkeys({
10
+ 'doc.save': { hotkey: 'Mod+S', meta: { name: 'Save' } },
11
+ 'help.shortcuts': { hotkey: 'Mod+/', meta: { name: 'Keyboard shortcuts' } },
12
+ })
13
+
14
+ declare module '@tanstack/lit-hotkeys' {
15
+ interface Register {
16
+ appHotkeys: typeof appHotkeys
17
+ }
18
+ }
19
+ ```
20
+
21
+ ```ts
22
+ // main.ts, before any element renders
23
+ import { provideAppHotkeys } from '@tanstack/lit-hotkeys'
24
+ import { appHotkeys } from './app-hotkeys'
25
+
26
+ provideAppHotkeys(appHotkeys, {
27
+ customHotkeys: JSON.parse(localStorage.getItem('hotkeys') ?? '{}'),
28
+ onCustomHotkeysChange: (custom) =>
29
+ localStorage.setItem('hotkeys', JSON.stringify(custom)),
30
+ })
31
+ ```
32
+
33
+ `AppHotkeysController` throws until `provideAppHotkeys` has run. When saved shortcuts come from a server, await them first. Register with `@hotkey(appHotkeys['doc.save'])` or `HotkeyController`; both follow the user's custom hotkey. `AppHotkeysController` has `list` for a cheat sheet, plus `setCustomHotkey`, `findConflicts`, and `resetCustomHotkey`.
34
+
35
+ A settings row records with `HotkeyRecorderController`:
36
+
37
+ ```ts no-check
38
+ import { LitElement, html } from 'lit'
39
+ import { property } from 'lit/decorators.js'
40
+ import {
41
+ AppHotkeysController,
42
+ HotkeyRecorderController,
43
+ formatForDisplay,
44
+ } from '@tanstack/lit-hotkeys'
45
+ import type { AppHotkeyId } from '@tanstack/lit-hotkeys'
46
+
47
+ export class ShortcutRow extends LitElement {
48
+ @property() hotkeyId!: AppHotkeyId
49
+ private app = new AppHotkeysController(this)
50
+ private recorder = new HotkeyRecorderController(this, () => ({
51
+ maxSteps: Infinity,
52
+ validate: (hotkey) => {
53
+ const [conflict] = this.app.findConflicts(this.hotkeyId, hotkey)
54
+ return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
55
+ },
56
+ onRecord: (hotkey) => this.app.setCustomHotkey(this.hotkeyId, hotkey),
57
+ onClear: () => this.app.setCustomHotkey(this.hotkeyId, null),
58
+ }))
59
+
60
+ render() {
61
+ const { meta, hotkey } = this.app.appHotkeys[this.hotkeyId]
62
+ return html`
63
+ ${meta?.name}: ${hotkey ? formatForDisplay(hotkey) : 'Unassigned'}
64
+ <button @click=${() => this.recorder.startRecording()}>Change</button>
65
+ <button @click=${() => this.app.resetCustomHotkey(this.hotkeyId)}>
66
+ Reset
67
+ </button>
68
+ `
69
+ }
70
+ }
71
+ ```