@tanstack/ember-hotkeys 0.11.1 → 0.13.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.
@@ -6,8 +6,9 @@ import { DefaultHotkeysOptions } from "./types.js";
6
6
  import { MaybeGetter } from "./utils.js";
7
7
  /**
8
8
  * Helpers and modifiers with shared defaults. Owners retain their own cleanup.
9
- * Pass the scope to `useHotkeyRecorder` to apply its recorder defaults, and to
10
- * `useFormatForDisplay` to apply its `platform`.
9
+ * Pass the scope to `useHotkeyRecorder` to apply its recorder defaults, to
10
+ * `useFormatForDisplay` to apply its `platform` and `keyboardLayout`, and to
11
+ * `useKeyboardLayout` to apply its `keyboardLayout`.
11
12
  */
12
13
  export interface EmberHotkeysScope {
13
14
  onHotkey: typeof OnHotkey;
@@ -0,0 +1,11 @@
1
+ import { formatForDisplay } from "@tanstack/hotkeys";
2
+ /**
3
+ * Formats a hotkey as a label, without the scope's platform or the user's
4
+ * keyboard layout.
5
+ *
6
+ * @deprecated Use `useFormatForDisplay()` in components. It returns this
7
+ * function with the scope's platform and the user's keyboard layout filled in.
8
+ * Outside a component, keep calling this function and pass `platform` yourself.
9
+ */
10
+ declare const formatForDisplay$1: typeof formatForDisplay;
11
+ export { formatForDisplay$1 as formatForDisplay };
@@ -0,0 +1,14 @@
1
+ import { formatForDisplay } from "@tanstack/hotkeys";
2
+
3
+ // src/formatForDisplay.ts
4
+ /**
5
+ * Formats a hotkey as a label, without the scope's platform or the user's
6
+ * keyboard layout.
7
+ *
8
+ * @deprecated Use `useFormatForDisplay()` in components. It returns this
9
+ * function with the scope's platform and the user's keyboard layout filled in.
10
+ * Outside a component, keep calling this function and pass `platform` yourself.
11
+ */
12
+ const formatForDisplay$1 = formatForDisplay;
13
+
14
+ export { formatForDisplay$1 as formatForDisplay };
package/dist/index.d.ts CHANGED
@@ -10,9 +10,11 @@ import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
10
10
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
11
11
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
12
12
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
13
+ import { formatForDisplay } from "./formatForDisplay.js";
14
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
13
15
  import { DefaultHotkeysOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget, HotkeyRegistrationsResult } from "./types.js";
14
16
  import { EmberHotkeysScope, createHotkeysScope } from "./createHotkeysScope.js";
15
17
  import { OnHotkey } from "./onHotkey.js";
16
18
  import { ElementHotkeyOptions, OnHotkeys } from "./onHotkeys.js";
17
19
  export * from "@tanstack/hotkeys";
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 };
20
+ export { DefaultHotkeysOptions, type ElementHotkeyOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget, EmberHotkeysScope, HotkeyRegistrationsResult, type MaybeGetter, createHotkeysScope, formatForDisplay, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold, useKeyboardLayout };
package/dist/index.js CHANGED
@@ -9,10 +9,12 @@ import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
9
9
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
10
10
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
11
11
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
12
+ import { formatForDisplay } from "./formatForDisplay.js";
13
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
12
14
  import { createHotkeysScope } from "./createHotkeysScope.js";
13
15
  import { OnHotkey } from "./onHotkey.js";
14
16
  import { OnHotkeys } from "./onHotkeys.js";
15
17
 
16
18
  export * from "@tanstack/hotkeys"
