@tanstack/lit-hotkeys 0.13.1 → 0.14.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/README.md CHANGED
@@ -67,6 +67,9 @@ Type-safe keyboard shortcuts for the web. Template-string bindings, parsed objec
67
67
  > [!NOTE]
68
68
  > You may know **TanStack Hotkeys** by our adapter names, too!
69
69
  >
70
+ > - [**Alpine Hotkeys**](https://tanstack.com/hotkeys/latest/docs/framework/alpine/quick-start)
71
+ > - [**Ember Hotkeys**](https://tanstack.com/hotkeys/latest/docs/framework/ember/quick-start)
72
+ > - [**Octane Hotkeys**](https://tanstack.com/hotkeys/latest/docs/framework/octane/quick-start)
70
73
  > - [**React Hotkeys**](https://tanstack.com/hotkeys/latest/docs/framework/react/react-hotkeys)
71
74
  > - [**Preact Hotkeys**](https://tanstack.com/hotkeys/latest/docs/framework/preact/preact-hotkeys)
72
75
  > - [**Solid Hotkeys**](https://tanstack.com/hotkeys/latest/docs/framework/solid/reference)
@@ -41,6 +41,7 @@ export declare class HotkeyRecorderController implements ReactiveController {
41
41
  private _options;
42
42
  /** The recorder instance. */
43
43
  private _recorder;
44
+ private _optionOverrides;
44
45
  /** The unsubscribe function to unsubscribe from the recorder store. */
45
46
  private _unsubscribe;
46
47
  /** Whether recording is currently active. */
@@ -53,9 +54,9 @@ export declare class HotkeyRecorderController implements ReactiveController {
53
54
  get recordedHotkey(): Hotkey | null;
54
55
  /**
55
56
  * @param _host - The Lit component that owns this controller.
56
- * @param _options - Configuration options for the recorder.
57
+ * @param _options - Options or a getter, read during recording.
57
58
  */
58
- constructor(_host: ReactiveControllerHost, _options: HotkeyRecorderOptions);
59
+ constructor(_host: ReactiveControllerHost, _options: HotkeyRecorderOptions | (() => HotkeyRecorderOptions));
59
60
  /** Subscribes to the recorder store and updates the internal state when changes occur. */
60
61
  hostConnected(): void;
61
62
  /** Unsubscribes from the recorder store and destroys the recorder instance to prevent memory leaks. */
@@ -41,6 +41,7 @@ var HotkeyRecorderController = class {
41
41
  _options;
42
42
  /** The recorder instance. */
43
43
  _recorder;
44
+ _optionOverrides = {};
44
45
  /** The unsubscribe function to unsubscribe from the recorder store. */
45
46
  _unsubscribe;
46
47
  /** Whether recording is currently active. */
@@ -57,12 +58,15 @@ var HotkeyRecorderController = class {
57
58
  }
58
59
  /**
59
60
  * @param _host - The Lit component that owns this controller.
60
- * @param _options - Configuration options for the recorder.
61
+ * @param _options - Options or a getter, read during recording.
61
62
  */
62
63
  constructor(_host, _options) {
63
64
  this._host = _host;
64
65
  this._options = _options;
65
- this._recorder = new HotkeyRecorder(_options);
66
+ this._recorder = new HotkeyRecorder(() => ({
67
+ ...typeof this._options === "function" ? this._options() : this._options,
68
+ ...this._optionOverrides
69
+ }));
66
70
  this._host.addController(this);
67
71
  }
68
72
  /** Subscribes to the recorder store and updates the internal state when changes occur. */
@@ -87,11 +91,10 @@ var HotkeyRecorderController = class {
87
91
  }
88
92
  /** Updates the recorder options (e.g. callbacks). */
89
93
  setOptions(options) {
90
- this._options = {
91
- ...this._options,
94
+ this._optionOverrides = {
95
+ ...this._optionOverrides,
92
96
  ...options
93
97
  };
94
- this._recorder.setOptions(this._options);
95
98
  }
96
99
  /** Start recording a new hotkey. */
97
100
  startRecording() {
@@ -43,6 +43,7 @@ export declare class HotkeySequenceRecorderController implements ReactiveControl
43
43
  private _options;
44
44
  /** The recorder instance. */
45
45
  private _recorder;
46
+ private _optionOverrides;
46
47
  /** The unsubscribe function. */
47
48
  private _unsubscribe;
48
49
  /** Whether recording is currently active. */
@@ -59,9 +60,9 @@ export declare class HotkeySequenceRecorderController implements ReactiveControl
59
60
  get recordedSequence(): HotkeySequence | null;
60
61
  /**
61
62
  * @param _host - The Lit component that owns this controller.
62
- * @param _options - Configuration options for the sequence recorder.
63
+ * @param _options - Options or a getter, read during recording.
63
64
  */
64
- constructor(_host: ReactiveControllerHost, _options: HotkeySequenceRecorderOptions);
65
+ constructor(_host: ReactiveControllerHost, _options: HotkeySequenceRecorderOptions | (() => HotkeySequenceRecorderOptions));
65
66
  /** Subscribes to the recorder store and updates internal state when changes occur. */
66
67
  hostConnected(): void;
67
68
  /** Unsubscribes from the recorder store and destroys the recorder instance to prevent memory leaks. */
@@ -43,6 +43,7 @@ var HotkeySequenceRecorderController = class {
43
43
  _options;
44
44
  /** The recorder instance. */
45
45
  _recorder;
46
+ _optionOverrides = {};
46
47
  /** The unsubscribe function. */
47
48
  _unsubscribe;
48
49
  /** Whether recording is currently active. */
@@ -65,12 +66,15 @@ var HotkeySequenceRecorderController = class {
65
66
  }
66
67
  /**
67
68
  * @param _host - The Lit component that owns this controller.
68
- * @param _options - Configuration options for the sequence recorder.
69
+ * @param _options - Options or a getter, read during recording.
69
70
  */
70
71
  constructor(_host, _options) {
71
72
  this._host = _host;
72
73
  this._options = _options;
73
- this._recorder = new HotkeySequenceRecorder(_options);
74
+ this._recorder = new HotkeySequenceRecorder(() => ({
75
+ ...typeof this._options === "function" ? this._options() : this._options,
76
+ ...this._optionOverrides
77
+ }));
74
78
  this._host.addController(this);
75
79
  }
76
80
  /** Subscribes to the recorder store and updates internal state when changes occur. */
@@ -97,11 +101,10 @@ var HotkeySequenceRecorderController = class {
97
101
  }
98
102
  /** Updates the recorder options. */
99
103
  setOptions(options) {
100
- this._options = {
101
- ...this._options,
104
+ this._optionOverrides = {
105
+ ...this._optionOverrides,
102
106
  ...options
103
107
  };
104
- this._recorder.setOptions(this._options);
105
108
  }
106
109
  /** Start recording a new sequence. */
107
110
  startRecording() {
@@ -20,23 +20,23 @@ import { ReactiveController, ReactiveControllerHost } from "lit";
20
20
  export declare class HotkeySequenceController implements ReactiveController {
21
21
  private _host;
22
22
  private _sequence;
23
- private _callback;
24
23
  private _options;
25
- /** The sequence registration handle. */
26
- private _registration;
24
+ private _bindings;
25
+ private _connected;
26
+ private _boundCallback;
27
27
  /**
28
- * @param _host - The Lit component that owns this controller (use `this` and pass it to `addController()`).
29
- * @param _sequence - The key sequence to listen for (e.g. `['G', 'G']`).
30
- * @param _callback - Function to run when the sequence is completed; called with the host as `this`.
31
- * @param _options - Optional sequence options (target, timeout, enabled, etc.).
32
- */
33
- constructor(_host: ReactiveControllerHost, _sequence: HotkeySequence, _callback: HotkeyCallback, _options?: SequenceOptions);
34
- /**
35
- * Registers the sequence with the global sequence manager when the host is connected to the DOM.
36
- * Skips registration if disabled, sequence is empty, or no target is available.
28
+ * @param _host - The Lit component that owns this controller. Add the controller with `addController()`.
29
+ * @param _sequence - The sequence to register.
30
+ * @param callback - Called with the host as `this`.
31
+ * @param _options - Options or a getter. Property getters are read on connection and after host updates.
37
32
  */
33
+ constructor(_host: ReactiveControllerHost, _sequence: HotkeySequence, callback: HotkeyCallback, _options?: SequenceOptions | (() => SequenceOptions));
34
+ /** Registers when connected and a target is available. */
38
35
  hostConnected(): void;
39
- /** Unregisters the sequence when the host is disconnected from the DOM. */
36
+ /** Refreshes options after rendering, when scoped targets are available. */
37
+ hostUpdated(): void;
38
+ private _sync;
39
+ /** Releases registrations when the host disconnects. */
40
40
  hostDisconnected(): void;
41
41
  }
42
42
  //#endregion
@@ -1,5 +1,5 @@
1
1
  import { HOTKEY_SEQUENCE_DEFAULT_OPTIONS } from "../constants.js";
2
- import { getSequenceManager } from "@tanstack/hotkeys";
2
+ import { createHotkeySequenceBindings } from "@tanstack/hotkeys/adapter";
3
3
 
4
4
  //#region src/controllers/hotkey-sequence.ts
5
5
  /**
@@ -21,41 +21,43 @@ import { getSequenceManager } from "@tanstack/hotkeys";
21
21
  var HotkeySequenceController = class {
22
22
  _host;
23
23
  _sequence;
24
- _callback;
25
24
  _options;
26
- /** The sequence registration handle. */
27
- _registration;
25
+ _bindings = createHotkeySequenceBindings();
26
+ _connected = false;
27
+ _boundCallback;
28
28
  /**
29
- * @param _host - The Lit component that owns this controller (use `this` and pass it to `addController()`).
30
- * @param _sequence - The key sequence to listen for (e.g. `['G', 'G']`).
31
- * @param _callback - Function to run when the sequence is completed; called with the host as `this`.
32
- * @param _options - Optional sequence options (target, timeout, enabled, etc.).
29
+ * @param _host - The Lit component that owns this controller. Add the controller with `addController()`.
30
+ * @param _sequence - The sequence to register.
31
+ * @param callback - Called with the host as `this`.
32
+ * @param _options - Options or a getter. Property getters are read on connection and after host updates.
33
33
  */
34
- constructor(_host, _sequence, _callback, _options = HOTKEY_SEQUENCE_DEFAULT_OPTIONS) {
34
+ constructor(_host, _sequence, callback, _options = HOTKEY_SEQUENCE_DEFAULT_OPTIONS) {
35
35
  this._host = _host;
36
36
  this._sequence = _sequence;
37
- this._callback = _callback;
38
37
  this._options = _options;
38
+ this._boundCallback = callback.bind(this._host);
39
39
  }
40
- /**
41
- * Registers the sequence with the global sequence manager when the host is connected to the DOM.
42
- * Skips registration if disabled, sequence is empty, or no target is available.
43
- */
40
+ /** Registers when connected and a target is available. */
44
41
  hostConnected() {
45
- const { target: _target, ...optionsWithoutTarget } = this._options;
46
- const manager = getSequenceManager();
47
- const resolvedTarget = "target" in this._options ? this._options.target ?? null : typeof document !== "undefined" ? document : null;
48
- if (!resolvedTarget) return;
49
- const boundCallback = this._callback.bind(this._host);
50
- this._registration = manager.register(this._sequence, boundCallback, {
51
- ...optionsWithoutTarget,
52
- target: resolvedTarget
53
- });
42
+ this._connected = true;
43
+ this._sync();
44
+ }
45
+ /** Refreshes options after rendering, when scoped targets are available. */
46
+ hostUpdated() {
47
+ if (this._connected) this._sync();
48
+ }
49
+ _sync() {
50
+ const options = typeof this._options === "function" ? this._options() : this._options;
51
+ this._bindings.update([{
52
+ sequence: this._sequence,
53
+ callback: this._boundCallback,
54
+ options
55
+ }]);
54
56
  }
55
- /** Unregisters the sequence when the host is disconnected from the DOM. */
57
+ /** Releases registrations when the host disconnects. */
56
58
  hostDisconnected() {
57
- if (this._registration?.isActive) this._registration.unregister();
58
- this._registration = void 0;
59
+ this._connected = false;
60
+ this._bindings.destroy();
59
61
  }
60
62
  };
61
63
 
@@ -20,23 +20,23 @@ import { ReactiveController, ReactiveControllerHost } from "lit";
20
20
  export declare class HotkeyController implements ReactiveController {
21
21
  private _host;
22
22
  private _hotkey;
23
- private _callback;
24
23
  private _options;
25
- /** The hotkey registration handle. */
26
- private _registration;
24
+ private _bindings;
25
+ private _connected;
26
+ private _boundCallback;
27
27
  /**
28
- * @param _host - The Lit component that owns this controller (use `this` and pass it to `addController()`).
29
- * @param _hotkey - The key or key combo to listen for (e.g. `'Mod+S'` or a raw hotkey object).
30
- * @param _callback - Function to run when the hotkey is pressed; called with the host as `this`.
31
- * @param _options - Optional registration options (target, platform, enabled, etc.).
32
- */
33
- constructor(_host: ReactiveControllerHost, _hotkey: RegisterableHotkey, _callback: HotkeyCallback, _options?: HotkeyOptions);
34
- /**
35
- * Registers the hotkey with the global manager when the host is connected to the DOM.
36
- * Skips registration if no target is available (e.g. no document or options.target is null).
28
+ * @param _host - The Lit component that owns this controller. Add the controller with `addController()`.
29
+ * @param _hotkey - The shortcut to register.
30
+ * @param callback - Called with the host as `this`.
31
+ * @param _options - Options or a getter. Property getters are read on connection and after host updates.
37
32
  */
33
+ constructor(_host: ReactiveControllerHost, _hotkey: RegisterableHotkey, callback: HotkeyCallback, _options?: HotkeyOptions | (() => HotkeyOptions));
34
+ /** Registers when connected and a target is available. */
38
35
  hostConnected(): void;
39
- /** Unregisters the hotkey when the host is disconnected from the DOM. */
36
+ /** Refreshes options after rendering, when scoped targets are available. */
37
+ hostUpdated(): void;
38
+ private _sync;
39
+ /** Releases registrations when the host disconnects. */
40
40
  hostDisconnected(): void;
41
41
  }
42
42
  //#endregion
@@ -1,5 +1,5 @@
1
1
  import { HOTKEY_DEFAULT_OPTIONS } from "../constants.js";
2
- import { detectPlatform, formatHotkey, getHotkeyManager, rawHotkeyToParsedHotkey } from "@tanstack/hotkeys";
2
+ import { createHotkeyBindings } from "@tanstack/hotkeys/adapter";
3
3
 
4
4
  //#region src/controllers/hotkey.ts
5
5
  /**
@@ -21,43 +21,43 @@ import { detectPlatform, formatHotkey, getHotkeyManager, rawHotkeyToParsedHotkey
21
21
  var HotkeyController = class {
22
22
  _host;
23
23
  _hotkey;
24
- _callback;
25
24
  _options;
26
- /** The hotkey registration handle. */
27
- _registration;
25
+ _bindings = createHotkeyBindings();
26
+ _connected = false;
27
+ _boundCallback;
28
28
  /**
29
- * @param _host - The Lit component that owns this controller (use `this` and pass it to `addController()`).
30
- * @param _hotkey - The key or key combo to listen for (e.g. `'Mod+S'` or a raw hotkey object).
31
- * @param _callback - Function to run when the hotkey is pressed; called with the host as `this`.
32
- * @param _options - Optional registration options (target, platform, enabled, etc.).
29
+ * @param _host - The Lit component that owns this controller. Add the controller with `addController()`.
30
+ * @param _hotkey - The shortcut to register.
31
+ * @param callback - Called with the host as `this`.
32
+ * @param _options - Options or a getter. Property getters are read on connection and after host updates.
33
33
  */
34
- constructor(_host, _hotkey, _callback, _options = HOTKEY_DEFAULT_OPTIONS) {
34
+ constructor(_host, _hotkey, callback, _options = HOTKEY_DEFAULT_OPTIONS) {
35
35
  this._host = _host;
36
36
  this._hotkey = _hotkey;
37
- this._callback = _callback;
38
37
  this._options = _options;
38
+ this._boundCallback = callback.bind(this._host);
39
39
  }
40
- /**
41
- * Registers the hotkey with the global manager when the host is connected to the DOM.
42
- * Skips registration if no target is available (e.g. no document or options.target is null).
43
- */
40
+ /** Registers when connected and a target is available. */
44
41
  hostConnected() {
45
- const manager = getHotkeyManager();
46
- const platform = this._options.platform ?? detectPlatform();
47
- const hotkeyString = typeof this._hotkey === "string" ? this._hotkey : formatHotkey(rawHotkeyToParsedHotkey(this._hotkey, platform));
48
- const resolvedTarget = "target" in this._options ? this._options.target ?? null : typeof document !== "undefined" ? document : null;
49
- if (!resolvedTarget) return;
50
- const { target: _target, ...optionsWithoutTarget } = this._options;
51
- const boundCallback = this._callback.bind(this._host);
52
- this._registration = manager.register(hotkeyString, boundCallback, {
53
- ...optionsWithoutTarget,
54
- target: resolvedTarget
55
- });
42
+ this._connected = true;
43
+ this._sync();
44
+ }
45
+ /** Refreshes options after rendering, when scoped targets are available. */
46
+ hostUpdated() {
47
+ if (this._connected) this._sync();
48
+ }
49
+ _sync() {
50
+ const options = typeof this._options === "function" ? this._options() : this._options;
51
+ this._bindings.update([{
52
+ hotkey: this._hotkey,
53
+ callback: this._boundCallback,
54
+ options
55
+ }]);
56
56
  }
57
- /** Unregisters the hotkey when the host is disconnected from the DOM. */
57
+ /** Releases registrations when the host disconnects. */
58
58
  hostDisconnected() {
59
- if (this._registration?.isActive) this._registration.unregister();
60
- this._registration = void 0;
59
+ this._connected = false;
60
+ this._bindings.destroy();
61
61
  }
62
62
  };
63
63
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/lit-hotkeys",
3
- "version": "0.13.1",
3
+ "version": "0.14.0",
4
4
  "description": "Lit adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  "dist"
34
34
  ],
35
35
  "dependencies": {
36
- "@tanstack/hotkeys": "0.10.1"
36
+ "@tanstack/hotkeys": "0.11.0"
37
37
  },
38
38
  "devDependencies": {
39
39
  "lit": "^3.3.3"