@tanstack/ember-hotkeys 0.9.0 → 0.10.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.
@@ -7,7 +7,8 @@ import { OnHotkey } from "./onHotkey.js";
7
7
  //#region src/createHotkeysScope.d.ts
8
8
  /**
9
9
  * Helpers and modifiers with shared defaults. Owners retain their own cleanup.
10
- * Pass the scope to `useHotkeyRecorder` to apply its recorder defaults.
10
+ * Pass the scope to `useHotkeyRecorder` to apply its recorder defaults, and to
11
+ * `useFormatForDisplay` to apply its `platform`.
11
12
  */
12
13
  export interface EmberHotkeysScope {
13
14
  onHotkey: typeof OnHotkey;
@@ -3,7 +3,7 @@ import { UseHotkeys } from "./useHotkeys.js";
3
3
  import { read, scopeDefaults } from "./utils.js";
4
4
  import { OnHotkey } from "./onHotkey.js";
5
5
  import { OnHotkeys } from "./onHotkeys.js";
6
- import { definedOptions } from "@tanstack/hotkeys/adapter";
6
+ import { definedOptions, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
7
7
 
8
8
  //#region src/createHotkeysScope.ts
9
9
  /**
@@ -11,29 +11,30 @@ import { definedOptions } from "@tanstack/hotkeys/adapter";
11
11
  * Pass the scope to child components through arguments to share configuration.
12
12
  */
13
13
  function createHotkeysScope(defaultOptions = {}) {
14
+ const defaults = () => mergeHotkeysProviderDefaults(void 0, read(defaultOptions), void 0);
14
15
  const scope = {
15
16
  onHotkey: class extends OnHotkey {
16
17
  modify(...[element, args, options]) {
17
- super.modify(element, args, { ...definedOptions(read(defaultOptions).hotkey, options) });
18
+ super.modify(element, args, { ...definedOptions(defaults().hotkey, options) });
18
19
  }
19
20
  },
20
21
  onHotkeys: class extends OnHotkeys {
21
22
  modify(...[element, args, options]) {
22
- super.modify(element, args, { ...definedOptions(read(defaultOptions).hotkey, options) });
23
+ super.modify(element, args, { ...definedOptions(defaults().hotkey, options) });
23
24
  }
24
25
  },
25
26
  useHotkey: class extends UseHotkey {
26
27
  compute(...[args, options]) {
27
- super.compute(args, { ...definedOptions(read(defaultOptions).hotkey, options) });
28
+ super.compute(args, { ...definedOptions(defaults().hotkey, options) });
28
29
  }
29
30
  },
30
31
  useHotkeys: class extends UseHotkeys {
31
32
  compute(...[args, options]) {
32
- super.compute(args, { ...definedOptions(read(defaultOptions).hotkey, options) });
33
+ super.compute(args, { ...definedOptions(defaults().hotkey, options) });
33
34
  }
34
35
  }
35
36
  };
36
- scopeDefaults.set(scope, defaultOptions);
37
+ scopeDefaults.set(scope, defaults);
37
38
  return scope;
38
39
  }
39
40
 
package/dist/index.d.ts CHANGED
@@ -13,5 +13,6 @@ import { OnHotkey } from "./onHotkey.js";
13
13
  import { EmberHotkeysScope, createHotkeysScope } from "./createHotkeysScope.js";
14
14
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
15
15
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
16
+ import { useFormatForDisplay } from "./useFormatForDisplay.js";
16
17
  export * from "@tanstack/hotkeys";
17
- export { DefaultHotkeysOptions, type ElementHotkeyOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget, EmberHotkeysScope, HotkeyRegistrationsResult, type MaybeGetter, createHotkeysScope, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold };
18
+ export { DefaultHotkeysOptions, type ElementHotkeyOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget, EmberHotkeysScope, HotkeyRegistrationsResult, type MaybeGetter, createHotkeysScope, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold };
package/dist/index.js CHANGED
@@ -8,10 +8,11 @@ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
8
8
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
9
9
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
10
10
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
11
+ import { useFormatForDisplay } from "./useFormatForDisplay.js";
11
12
  import { OnHotkey } from "./onHotkey.js";
12
13
  import { OnHotkeys } from "./onHotkeys.js";
13
14
  import { createHotkeysScope } from "./createHotkeysScope.js";
14
15
 
15
16
  export * from "@tanstack/hotkeys"
16
17
 
17
- export { createHotkeysScope, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold };
18
+ export { createHotkeysScope, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold };
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, HotkeyChordRecorderOptions, HotkeyRegistrationView, HotkeySequence, HotkeySequenceRecorderOptions, RegisteredAppHotkeys, Target } from "@tanstack/hotkeys";
1
+ import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeyRegistrationView, HotkeySequence, HotkeySequenceRecorderOptions, RegisteredAppHotkeys, Target } from "@tanstack/hotkeys";
2
2
  import { AdapterCommonOptions, AdapterHotkeyDefinition } from "@tanstack/hotkeys/adapter";
