@tanstack/alpine-hotkeys 0.12.1 → 0.14.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.
@@ -1,10 +1,9 @@
1
1
  import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
2
+ import { bindCustomHotkeyActions, requireAppHotkeysStore } from "@tanstack/hotkeys/adapter";
2
3
 
3
4
  // src/createAppHotkeys.ts
4
5
  function requireStore(scope) {
5
- const store = scope.appHotkeys();
6
- if (!store) throw new Error("[hotkeys] App hotkeys require provideAppHotkeys()");
7
- return store;
6
+ return requireAppHotkeysStore(scope.appHotkeys(), "App hotkeys require provideAppHotkeys()");
8
7
  }
9
8
  function createAppHotkeys(scope) {
10
9
  return { get value() {
@@ -22,10 +21,7 @@ function createCustomHotkeys(scope) {
22
21
  get value() {
23
22
  return requireStore(scope).customHotkeys;
24
23
  },
25
- setCustomHotkey: (id, hotkey) => requireStore(scope).setCustomHotkey(id, hotkey),
26
- resetCustomHotkey: (id) => requireStore(scope).resetCustomHotkey(id),
27
- resetCustomHotkeys: () => requireStore(scope).resetCustomHotkeys(),
28
- findConflicts: (id, hotkey) => requireStore(scope).findConflicts(id, hotkey)
24
+ ...bindCustomHotkeyActions(() => requireStore(scope))
29
25
  };
30
26
  }
31
27
 
@@ -0,0 +1,10 @@
1
+ import { createKeyboardLayout } from "./createKeyboardLayout.js";
2
+ import { createFormatForDisplay } from "@tanstack/hotkeys/adapter";
3
+
4
+ // src/createFormatForDisplay.ts
5
+ function createFormatForDisplay$1(scope) {
6
+ const layout = createKeyboardLayout(scope);
7
+ return createFormatForDisplay(() => scope.defaultOptions().platform, () => layout.value);
8
+ }
9
+
10
+ export { createFormatForDisplay$1 as createFormatForDisplay };
@@ -1,12 +1,12 @@
1
1
  import { createHotkeys } from "./createHotkeys.js";
2
- import { read } from "./scope.js";
2
+ import { resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
3
3
 
4
4
  // src/createHotkey.ts
5
5
  function createHotkey(scope, hotkey, callback, options = {}) {
6
6
  createHotkeys(scope, () => [{
7
- hotkey: read(hotkey),
7
+ hotkey: resolveMaybeGetter(hotkey),
8
8
  callback,
9
- options: read(options)
9
+ options: resolveMaybeGetter(options)
10
10
  }]);
11
11
  }
12
12
 
@@ -1,14 +1,15 @@
1
- import { read } from "./scope.js";
2
- import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
1
+ import { getHotkeyManager, getKeyStateTracker, isAppHotkey } from "@tanstack/hotkeys";
2
+ import { isHotkeyHintVisible, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
3
3
 
4
4
  // src/createHotkeyHint.ts
5
5
  function createHotkeyHint(scope, hotkey, options = {}) {
6
6
  const manager = getHotkeyManager();
7
7
  const registrations = scope.select(manager.registrations, (state) => state);
8
8
  return scope.select(getKeyStateTracker().store, (state) => {
9
- const value = read(hotkey);
10
- const resolved = isAppHotkey(value) ? resolveAppHotkey(value, scope.appHotkeys()).hotkey : value;
11
- return resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, read(options)) && registrations.value.size > 0 && manager.hasActiveRegistration(value);
9
+ const value = resolveMaybeGetter(hotkey);
10
+ registrations.value;
11
+ const appHotkeysStore = isAppHotkey(value) ? scope.appHotkeys() : null;
12
+ return isHotkeyHintVisible(value, state.heldKeys, resolveMaybeGetter(options), appHotkeysStore);
12
13
  });
13
14
  }
14
15
 
@@ -1,6 +1,6 @@
1
- import { read, scopeOf } from "./scope.js";
1
+ import { scopeOf } from "./scope.js";
2
2
  import { HotkeyRecorder } from "@tanstack/hotkeys";
3
- import { withDefaults } from "@tanstack/hotkeys/adapter";
3
+ import { resolveMaybeGetter, withDefaults } from "@tanstack/hotkeys/adapter";
4
4
 
5
5
  // src/createHotkeyRecorder.ts
6
6
  function createHotkeyRecorder(owner, options) {
@@ -8,7 +8,7 @@ function createHotkeyRecorder(owner, options) {
8
8
  }
9
9
  function createRecorder(scope, options) {
10
10
  scope.assertActive();
11
- const instance = new HotkeyRecorder(() => withDefaults(scope.defaultOptions().hotkeyRecorder, read(options)));
11
+ const instance = new HotkeyRecorder(() => withDefaults(scope.defaultOptions().hotkeyRecorder, resolveMaybeGetter(options)));
12
12
  const state = scope.select(instance.store, (value) => value);
13
13
  scope.addCleanup(() => instance.destroy());
14
14
  return {
@@ -1,8 +1,9 @@
1
- import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
1
+ import { getHotkeyManager } from "@tanstack/hotkeys";
2
+ import { getHotkeyRegistrationViews } from "@tanstack/hotkeys/adapter";
2
3
 
3
4
  // src/createHotkeyRegistrations.ts
4
5
  function createHotkeyRegistrations(scope) {
5
- const state = scope.select(getHotkeyManager().registrations, (value) => Array.from(value.values()).map(toHotkeyRegistrationView));
6
+ const state = scope.select(getHotkeyManager().registrations, (value) => getHotkeyRegistrationViews(value));
6
7
  return { get hotkeys() {
7
8
  return state.value;
8
9
  } };
@@ -1,5 +1,4 @@
1
- import { read } from "./scope.js";
2
- import { createHotkeyBindings, prepareHotkeyDefinitions } from "@tanstack/hotkeys/adapter";
1
+ import { createHotkeyBindings, prepareHotkeyDefinitions, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
3
2
 
4
3
  // src/createHotkeys.ts
5
4
  function createHotkeys(scope, definitions, options = {}) {
@@ -7,10 +6,10 @@ function createHotkeys(scope, definitions, options = {}) {
7
6
  const bindings = createHotkeyBindings();
8
7
  scope.addCleanup(bindings.destroy);
9
8
  try {
10
- scope.effect(() => bindings.update(prepareHotkeyDefinitions(read(definitions), {
9
+ scope.effect(() => bindings.update(prepareHotkeyDefinitions(resolveMaybeGetter(definitions), {
11
10
  appHotkeysStore: scope.appHotkeys(),
12
11
  defaults: scope.defaultOptions().hotkey,
13
- commonOptions: read(options),
12
+ commonOptions: resolveMaybeGetter(options),
14
13
  resolveTarget: (target) => target
15
14
  })));
16
15
  } catch (error) {
@@ -1,8 +1,9 @@
1
- import { AlpineHotkeys, DefaultHotkeysOptions, MaybeGetter } from "./types.js";
1
+ import { AlpineHotkeys, DefaultHotkeysOptions } from "./types.js";
2
+ import { MaybeGetter } from "@tanstack/hotkeys";
2
3
  /**
3
4
  * Owns Alpine effects and Store subscriptions, with optional shared defaults.
4
5
  * App hotkeys come from `provideAppHotkeys`. Call destroy from x-data's
5
6
  * destroy hook.
6
7
  */
7
8
  export declare function createHotkeysScope(defaultOptions?: MaybeGetter<DefaultHotkeysOptions>): AlpineHotkeys;
8
- export type { AlpineHotkeys, DefaultHotkeysOptions, MaybeGetter };
9
+ export type { AlpineHotkeys, DefaultHotkeysOptions };
@@ -1,4 +1,5 @@
1
1
  import { createAppHotkeyList, createAppHotkeys, createCustomHotkeys } from "./createAppHotkeys.js";
2
+ import { createFormatForDisplay } from "./createFormatForDisplay.js";
2
3
  import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
3
4
  import { createHeldKeys } from "./createHeldKeys.js";
4
5
  import { createHotkey } from "./createHotkey.js";
@@ -7,6 +8,7 @@ import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
7
8
  import { createHotkeyScopesState } from "./createHotkeyScopesState.js";
8
9
  import { createHotkeys } from "./createHotkeys.js";
9
10
  import { createKeyHold } from "./createKeyHold.js";
11
+ import { createKeyboardLayout } from "./createKeyboardLayout.js";
10
12
  import { createScope, ownerScopes } from "./scope.js";
11
13
 
12
14
  // src/createHotkeysScope.ts
@@ -26,6 +28,8 @@ function createHotkeysScope(defaultOptions = {}) {
26
28
  createHotkeyHint: (...args) => createHotkeyHint(scope, ...args),
27
29
  createHotkeyRegistrations: (...args) => createHotkeyRegistrations(scope, ...args),
28
30
  createHotkeyScopesState: () => createHotkeyScopesState(scope),
31
+ createFormatForDisplay: () => createFormatForDisplay(scope),
32
+ createKeyboardLayout: () => createKeyboardLayout(scope),
29
33
  createAppHotkeys: () => createAppHotkeys(scope),
30
34
  createAppHotkeyList: () => createAppHotkeyList(scope),
31
35
  createCustomHotkeys: () => createCustomHotkeys(scope),
@@ -1,10 +1,10 @@
1
- import { read } from "./scope.js";
2
1
  import { getKeyStateTracker } from "@tanstack/hotkeys";
2
+ import { resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
3
3
 
4
4
  // src/createKeyHold.ts
5
5
  function createKeyHold(scope, key) {
6
6
  const tracker = getKeyStateTracker();
7
- return scope.select(tracker.store, () => tracker.isKeyHeld(read(key)));
7
+ return scope.select(tracker.store, () => tracker.isKeyHeld(resolveMaybeGetter(key)));
8
8
  }
9
9
 
10
10
  export { createKeyHold };
@@ -0,0 +1,21 @@
1
+ import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
2
+
3
+ // src/createKeyboardLayout.ts
4
+ function createKeyboardLayout(scope) {
5
+ const layout = scope.select(getKeyboardLayoutStore(), (layoutMap) => layoutMap);
6
+ const enabled = () => scope.defaultOptions().keyboardLayout !== false;
7
+ let stop;
8
+ scope.effect(() => {
9
+ if (enabled()) stop ??= loadKeyboardLayout();
10
+ else {
11
+ stop?.();
12
+ stop = void 0;
13
+ }
14
+ });
15
+ scope.addCleanup(() => stop?.());
16
+ return { get value() {
17
+ return enabled() ? layout.value : null;
18
+ } };
19
+ }
20
+
21
+ export { createKeyboardLayout };
@@ -0,0 +1,12 @@
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 `createFormatForDisplay()` on a `createHotkeysScope` owner in
7
+ * components. It returns this function with the scope's platform and the user's
8
+ * keyboard layout filled in. Outside a component, keep calling this function
9
+ * and pass `platform` yourself.
10
+ */
11
+ declare const formatForDisplay$1: typeof formatForDisplay;
12
+ export { formatForDisplay$1 as formatForDisplay };
@@ -0,0 +1,15 @@
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 `createFormatForDisplay()` on a `createHotkeysScope` owner in
9
+ * components. It returns this function with the scope's platform and the user's
10
+ * keyboard layout filled in. Outside a component, keep calling this function
11
+ * and pass `platform` yourself.
12
+ */
13
+ const formatForDisplay$1 = formatForDisplay;
14
+
15
+ export { formatForDisplay$1 as formatForDisplay };
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { createHotkeysScope } from "./createHotkeysScope.js";
2
+ import { formatForDisplay } from "./formatForDisplay.js";
2
3
  import { hotkeysPlugin } from "./hotkeysPlugin.js";
3
4
  import { createHotkeyRecorder } from "./createHotkeyRecorder.js";
4
5
  import { hotkeyRecorderPlugin } from "./hotkeyRecorderPlugin.js";
5
- import { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions, HotkeyRegistrationsResult, MaybeGetter } from "./types.js";
6
+ import { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions } from "./types.js";
6
7
  export * from "@tanstack/hotkeys";
7
- export { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions, HotkeyRegistrationsResult, MaybeGetter, createHotkeyRecorder, createHotkeysScope, hotkeyRecorderPlugin, hotkeysPlugin };
8
+ export { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions, createHotkeyRecorder, createHotkeysScope, formatForDisplay, hotkeyRecorderPlugin, hotkeysPlugin };
package/dist/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  import { createHotkeysScope } from "./createHotkeysScope.js";
2
+ import { formatForDisplay } from "./formatForDisplay.js";
2
3
  import { hotkeysPlugin } from "./hotkeysPlugin.js";
3
4
  import { createHotkeyRecorder } from "./createHotkeyRecorder.js";
4
5
  import { hotkeyRecorderPlugin } from "./hotkeyRecorderPlugin.js";
5
6
 
6
7
  export * from "@tanstack/hotkeys"
7
8
 
8
- export { createHotkeyRecorder, createHotkeysScope, hotkeyRecorderPlugin, hotkeysPlugin };
9
+ export { createHotkeyRecorder, createHotkeysScope, formatForDisplay, hotkeyRecorderPlugin, hotkeysPlugin };
package/dist/scope.js CHANGED
@@ -1,5 +1,4 @@
1
- import { getHotkeyManager } from "@tanstack/hotkeys";
2
- import { subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
1
+ import { getPublishedAppHotkeysStore, mergeHotkeysProviderDefaults, resolveMaybeGetter, subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
3
2
 
4
3
  // src/scope.ts
5
4
  let pluginAlpine;
@@ -21,9 +20,6 @@ const ownerScopes = /* @__PURE__ */ new WeakMap();
21
20
  function scopeOf(owner) {
22
21
  return ownerScopes.get(getAlpine().raw(owner));
23
22
  }
24
- function read(value) {
25
- return typeof value === "function" ? value() : value;
26
- }
27
23
  function createScope(defaultOptions) {
28
24
  const cleanups = /* @__PURE__ */ new Set();
29
25
  let destroyed = false;
@@ -74,14 +70,14 @@ function createScope(defaultOptions) {
74
70
  cleanups.add(subscribePublishedAppHotkeys(() => state.version++));
75
71
  }
76
72
  appHotkeysState.version;
77
- return getHotkeyManager().appHotkeys.state;
73
+ return getPublishedAppHotkeysStore();
78
74
  }
79
75
  return {
80
76
  assertActive,
81
77
  effect,
82
78
  select,
83
79
  appHotkeys,
84
- defaultOptions: () => read(defaultOptions),
80
+ defaultOptions: () => mergeHotkeysProviderDefaults(void 0, resolveMaybeGetter(defaultOptions), void 0),
85
81
  addCleanup: (cleanup) => cleanups.add(cleanup),
86
82
  get destroyed() {
87
83
  return destroyed;
@@ -95,4 +91,4 @@ function createScope(defaultOptions) {
95
91
  };
96
92
  }
97
93
 
98
- export { createScope, ownerScopes, read, scopeOf, setAlpine };
94
+ export { createScope, ownerScopes, scopeOf, setAlpine };
package/dist/types.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, HeldModifierOptions, Hotkey, HotkeyCallback, HotkeyCallbackContextFor, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderOptions, HotkeyRegistrationView, HotkeyScopesState, HotkeySequence, HotkeySequenceRecorderOptions, IndividualKey, RegisterableHotkey, RegisteredAppHotkeys, Target } from "@tanstack/hotkeys";
2
- import { AdapterAppHotkeyOptions, AdapterCommonOptions, AdapterHotkeyDefinition, AdapterOptionsFor } from "@tanstack/hotkeys/adapter";
1
+ import { AppHotkey, CustomHotkeyActions, CustomHotkeys, HeldModifierOptions, Hotkey, HotkeyCallback, HotkeyCallbackContextFor, HotkeyChord, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeyRecorderActions, HotkeyRecorderOptions, HotkeyRecorderState, HotkeyRegistrationsResult, HotkeyScopesState, HotkeySequenceRecorderOptions, IndividualKey, KeyboardLayoutMap, MaybeGetter, RegisterableHotkey, RegisteredAppHotkeyListItem, RegisteredAppHotkeys, Target, formatForDisplay } from "@tanstack/hotkeys";
2
+ import { AdapterAppHotkeyOptions, AdapterCommonOptions, AdapterHotkeyDefinition, AdapterOptionsFor, HotkeyRecorderDefaults } from "@tanstack/hotkeys/adapter";
3
3
  /** A reactive value. Read `.value` where the framework tracks dependencies. */
4
4
  export interface AlpineHotkeyState<T> {
5
5
  readonly value: T;
@@ -18,45 +18,12 @@ export type AlpineHotkeyOptionsFor<T extends RegisterableHotkey> = AdapterOption
18
18
  /** Call-site options for an app hotkey, typed by its declared default kind. */
19
19
  export type AlpineAppHotkeyOptions<T extends AppHotkey = AppHotkey> = AdapterAppHotkeyOptions<T, AlpineHotkeyTarget>;
20
20
  /** Recording state and controls owned by the containing scope. */
21
- export interface AlpineHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
22
- /** Whether the recorder is listening for a shortcut. */
23
- readonly isRecording: boolean;
24
- /** Chords captured so far in a multi-step recording. */
25
- readonly steps: HotkeySequence;
26
- /** The last committed hotkey, including brackets for physical codes. */
27
- readonly recordedHotkey: THotkey | null;
28
- /** Start a new recording session. */
29
- startRecording: () => void;
30
- /** Stop and clear recorder state without calling onRecord or onCancel. */
31
- stopRecording: () => void;
32
- /** Stop, clear recorder state, and call onCancel. */
33
- cancelRecording: () => void;
34
- /** Commit the current steps (multi-step recordings). */
35
- commitRecording: () => void;
36
- }
37
- /** Live registration views from the shared hotkey manager. */
38
- export interface HotkeyRegistrationsResult {
39
- /** Every registration, chords and sequences. Narrow each view with `kind`. */
40
- readonly hotkeys: Array<HotkeyRegistrationView>;
41
- }
21
+ export interface AlpineHotkeyRecorder<THotkey extends Hotkey = Hotkey> extends Readonly<HotkeyRecorderState<THotkey>>, HotkeyRecorderActions {}
42
22
  /** Custom hotkey state and actions. */
43
- export interface AlpineCustomHotkeys {
23
+ export interface AlpineCustomHotkeys extends CustomHotkeyActions {
44
24
  /** The user's hotkeys that differ from the defaults. */
45
25
  readonly value: CustomHotkeys;
46
- /**
47
- * Sets the user's hotkey for an app hotkey. `null` unbinds it.
48
- * Returns conflicting app hotkeys; when any exist, nothing changes.
49
- */
50
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
51
- /** Restores the default hotkey for an app hotkey. */
52
- resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
53
- /** Restores every default hotkey. */
54
- resetCustomHotkeys: () => void;
55
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
56
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
57
26
  }
58
- /** A value or a getter evaluated within an Alpine effect or reactive read. */
59
- export type MaybeGetter<T> = T | (() => T);
60
27
  /** Registers a hotkey: a chord, a sequence, or an app hotkey. */
61
28
  export interface AlpineCreateHotkey {
62
29
  <T extends AppHotkey>(hotkey: MaybeGetter<T | null | undefined>, callback: HotkeyCallback, options?: MaybeGetter<AlpineAppHotkeyOptions<T>>): void;
@@ -95,10 +62,75 @@ export interface AlpineHotkeys {
95
62
  createHotkeyRegistrations: () => HotkeyRegistrationsResult;
96
63
  /** Read the active, exclusive, allowed, and suspended scopes through `.value`. */
97
64
  createHotkeyScopesState: () => AlpineHotkeyState<HotkeyScopesState>;
65
+ /**
66
+ * Returns `formatForDisplay` with the scope's `platform` as the default, or
67
+ * the detected platform without one. A `platform` passed to the returned
68
+ * function wins.
69
+ *
70
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
71
+ * types for this user in Chromium browsers, once the keyboard layout has
72
+ * loaded. A getter or an `x-text` expression that calls the function runs
73
+ * again when the layout arrives. See `createKeyboardLayout`. A `layoutMap`
74
+ * passed to the returned function wins. To never ask the browser for the
75
+ * layout, set `keyboardLayout: false` in the scope's default options. Firefox
76
+ * and Safari report no layout, so labels there keep the key's US character.
77
+ *
78
+ * Create it once, then call the returned function anywhere.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * Alpine.data('saveButton', () => {
83
+ * const hotkeys = createHotkeysScope()
84
+ * const formatForDisplay = hotkeys.createFormatForDisplay()
85
+ *
86
+ * return {
87
+ * get label() {
88
+ * return formatForDisplay('Mod+S')
89
+ * },
90
+ * destroy() {
91
+ * hotkeys.destroy()
92
+ * },
93
+ * }
94
+ * })
95
+ * ```
96
+ */
97
+ createFormatForDisplay: () => typeof formatForDisplay;
98
+ /**
99
+ * Read the browser's keyboard layout through `.value`, or `null` until it
100
+ * loads and in browsers that don't report one. The layout maps each physical
101
+ * code to the character it types, such as `KeyQ` to `a` on a French AZERTY
102
+ * keyboard. Only Chromium browsers report one.
103
+ *
104
+ * Creating it is what loads the layout. The layout is read again when the
105
+ * window regains focus, until the scope is destroyed.
106
+ * `createFormatForDisplay` uses it, so you only need it for labels you draw
107
+ * yourself.
108
+ *
109
+ * To never ask the browser for the layout, set `keyboardLayout: false` in
110
+ * the scope's default options. `.value` is then `null`.
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * Alpine.data('moveKeys', () => {
115
+ * const hotkeys = createHotkeysScope()
116
+ * const layout = hotkeys.createKeyboardLayout()
117
+ *
118
+ * return {
119
+ * label(code: string) {
120
+ * return (layout.value?.get(code) ?? code).toUpperCase()
121
+ * },
122
+ * destroy() {
123
+ * hotkeys.destroy()
124
+ * },
125
+ * }
126
+ * })
127
+ * ```
128
+ */
129
+ createKeyboardLayout: () => AlpineHotkeyState<KeyboardLayoutMap | null>;
98
130
  /** Read the effective app hotkeys from `provideAppHotkeys` through `.value`. */
99
131
  createAppHotkeys: () => AlpineHotkeyState<RegisteredAppHotkeys>;
100
132
  /** Read every app hotkey with its live status through `.value`. */
101
- createAppHotkeyList: () => AlpineHotkeyState<Array<AppHotkeyListItem<RegisteredAppHotkeys[keyof RegisteredAppHotkeys]>>>;
133
+ createAppHotkeyList: () => AlpineHotkeyState<Array<RegisteredAppHotkeyListItem>>;
102
134
  /** Read and change the user's custom hotkeys. */
103
135
  createCustomHotkeys: () => AlpineCustomHotkeys;
104
136
  /** Release every registration, subscription, effect, and recorder. Idempotent. */
@@ -106,8 +138,22 @@ export interface AlpineHotkeys {
106
138
  }
107
139
  /** Shared defaults; call-specific options take precedence. */
108
140
  export interface DefaultHotkeysOptions {
141
+ /**
142
+ * The user's platform, for labels from `createFormatForDisplay`, hotkeys,
143
+ * and recorders. `hotkey.platform` and `hotkeyRecorder.platform` win.
144
+ * Detected when unset.
145
+ */
146
+ platform?: HotkeyPlatform;
147
+ /**
148
+ * Whether labels from `createFormatForDisplay` follow the user's keyboard
149
+ * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
150
+ * loads from the browser when the first `createFormatForDisplay` or
151
+ * `createKeyboardLayout` runs. Only Chromium browsers report one. Set
152
+ * `false` to never ask the browser for it. Default: `true`.
153
+ */
154
+ keyboardLayout?: boolean;
109
155
  /** Defaults for every hotkey. `requireReset` applies to chords and `timeout` to sequences. */
110
156
  hotkey?: AlpineHotkeyCommonOptions;
111
157
  /** Defaults for every recorder. */
112
- hotkeyRecorder?: Partial<Omit<HotkeyChordRecorderOptions & HotkeySequenceRecorderOptions, 'maxSteps' | 'onRecord' | 'validate'>>;
158
+ hotkeyRecorder?: HotkeyRecorderDefaults;
113
159
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/alpine-hotkeys",
3
- "version": "0.12.1",
3
+ "version": "0.14.1",
4
4
  "description": "Alpine 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.27.1"
38
+ "@tanstack/hotkeys": "0.30.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@tanstack/hotkeys-test-utils": "0.0.0",
@@ -75,22 +75,23 @@ Install `Alpine.plugin(hotkeysPlugin)` before `Alpine.start()`. It gives the ada
75
75
 
76
76
  Each owner has these methods. Read their state through `.value`, or `.hotkeys` for registrations.
77
77
 
78
- | Method | What it's for |
79
- | -------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
80
- | `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey until the owner is destroyed; options can be a getter. |
81
- | `createHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a getter of a list built from data, in one call. |
82
- | `createAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied. |
83
- | `createAppHotkeyList()` | List every app hotkey with its status, for a cheat sheet or settings screen. |
84
- | `createCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
85
- | `createHotkeyRegistrations()` | List every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
86
- | `createHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
87
- | `createHotkeyHint(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. |
88
- | `createKeyHold(key)` | Get whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
89
- | `createHeldKeys()` | Get every key that's held right now. |
90
- | `createHeldKeyCodes()` | Map each held key to its physical `event.code`. |
91
- | `destroy()` | Release every hotkey, subscription, and recorder the owner created. |
92
-
93
- The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
78
+ | Method | What it's for |
79
+ | -------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
80
+ | `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey until the owner is destroyed; options can be a getter. |
81
+ | `createHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a getter of a list built from data, in one call. |
82
+ | `createFormatForDisplay()` | Get the function that formats a hotkey as a label, with the owner's default `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: `const formatForDisplay = hotkeys.createFormatForDisplay()` once, then `formatForDisplay(hotkey)` in a getter or an `x-text` expression. |
83
+ | `createAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied. |
84
+ | `createAppHotkeyList()` | List every app hotkey with its status, for a cheat sheet or settings screen. |
85
+ | `createCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
86
+ | `createHotkeyRegistrations()` | List every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
87
+ | `createHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
88
+ | `createHotkeyHint(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. |
89
+ | `createKeyHold(key)` | Get whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
90
+ | `createHeldKeys()` | Get every key that's held right now. |
91
+ | `createHeldKeyCodes()` | Map each held key to its physical `event.code`. |
92
+ | `destroy()` | Release every hotkey, subscription, and recorder the owner created. |
93
+
94
+ The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use the owner's `createFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`.
94
95
 
95
96
  ## Pitfalls
96
97
 
@@ -38,22 +38,22 @@ A settings row records with `createHotkeyRecorder` on its owner:
38
38
  import {
39
39
  createHotkeyRecorder,
40
40
  createHotkeysScope,
41
- formatForDisplay,
42
41
  } from '@tanstack/alpine-hotkeys'
43
42
  import Alpine from 'alpinejs'
44
43
  import type { AppHotkeyId } from '@tanstack/alpine-hotkeys'
45
44
 
46
45
  Alpine.data('shortcutRow', (id: AppHotkeyId) => {
47
46
  const hotkeys = createHotkeysScope()
47
+ const formatForDisplay = hotkeys.createFormatForDisplay()
48
48
  const appHotkeys = hotkeys.createAppHotkeys()
49
49
  const custom = hotkeys.createCustomHotkeys()
50
50
  const recorder = createHotkeyRecorder(hotkeys, {
51
51
  maxSteps: Infinity,
52
- validate: (hotkey) => {
53
- const [conflict] = custom.findConflicts(id, hotkey)
52
+ validate: (hotkey, context) => {
53
+ const [conflict] = custom.findConflicts(id, hotkey, context)
54
54
  return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
55
55
  },
56
- onRecord: (hotkey) => custom.setCustomHotkey(id, hotkey),
56
+ onRecord: (hotkey, context) => custom.setCustomHotkey(id, hotkey, context),
57
57
  onClear: () => custom.setCustomHotkey(id, null),
58
58
  })
59
59