@tanstack/lit-hotkeys 0.28.0 → 0.29.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.
@@ -7,7 +7,8 @@ import { AppHotkeyControllerOptions, HotkeyController, HotkeyControllerChordOpti
7
7
  import { HotkeyRecorderController } from "./hotkey-recorder.js";
8
8
  import { HotkeyRegistrationsController } from "./hotkey-registrations.js";
9
9
  import { KeyHoldController } from "./key-hold.js";
10
+ import { KeyLockStateController } from "./key-lock-state.js";
10
11
  import { HotkeyHintController } from "./hotkey-hint.js";
11
12
  import { HotkeyScopesStateController } from "./hotkey-scopes-state.js";
12
13
  import { KeyboardLayoutController } from "./keyboard-layout.js";
13
- export { AppHotkeyControllerOptions, AppHotkeysController, FormatForAriaController, FormatForAriaControllerOptions, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController };
14
+ export { AppHotkeyControllerOptions, AppHotkeysController, FormatForAriaController, FormatForAriaControllerOptions, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyLockStateController, KeyboardLayoutController };
@@ -0,0 +1,61 @@
1
+ import { LockKey, MaybeGetter } from "@tanstack/hotkeys";
2
+ import { ReactiveController, ReactiveControllerHost } from "lit";
3
+ /**
4
+ * A Lit ReactiveController that tracks whether a lock key, such as Caps Lock,
5
+ * is on.
6
+ *
7
+ * Lock keys toggle instead of being held, so this reads the lock state that
8
+ * the browser reports on key and mouse events. Its value is `null` until the
9
+ * user presses a key, clicks, or moves the mouse, after the window loses
10
+ * focus or the page is hidden, and always for Num Lock and Scroll Lock on macOS and iOS. Pass a
11
+ * getter to track a key that changes, such as a reactive property; it is read
12
+ * again on each host update.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * class PasswordField extends LitElement {
17
+ * private capsLock = new KeyLockStateController(this, 'CapsLock')
18
+ *
19
+ * render() {
20
+ * return html`
21
+ * <label>
22
+ * Password
23
+ * <input type="password" />
24
+ * ${this.capsLock.value
25
+ * ? html`<span role="status">Caps Lock is on</span>`
26
+ * : null}
27
+ * </label>
28
+ * `
29
+ * }
30
+ * }
31
+ * ```
32
+ */
33
+ export declare class KeyLockStateController implements ReactiveController {
34
+ private _host;
35
+ private _key;
36
+ /** The unsubscribe function to unsubscribe from the tracker store. */
37
+ private _unsubscribe;
38
+ /** Whether the tracked lock is on, or `null` when it is unknown. */
39
+ private _value;
40
+ /**
41
+ * `true` when the lock is on, `false` when it is off, or `null` when the
42
+ * browser has not reported it.
43
+ */
44
+ get value(): boolean | null;
45
+ /**
46
+ * @param _host - The Lit component that owns this controller.
47
+ * @param _key - `'CapsLock'`, `'NumLock'`, or `'ScrollLock'`, or a getter.
48
+ */
49
+ constructor(_host: ReactiveControllerHost, _key: MaybeGetter<LockKey>);
50
+ hostConnected(): void;
51
+ /** Re-reads a getter key when host properties trigger an update. */
52
+ hostUpdate(): void;
53
+ hostDisconnected(): void;
54
+ /**
55
+ * Releases the controller now and removes it from its host. Use it to
56
+ * remove a controller early: Lit's `removeController` alone leaves it active.
57
+ */
58
+ dispose(): void;
59
+ /** Recomputes the lock state and reports whether it changed. */
60
+ private _refresh;
61
+ }
@@ -0,0 +1,94 @@
1
+ import { isServer } from "./_server.js";
2
+ import { getKeyStateTracker } from "@tanstack/hotkeys";
3
+ import { resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
4
+
5
+ // src/controllers/key-lock-state.ts
6
+ /**
7
+ * A Lit ReactiveController that tracks whether a lock key, such as Caps Lock,
8
+ * is on.
9
+ *
10
+ * Lock keys toggle instead of being held, so this reads the lock state that
11
+ * the browser reports on key and mouse events. Its value is `null` until the
12
+ * user presses a key, clicks, or moves the mouse, after the window loses
13
+ * focus or the page is hidden, and always for Num Lock and Scroll Lock on macOS and iOS. Pass a
14
+ * getter to track a key that changes, such as a reactive property; it is read
15
+ * again on each host update.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * class PasswordField extends LitElement {
20
+ * private capsLock = new KeyLockStateController(this, 'CapsLock')
21
+ *
22
+ * render() {
23
+ * return html`
24
+ * <label>
25
+ * Password
26
+ * <input type="password" />
27
+ * ${this.capsLock.value
28
+ * ? html`<span role="status">Caps Lock is on</span>`
29
+ * : null}
30
+ * </label>
31
+ * `
32
+ * }
33
+ * }
34
+ * ```
35
+ */
36
+ var KeyLockStateController = class {
37
+ _host;
38
+ _key;
39
+ /** The unsubscribe function to unsubscribe from the tracker store. */
40
+ _unsubscribe;
41
+ /** Whether the tracked lock is on, or `null` when it is unknown. */
42
+ _value = null;
43
+ /**
44
+ * `true` when the lock is on, `false` when it is off, or `null` when the
45
+ * browser has not reported it.
46
+ */
47
+ get value() {
48
+ return this._value;
49
+ }
50
+ /**
51
+ * @param _host - The Lit component that owns this controller.
52
+ * @param _key - `'CapsLock'`, `'NumLock'`, or `'ScrollLock'`, or a getter.
53
+ */
54
+ constructor(_host, _key) {
55
+ this._host = _host;
56
+ this._key = _key;
57
+ this._host.addController(this);
58
+ }
59
+ hostConnected() {
60
+ if (this._unsubscribe) return;
61
+ if (this._refresh()) this._host.requestUpdate();
62
+ if (isServer()) return;
63
+ const subscription = getKeyStateTracker().store.subscribe(() => {
64
+ if (this._refresh()) this._host.requestUpdate();
65
+ });
66
+ this._unsubscribe = () => subscription.unsubscribe();
67
+ }
68
+ /** Re-reads a getter key when host properties trigger an update. */
69
+ hostUpdate() {
70
+ this._refresh();
71
+ }
72
+ hostDisconnected() {
73
+ this._unsubscribe?.();
74
+ this._unsubscribe = void 0;
75
+ this._value = null;
76
+ }
77
+ /**
78
+ * Releases the controller now and removes it from its host. Use it to
79
+ * remove a controller early: Lit's `removeController` alone leaves it active.
80
+ */
81
+ dispose() {
82
+ this.hostDisconnected();
83
+ this._host.removeController(this);
84
+ }
85
+ /** Recomputes the lock state and reports whether it changed. */
86
+ _refresh() {
87
+ const next = getKeyStateTracker().store.state.locks[resolveMaybeGetter(this._key)];
88
+ const changed = next !== this._value;
89
+ this._value = next;
90
+ return changed;
91
+ }
92
+ };
93
+
94
+ export { KeyLockStateController };
package/dist/index.d.ts CHANGED
@@ -9,6 +9,7 @@ import { HeldKeysController } from "./controllers/held-keys.js";
9
9
  import { HotkeyRecorderController } from "./controllers/hotkey-recorder.js";
10
10
  import { HotkeyRegistrationsController } from "./controllers/hotkey-registrations.js";
11
11
  import { KeyHoldController } from "./controllers/key-hold.js";
12
+ import { KeyLockStateController } from "./controllers/key-lock-state.js";
12
13
  import { HotkeyHintController } from "./controllers/hotkey-hint.js";
13
14
  import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.js";
14
15
  import { KeyboardLayoutController } from "./controllers/keyboard-layout.js";
@@ -16,4 +17,4 @@ import "./controllers/index.js";
16
17
  import { formatForDisplay } from "./format-for-display.js";
17
18
  import { formatForAria } from "./format-for-aria.js";
18
19
  export * from "@tanstack/hotkeys";
19
- export { AppHotkeyControllerOptions, AppHotkeysController, FormatForAriaController, FormatForAriaControllerOptions, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyDecorator, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, formatForAria, formatForDisplay, hotkey };
20
+ export { AppHotkeyControllerOptions, AppHotkeysController, FormatForAriaController, FormatForAriaControllerOptions, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyDecorator, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyLockStateController, KeyboardLayoutController, formatForAria, formatForDisplay, hotkey };
package/dist/index.js CHANGED
@@ -9,6 +9,7 @@ import { HeldKeyCodesController } from "./controllers/held-key-codes.js";
9
9
  import { HeldKeysController } from "./controllers/held-keys.js";
10
10
  import { HotkeyRecorderController } from "./controllers/hotkey-recorder.js";
11
11
  import { KeyHoldController } from "./controllers/key-hold.js";
12
+ import { KeyLockStateController } from "./controllers/key-lock-state.js";
12
13
  import { HotkeyHintController } from "./controllers/hotkey-hint.js";
13
14
  import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.js";
14
15
  import { formatForDisplay } from "./format-for-display.js";
@@ -16,4 +17,4 @@ import { formatForAria } from "./format-for-aria.js";
16
17
 
17
18
  export * from "@tanstack/hotkeys"
18
19
 
19
- export { AppHotkeysController, FormatForAriaController, FormatForDisplayController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, formatForAria, formatForDisplay, hotkey };
20
+ export { AppHotkeysController, FormatForAriaController, FormatForDisplayController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyLockStateController, KeyboardLayoutController, formatForAria, formatForDisplay, hotkey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/lit-hotkeys",
3
- "version": "0.28.0",
3
+ "version": "0.29.1",
4
4
  "description": "Lit adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "skills"
36
36
  ],
37
37
  "dependencies": {
38
- "@tanstack/hotkeys": "0.37.0"
38
+ "@tanstack/hotkeys": "0.39.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@lit-labs/ssr": "^4.1.0",
@@ -72,6 +72,7 @@ export class MyEditor extends LitElement {
72
72
  | `new HotkeyScopesStateController(host)` | Read `.value`, the active scopes and the exclusive scope that owns the keyboard. |
73
73
  | `new HotkeyHintController(host, hotkey, options?)` | Read `.value`, whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
74
74
  | `new KeyHoldController(host, key)` | Read `.value`, whether one key is held. Pass a getter, such as `() => this.key`, for a key that changes. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
75
+ | `new KeyLockStateController(host, key)` | Read `.value`, whether `'CapsLock'`, `'NumLock'`, or `'ScrollLock'` is on: `true`, `false`, or `null` until a key press, click, or mouse move reports it. Accepts a getter. Resets to `null` when the window loses focus. Num Lock and Scroll Lock are always `null` on macOS and iOS. Use it for lock state, not `KeyHoldController`, which is true only while the key is down. |
75
76
  | `new HeldKeysController(host)` | Read `.value`, every key that's held right now. |
76
77
  | `new HeldKeyCodesController(host)` | Read `.value`, which maps each held key to its physical `event.code`. |
77
78