17
19
 
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 };
20
+ export { createHotkeysScope, formatForDisplay, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold, useKeyboardLayout };
@@ -0,0 +1,64 @@
1
+ import { scopeDefaults } from "./utils.js";
2
+ import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
3
+ import { schedule, scheduleOnce } from "@ember/runloop";
4
+ import { trackedObject } from "@ember/reactive/collections";
5
+
6
+ // src/keyboardLayout.ts
7
+ /** Whether a scope's defaults let labels follow the keyboard layout. */
8
+ function layoutEnabled(scope) {
9
+ return !scope || scopeDefaults.get(scope)().keyboardLayout !== false;
10
+ }
11
+ /**
12
+ * Loads the layout and returns a function that ends the caller's interest.
13
+ * FastBoot defines a `window` that has no events, so this checks `document`.
14
+ */
15
+ function startLayout() {
16
+ return typeof document === "undefined" ? () => {} : loadKeyboardLayout();
17
+ }
18
+ function readLayout() {
19
+ schedule("destroy", startLayout());
20
+ }
21
+ /**
22
+ * Reads the layout once after the current render, unless a caller with an
23
+ * owner already keeps it loaded. Many requests in one render read it once.
24
+ */
25
+ function requestLayout() {
26
+ scheduleOnce("afterRender", readLayout);
27
+ }
28
+ let changes;
29
+ /**
30
+ * Reads the layout store and tracks its changes. One subscription serves every
31
+ * reader for as long as the store lives, so readers need no owner.
32
+ */
33
+ function trackedLayout() {
34
+ const store = getKeyboardLayoutStore();
35
+ if (!changes) {
36
+ const state = changes = trackedObject({ version: 0 });
37
+ store.subscribe(() => {
38
+ state.version++;
39
+ });
40
+ }
41
+ changes.version;
42
+ return store.state;
43
+ }
44
+ /**
45
+ * Wraps a tracked layout getter so it returns `null` until the current render
46
+ * has finished. Server HTML never has a layout, so a rehydrating app must
47
+ * first render without one, even when the layout has already loaded.
48
+ */
49
+ function untilRendered(read, onRendered) {
50
+ let rendered = false;
51
+ const state = trackedObject({ version: 0 });
52
+ schedule("afterRender", () => {
53
+ rendered = true;
54
+ onRendered?.();
55
+ if (getKeyboardLayoutStore().state) state.version++;
56
+ });
57
+ return () => {
58
+ state.version;
59
+ const layoutMap = read();
60
+ return rendered ? layoutMap : null;
61
+ };
62
+ }
63
+
64
+ export { layoutEnabled, requestLayout, startLayout, trackedLayout, untilRendered };
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeyRegistrationView, HotkeySequence, HotkeySequenceRecorderOptions, RegisteredAppHotkeys, Target } from "@tanstack/hotkeys";
1
+ import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeyRegistrationView, HotkeySequence, HotkeySequenceRecorderOptions, RecordedKeyPresses, RegisteredAppHotkeys, Target } from "@tanstack/hotkeys";
2
2
  import { AdapterCommonOptions, AdapterHotkeyDefinition } from "@tanstack/hotkeys/adapter";
3
3
  /** A reactive value. Read `.value` where the framework tracks dependencies. */