3
3
  //#region src/types.d.ts
4
4
  /** A reactive value. Read `.value` where the framework tracks dependencies. */
@@ -53,6 +53,13 @@ export interface EmberCustomHotkeys {
53
53
  }
54
54
  /** Shared defaults; call-specific options take precedence. */
55
55
  export interface DefaultHotkeysOptions {
56
+ /**
57
+ * The user's platform, for labels from `useFormatForDisplay`, hotkeys, and
58
+ * recorders. `hotkey.platform` and `hotkeyRecorder.platform` win. Detected
59
+ * when unset. On a server, set it from the request with
60
+ * `detectPlatform(userAgent)`.
61
+ */
62
+ platform?: HotkeyPlatform;
56
63
  /** Defaults for every hotkey. `requireReset` applies to chords and `timeout` to sequences. */
57
64
  hotkey?: EmberHotkeyCommonOptions;
58
65
  /** Defaults for every recorder. */
@@ -0,0 +1,25 @@
1
+ import { EmberHotkeysScope } from "./createHotkeysScope.js";
2
+ import { formatForDisplay } from "@tanstack/hotkeys";
3
+ //#region src/useFormatForDisplay.d.ts
4
+ /**
5
+ * Returns `formatForDisplay` with the scope's `platform` as the default, so
6
+ * labels match the user's platform. The returned function works anywhere,
7
+ * including templates, and reads the scope's defaults when called, so
8
+ * templates update when they change. An explicit `platform` option wins.
9
+ *
10
+ * Server-rendered apps should set the scope's default `platform` from the
11
+ * request user agent, such as
12
+ * `detectPlatform(fastboot.request.headers.get('user-agent'))` with the
13
+ * FastBoot service.
14
+ *
15
+ * @example
16
+ * ```gts
17
+ * class SaveButton extends Component<{ Args: { hotkeys: EmberHotkeysScope } }> {
18
+ * format = useFormatForDisplay(this.args.hotkeys)
19
+ *
20
+ * <template><kbd>{{this.format "Mod+S"}}</kbd></template>
21
+ * }
22
+ * ```
23
+ */
24
+ export declare function useFormatForDisplay(scope?: EmberHotkeysScope): typeof formatForDisplay;
25
+ //#endregion
@@ -0,0 +1,33 @@
1
+ import { scopeDefaults } from "./utils.js";
2
+ import { formatForDisplay } from "@tanstack/hotkeys";
3
+
4
+ //#region src/useFormatForDisplay.ts
5
+ /**
6
+ * Returns `formatForDisplay` with the scope's `platform` as the default, so
7
+ * labels match the user's platform. The returned function works anywhere,
8
+ * including templates, and reads the scope's defaults when called, so
9
+ * templates update when they change. An explicit `platform` option wins.
10
+ *
11
+ * Server-rendered apps should set the scope's default `platform` from the
12
+ * request user agent, such as
13
+ * `detectPlatform(fastboot.request.headers.get('user-agent'))` with the
14
+ * FastBoot service.
15
+ *
16
+ * @example
17
+ * ```gts
18
+ * class SaveButton extends Component<{ Args: { hotkeys: EmberHotkeysScope } }> {
19
+ * format = useFormatForDisplay(this.args.hotkeys)
20
+ *
21
+ * <template><kbd>{{this.format "Mod+S"}}</kbd></template>
22
+ * }
23
+ * ```
24
+ */
25
+ function useFormatForDisplay(scope) {
26
+ return ((hotkey, options) => formatForDisplay(hotkey, {
27
+ ...options,
28
+ platform: options?.platform ?? (scope && scopeDefaults.get(scope)().platform)
29
+ }));
30
+ }
31
+
32
+ //#endregion
33
+ export { useFormatForDisplay };
@@ -5,7 +5,7 @@ import { withDefaults } from "@tanstack/hotkeys/adapter";
5
5
 
6
6
  //#region src/useHotkeyRecorder.ts
7
7
  function useHotkeyRecorder(owner, options, scope) {
8
- const recorder = new HotkeyRecorder(() => withDefaults(scope && read(scopeDefaults.get(scope)).hotkeyRecorder, read(options)));
8
+ const recorder = new HotkeyRecorder(() => withDefaults(scope && scopeDefaults.get(scope)().hotkeyRecorder, read(options)));
9
9
  const state = select(owner, recorder.store, (value) => value);
10
10
  registerDestructor(owner, () => recorder.destroy());
11
11
  return {
package/dist/utils.js CHANGED
@@ -4,7 +4,7 @@ import { subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
4
4
  import { trackedObject } from "@ember/reactive/collections";
5
5
 
6
6
  //#region src/utils.ts
7
- /** The defaults behind each `createHotkeysScope` result. */
7
+ /** Reads the merged defaults behind each `createHotkeysScope` result. */
8
8
  const scopeDefaults = /* @__PURE__ */ new WeakMap();
9
9
  function read(value) {
10
10
  return typeof value === "function" ? value() : value;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ember-hotkeys",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Ember adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -40,7 +40,7 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@embroider/addon-shim": "^1.10.3",
43
- "@tanstack/hotkeys": "0.22.1",
43
+ "@tanstack/hotkeys": "0.23.0",
44
44
  "ember-modifier": "^4.2.2"
45
45
  },
46
46
  "devDependencies": {
@@ -62,6 +62,7 @@ export default class Editor extends Component {
62
62
  | `{{onHotkey hotkey callback ...options}}` | Attach a hotkey to the element the modifier is on, so it fires while focus is inside that element. |
63
63
  | `{{onHotkeys definitions}}` | Attach several hotkeys to one element. |
64
64
  | `createHotkeysScope(defaults?)` | Create `useHotkey`, `useHotkeys`, `onHotkey`, and `onHotkeys` that share default options. |
65
+ | `useFormatForDisplay(scope?)` | Get `formatForDisplay` with the scope's `platform` filled in, for labels rendered by FastBoot. |
65
66
  | `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys before the app boots, since Ember has no provider. |
66
67
  | `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
68
  | `useAppHotkeys(this)` | Get the declared app hotkeys with the user's custom hotkeys applied. |
@@ -86,6 +87,7 @@ The helpers are template helpers, not JavaScript functions. State readers take t
86
87
  - **`createHotkeysScope` isn't a hotkey scope.** It only sets defaults for its helpers. For a dialog, call `getHotkeyManager().activateScope(name, { exclusive: true })` in the constructor, pass the result to `registerDestructor(this, release)`, and put `scope=name` on the dialog's hotkeys. Give each open dialog its own name.
87
88
  - **Read app hotkeys through getters.** Template paths can't index ids that contain dots.
88
89
  - **Scopes have no `useHotkeyRecorder`** since v0.9.0. Call `useHotkeyRecorder(this, options, this.hotkeys)`, so apps that set defaults but don't record don't ship the recorder.
90
+ - **Server-rendered labels need the user's platform.** On the server, `formatForDisplay` can't see the user's OS, so it renders Linux labels that differ after hydration for Mac and Windows users. Pass `platform: detectPlatform(userAgent)` to `createHotkeysScope`, with the user agent from FastBoot's request, and format labels with `format = useFormatForDisplay(this.hotkeys)`, then `{{this.format hotkey}}`. Keep plain `formatForDisplay` for labels that render only in the browser.
89
91
 
90
92
  When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Ember](references/app-hotkeys.md).
91
93