@tanstack/alpine-hotkeys 0.12.0 → 0.14.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.
@@ -22,10 +22,10 @@ function createCustomHotkeys(scope) {
22
22
  get value() {
23
23
  return requireStore(scope).customHotkeys;
24
24
  },
25
- setCustomHotkey: (id, hotkey) => requireStore(scope).setCustomHotkey(id, hotkey),
25
+ setCustomHotkey: (id, hotkey, recordedWith) => requireStore(scope).setCustomHotkey(id, hotkey, recordedWith),
26
26
  resetCustomHotkey: (id) => requireStore(scope).resetCustomHotkey(id),
27
27
  resetCustomHotkeys: () => requireStore(scope).resetCustomHotkeys(),
28
- findConflicts: (id, hotkey) => requireStore(scope).findConflicts(id, hotkey)
28
+ findConflicts: (id, hotkey, recordedWith) => requireStore(scope).findConflicts(id, hotkey, recordedWith)
29
29
  };
30
30
  }
31
31
 
@@ -0,0 +1,15 @@
1
+ import { createKeyboardLayout } from "./createKeyboardLayout.js";
2
+ import { formatForDisplay } from "@tanstack/hotkeys";
3
+ import { namesPhysicalKey } from "@tanstack/hotkeys/adapter";
4
+
5
+ // src/createFormatForDisplay.ts
6
+ function createFormatForDisplay(scope) {
7
+ const layout = createKeyboardLayout(scope);
8
+ return ((hotkey, options) => formatForDisplay(hotkey, {
9
+ ...options,
10
+ platform: options?.platform ?? scope.defaultOptions().platform,
11
+ layoutMap: options?.layoutMap ?? (namesPhysicalKey(hotkey) ? layout.value ?? void 0 : void 0)
12
+ }));
13
+ }
14
+
15
+ export { createFormatForDisplay };
@@ -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),
@@ -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
6
  import { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions, HotkeyRegistrationsResult, MaybeGetter } 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, HotkeyRegistrationsResult, MaybeGetter, 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,5 @@
1
1
  import { getHotkeyManager } from "@tanstack/hotkeys";