4
4
  export interface EmberHotkeyState<T> {
@@ -43,15 +43,20 @@ export interface EmberCustomHotkeys {
43
43
  readonly value: CustomHotkeys;
44
44
  /**
45
45
  * Sets the user's hotkey for an app hotkey. `null` unbinds it.
46
- * Returns conflicting app hotkeys; when any exist, nothing changes.
46
+ * Returns conflicting app hotkeys; when any exist, nothing changes. Pass
47
+ * the recorder's `context` for a recorded hotkey, as for `findConflicts`.
47
48
  */
48
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
49
+ setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
49
50
  /** Restores the default hotkey for an app hotkey. */
50
51
  resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
51
52
  /** Restores every default hotkey. */
52
53
  resetCustomHotkeys: () => void;
53
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
54
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
54
+ /**
55
+ * Other app hotkeys that would collide if `id` used `hotkey`. Pass the
56
+ * recorder's `context` for a recorded hotkey: it has the key press, which
57
+ * also finds a hotkey written differently that the same press fires.
58
+ */
59
+ findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
55
60
  }
56
61
  /** Shared defaults; call-specific options take precedence. */
57
62
  export interface DefaultHotkeysOptions {
@@ -62,6 +67,14 @@ export interface DefaultHotkeysOptions {
62
67
  * `detectPlatform(userAgent)`.
63
68
  */
64
69
  platform?: HotkeyPlatform;
70
+ /**
71
+ * Whether labels from `useFormatForDisplay` follow the user's keyboard
72
+ * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
73
+ * loads from the browser when the first label renders. Only Chromium
74
+ * browsers report one. Set `false` to never ask the browser for it.
75
+ * Default: `true`.
76
+ */
77
+ keyboardLayout?: boolean;
65
78
  /** Defaults for every hotkey. `requireReset` applies to chords and `timeout` to sequences. */
66
79
  hotkey?: EmberHotkeyCommonOptions;
67
80
  /** Defaults for every recorder. */
@@ -37,10 +37,10 @@ function useCustomHotkeys(owner) {
37
37
  get value() {
38
38
  return requireStore(store).customHotkeys;
39
39
  },
40
- setCustomHotkey: (id, hotkey) => requireStore(store).setCustomHotkey(id, hotkey),
40
+ setCustomHotkey: (id, hotkey, recordedWith) => requireStore(store).setCustomHotkey(id, hotkey, recordedWith),
41
41
  resetCustomHotkey: (id) => requireStore(store).resetCustomHotkey(id),
42
42
  resetCustomHotkeys: () => requireStore(store).resetCustomHotkeys(),
43
- findConflicts: (id, hotkey) => requireStore(store).findConflicts(id, hotkey)
43
+ findConflicts: (id, hotkey, recordedWith) => requireStore(store).findConflicts(id, hotkey, recordedWith)
44
44
  };
45
45
  }
46
46
 
@@ -6,6 +6,16 @@ import { formatForDisplay } from "@tanstack/hotkeys";
6
6
  * including templates, and reads the scope's defaults when called, so
7
7
  * templates update when they change. An explicit `platform` option wins.
8
8
  *
9
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
10
+ * types for this user in Chromium browsers, once the keyboard layout has
11
+ * loaded. The returned function reads the layout from the browser after the
12
+ * render in which it first formats a label. It has no owner, so it doesn't read
13
+ * the layout again when the window regains focus: see `useKeyboardLayout` for
14
+ * that. A `layoutMap` passed to the returned function wins. To never ask the
15
+ * browser for the layout, set `keyboardLayout: false` in the scope's default
16
+ * options. Firefox and Safari report no layout, so labels there keep the key's
17
+ * US character.
18
+ *
9
19
  * Server-rendered apps should set the scope's default `platform` from the
10
20
  * request user agent, such as
11
21
  * `detectPlatform(fastboot.request.headers.get('user-agent'))` with the
@@ -14,9 +24,9 @@ import { formatForDisplay } from "@tanstack/hotkeys";
14
24
  * @example
15
25
  * ```gts
16
26
  * class SaveButton extends Component<{ Args: { hotkeys: EmberHotkeysScope } }> {
17
- * format = useFormatForDisplay(this.args.hotkeys)
27
+ * formatForDisplay = useFormatForDisplay(this.args.hotkeys)
18
28
  *
19
- * <template><kbd>{{this.format "Mod+S"}}</kbd></template>
29
+ * <template><kbd>{{this.formatForDisplay "Mod+S"}}</kbd></template>
20
30
  * }
21
31
  * ```
22
32
  */
@@ -1,5 +1,7 @@
1
+ import { layoutEnabled, requestLayout, trackedLayout, untilRendered } from "./keyboardLayout.js";
1
2
  import { scopeDefaults } from "./utils.js";
2
3
  import { formatForDisplay } from "@tanstack/hotkeys";
4
+ import { namesPhysicalKey } from "@tanstack/hotkeys/adapter";
3
5
 
4
6
  // src/useFormatForDisplay.ts
5
7
  /**
@@ -8,6 +10,16 @@ import { formatForDisplay } from "@tanstack/hotkeys";
8
10
  * including templates, and reads the scope's defaults when called, so
9
11
  * templates update when they change. An explicit `platform` option wins.
10
12
  *
13
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
14
+ * types for this user in Chromium browsers, once the keyboard layout has
15
+ * loaded. The returned function reads the layout from the browser after the
16
+ * render in which it first formats a label. It has no owner, so it doesn't read
17
+ * the layout again when the window regains focus: see `useKeyboardLayout` for
18
+ * that. A `layoutMap` passed to the returned function wins. To never ask the
19
+ * browser for the layout, set `keyboardLayout: false` in the scope's default
20
+ * options. Firefox and Safari report no layout, so labels there keep the key's
21
+ * US character.
22
+ *
11
23
  * Server-rendered apps should set the scope's default `platform` from the
12
24
  * request user agent, such as
13
25
  * `detectPlatform(fastboot.request.headers.get('user-agent'))` with the
@@ -16,16 +28,31 @@ import { formatForDisplay } from "@tanstack/hotkeys";
16
28
  * @example
17
29
  * ```gts
18
30
  * class SaveButton extends Component<{ Args: { hotkeys: EmberHotkeysScope } }> {
19
- * format = useFormatForDisplay(this.args.hotkeys)
31
+ * formatForDisplay = useFormatForDisplay(this.args.hotkeys)
20
32
  *
21
- * <template><kbd>{{this.format "Mod+S"}}</kbd></template>
33
+ * <template><kbd>{{this.formatForDisplay "Mod+S"}}</kbd></template>
22
34
  * }
23
35
  * ```
24
36
  */
25
37
  function useFormatForDisplay(scope) {
38
+ const layoutMap = untilRendered(trackedLayout);
39
+ let requested = false;
40
+ /** The loaded layout, unless the scope turns it off. */
41
+ const loadedLayout = () => {
42
+ if (!layoutEnabled(scope)) {
43
+ requested = false;
44
+ return;
45
+ }
46
+ if (!requested) {
47
+ requested = true;
48
+ requestLayout();
49
+ }
50
+ return layoutMap() ?? void 0;
51
+ };
26
52
  return ((hotkey, options) => formatForDisplay(hotkey, {
27
53
  ...options,
28
- platform: options?.platform ?? (scope && scopeDefaults.get(scope)().platform)
54
+ platform: options?.platform ?? (scope && scopeDefaults.get(scope)().platform),
55
+ layoutMap: options?.layoutMap ?? (namesPhysicalKey(hotkey) ? loadedLayout() : void 0)
29
56
  }));
30
57
  }
31
58
 
@@ -0,0 +1,34 @@
1
+ import { EmberHotkeysScope } from "./createHotkeysScope.js";
2
+ import { EmberHotkeyState } from "./types.js";
3
+ import { KeyboardLayoutMap } from "@tanstack/hotkeys";
4
+ /**
5
+ * The browser's keyboard layout, or `null` until it loads and in browsers
6
+ * that don't report one. The layout maps each physical code to the character
7
+ * it types, such as `KeyQ` to `a` on a French AZERTY keyboard. Only Chromium
8
+ * browsers report one. Pass the containing component as owner.
9
+ *
10
+ * The layout loads after the render that created the utility, and is read
11
+ * again when the window regains focus, until the owner is destroyed.
12
+ * `useFormatForDisplay` already labels physical bindings with the layout, so
13
+ * you only need this utility for labels you draw yourself, or to keep the
14
+ * layout current while its owner lives.
15
+ *
16
+ * `value` is `null` in FastBoot and until the render that created the utility
17
+ * has finished, so server-rendered labels rehydrate unchanged.
18
+ *
19
+ * To never ask the browser for the layout, pass a scope whose default options
20
+ * set `keyboardLayout: false`. `value` is then `null`.
21
+ *
22
+ * @example
23
+ * ```gts
24
+ * class MoveKeys extends Component {
25
+ * layout = useKeyboardLayout(this)
26
+ *
27
+ * label = (code: string) =>
28
+ * (this.layout.value?.get(code) ?? code).toUpperCase()
29
+ *
30
+ * <template><kbd>{{this.label "KeyW"}}</kbd></template>
31
+ * }
32
+ * ```
33
+ */
34
+ export declare function useKeyboardLayout(owner: object, scope?: EmberHotkeysScope): EmberHotkeyState<KeyboardLayoutMap | null>;
@@ -0,0 +1,60 @@
1
+ import { layoutEnabled, startLayout, untilRendered } from "./keyboardLayout.js";
2
+ import { select } from "./utils.js";
3
+ import { getKeyboardLayoutStore } from "@tanstack/hotkeys";
4
+ import { isDestroying, registerDestructor } from "@ember/destroyable";
5
+
6
+ // src/useKeyboardLayout.ts
7
+ /**
8
+ * The browser's keyboard layout, or `null` until it loads and in browsers
9
+ * that don't report one. The layout maps each physical code to the character
10
+ * it types, such as `KeyQ` to `a` on a French AZERTY keyboard. Only Chromium
11
+ * browsers report one. Pass the containing component as owner.
12
+ *
13
+ * The layout loads after the render that created the utility, and is read
14
+ * again when the window regains focus, until the owner is destroyed.
15
+ * `useFormatForDisplay` already labels physical bindings with the layout, so
16
+ * you only need this utility for labels you draw yourself, or to keep the
17
+ * layout current while its owner lives.
18
+ *
19
+ * `value` is `null` in FastBoot and until the render that created the utility
20
+ * has finished, so server-rendered labels rehydrate unchanged.
21
+ *
22
+ * To never ask the browser for the layout, pass a scope whose default options
23
+ * set `keyboardLayout: false`. `value` is then `null`.
24
+ *
25
+ * @example
26
+ * ```gts
27
+ * class MoveKeys extends Component {
28
+ * layout = useKeyboardLayout(this)
29
+ *
30
+ * label = (code: string) =>
31
+ * (this.layout.value?.get(code) ?? code).toUpperCase()
32
+ *
33
+ * <template><kbd>{{this.label "KeyW"}}</kbd></template>
34
+ * }
35
+ * ```
36
+ */
37
+ function useKeyboardLayout(owner, scope) {
38
+ const layout = select(owner, getKeyboardLayoutStore(), (layoutMap) => layoutMap);
39
+ let rendered = false;
40
+ let stop;
41
+ /** Loads the layout while the scope allows it, and stops when it doesn't. */
42
+ const sync = () => {
43
+ if (layoutEnabled(scope) && !isDestroying(owner)) stop ??= startLayout();
44
+ else {
45
+ stop?.();
46
+ stop = void 0;
47
+ }
48
+ };
49
+ const read = untilRendered(() => layout.value, () => {
50
+ rendered = true;
51
+ sync();
52
+ });
53
+ registerDestructor(owner, () => stop?.());
54
+ return { get value() {
55
+ if (rendered) sync();
56
+ return layoutEnabled(scope) ? read() : null;
57
+ } };
58
+ }
59
+
60
+ export { useKeyboardLayout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ember-hotkeys",
3
- "version": "0.11.1",
3
+ "version": "0.13.0",
4
4
  "description": "Ember adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -40,11 +40,11 @@
40
40
  ],
41
41
  "dependencies": {
42
42
  "@embroider/addon-shim": "^1.11.0",
43
- "@tanstack/hotkeys": "0.27.1",
43
+ "@tanstack/hotkeys": "0.29.0",
44
44
  "ember-modifier": "^4.2.2"
45
45
  },
46
46
  "devDependencies": {
47
- "@babel/core": "7.29.7",
47
+ "@babel/core": "7.29.13",
48
48
  "@babel/plugin-transform-typescript": "7.29.9",
49
49
  "@ember/app-tsconfig": "2.1.0",
50
50
  "@ember/test-helpers": "^5.8.0",
@@ -66,7 +66,7 @@
66
66
  "qunit": "^3.0.0",
67
67
  "qunit-dom": "^3.6.0",
68
68
  "testem": "^3.21.5",
69
- "vite": "^8.3.6"
69
+ "vite": "^8.3.7"
70
70
  },
71
71
  "peerDependencies": {
72
72
  "ember-source": ">=6.8.3 <6.9.0 || >=6.10.1"
@@ -55,28 +55,28 @@ export default class Editor extends Component {
55
55
 
56
56
  ## API
57
57
 
58
- | API | What it's for |
59
- | --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
60
- | `{{useHotkey hotkey callback ...options}}` | Register one chord, sequence, or app hotkey while the template renders the helper. |
61
- | `{{useHotkeys definitions}}` | Register several hotkeys, or a list built from data, in one helper. |
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
- | `{{onHotkeys definitions}}` | Attach several hotkeys to one element. |
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. |
66
- | `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys before the app boots, since Ember has no provider. |
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. |
68
- | `useAppHotkeys(this)` | Get the declared app hotkeys with the user's custom hotkeys applied. |
69
- | `useAppHotkeyList(this)` | List every app hotkey with its status, for a cheat sheet or settings screen. |
70
- | `useCustomHotkeys(this)` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
71
- | `useHotkeyRecorder(this, options, scope?)` | Record a chord or sequence the user presses, for a settings screen. Pass a `createHotkeysScope` result to apply its `hotkeyRecorder` defaults. |
72
- | `useHotkeyRegistrations(this)` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
73
- | `useHotkeyScopesState(this)` | Get the active scopes and the exclusive scope that owns the keyboard. |
74
- | `useHotkeyHint(this, hotkey, options?)` | Get 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. |
75
- | `useKeyHold(this, key)` | Get whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
76
- | `useHeldKeys(this)` | Get every key that's held right now. |
77
- | `useHeldKeyCodes(this)` | Map each held key to its physical `event.code`. |
58
+ | API | What it's for |
59
+ | --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
60
+ | `{{useHotkey hotkey callback ...options}}` | Register one chord, sequence, or app hotkey while the template renders the helper. |
61
+ | `{{useHotkeys definitions}}` | Register several hotkeys, or a list built from data, in one helper. |
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
+ | `{{onHotkeys definitions}}` | Attach several hotkeys to one element. |
64
+ | `createHotkeysScope(defaults?)` | Create `useHotkey`, `useHotkeys`, `onHotkey`, and `onHotkeys` that share default options. |
65
+ | `useFormatForDisplay(scope?)` | Get the function that formats a hotkey as a label, with the scope's `platform` 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 a component: `formatForDisplay = useFormatForDisplay()` as a class field, then `{{this.formatForDisplay hotkey}}`. |
66
+ | `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys before the app boots, since Ember has no provider. |
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. |
68
+ | `useAppHotkeys(this)` | Get the declared app hotkeys with the user's custom hotkeys applied. |
69
+ | `useAppHotkeyList(this)` | List every app hotkey with its status, for a cheat sheet or settings screen. |
70
+ | `useCustomHotkeys(this)` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
71
+ | `useHotkeyRecorder(this, options, scope?)` | Record a chord or sequence the user presses, for a settings screen. Pass a `createHotkeysScope` result to apply its `hotkeyRecorder` defaults. |
72
+ | `useHotkeyRegistrations(this)` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
73
+ | `useHotkeyScopesState(this)` | Get the active scopes and the exclusive scope that owns the keyboard. |
74
+ | `useHotkeyHint(this, hotkey, options?)` | Get 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. |
75
+ | `useKeyHold(this, key)` | Get whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
76
+ | `useHeldKeys(this)` | Get every key that's held right now. |
77
+ | `useHeldKeyCodes(this)` | Map each held key to its physical `event.code`. |
78
78
 
79
- The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
79
+ The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `useFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`.
80
80
 
81
81
  The helpers are template helpers, not JavaScript functions. State readers take the component as their owner, and their state is read through `.value`, or `.hotkeys` for registrations. Use Ember 6.8.3 or newer, except 6.9.0 and 6.10.0, and strict-mode `.gts` components.
82
82
 
@@ -87,7 +87,7 @@ The helpers are template helpers, not JavaScript functions. State readers take t
87
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.
88
88
  - **Read app hotkeys through getters.** Template paths can't index ids that contain dots.
89
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, when no scope sets `platform`.
90
+ - **Server-rendered labels need the user's platform.** The server can't see the user's OS, so without a platform 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 pass the scope to the utility: `formatForDisplay = useFormatForDisplay(this.hotkeys)`.
91
91
 
92
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).
93
93
 
@@ -37,25 +37,26 @@ A settings row records with `useHotkeyRecorder` and saves through `useCustomHotk
37
37
  ```gts
38
38
  import Component from '@glimmer/component'
39
39
  import { on } from '@ember/modifier'
40
- import { formatForDisplay, useAppHotkeys, useCustomHotkeys, useHotkeyRecorder } from '@tanstack/ember-hotkeys'
40
+ import { useAppHotkeys, useCustomHotkeys, useFormatForDisplay, useHotkeyRecorder } from '@tanstack/ember-hotkeys'
41
41
  import type { AppHotkeyId } from '@tanstack/ember-hotkeys'
42
42
 
43
43
  export default class ShortcutRow extends Component<{ Args: { id: AppHotkeyId } }> {
44
+ formatForDisplay = useFormatForDisplay()
44
45
  appHotkeys = useAppHotkeys(this)
45
46
  custom = useCustomHotkeys(this)
46
47
  recorder = useHotkeyRecorder(this, () => ({
47
48
  maxSteps: Infinity,
48
- validate: (hotkey) => {
49
- const [conflict] = this.custom.findConflicts(this.args.id, hotkey)
49
+ validate: (hotkey, context) => {
50
+ const [conflict] = this.custom.findConflicts(this.args.id, hotkey, context)
50
51
  return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
51
52
  },
52
- onRecord: (hotkey) => this.custom.setCustomHotkey(this.args.id, hotkey),
53
+ onRecord: (hotkey, context) => this.custom.setCustomHotkey(this.args.id, hotkey, context),
53
54
  onClear: () => this.custom.setCustomHotkey(this.args.id, null),
54
55
  }))
55
56
 
56
57
  get label() {
57
58
  const { meta, hotkey } = this.appHotkeys.value[this.args.id]
58
- return `${meta?.name}: ${hotkey ? formatForDisplay(hotkey) : 'Unassigned'}`
59
+ return `${meta?.name}: ${hotkey ? this.formatForDisplay(hotkey) : 'Unassigned'}`
59
60
  }
60
61
  reset = () => this.custom.resetCustomHotkey(this.args.id)
61
62