@tanstack/alpine-hotkeys 0.15.6 → 0.17.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.
- package/dist/createFormatForAria.js +13 -0
- package/dist/createHotkeysScope.js +4 -0
- package/dist/createKeyLockState.js +10 -0
- package/dist/formatForAria.d.ts +12 -0
- package/dist/formatForAria.js +15 -0
- package/dist/hotkeyDirective.d.ts +9 -0
- package/dist/hotkeyDirective.js +39 -0
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/types.d.ts +75 -1
- package/package.json +4 -2
- package/skills/alpine-hotkeys/SKILL.md +8 -1
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
|
|
2
|
+
import { createKeyboardLayout } from "./createKeyboardLayout.js";
|
|
3
|
+
import { formatForAria } from "@tanstack/hotkeys";
|
|
4
|
+
import { announceLiveHotkeys, createFormatter } from "@tanstack/hotkeys/adapter";
|
|
5
|
+
|
|
6
|
+
// src/createFormatForAria.ts
|
|
7
|
+
function createFormatForAria(scope) {
|
|
8
|
+
const layout = createKeyboardLayout(scope);
|
|
9
|
+
const registrations = createHotkeyRegistrations(scope);
|
|
10
|
+
return announceLiveHotkeys(createFormatter(formatForAria, () => scope.defaultOptions().platform, () => layout.value), () => registrations.hotkeys);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { createFormatForAria };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createAppHotkeyList, createAppHotkeys, createCustomHotkeys } from "./createAppHotkeys.js";
|
|
2
|
+
import { createFormatForAria } from "./createFormatForAria.js";
|
|
2
3
|
import { createFormatForDisplay } from "./createFormatForDisplay.js";
|
|
3
4
|
import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
|
|
4
5
|
import { createHeldKeys } from "./createHeldKeys.js";
|
|
@@ -8,6 +9,7 @@ import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
|
|
|
8
9
|
import { createHotkeyScopesState } from "./createHotkeyScopesState.js";
|
|
9
10
|
import { createHotkeys } from "./createHotkeys.js";
|
|
10
11
|
import { createKeyHold } from "./createKeyHold.js";
|
|
12
|
+
import { createKeyLockState } from "./createKeyLockState.js";
|
|
11
13
|
import { createKeyboardLayout } from "./createKeyboardLayout.js";
|
|
12
14
|
import { createScope, ownerScopes } from "./scope.js";
|
|
13
15
|
|
|
@@ -25,10 +27,12 @@ function createHotkeysScope(defaultOptions = {}) {
|
|
|
25
27
|
createHeldKeys: (...args) => createHeldKeys(scope, ...args),
|
|
26
28
|
createHeldKeyCodes: (...args) => createHeldKeyCodes(scope, ...args),
|
|
27
29
|
createKeyHold: (...args) => createKeyHold(scope, ...args),
|
|
30
|
+
createKeyLockState: (...args) => createKeyLockState(scope, ...args),
|
|
28
31
|
createHotkeyHint: (...args) => createHotkeyHint(scope, ...args),
|
|
29
32
|
createHotkeyRegistrations: (...args) => createHotkeyRegistrations(scope, ...args),
|
|
30
33
|
createHotkeyScopesState: () => createHotkeyScopesState(scope),
|
|
31
34
|
createFormatForDisplay: () => createFormatForDisplay(scope),
|
|
35
|
+
createFormatForAria: () => createFormatForAria(scope),
|
|
32
36
|
createKeyboardLayout: () => createKeyboardLayout(scope),
|
|
33
37
|
createAppHotkeys: () => createAppHotkeys(scope),
|
|
34
38
|
createAppHotkeyList: () => createAppHotkeyList(scope),
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { getKeyStateTracker } from "@tanstack/hotkeys";
|
|
2
|
+
import { resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
|
|
3
|
+
|
|
4
|
+
// src/createKeyLockState.ts
|
|
5
|
+
function createKeyLockState(scope, key) {
|
|
6
|
+
const tracker = getKeyStateTracker();
|
|
7
|
+
return scope.select(tracker.store, (state) => state.locks[resolveMaybeGetter(key)]);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { createKeyLockState };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { formatForAria } from "@tanstack/hotkeys";
|
|
2
|
+
/**
|
|
3
|
+
* Formats a hotkey for `aria-keyshortcuts`, without the scope's platform or the
|
|
4
|
+
* user's keyboard layout.
|
|
5
|
+
*
|
|
6
|
+
* @deprecated Use `createFormatForAria()` 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 formatForAria$1: typeof formatForAria;
|
|
12
|
+
export { formatForAria$1 as formatForAria };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { formatForAria } from "@tanstack/hotkeys";
|
|
2
|
+
|
|
3
|
+
// src/formatForAria.ts
|
|
4
|
+
/**
|
|
5
|
+
* Formats a hotkey for `aria-keyshortcuts`, without the scope's platform or the
|
|
6
|
+
* user's keyboard layout.
|
|
7
|
+
*
|
|
8
|
+
* @deprecated Use `createFormatForAria()` 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 formatForAria$1 = formatForAria;
|
|
14
|
+
|
|
15
|
+
export { formatForAria$1 as formatForAria };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import "@tanstack/hotkeys";
|
|
2
|
+
import Alpine from "alpinejs";
|
|
3
|
+
/**
|
|
4
|
+
* Installs `x-hotkey`, which registers the hotkey named by its modifiers while
|
|
5
|
+
* its element exists: `x-hotkey.mod.s="save()"`, `x-hotkey.escape="close()"`,
|
|
6
|
+
* `x-hotkey.g.h="goHome()"`. The expression gets `$event`. Alpine removes the
|
|
7
|
+
* hotkey with its element, and when a morph changes or removes the attribute.
|
|
8
|
+
*/
|
|
9
|
+
export declare function hotkeyDirective(alpine: typeof Alpine): void;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { MODIFIER_ALIASES, getHotkeyManager, normalizeKeyName } from "@tanstack/hotkeys";
|
|
2
|
+
|
|
3
|
+
// src/hotkeyDirective.ts
|
|
4
|
+
/**
|
|
5
|
+
* Reads a hotkey from directive modifiers. HTML lowercases attribute names, so
|
|
6
|
+
* `arrow-up` names `ArrowUp`. Each key ends a step, so `['mod', 'k']` is
|
|
7
|
+
* `Mod+K` and `['g', 'h']` is the sequence `['G', 'H']`.
|
|
8
|
+
*/
|
|
9
|
+
function hotkeyFromModifiers(modifiers) {
|
|
10
|
+
const steps = [];
|
|
11
|
+
let step = "";
|
|
12
|
+
for (const part of modifiers) if (Object.hasOwn(MODIFIER_ALIASES, part)) step += `${MODIFIER_ALIASES[part]}+`;
|
|
13
|
+
else {
|
|
14
|
+
steps.push(step + ([...part].length === 1 ? normalizeKeyName(part) : part.replace(/(?:^|-)(.)/g, (_, char) => char.toUpperCase())));
|
|
15
|
+
step = "";
|
|
16
|
+
}
|
|
17
|
+
if (step) return void 0;
|
|
18
|
+
return steps.length > 1 ? steps : steps[0];
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Installs `x-hotkey`, which registers the hotkey named by its modifiers while
|
|
22
|
+
* its element exists: `x-hotkey.mod.s="save()"`, `x-hotkey.escape="close()"`,
|
|
23
|
+
* `x-hotkey.g.h="goHome()"`. The expression gets `$event`. Alpine removes the
|
|
24
|
+
* hotkey with its element, and when a morph changes or removes the attribute.
|
|
25
|
+
*/
|
|
26
|
+
function hotkeyDirective(alpine) {
|
|
27
|
+
alpine.directive("hotkey", alpine.skipDuringClone((_element, { modifiers, expression }, { evaluateLater, cleanup }) => {
|
|
28
|
+
const hotkey = hotkeyFromModifiers(modifiers);
|
|
29
|
+
if (!hotkey) return;
|
|
30
|
+
const run = evaluateLater(expression);
|
|
31
|
+
const handle = getHotkeyManager().register(hotkey, (event) => run(() => {}, {
|
|
32
|
+
scope: { $event: event },
|
|
33
|
+
params: [event]
|
|
34
|
+
}));
|
|
35
|
+
cleanup(() => handle.unregister());
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export { hotkeyDirective, hotkeyFromModifiers };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { createHotkeysScope } from "./createHotkeysScope.js";
|
|
2
2
|
import { formatForDisplay } from "./formatForDisplay.js";
|
|
3
|
+
import { formatForAria } from "./formatForAria.js";
|
|
3
4
|
import { hotkeysPlugin } from "./hotkeysPlugin.js";
|
|
5
|
+
import { hotkeyDirective } from "./hotkeyDirective.js";
|
|
4
6
|
import { createHotkeyRecorder } from "./createHotkeyRecorder.js";
|
|
5
7
|
import { hotkeyRecorderPlugin } from "./hotkeyRecorderPlugin.js";
|
|
6
8
|
import { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions } from "./types.js";
|
|
7
9
|
export * from "@tanstack/hotkeys";
|
|
8
|
-
export { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions, createHotkeyRecorder, createHotkeysScope, formatForDisplay, hotkeyRecorderPlugin, hotkeysPlugin };
|
|
10
|
+
export { AlpineAppHotkeyOptions, AlpineCreateHotkey, AlpineCreateHotkeyRecorder, AlpineCustomHotkeys, AlpineHotkeyCommonOptions, AlpineHotkeyDefinition, AlpineHotkeyOptionsFor, AlpineHotkeyRecorder, AlpineHotkeyState, AlpineHotkeyTarget, AlpineHotkeys, DefaultHotkeysOptions, createHotkeyRecorder, createHotkeysScope, formatForAria, formatForDisplay, hotkeyDirective, hotkeyRecorderPlugin, hotkeysPlugin };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { createHotkeysScope } from "./createHotkeysScope.js";
|
|
2
2
|
import { formatForDisplay } from "./formatForDisplay.js";
|
|
3
|
+
import { formatForAria } from "./formatForAria.js";
|
|
3
4
|
import { hotkeysPlugin } from "./hotkeysPlugin.js";
|
|
5
|
+
import { hotkeyDirective } from "./hotkeyDirective.js";
|
|
4
6
|
import { createHotkeyRecorder } from "./createHotkeyRecorder.js";
|
|
5
7
|
import { hotkeyRecorderPlugin } from "./hotkeyRecorderPlugin.js";
|
|
6
8
|
|
|
7
9
|
export * from "@tanstack/hotkeys"
|
|
8
10
|
|
|
9
|
-
export { createHotkeyRecorder, createHotkeysScope, formatForDisplay, hotkeyRecorderPlugin, hotkeysPlugin };
|
|
11
|
+
export { createHotkeyRecorder, createHotkeysScope, formatForAria, formatForDisplay, hotkeyDirective, hotkeyRecorderPlugin, hotkeysPlugin };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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";
|
|
1
|
+
import { AppHotkey, CustomHotkeyActions, CustomHotkeys, HeldModifierOptions, Hotkey, HotkeyCallback, HotkeyCallbackContextFor, HotkeyChord, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeyRecorderActions, HotkeyRecorderOptions, HotkeyRecorderState, HotkeyRegistrationsResult, HotkeyScopesState, HotkeySequenceRecorderOptions, IndividualKey, KeyboardLayoutMap, LockKey, MaybeGetter, RegisterableHotkey, RegisteredAppHotkeyListItem, RegisteredAppHotkeys, Target, formatForAria, formatForDisplay } from "@tanstack/hotkeys";
|
|
2
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> {
|
|
@@ -53,6 +53,42 @@ export interface AlpineHotkeys {
|
|
|
53
53
|
createHeldKeyCodes: () => AlpineHotkeyState<Record<string, string>>;
|
|
54
54
|
/** Read whether a key is held through `.value`. */
|
|
55
55
|
createKeyHold: (key: MaybeGetter<IndividualKey>) => AlpineHotkeyState<boolean>;
|
|
56
|
+
/**
|
|
57
|
+
* Read whether a lock key, such as Caps Lock, is on through `.value`: `true`
|
|
58
|
+
* when the lock is on, `false` when it is off, or `null`. Pass
|
|
59
|
+
* `'CapsLock'`, `'NumLock'`, or `'ScrollLock'`, or a getter.
|
|
60
|
+
*
|
|
61
|
+
* Lock keys toggle instead of being held, so this reads the lock state that
|
|
62
|
+
* the browser reports on key and mouse events. `.value` is `null` until the
|
|
63
|
+
* user presses a key, clicks, or moves the mouse, after the window loses
|
|
64
|
+
* focus or the page is hidden, and always for Num Lock and Scroll Lock on macOS and iOS.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* Alpine.data('passwordField', () => {
|
|
69
|
+
* const hotkeys = createHotkeysScope()
|
|
70
|
+
* const capsLock = hotkeys.createKeyLockState('CapsLock')
|
|
71
|
+
*
|
|
72
|
+
* return {
|
|
73
|
+
* get capsLockOn() {
|
|
74
|
+
* return capsLock.value === true
|
|
75
|
+
* },
|
|
76
|
+
* destroy() {
|
|
77
|
+
* hotkeys.destroy()
|
|
78
|
+
* },
|
|
79
|
+
* }
|
|
80
|
+
* })
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* ```html
|
|
84
|
+
* <label x-data="passwordField">
|
|
85
|
+
* Password
|
|
86
|
+
* <input type="password" />
|
|
87
|
+
* <span x-show="capsLockOn" role="status">Caps Lock is on</span>
|
|
88
|
+
* </label>
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
createKeyLockState: (key: MaybeGetter<LockKey>) => AlpineHotkeyState<boolean | null>;
|
|
56
92
|
/**
|
|
57
93
|
* Read whether held modifiers reveal a hotkey through `.value`. No hotkey
|
|
58
94
|
* (`undefined`, `null`, or `''`) is never visible.
|
|
@@ -95,6 +131,44 @@ export interface AlpineHotkeys {
|
|
|
95
131
|
* ```
|
|
96
132
|
*/
|
|
97
133
|
createFormatForDisplay: () => typeof formatForDisplay;
|
|
134
|
+
/**
|
|
135
|
+
* Returns `formatForAria` with the scope's `platform` as the default, or the
|
|
136
|
+
* detected platform without one. Use its value for `aria-keyshortcuts`, so
|
|
137
|
+
* screen readers announce the shortcut. It returns `undefined` for a
|
|
138
|
+
* multi-step sequence, no hotkey, or a hotkey that can't fire right now (one
|
|
139
|
+
* that is disabled, in a suspended scope, a suspended character key, or not
|
|
140
|
+
* registered), so `x-bind` omits the attribute. An explicit `platform`
|
|
141
|
+
* option wins.
|
|
142
|
+
*
|
|
143
|
+
* Physical bindings such as `[KeyQ]` name the key the user presses in
|
|
144
|
+
* Chromium browsers, once the keyboard layout has loaded, the same way as
|
|
145
|
+
* `createFormatForDisplay`. A `layoutMap` passed to the returned function
|
|
146
|
+
* wins.
|
|
147
|
+
*
|
|
148
|
+
* Create it once, then call the returned function anywhere.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* Alpine.data('searchButton', () => {
|
|
153
|
+
* const hotkeys = createHotkeysScope()
|
|
154
|
+
* const formatForAria = hotkeys.createFormatForAria()
|
|
155
|
+
*
|
|
156
|
+
* return {
|
|
157
|
+
* get shortcut() {
|
|
158
|
+
* return formatForAria('Mod+K')
|
|
159
|
+
* },
|
|
160
|
+
* destroy() {
|
|
161
|
+
* hotkeys.destroy()
|
|
162
|
+
* },
|
|
163
|
+
* }
|
|
164
|
+
* })
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* ```html
|
|
168
|
+
* <button x-data="searchButton" :aria-keyshortcuts="shortcut">Search</button>
|
|
169
|
+
* ```
|
|
170
|
+
*/
|
|
171
|
+
createFormatForAria: () => typeof formatForAria;
|
|
98
172
|
/**
|
|
99
173
|
* Read the browser's keyboard layout through `.value`, or `null` until it
|
|
100
174
|
* loads and in browsers that don't report one. The layout maps each physical
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/alpine-hotkeys",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "Alpine adapter for TanStack Hotkeys",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -35,9 +35,11 @@
|
|
|
35
35
|
"skills"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@tanstack/hotkeys": "0.
|
|
38
|
+
"@tanstack/hotkeys": "0.38.0"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
+
"@alpinejs/csp": "3.17.5",
|
|
42
|
+
"@alpinejs/morph": "3.17.5",
|
|
41
43
|
"@tanstack/hotkeys-test-utils": "0.0.0",
|
|
42
44
|
"@types/alpinejs": "^3.17.0",
|
|
43
45
|
"alpinejs": "^3.17.5"
|
|
@@ -62,6 +62,8 @@ Alpine.data('panel', () => ({
|
|
|
62
62
|
|
|
63
63
|
Install `Alpine.plugin(hotkeysPlugin)` before `Alpine.start()`. It gives the adapter the app's Alpine instance; without it, the adapter falls back to `window.Alpine` and throws when neither exists. The plugin also adds the `$hotkeys` magic for markup-only components, which is destroyed with its element: `x-init="$hotkeys.createHotkey('Mod+S', () => save())"`.
|
|
64
64
|
|
|
65
|
+
For a fixed hotkey in markup, import `hotkeyDirective` from `@tanstack/alpine-hotkeys`, install `Alpine.plugin(hotkeyDirective)`, and write `x-hotkey.mod.s="save()"`. Modifiers name the hotkey in lowercase, with dashes for multi-word keys (`x-hotkey.arrow-up`), and `x-hotkey.g.h` is a sequence. The expression gets `$event`. The directive uses the built-in options, and it lives as long as its attribute, so it follows Livewire and `Alpine.morph` updates.
|
|
66
|
+
|
|
65
67
|
## API
|
|
66
68
|
|
|
67
69
|
| API | What it's for |
|
|
@@ -69,6 +71,7 @@ Install `Alpine.plugin(hotkeysPlugin)` before `Alpine.start()`. It gives the ada
|
|
|
69
71
|
| `Alpine.plugin(hotkeysPlugin)` | Give the adapter the app's Alpine, and install the `$hotkeys` magic, an owner destroyed with its element. |
|
|
70
72
|
| `createHotkeysScope(defaults?)` | Create an owner for one `Alpine.data` component, with optional default options. |
|
|
71
73
|
| `createHotkeyRecorder(owner, options)` | Record a chord or sequence the user presses, for a settings screen. The owner destroys it and supplies its `hotkeyRecorder` defaults. |
|
|
74
|
+
| `Alpine.plugin(hotkeyDirective)` | Install `x-hotkey`, which registers the hotkey its modifiers name while the attribute exists. |
|
|
72
75
|
| `Alpine.plugin(hotkeyRecorderPlugin)` | Install the `$hotkeyRecorder(options)` magic, which creates recorders destroyed with their element. |
|
|
73
76
|
| `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys once at startup, since Alpine has no provider. |
|
|
74
77
|
| `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. |
|
|
@@ -80,6 +83,7 @@ Each owner has these methods. Read their state through `.value`, or `.hotkeys` f
|
|
|
80
83
|
| `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey until the owner is destroyed; options can be a getter. |
|
|
81
84
|
| `createHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a getter of a list built from data, in one call. |
|
|
82
85
|
| `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. |
|
|
86
|
+
| `createFormatForAria()` | Get the function that formats a hotkey for `aria-keyshortcuts`, with the owner's default `platform` and the user's keyboard layout filled in. Returns `undefined` for a multi-step sequence, no hotkey, or a hotkey that can't fire right now, such as a disabled or suspended one, so the attribute is omitted. Set `aria-keyshortcuts` on every control that has a shortcut, next to the visible label. |
|
|
83
87
|
| `createAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied. |
|
|
84
88
|
| `createAppHotkeyList()` | List every app hotkey with its status, for a cheat sheet or settings screen. |
|
|
85
89
|
| `createCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
|
|
@@ -87,11 +91,12 @@ Each owner has these methods. Read their state through `.value`, or `.hotkeys` f
|
|
|
87
91
|
| `createHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
|
|
88
92
|
| `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
93
|
| `createKeyHold(key)` | Get whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
|
|
94
|
+
| `createKeyLockState(key)` | Read `.value`, whether `'CapsLock'`, `'NumLock'`, or `'ScrollLock'` is on: `true`, `false`, or `null` until a key press, click, or mouse move reports it. Resets to `null` when the window loses focus. Num Lock and Scroll Lock are always `null` on macOS and iOS. Use it for lock state, not `createKeyHold`, which is true only while the key is down. |
|
|
90
95
|
| `createHeldKeys()` | Get every key that's held right now. |
|
|
91
96
|
| `createHeldKeyCodes()` | Map each held key to its physical `event.code`. |
|
|
92
97
|
| `destroy()` | Release every hotkey, subscription, and recorder the owner created. |
|
|
93
98
|
|
|
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`.
|
|
99
|
+
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`. The plain `formatForAria` export is deprecated the same way, in favor of the owner's `createFormatForAria()`.
|
|
95
100
|
|
|
96
101
|
## Pitfalls
|
|
97
102
|
|
|
@@ -99,6 +104,8 @@ The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent`
|
|
|
99
104
|
- **`createHotkeysScope` isn't a hotkey scope.** It only owns registrations and sets defaults. For a dialog, activate a real scope in `init()` with `release = getHotkeyManager().activateScope(name, { exclusive: true })`, pass `{ scope: name }` to the dialog's hotkeys, and call `release()` in `destroy()`. Give each open dialog its own name.
|
|
100
105
|
- **Pass options as a getter.** `{ enabled: this.dirty }` is read once. Use `() => ({ enabled: this.dirty })`.
|
|
101
106
|
- **Use either `$hotkeys` or your own owner for a hotkey, not both.** Otherwise it registers twice.
|
|
107
|
+
- **Use `x-hotkey` on pages that Livewire or `Alpine.morph` updates.** A morph that changes `x-init` runs it again, but `$hotkeys` keeps the old registration until its element is removed, so both hotkeys fire.
|
|
108
|
+
- **Alpine's CSP build can't parse arrow functions in markup.** Put the callback in an `Alpine.data` method and name it: `x-hotkey.mod.s="save"`. A call such as `save()` needs `@alpinejs/csp` 3.15 or newer, or use the `init()` pattern.
|
|
102
109
|
- **Call `create*` in `x-init` or `init()`, and read `.value` anywhere.** Calling `create*` inside a reactive expression such as `x-text="$hotkeys.createHeldKeys().value"` creates a new subscription on every evaluation.
|
|
103
110
|
- **Owners have no `createHotkeyRecorder` method** since v0.10.0. Call `createHotkeyRecorder(this.hotkeys, options)`, so apps that don't record don't ship the recorder.
|
|
104
111
|
- With server rendering, load the adapter only in the browser.
|