2
- import { subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
2
+ import { mergeHotkeysProviderDefaults, subscribePublishedAppHotkeys } from "@tanstack/hotkeys/adapter";
3
3
 
4
4
  // src/scope.ts
5
5
  let pluginAlpine;
@@ -81,7 +81,7 @@ function createScope(defaultOptions) {
81
81
  effect,
82
82
  select,
83
83
  appHotkeys,
84
- defaultOptions: () => read(defaultOptions),
84
+ defaultOptions: () => mergeHotkeysProviderDefaults(void 0, read(defaultOptions), void 0),
85
85
  addCleanup: (cleanup) => cleanups.add(cleanup),
86
86
  get destroyed() {
87
87
  return destroyed;
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, HeldModifierOptions, Hotkey, HotkeyCallback, HotkeyCallbackContextFor, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderOptions, HotkeyRegistrationView, HotkeyScopesState, HotkeySequence, HotkeySequenceRecorderOptions, IndividualKey, RegisterableHotkey, RegisteredAppHotkeys, Target } from "@tanstack/hotkeys";
1
+ import { AppHotkey, AppHotkeyListItem, CustomHotkeys, HeldModifierOptions, Hotkey, HotkeyCallback, HotkeyCallbackContextFor, HotkeyChord, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeyRecorderOptions, HotkeyRegistrationView, HotkeyScopesState, HotkeySequence, HotkeySequenceRecorderOptions, IndividualKey, KeyboardLayoutMap, RecordedKeyPresses, RegisterableHotkey, RegisteredAppHotkeys, Target, formatForDisplay } from "@tanstack/hotkeys";
2
2
  import { AdapterAppHotkeyOptions, AdapterCommonOptions, AdapterHotkeyDefinition, AdapterOptionsFor } from "@tanstack/hotkeys/adapter";
3
3
  /** A reactive value. Read `.value` where the framework tracks dependencies. */
4
4
  export interface AlpineHotkeyState<T> {
@@ -45,15 +45,20 @@ export interface AlpineCustomHotkeys {
45
45
  readonly value: CustomHotkeys;
46
46
  /**
47
47
  * Sets the user's hotkey for an app hotkey. `null` unbinds it.
48
- * Returns conflicting app hotkeys; when any exist, nothing changes.
48
+ * Returns conflicting app hotkeys; when any exist, nothing changes. Pass
49
+ * the recorder's `context` for a recorded hotkey, as for `findConflicts`.
49
50
  */
50
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
51
+ setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
51
52
  /** Restores the default hotkey for an app hotkey. */
52
53
  resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
53
54
  /** Restores every default hotkey. */
54
55
  resetCustomHotkeys: () => void;
55
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
56
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
56
+ /**
57
+ * Other app hotkeys that would collide if `id` used `hotkey`. Pass the
58
+ * recorder's `context` for a recorded hotkey: it has the key press, which
59
+ * also finds a hotkey written differently that the same press fires.
60
+ */
61
+ findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
57
62
  }
58
63
  /** A value or a getter evaluated within an Alpine effect or reactive read. */
59
64
  export type MaybeGetter<T> = T | (() => T);
@@ -95,6 +100,71 @@ export interface AlpineHotkeys {
95
100
  createHotkeyRegistrations: () => HotkeyRegistrationsResult;
96
101
  /** Read the active, exclusive, allowed, and suspended scopes through `.value`. */
97
102
  createHotkeyScopesState: () => AlpineHotkeyState<HotkeyScopesState>;
103
+ /**
104
+ * Returns `formatForDisplay` with the scope's `platform` as the default, or
105
+ * the detected platform without one. A `platform` passed to the returned
106
+ * function wins.
107
+ *
108
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
109
+ * types for this user in Chromium browsers, once the keyboard layout has
110
+ * loaded. A getter or an `x-text` expression that calls the function runs
111
+ * again when the layout arrives. See `createKeyboardLayout`. A `layoutMap`
112
+ * passed to the returned function wins. To never ask the browser for the
113
+ * layout, set `keyboardLayout: false` in the scope's default options. Firefox
114
+ * and Safari report no layout, so labels there keep the key's US character.
115
+ *
116
+ * Create it once, then call the returned function anywhere.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * Alpine.data('saveButton', () => {
121
+ * const hotkeys = createHotkeysScope()
122
+ * const formatForDisplay = hotkeys.createFormatForDisplay()
123
+ *
124
+ * return {
125
+ * get label() {
126
+ * return formatForDisplay('Mod+S')
127
+ * },
128
+ * destroy() {
129
+ * hotkeys.destroy()
130
+ * },
131
+ * }
132
+ * })
133
+ * ```
134
+ */
135
+ createFormatForDisplay: () => typeof formatForDisplay;
136
+ /**
137
+ * Read the browser's keyboard layout through `.value`, or `null` until it
138
+ * loads and in browsers that don't report one. The layout maps each physical
139
+ * code to the character it types, such as `KeyQ` to `a` on a French AZERTY
140
+ * keyboard. Only Chromium browsers report one.
141
+ *
142
+ * Creating it is what loads the layout. The layout is read again when the
143
+ * window regains focus, until the scope is destroyed.
144
+ * `createFormatForDisplay` uses it, so you only need it for labels you draw
145
+ * yourself.
146
+ *
147
+ * To never ask the browser for the layout, set `keyboardLayout: false` in
148
+ * the scope's default options. `.value` is then `null`.
149
+ *
150
+ * @example
151
+ * ```ts
152
+ * Alpine.data('moveKeys', () => {
153
+ * const hotkeys = createHotkeysScope()
154
+ * const layout = hotkeys.createKeyboardLayout()
155
+ *
156
+ * return {
157
+ * label(code: string) {
158
+ * return (layout.value?.get(code) ?? code).toUpperCase()
159
+ * },
160
+ * destroy() {
161
+ * hotkeys.destroy()
162
+ * },
163
+ * }
164
+ * })
165
+ * ```
166
+ */
167
+ createKeyboardLayout: () => AlpineHotkeyState<KeyboardLayoutMap | null>;
98
168
  /** Read the effective app hotkeys from `provideAppHotkeys` through `.value`. */
99
169
  createAppHotkeys: () => AlpineHotkeyState<RegisteredAppHotkeys>;
100
170
  /** Read every app hotkey with its live status through `.value`. */
@@ -106,6 +176,20 @@ export interface AlpineHotkeys {
106
176
  }
107
177
  /** Shared defaults; call-specific options take precedence. */
108
178
  export interface DefaultHotkeysOptions {
179
+ /**
180
+ * The user's platform, for labels from `createFormatForDisplay`, hotkeys,
181
+ * and recorders. `hotkey.platform` and `hotkeyRecorder.platform` win.
182
+ * Detected when unset.
183
+ */
184
+ platform?: HotkeyPlatform;
185
+ /**
186
+ * Whether labels from `createFormatForDisplay` follow the user's keyboard
187
+ * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
188
+ * loads from the browser when the first `createFormatForDisplay` or
189
+ * `createKeyboardLayout` runs. Only Chromium browsers report one. Set
190
+ * `false` to never ask the browser for it. Default: `true`.
191
+ */
192
+ keyboardLayout?: boolean;
109
193
  /** Defaults for every hotkey. `requireReset` applies to chords and `timeout` to sequences. */
110
194
  hotkey?: AlpineHotkeyCommonOptions;
111
195
  /** Defaults for every recorder. */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/alpine-hotkeys",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
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.0"
38
+ "@tanstack/hotkeys": "0.29.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