@tanstack/lit-hotkeys 0.27.5 → 0.28.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.
@@ -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 };
@@ -73,5 +73,4 @@ export declare class FormatForDisplayController implements ReactiveController {
73
73
  */
74
74
  dispose(): void;
75
75
  private _read;
76
- private _sync;
77
76
  }
@@ -1,4 +1,4 @@
1
- import { KeyboardLayoutController } from "./keyboard-layout.js";
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._read().keyboardLayout !== false && this._layout?.value || null);
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._sync();
59
+ this._layout.sync();
59
60
  }
60
61
  /** Follows a `keyboardLayout` option that a host property changed. */
61
62
  hostUpdate() {
62
- this._sync();
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?.dispose();
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";
@@ -9,4 +10,4 @@ import { KeyHoldController } from "./key-hold.js";
9
10
  import { HotkeyHintController } from "./hotkey-hint.js";
10
11
  import { HotkeyScopesStateController } from "./hotkey-scopes-state.js";
11
12
  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 };
13
+ export { AppHotkeyControllerOptions, AppHotkeysController, FormatForAriaController, FormatForAriaControllerOptions, FormatForDisplayController, FormatForDisplayControllerOptions, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyControllerChordOptions, HotkeyControllerCommonOptions, HotkeyControllerOptions, HotkeyControllerOptionsFor, HotkeyControllerSequenceOptions, HotkeyControllerTarget, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController };
@@ -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,6 +2,7 @@ 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";
@@ -13,5 +14,6 @@ import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.j
13
14
  import { KeyboardLayoutController } from "./controllers/keyboard-layout.js";
14
15
  import "./controllers/index.js";
15
16
  import { formatForDisplay } from "./format-for-display.js";
17
+ import { formatForAria } from "./format-for-aria.js";
16
18
  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 };
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 };
package/dist/index.js CHANGED
@@ -2,16 +2,18 @@ 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";
11
12
  import { HotkeyHintController } from "./controllers/hotkey-hint.js";
12
13
  import { HotkeyScopesStateController } from "./controllers/hotkey-scopes-state.js";
13
14
  import { formatForDisplay } from "./format-for-display.js";
15
+ import { formatForAria } from "./format-for-aria.js";
14
16
 
15
17
  export * from "@tanstack/hotkeys"
16
18
 
17
- export { AppHotkeysController, FormatForDisplayController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, formatForDisplay, hotkey };
19
+ export { AppHotkeysController, FormatForAriaController, FormatForDisplayController, HeldKeyCodesController, HeldKeysController, HotkeyController, HotkeyHintController, HotkeyRecorderController, HotkeyRegistrationsController, HotkeyScopesStateController, KeyHoldController, KeyboardLayoutController, formatForAria, formatForDisplay, hotkey };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/lit-hotkeys",
3
- "version": "0.27.5",
3
+ "version": "0.28.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.35.0"
38
+ "@tanstack/hotkeys": "0.37.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@lit-labs/ssr": "^4.1.0",
@@ -65,6 +65,7 @@ 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). |
@@ -74,7 +75,7 @@ export class MyEditor extends LitElement {
74
75
  | `new HeldKeysController(host)` | Read `.value`, every key that's held right now. |
75
76
  | `new HeldKeyCodesController(host)` | Read `.value`, which maps each held key to its physical `event.code`. |
76
77
 
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`.
78
+ 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
79
 
79
80
  ## Pitfalls
80
81