@tanstack/ember-hotkeys 0.11.1 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/bindings.js CHANGED
@@ -1,7 +1,7 @@
1
- import { getHotkeyManager, isAppHotkey, isHotkeySequence } from "@tanstack/hotkeys";
1
+ import { isAppHotkey, isHotkeySequence } from "@tanstack/hotkeys";
2
2
  import { isDestroyed, isDestroying, registerDestructor } from "@ember/destroyable";
3
3
  import { scheduleOnce } from "@ember/runloop";
4
- import { createHotkeyBindings, prepareHotkeyDefinitions, subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
4
+ import { createHotkeyBindings, getPublishedAppHotkeysStore, prepareHotkeyDefinitions, subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
5
5
 
6
6
  // src/bindings.ts
7
7
  /** Copies a hotkey so tracked getters are read while Ember tracks the caller. */
@@ -27,7 +27,7 @@ function createOwnedBindings(owner) {
27
27
  if (isDestroying(owner) || isDestroyed(owner)) return;
28
28
  const { definitions, commonOptions, element } = state;
29
29
  bindings.update(prepareHotkeyDefinitions(definitions, {
30
- appHotkeysStore: getHotkeyManager().appHotkeys.state,
30
+ appHotkeysStore: getPublishedAppHotkeysStore(),
31
31
  commonOptions: element ? {
32
32
  ...commonOptions,
33
33
  target: element
@@ -3,11 +3,12 @@ import { OnHotkeys } from "./onHotkeys.js";
3
3
  import { UseHotkey } from "./useHotkey.js";
4
4
  import { UseHotkeys } from "./useHotkeys.js";
5
5
  import { DefaultHotkeysOptions } from "./types.js";
6
- import { MaybeGetter } from "./utils.js";
6
+ import { MaybeGetter } from "@tanstack/hotkeys";
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;
@@ -2,8 +2,8 @@ import { OnHotkey } from "./onHotkey.js";
2
2
  import { OnHotkeys } from "./onHotkeys.js";
3
3
  import { UseHotkey } from "./useHotkey.js";
4
4
  import { UseHotkeys } from "./useHotkeys.js";
5
- import { read, scopeDefaults } from "./utils.js";
6
- import { definedOptions, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
5
+ import { scopeDefaults } from "./utils.js";
6
+ import { definedOptions, mergeHotkeysProviderDefaults, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
7
7
 
8
8
  // src/createHotkeysScope.ts
9
9
  /**
@@ -11,7 +11,7 @@ import { definedOptions, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/
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
+ const defaults = () => mergeHotkeysProviderDefaults(void 0, resolveMaybeGetter(defaultOptions), void 0);
15
15
  const scope = {
16
16
  onHotkey: class extends OnHotkey {
17
17
  modify(...[element, args, options]) {
@@ -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
@@ -1,6 +1,5 @@
1
1
  import { UseHotkey } from "./useHotkey.js";
2
2
  import { UseHotkeys } from "./useHotkeys.js";
3
- import { MaybeGetter } from "./utils.js";
4
3
  import { useHeldKeys } from "./useHeldKeys.js";
5
4
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
6
5
  import { useKeyHold } from "./useKeyHold.js";
@@ -10,9 +9,11 @@ import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
10
9
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
11
10
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
12
11
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
13
- import { DefaultHotkeysOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget, HotkeyRegistrationsResult } from "./types.js";
12
+ import { formatForDisplay } from "./formatForDisplay.js";
13
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
14
+ import { DefaultHotkeysOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget } from "./types.js";
14
15
  import { EmberHotkeysScope, createHotkeysScope } from "./createHotkeysScope.js";
15
16
  import { OnHotkey } from "./onHotkey.js";
16
17
  import { ElementHotkeyOptions, OnHotkeys } from "./onHotkeys.js";
17
18
  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 };
19
+ export { DefaultHotkeysOptions, type ElementHotkeyOptions, EmberAppHotkeyList, EmberCustomHotkeys, EmberHotkeyCommonOptions, EmberHotkeyDefinition, EmberHotkeyRecorder, EmberHotkeyState, EmberHotkeyTarget, EmberHotkeysScope, 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,5 +1,5 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeyRegistrationView, HotkeySequence, HotkeySequenceRecorderOptions, RegisteredAppHotkeys, Target } from "@tanstack/hotkeys";
2
- import { AdapterCommonOptions, AdapterHotkeyDefinition } from "@tanstack/hotkeys/adapter";
1
+ import { CustomHotkeyActions, CustomHotkeys, Hotkey, HotkeyPlatform, HotkeyRecorderActions, HotkeyRecorderState, RegisteredAppHotkeyListItem, Target } from "@tanstack/hotkeys";
2
+ import { AdapterCommonOptions, AdapterHotkeyDefinition, HotkeyRecorderDefaults } from "@tanstack/hotkeys/adapter";
3
3
  /** A reactive value. Read `.value` where the framework tracks dependencies. */
4
4
  export interface EmberHotkeyState<T> {
5
5
  readonly value: T;
@@ -14,44 +14,13 @@ export type EmberHotkeyDefinition = AdapterHotkeyDefinition<EmberHotkeyTarget>;
14
14
  /** Options for every kind; each applies to the kinds that support it. */
15
15
  export type EmberHotkeyCommonOptions = AdapterCommonOptions<EmberHotkeyTarget>;
16
16
  /** Recording state and controls owned by the containing component. */
17
- export interface EmberHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
18
- /** Whether the recorder is listening for a shortcut. */
19
- readonly isRecording: boolean;
20
- /** Chords captured so far in a multi-step recording. */
21
- readonly steps: HotkeySequence;
22
- /** The last committed hotkey, including brackets for physical codes. */
23
- readonly recordedHotkey: THotkey | null;
24
- /** Start a new recording session. */
25
- startRecording: () => void;
26
- /** Stop and clear recorder state without calling onRecord or onCancel. */
27
- stopRecording: () => void;
28
- /** Stop, clear recorder state, and call onCancel. */
29
- cancelRecording: () => void;
30
- /** Commit the current steps (multi-step recordings). */
31
- commitRecording: () => void;
32
- }
33
- /** Live registration views from the shared hotkey manager. */
34
- export interface HotkeyRegistrationsResult {
35
- /** Every registration, chords and sequences. Narrow each view with `kind`. */
36
- readonly hotkeys: Array<HotkeyRegistrationView>;
37
- }
17
+ export interface EmberHotkeyRecorder<THotkey extends Hotkey = Hotkey> extends Readonly<HotkeyRecorderState<THotkey>>, HotkeyRecorderActions {}
38
18
  /** Every app hotkey with its live status. */
39
- export type EmberAppHotkeyList = EmberHotkeyState<Array<AppHotkeyListItem<RegisteredAppHotkeys[keyof RegisteredAppHotkeys]>>>;
19
+ export type EmberAppHotkeyList = EmberHotkeyState<Array<RegisteredAppHotkeyListItem>>;
40
20
  /** Custom hotkey state and actions. */
41
- export interface EmberCustomHotkeys {
21
+ export interface EmberCustomHotkeys extends CustomHotkeyActions {
42
22
  /** The user's hotkeys that differ from the defaults. */
43
23
  readonly value: CustomHotkeys;
44
- /**
45
- * Sets the user's hotkey for an app hotkey. `null` unbinds it.
46
- * Returns conflicting app hotkeys; when any exist, nothing changes.
47
- */
48
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
49
- /** Restores the default hotkey for an app hotkey. */
50
- resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
51
- /** Restores every default hotkey. */
52
- resetCustomHotkeys: () => void;
53
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
54
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
55
24
  }
56
25
  /** Shared defaults; call-specific options take precedence. */
57
26
  export interface DefaultHotkeysOptions {
@@ -62,8 +31,16 @@ export interface DefaultHotkeysOptions {
62
31
  * `detectPlatform(userAgent)`.
63
32
  */
64
33
  platform?: HotkeyPlatform;
34
+ /**
35
+ * Whether labels from `useFormatForDisplay` follow the user's keyboard
36
+ * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
37
+ * loads from the browser when the first label renders. Only Chromium
38
+ * browsers report one. Set `false` to never ask the browser for it.
39
+ * Default: `true`.
40
+ */
41
+ keyboardLayout?: boolean;
65
42
  /** Defaults for every hotkey. `requireReset` applies to chords and `timeout` to sequences. */
66
43
  hotkey?: EmberHotkeyCommonOptions;
67
44
  /** Defaults for every recorder. */
68
- hotkeyRecorder?: Partial<Omit<HotkeyChordRecorderOptions & HotkeySequenceRecorderOptions, 'maxSteps' | 'onRecord' | 'validate'>>;
45
+ hotkeyRecorder?: HotkeyRecorderDefaults;
69
46
  }
@@ -1,11 +1,10 @@
1
1
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
2
2
  import { trackPublishedAppHotkeys } from "./utils.js";
3
+ import { bindCustomHotkeyActions, requireAppHotkeysStore } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/useAppHotkeys.ts
5
6
  function requireStore(store) {
6
- const current = store();
7
- if (!current) throw new Error("[hotkeys] App hotkeys require provideAppHotkeys()");
8
- return current;
7
+ return requireAppHotkeysStore(store(), "App hotkeys require provideAppHotkeys()");
9
8
  }
10
9
  /** Reads the effective app hotkeys from `provideAppHotkeys`, typed by `Register`. */
11
10
  function useAppHotkeys(owner) {
@@ -37,10 +36,7 @@ function useCustomHotkeys(owner) {
37
36
  get value() {
38
37
  return requireStore(store).customHotkeys;
39
38
  },
40
- setCustomHotkey: (id, hotkey) => requireStore(store).setCustomHotkey(id, hotkey),
41
- resetCustomHotkey: (id) => requireStore(store).resetCustomHotkey(id),
42
- resetCustomHotkeys: () => requireStore(store).resetCustomHotkeys(),
43
- findConflicts: (id, hotkey) => requireStore(store).findConflicts(id, hotkey)
39
+ ...bindCustomHotkeyActions(() => requireStore(store))
44
40
  };
45
41
  }
46
42
 
@@ -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,6 @@
1
+ import { layoutEnabled, requestLayout, trackedLayout, untilRendered } from "./keyboardLayout.js";
1
2
  import { scopeDefaults } from "./utils.js";
2
- import { formatForDisplay } from "@tanstack/hotkeys";
3
+ import { createFormatForDisplay } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/useFormatForDisplay.ts
5
6
  /**
@@ -8,6 +9,16 @@ import { formatForDisplay } from "@tanstack/hotkeys";
8
9
  * including templates, and reads the scope's defaults when called, so
9
10
  * templates update when they change. An explicit `platform` option wins.
10
11
  *
12
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
13
+ * types for this user in Chromium browsers, once the keyboard layout has
14
+ * loaded. The returned function reads the layout from the browser after the
15
+ * render in which it first formats a label. It has no owner, so it doesn't read
16
+ * the layout again when the window regains focus: see `useKeyboardLayout` for
17
+ * that. A `layoutMap` passed to the returned function wins. To never ask the
18
+ * browser for the layout, set `keyboardLayout: false` in the scope's default
19
+ * options. Firefox and Safari report no layout, so labels there keep the key's
20
+ * US character.
21
+ *
11
22
  * Server-rendered apps should set the scope's default `platform` from the
12
23
  * request user agent, such as
13
24
  * `detectPlatform(fastboot.request.headers.get('user-agent'))` with the
@@ -16,17 +27,28 @@ import { formatForDisplay } from "@tanstack/hotkeys";
16
27
  * @example
17
28
  * ```gts
18
29
  * class SaveButton extends Component<{ Args: { hotkeys: EmberHotkeysScope } }> {
19
- * format = useFormatForDisplay(this.args.hotkeys)
30
+ * formatForDisplay = useFormatForDisplay(this.args.hotkeys)
20
31
  *
21
- * <template><kbd>{{this.format "Mod+S"}}</kbd></template>
32
+ * <template><kbd>{{this.formatForDisplay "Mod+S"}}</kbd></template>
22
33
  * }
23
34
  * ```
24
35
  */
25
36
  function useFormatForDisplay(scope) {
26
- return ((hotkey, options) => formatForDisplay(hotkey, {
27
- ...options,
28
- platform: options?.platform ?? (scope && scopeDefaults.get(scope)().platform)
29
- }));
37
+ const layoutMap = untilRendered(trackedLayout);
38
+ let requested = false;
39
+ /** The loaded layout, unless the scope turns it off. */
40
+ const loadedLayout = () => {
41
+ if (!layoutEnabled(scope)) {
42
+ requested = false;
43
+ return;
44
+ }
45
+ if (!requested) {
46
+ requested = true;
47
+ requestLayout();
48
+ }
49
+ return layoutMap() ?? void 0;
50
+ };
51
+ return createFormatForDisplay(() => scope && scopeDefaults.get(scope)().platform, loadedLayout);
30
52
  }
31
53
 
32
54
  export { useFormatForDisplay };
@@ -1,6 +1,5 @@
1
1
  import { EmberHotkeyState } from "./types.js";
2
- import { MaybeGetter } from "./utils.js";
3
- import { AppHotkey, HeldModifierOptions, RegisterableHotkey } from "@tanstack/hotkeys";
2
+ import { AppHotkey, HeldModifierOptions, MaybeGetter, RegisterableHotkey } from "@tanstack/hotkeys";
4
3
  /**
5
4
  * Recomputes for held modifiers and tracked changes to the hotkey or options.
6
5
  * The hint shows only while the hotkey can fire: some registration of it is
@@ -1,5 +1,6 @@
1
- import { read, select, trackPublishedAppHotkeys } from "./utils.js";
2
- import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
1
+ import { select, trackPublishedAppHotkeys } from "./utils.js";
2
+ import { getHotkeyManager, getKeyStateTracker, isAppHotkey } from "@tanstack/hotkeys";
3
+ import { isHotkeyHintVisible, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/useHotkeyHint.ts
5
6
  /**
@@ -13,12 +14,11 @@ import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers
13
14
  function useHotkeyHint(owner, hotkey, options = {}) {
14
15
  const appHotkeys = trackPublishedAppHotkeys(owner);
15
16
  const manager = getHotkeyManager();
16
- const active = select(owner, manager.registrations, () => manager.hasActiveRegistration(read(hotkey)));
17
+ const active = select(owner, manager.registrations, () => manager.hasActiveRegistration(resolveMaybeGetter(hotkey)));
17
18
  return select(owner, getKeyStateTracker().store, (state) => {
18
- const isActive = active.value;
19
- const value = read(hotkey);
20
- const resolved = isAppHotkey(value) ? resolveAppHotkey(value, appHotkeys()).hotkey : value;
21
- return resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, read(options)) && isActive;
19
+ active.value;
20
+ const value = resolveMaybeGetter(hotkey);
21
+ return isHotkeyHintVisible(value, state.heldKeys, resolveMaybeGetter(options), isAppHotkey(value) ? appHotkeys() : null);
22
22
  });
23
23
  }
24
24
 
@@ -1,11 +1,11 @@
1
- import { read, scopeDefaults, select } from "./utils.js";
1
+ import { scopeDefaults, select } from "./utils.js";
2
2
  import { HotkeyRecorder } from "@tanstack/hotkeys";
3
3
  import { isDestroyed, isDestroying, registerDestructor } from "@ember/destroyable";
4
- import { withDefaults } from "@tanstack/hotkeys/adapter";
4
+ import { resolveMaybeGetter, withDefaults } from "@tanstack/hotkeys/adapter";
5
5
 
6
6
  // src/useHotkeyRecorder.ts
7
7
  function useHotkeyRecorder(owner, options, scope) {
8
- const recorder = new HotkeyRecorder(() => withDefaults(scope && scopeDefaults.get(scope)().hotkeyRecorder, read(options)));
8
+ const recorder = new HotkeyRecorder(() => withDefaults(scope && scopeDefaults.get(scope)().hotkeyRecorder, resolveMaybeGetter(options)));
9
9
  const state = select(owner, recorder.store, (value) => value);
10
10
  registerDestructor(owner, () => recorder.destroy());
11
11
  return {
@@ -1,3 +1,3 @@
1
- import { HotkeyRegistrationsResult } from "./types.js";
1
+ import { HotkeyRegistrationsResult } from "@tanstack/hotkeys";
2
2
  /** Reads public snapshots of every registration, chords and sequences. */
3
3
  export declare function useHotkeyRegistrations(owner: object): HotkeyRegistrationsResult;
@@ -1,10 +1,11 @@
1
1
  import { select } from "./utils.js";
2
- import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
2
+ import { getHotkeyManager } from "@tanstack/hotkeys";
3
+ import { getHotkeyRegistrationViews } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/useHotkeyRegistrations.ts
5
6
  /** Reads public snapshots of every registration, chords and sequences. */
6
7
  function useHotkeyRegistrations(owner) {
7
- const hotkeys = select(owner, getHotkeyManager().registrations, (state) => Array.from(state.values()).map(toHotkeyRegistrationView));
8
+ const hotkeys = select(owner, getHotkeyManager().registrations, (state) => getHotkeyRegistrationViews(state));
8
9
  return { get hotkeys() {
9
10
  return hotkeys.value;
10
11
  } };
@@ -1,5 +1,4 @@
1
1
  import { EmberHotkeyState } from "./types.js";
2
- import { MaybeGetter } from "./utils.js";
3
- import { IndividualKey } from "@tanstack/hotkeys";
2
+ import { IndividualKey, MaybeGetter } from "@tanstack/hotkeys";
4
3
  /** Getter arguments can read changing tracked component properties. */
5
4
  export declare function useKeyHold(owner: object, key: MaybeGetter<IndividualKey>): EmberHotkeyState<boolean>;
@@ -1,11 +1,12 @@
1
- import { read, select } from "./utils.js";
1
+ import { select } from "./utils.js";
2
2
  import { getKeyStateTracker } from "@tanstack/hotkeys";
3
+ import { resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/useKeyHold.ts
5
6
  /** Getter arguments can read changing tracked component properties. */
6
7
  function useKeyHold(owner, key) {
7
8
  const tracker = getKeyStateTracker();
8
- return select(owner, tracker.store, () => tracker.isKeyHeld(read(key)));
9
+ return select(owner, tracker.store, () => tracker.isKeyHeld(resolveMaybeGetter(key)));
9
10
  }
10
11
 
11
12
  export { useKeyHold };
@@ -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/dist/utils.js CHANGED
@@ -1,14 +1,10 @@
1
- import { getHotkeyManager } from "@tanstack/hotkeys";
2
1
  import { registerDestructor } from "@ember/destroyable";
3
- import { subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
2
+ import { getPublishedAppHotkeysStore, subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
4
3
  import { trackedObject } from "@ember/reactive/collections";
5
4
 
6
5
  // src/utils.ts
7
6
  /** Reads the merged defaults behind each `createHotkeysScope` result. */
8
7
  const scopeDefaults = /* @__PURE__ */ new WeakMap();
9
- function read(value) {
10
- return typeof value === "function" ? value() : value;
11
- }
12
8
  function select(owner, store, selector) {
13
9
  const state = trackedObject({ version: 0 });
14
10
  let last;
@@ -36,8 +32,8 @@ function trackPublishedAppHotkeys(owner) {
36
32
  }));
37
33
  return () => {
38
34
  state.version;
39
- return getHotkeyManager().appHotkeys.state;
35
+ return getPublishedAppHotkeysStore();
40
36
  };
41
37
  }
42
38
 
43
- export { read, scopeDefaults, select, trackPublishedAppHotkeys };
39
+ export { scopeDefaults, select, trackPublishedAppHotkeys };
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.1",
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.30.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
 
package/dist/utils.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import "./types.js";
2
- import "@tanstack/hotkeys";
3
- import "@tanstack/hotkeys/adapter";
4
- export type MaybeGetter<T> = T | (() => T);