@tanstack/lit-hotkeys 0.27.6 → 0.29.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/dist/controllers/_formatter-layout.js +35 -0
- package/dist/controllers/format-for-aria.d.ts +78 -0
- package/dist/controllers/format-for-aria.js +85 -0
- package/dist/controllers/format-for-display.d.ts +0 -1
- package/dist/controllers/format-for-display.js +6 -14
- package/dist/controllers/hotkey-registrations.js +1 -0
- package/dist/controllers/index.d.ts +3 -1
- package/dist/controllers/key-lock-state.d.ts +61 -0
- package/dist/controllers/key-lock-state.js +94 -0
- package/dist/format-for-aria.d.ts +12 -0
- package/dist/format-for-aria.js +15 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +5 -2
- package/package.json +2 -2
- package/skills/lit-hotkeys/SKILL.md +3 -1
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { KeyboardLayoutController } from "./keyboard-layout.js";
|
|
2
|
+
|
|
3
|
+
// src/controllers/_formatter-layout.ts
|
|
4
|
+
/**
|
|
5
|
+
* The keyboard layout for a formatter controller's host. `sync` adds a
|
|
6
|
+
* layout controller to the host, or removes it when `enabled` returns false.
|
|
7
|
+
*/
|
|
8
|
+
var FormatterLayout = class {
|
|
9
|
+
_host;
|
|
10
|
+
_enabled;
|
|
11
|
+
_layout;
|
|
12
|
+
constructor(_host, _enabled) {
|
|
13
|
+
this._host = _host;
|
|
14
|
+
this._enabled = _enabled;
|
|
15
|
+
}
|
|
16
|
+
/** The loaded layout, or null when there is none or it is turned off. */
|
|
17
|
+
get value() {
|
|
18
|
+
return this._enabled() && this._layout?.value || null;
|
|
19
|
+
}
|
|
20
|
+
/** Follows the `enabled` option: starts or stops reading the layout. */
|
|
21
|
+
sync() {
|
|
22
|
+
if (!this._enabled()) {
|
|
23
|
+
this.dispose();
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
this._layout ??= new KeyboardLayoutController(this._host);
|
|
27
|
+
}
|
|
28
|
+
/** Releases the layout controller and removes it from the host. */
|
|
29
|
+
dispose() {
|
|
30
|
+
this._layout?.dispose();
|
|
31
|
+
this._layout = void 0;
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export { FormatterLayout };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { HotkeyPlatform, MaybeGetter, formatForAria } from "@tanstack/hotkeys";
|
|
2
|
+
import { ReactiveController, ReactiveControllerHost } from "lit";
|
|
3
|
+
/** Options for `FormatForAriaController`. */
|
|
4
|
+
export interface FormatForAriaControllerOptions {
|
|
5
|
+
/**
|
|
6
|
+
* The user's platform, for every value from `format`. Detected when unset.
|
|
7
|
+
* On a server, set it from the request with `detectPlatform(userAgent)`.
|
|
8
|
+
*/
|
|
9
|
+
platform?: HotkeyPlatform;
|
|
10
|
+
/**
|
|
11
|
+
* Whether physical bindings follow the user's keyboard layout, so `[KeyQ]`
|
|
12
|
+
* is `A` on a French AZERTY keyboard. The layout loads from the browser
|
|
13
|
+
* when the first host connects. Only Chromium browsers report one. Set
|
|
14
|
+
* `false` to never ask the browser for it. Default: `true`.
|
|
15
|
+
*/
|
|
16
|
+
keyboardLayout?: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A Lit ReactiveController whose `format` is `formatForAria` with the
|
|
20
|
+
* controller's `platform` option, or the detected platform without one. A
|
|
21
|
+
* `platform` passed to `format` wins. Use its value for `aria-keyshortcuts`
|
|
22
|
+
* with Lit's `ifDefined`, so Lit omits the attribute for a multi-step
|
|
23
|
+
* sequence, no hotkey, or a hotkey that can't fire right now (one that is
|
|
24
|
+
* disabled, in a suspended scope, a suspended character key, or not
|
|
25
|
+
* registered). The host updates when a registration changes.
|
|
26
|
+
*
|
|
27
|
+
* Physical bindings such as `[KeyQ]` name the key the user presses in
|
|
28
|
+
* Chromium browsers, once the keyboard layout has loaded. The host updates
|
|
29
|
+
* when the layout loads or changes. A `layoutMap` passed to `format` wins.
|
|
30
|
+
*
|
|
31
|
+
* Lit has no provider, so the options are the only defaults. Pass a getter to
|
|
32
|
+
* read them from host properties.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* class SearchButton extends LitElement {
|
|
37
|
+
* private formatForAria = new FormatForAriaController(this)
|
|
38
|
+
*
|
|
39
|
+
* render() {
|
|
40
|
+
* return html`
|
|
41
|
+
* <button aria-keyshortcuts=${ifDefined(this.formatForAria.format('Mod+K'))}>
|
|
42
|
+
* Search
|
|
43
|
+
* </button>
|
|
44
|
+
* `
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare class FormatForAriaController implements ReactiveController {
|
|
50
|
+
private _host;
|
|
51
|
+
private _options;
|
|
52
|
+
/** Reads the layout for the host, unless `keyboardLayout` is false. */
|
|
53
|
+
private _layout;
|
|
54
|
+
/** Updates the host when a registration changes. */
|
|
55
|
+
private _registrations;
|
|
56
|
+
/**
|
|
57
|
+
* Formats a hotkey for `aria-keyshortcuts`, with the controller's platform
|
|
58
|
+
* and the user's keyboard layout filled in. It takes the same options as
|
|
59
|
+
* `formatForAria` and needs no `this`, so it can be passed around.
|
|
60
|
+
*/
|
|
61
|
+
format: typeof formatForAria;
|
|
62
|
+
/**
|
|
63
|
+
* @param _host - The Lit component that owns this controller. The controller adds itself to it.
|
|
64
|
+
* @param _options - Options or a getter. `platform` is read for each value. `keyboardLayout` is read on connection and before host updates.
|
|
65
|
+
*/
|
|
66
|
+
constructor(_host: ReactiveControllerHost, _options?: MaybeGetter<FormatForAriaControllerOptions>);
|
|
67
|
+
/** Starts reading the layout, unless `keyboardLayout` is false. */
|
|
68
|
+
hostConnected(): void;
|
|
69
|
+
/** Follows a `keyboardLayout` option that a host property changed. */
|
|
70
|
+
hostUpdate(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Releases the controller now and removes it from its host, with the
|
|
73
|
+
* layout controller it added. Use this method to remove a controller early,
|
|
74
|
+
* because Lit's `removeController` alone leaves it active.
|
|
75
|
+
*/
|
|
76
|
+
dispose(): void;
|
|
77
|
+
private _read;
|
|
78
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { FormatterLayout } from "./_formatter-layout.js";
|
|
2
|
+
import { HotkeyRegistrationsController } from "./hotkey-registrations.js";
|
|
3
|
+
import { formatForAria } from "@tanstack/hotkeys";
|
|
4
|
+
import { announceLiveHotkeys, createFormatter, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
|
|
5
|
+
|
|
6
|
+
// src/controllers/format-for-aria.ts
|
|
7
|
+
/**
|
|
8
|
+
* A Lit ReactiveController whose `format` is `formatForAria` with the
|
|
9
|
+
* controller's `platform` option, or the detected platform without one. A
|
|
10
|
+
* `platform` passed to `format` wins. Use its value for `aria-keyshortcuts`
|
|
11
|
+
* with Lit's `ifDefined`, so Lit omits the attribute for a multi-step
|
|
12
|
+
* sequence, no hotkey, or a hotkey that can't fire right now (one that is
|
|
13
|
+
* disabled, in a suspended scope, a suspended character key, or not
|
|
14
|
+
* registered). The host updates when a registration changes.
|
|
15
|
+
*
|
|
16
|
+
* Physical bindings such as `[KeyQ]` name the key the user presses in
|
|
17
|
+
* Chromium browsers, once the keyboard layout has loaded. The host updates
|
|
18
|
+
* when the layout loads or changes. A `layoutMap` passed to `format` wins.
|
|
19
|
+
*
|
|
20
|
+
* Lit has no provider, so the options are the only defaults. Pass a getter to
|
|
21
|
+
* read them from host properties.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* class SearchButton extends LitElement {
|
|
26
|
+
* private formatForAria = new FormatForAriaController(this)
|
|
27
|
+
*
|
|
28
|
+
* render() {
|
|
29
|
+
* return html`
|
|
30
|
+
* <button aria-keyshortcuts=${ifDefined(this.formatForAria.format('Mod+K'))}>
|
|
31
|
+
* Search
|
|
32
|
+
* </button>
|
|
33
|
+
* `
|
|
34
|
+
* }
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
var FormatForAriaController = class {
|
|
39
|
+
_host;
|
|
40
|
+
_options;
|
|
41
|
+
/** Reads the layout for the host, unless `keyboardLayout` is false. */
|
|
42
|
+
_layout;
|
|
43
|
+
/** Updates the host when a registration changes. */
|
|
44
|
+
_registrations;
|
|
45
|
+
/**
|
|
46
|
+
* Formats a hotkey for `aria-keyshortcuts`, with the controller's platform
|
|
47
|
+
* and the user's keyboard layout filled in. It takes the same options as
|
|
48
|
+
* `formatForAria` and needs no `this`, so it can be passed around.
|
|
49
|
+
*/
|
|
50
|
+
format = announceLiveHotkeys(createFormatter(formatForAria, () => this._read().platform, () => this._layout.value), () => this._registrations.hotkeys);
|
|
51
|
+
/**
|
|
52
|
+
* @param _host - The Lit component that owns this controller. The controller adds itself to it.
|
|
53
|
+
* @param _options - Options or a getter. `platform` is read for each value. `keyboardLayout` is read on connection and before host updates.
|
|
54
|
+
*/
|
|
55
|
+
constructor(_host, _options = {}) {
|
|
56
|
+
this._host = _host;
|
|
57
|
+
this._options = _options;
|
|
58
|
+
this._layout = new FormatterLayout(_host, () => this._read().keyboardLayout !== false);
|
|
59
|
+
this._registrations = new HotkeyRegistrationsController(_host);
|
|
60
|
+
this._host.addController(this);
|
|
61
|
+
}
|
|
62
|
+
/** Starts reading the layout, unless `keyboardLayout` is false. */
|
|
63
|
+
hostConnected() {
|
|
64
|
+
this._layout.sync();
|
|
65
|
+
}
|
|
66
|
+
/** Follows a `keyboardLayout` option that a host property changed. */
|
|
67
|
+
hostUpdate() {
|
|
68
|
+
this._layout.sync();
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Releases the controller now and removes it from its host, with the
|
|
72
|
+
* layout controller it added. Use this method to remove a controller early,
|
|
73
|
+
* because Lit's `removeController` alone leaves it active.
|
|
74
|
+
*/
|
|
75
|
+
dispose() {
|
|
76
|
+
this._layout.dispose();
|
|
77
|
+
this._registrations.dispose();
|
|
78
|
+
this._host.removeController(this);
|
|
79
|
+
}
|
|
80
|
+
_read() {
|
|
81
|
+
return resolveMaybeGetter(this._options);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
export { FormatForAriaController };
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FormatterLayout } from "./_formatter-layout.js";
|
|
2
2
|
import { createFormatForDisplay, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
|
|
3
3
|
|
|
4
4
|
// src/controllers/format-for-display.ts
|
|
@@ -43,7 +43,7 @@ var FormatForDisplayController = class {
|
|
|
43
43
|
* user's keyboard layout filled in. It takes the same options as
|
|
44
44
|
* `formatForDisplay` and needs no `this`, so it can be passed around.
|
|
45
45
|
*/
|
|
46
|
-
format = createFormatForDisplay(() => this._read().platform, () => this.
|
|
46
|
+
format = createFormatForDisplay(() => this._read().platform, () => this._layout.value);
|
|
47
47
|
/**
|
|
48
48
|
* @param _host - The Lit component that owns this controller. The controller adds itself to it.
|
|
49
49
|
* @param _options - Options or a getter. `platform` is read for each label. `keyboardLayout` is read on connection and before host updates.
|
|
@@ -51,15 +51,16 @@ var FormatForDisplayController = class {
|
|
|
51
51
|
constructor(_host, _options = {}) {
|
|
52
52
|
this._host = _host;
|
|
53
53
|
this._options = _options;
|
|
54
|
+
this._layout = new FormatterLayout(_host, () => this._read().keyboardLayout !== false);
|
|
54
55
|
this._host.addController(this);
|
|
55
56
|
}
|
|
56
57
|
/** Starts reading the layout, unless `keyboardLayout` is false. */
|
|
57
58
|
hostConnected() {
|
|
58
|
-
this.
|
|
59
|
+
this._layout.sync();
|
|
59
60
|
}
|
|
60
61
|
/** Follows a `keyboardLayout` option that a host property changed. */
|
|
61
62
|
hostUpdate() {
|
|
62
|
-
this.
|
|
63
|
+
this._layout.sync();
|
|
63
64
|
}
|
|
64
65
|
/**
|
|
65
66
|
* Releases the controller now and removes it from its host, with the
|
|
@@ -67,21 +68,12 @@ var FormatForDisplayController = class {
|
|
|
67
68
|
* `removeController` alone leaves it active.
|
|
68
69
|
*/
|
|
69
70
|
dispose() {
|
|
70
|
-
this._layout
|
|
71
|
-
this._layout = void 0;
|
|
71
|
+
this._layout.dispose();
|
|
72
72
|
this._host.removeController(this);
|
|
73
73
|
}
|
|
74
74
|
_read() {
|
|
75
75
|
return resolveMaybeGetter(this._options);
|
|
76
76
|
}
|
|
77
|
-
_sync() {
|
|
78
|
-
if (this._read().keyboardLayout === false) {
|
|
79
|
-
this._layout?.dispose();
|
|
80
|
-
this._layout = void 0;
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
this._layout ??= new KeyboardLayoutController(this._host);
|
|
84
|
-
}
|
|
85
77
|
};
|
|
86
78
|
|
|
87
79
|
export { FormatForDisplayController };
|
|
@@ -41,6 +41,7 @@ var HotkeyRegistrationsController = class {
|
|
|
41
41
|
const manager = getHotkeyManager();
|
|
42
42
|
const read = () => getHotkeyRegistrationViews(manager.registrations.state);
|
|
43
43
|
this._hotkeys = read();
|
|
44
|
+
this._host.requestUpdate();
|
|
44
45
|
if (isServer()) return;
|
|
45
46
|
const subscription = manager.registrations.subscribe(() => {
|
|
46
47
|
this._hotkeys = read();
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { AppHotkeysController } from "./app-hotkeys.js";
|
|
2
|
+
import { FormatForAriaController, FormatForAriaControllerOptions } from "./format-for-aria.js";
|
|
2
3
|
import { FormatForDisplayController, FormatForDisplayControllerOptions } from "./format-for-display.js";
|
|
3
4
|
import { HeldKeyCodesController } from "./held-key-codes.js";
|
|
4
5
|
import { HeldKeysController } from "./held-keys.js";
|
|
@@ -6,7 +7,8 @@ import { AppHotkeyControllerOptions, HotkeyController, HotkeyControllerChordOpti
|
|
|
6
7
|
import { HotkeyRecorderController } from "./hotkey-recorder.js";
|
|
7
8
|
import { HotkeyRegistrationsController } from "./hotkey-registrations.js";
|
|
8
9
|
import { KeyHoldController } from "./key-hold.js";
|
|
10
|
+
import { KeyLockStateController } from "./key-lock-state.js";
|
|
9
11
|
import { HotkeyHintController } from "./hotkey-hint.js";
|
|
10
12
|
import { HotkeyScopesStateController } from "./hotkey-scopes-state.js";
|
|
11
13
|
import { KeyboardLayoutController } from "./keyboard-layout.js";
|
|
12
|
-
export { AppHotkeyControllerOptions, AppHotkeysController, 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 };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { formatForAria } from "@tanstack/hotkeys";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a hotkey for `aria-keyshortcuts`, without a controller's platform or
|
|
4
|
+
* the user's keyboard layout.
|
|
5
|
+
*
|
|
6
|
+
* @deprecated Use `FormatForAriaController` in elements. Its `format` is
|
|
7
|
+
* this function with the controller's platform and the user's keyboard layout
|
|
8
|
+
* filled in. Outside an element, keep calling this function and pass `platform`
|
|
9
|
+
* yourself.
|
|
10
|
+
*/
|
|
11
|
+
declare const formatForAria$1: typeof formatForAria;
|
|
12
|
+
export { formatForAria$1 as formatForAria };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { formatForAria } from "@tanstack/hotkeys";
|
|
2
|
+
|
|
3
|
+
// src/format-for-aria.ts
|
|
4
|
+
/**
|
|
5
|
+
* Formats a hotkey for `aria-keyshortcuts`, without a controller's platform or
|
|
6
|
+
* the user's keyboard layout.
|
|
7
|
+
*
|
|
8
|
+
* @deprecated Use `FormatForAriaController` in elements. Its `format` is
|
|
9
|
+
* this function with the controller's platform and the user's keyboard layout
|
|
10
|
+
* filled in. Outside an element, keep calling this function and pass `platform`
|
|
11
|
+
* yourself.
|
|
12
|
+
*/
|
|
13
|
+
const formatForAria$1 = formatForAria;
|
|
14
|
+
|
|
15
|
+
export { formatForAria$1 as formatForAria };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,16 +2,19 @@ import { AppHotkeyControllerOptions, HotkeyController, HotkeyControllerChordOpti
|
|
|
2
2
|
import { HotkeyDecorator, hotkey } from "./decorators/hotkey.js";
|
|
3
3
|
import "./decorators/index.js";
|
|
4
4
|
import { AppHotkeysController } from "./controllers/app-hotkeys.js";
|
|
5
|
+
import { FormatForAriaController, FormatForAriaControllerOptions } from "./controllers/format-for-aria.js";
|
|
5
6
|
import { FormatForDisplayController, FormatForDisplayControllerOptions } from "./controllers/format-for-display.js";
|
|
6
7
|
import { HeldKeyCodesController } from "./controllers/held-key-codes.js";
|
|
7
8
|
import { HeldKeysController } from "./controllers/held-keys.js";
|
|
8
9
|
import { HotkeyRecorderController } from "./controllers/hotkey-recorder.js";
|
|
9
10
|
import { HotkeyRegistrationsController } from "./controllers/hotkey-registrations.js";
|
|
10
11
|
import { KeyHoldController } from "./controllers/key-hold.js";
|
|
12
|
+
import { KeyLockStateController } from "./controllers/key-lock-state.js";
|
|
11
13
|
import { HotkeyHintController } from "./controllers/hotkey-hint.js";
|
|
12
14
|
import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.js";
|
|
13
15
|
import { KeyboardLayoutController } from "./controllers/keyboard-layout.js";
|
|
14
16
|
import "./controllers/index.js";
|
|
15
17
|
import { formatForDisplay } from "./format-for-display.js";
|
|
18
|
+
import { formatForAria } from "./format-for-aria.js";
|
|
16
19
|
export * from "@tanstack/hotkeys";
|
|
17
|
-
export { AppHotkeyControllerOptions, AppHotkeysController, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyDecorator, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, 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
|
@@ -2,16 +2,19 @@ import { HotkeyController } from "./controllers/hotkey.js";
|
|
|
2
2
|
import { hotkey } from "./decorators/hotkey.js";
|
|
3
3
|
import { AppHotkeysController } from "./controllers/app-hotkeys.js";
|
|
4
4
|
import { KeyboardLayoutController } from "./controllers/keyboard-layout.js";
|
|
5
|
+
import { HotkeyRegistrationsController } from "./controllers/hotkey-registrations.js";
|
|
6
|
+
import { FormatForAriaController } from "./controllers/format-for-aria.js";
|
|
5
7
|
import { FormatForDisplayController } from "./controllers/format-for-display.js";
|
|
6
8
|
import { HeldKeyCodesController } from "./controllers/held-key-codes.js";
|
|
7
9
|
import { HeldKeysController } from "./controllers/held-keys.js";
|
|
8
10
|
import { HotkeyRecorderController } from "./controllers/hotkey-recorder.js";
|
|
9
|
-
import { HotkeyRegistrationsController } from "./controllers/hotkey-registrations.js";
|
|
10
11
|
import { KeyHoldController } from "./controllers/key-hold.js";
|
|
12
|
+
import { KeyLockStateController } from "./controllers/key-lock-state.js";
|
|
11
13
|
import { HotkeyHintController } from "./controllers/hotkey-hint.js";
|
|
12
14
|
import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.js";
|
|
13
15
|
import { formatForDisplay } from "./format-for-display.js";
|
|
16
|
+
import { formatForAria } from "./format-for-aria.js";
|
|
14
17
|
|
|
15
18
|
export * from "@tanstack/hotkeys"
|
|
16
19
|
|
|
17
|
-
export { AppHotkeysController, FormatForDisplayController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, 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.
|
|
3
|
+
"version": "0.29.0",
|
|
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.
|
|
38
|
+
"@tanstack/hotkeys": "0.38.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@lit-labs/ssr": "^4.1.0",
|
|
@@ -65,16 +65,18 @@ export class MyEditor extends LitElement {
|
|
|
65
65
|
| `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys once at startup, since Lit has no provider. |
|
|
66
66
|
| `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. |
|
|
67
67
|
| `new FormatForDisplayController(host, options?)` | Read `.format`, the function that formats a hotkey as a label, with the controller's `platform` option and the user's keyboard layout filled in. Only Chromium browsers report a layout, so physical keys keep their US labels in Firefox and Safari. Use it for every label in an element: `private formatForDisplay = new FormatForDisplayController(this)` as a field, then `this.formatForDisplay.format(hotkey)`. With Lit SSR, pass `() => ({ platform: this.platform })` and set that property from `detectPlatform(userAgent)`. |
|
|
68
|
+
| `new FormatForAriaController(host, options?)` | Read `.format`, the function that formats a hotkey for `aria-keyshortcuts`, with the controller's `platform` and the user's keyboard layout filled in. Returns `undefined` for a multi-step sequence, no hotkey, or a hotkey that can't fire right now, such as a disabled or suspended one. Bind the result with `ifDefined` so Lit omits the attribute. |
|
|
68
69
|
| `new AppHotkeysController(host)` | Read `appHotkeys`, the cheat sheet `list`, and `customHotkeys`, and change custom hotkeys with `setCustomHotkey`, `resetCustomHotkey`, and `findConflicts`. |
|
|
69
70
|
| `new HotkeyRecorderController(host, options)` | Record a chord or sequence the user presses, for a settings screen. |
|
|
70
71
|
| `new HotkeyRegistrationsController(host)` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
|
|
71
72
|
| `new HotkeyScopesStateController(host)` | Read `.value`, the active scopes and the exclusive scope that owns the keyboard. |
|
|
72
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. |
|
|
73
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. |
|
|
74
76
|
| `new HeldKeysController(host)` | Read `.value`, every key that's held right now. |
|
|
75
77
|
| `new HeldKeyCodesController(host)` | Read `.value`, which maps each held key to its physical `event.code`. |
|
|
76
78
|
|
|
77
|
-
The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `FormatForDisplayController` in elements. Outside an element, call the plain function and pass `platform`.
|
|
79
|
+
The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `FormatForDisplayController` in elements. Outside an element, call the plain function and pass `platform`. The plain `formatForAria` export is deprecated the same way, in favor of `FormatForAriaController`.
|
|
78
80
|
|
|
79
81
|
## Pitfalls
|
|
80
82
|
|