@tanstack/ember-hotkeys 0.4.1 → 0.6.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/index.d.ts CHANGED
@@ -7,10 +7,11 @@ import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
7
7
  import { useKeyHold } from "./useKeyHold.js";
8
8
  import { useHotkeyHint } from "./useHotkeyHint.js";
9
9
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
10
+ import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
10
11
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
11
12
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
12
13
  import { ElementHotkeyOptions, OnHotkeys } from "./onHotkeys.js";
13
14
  import { OnHotkey } from "./onHotkey.js";
14
15
  import { EmberHotkeysScope, createHotkeysScope } from "./createHotkeysScope.js";
15
16
  export * from "@tanstack/hotkeys";
16
- 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, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, UseHotkeys as useHotkeys, useKeyHold };
17
+ 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, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold };
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
5
5
  import { useKeyHold } from "./useKeyHold.js";
6
6
  import { useHotkeyHint } from "./useHotkeyHint.js";
7
7
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
8
+ import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
8
9
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
9
10
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
10
11
  import { OnHotkey } from "./onHotkey.js";
@@ -13,4 +14,4 @@ import { createHotkeysScope } from "./createHotkeysScope.js";
13
14
 
14
15
  export * from "@tanstack/hotkeys"
15
16
 
16
- export { createHotkeysScope, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, UseHotkeys as useHotkeys, useKeyHold };
17
+ export { createHotkeysScope, OnHotkey as onHotkey, OnHotkeys as onHotkeys, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useHeldKeyCodes, useHeldKeys, UseHotkey as useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, UseHotkeys as useHotkeys, useKeyHold };
@@ -4,8 +4,10 @@ import { AppHotkey, HeldModifierOptions, RegisterableHotkey } from "@tanstack/ho
4
4
  //#region src/useHotkeyHint.d.ts
5
5
  /**
6
6
  * Recomputes for held modifiers and tracked changes to the hotkey or options.
7
- * A sequence reveals its hint from its first step, and an app hotkey uses its
8
- * effective hotkey (false while unbound).
7
+ * The hint shows only while the hotkey can fire: some registration of it is
8
+ * enabled and in an active scope. A sequence reveals its hint from its first
9
+ * step, and an app hotkey matches its registrations by id and uses its
10
+ * effective hotkey (false while unbound). A hotkey on an element counts only while focus is inside it.
9
11
  */
10
12
  export declare function useHotkeyHint(owner: object, hotkey: MaybeGetter<RegisterableHotkey | AppHotkey>, options?: MaybeGetter<HeldModifierOptions>): EmberHotkeyState<boolean>;
11
13
  //#endregion
@@ -1,18 +1,22 @@
1
1
  import { read, select, trackPublishedAppHotkeys } from "./utils.js";
2
- import { getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
2
+ import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
3
3
 
4
4
  //#region src/useHotkeyHint.ts
5
5
  /**
6
6
  * Recomputes for held modifiers and tracked changes to the hotkey or options.
7
- * A sequence reveals its hint from its first step, and an app hotkey uses its
8
- * effective hotkey (false while unbound).
7
+ * The hint shows only while the hotkey can fire: some registration of it is
8
+ * enabled and in an active scope. A sequence reveals its hint from its first
9
+ * step, and an app hotkey matches its registrations by id and uses its
10
+ * effective hotkey (false while unbound). A hotkey on an element counts only while focus is inside it.
9
11
  */
10
12
  function useHotkeyHint(owner, hotkey, options = {}) {
11
13
  const appHotkeys = trackPublishedAppHotkeys(owner);
14
+ const manager = getHotkeyManager();
15
+ const registrations = select(owner, manager.registrations, (state) => state);
12
16
  return select(owner, getKeyStateTracker().store, (state) => {
13
17
  const value = read(hotkey);
14
18
  const resolved = isAppHotkey(value) ? resolveAppHotkey(value, appHotkeys()).hotkey : value;
15
- return resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, read(options));
19
+ return resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, read(options)) && registrations.value.size > 0 && manager.hasActiveRegistration(value);
16
20
  });
17
21
  }
18
22
 
@@ -0,0 +1,10 @@
1
+ import { EmberHotkeyState } from "./types.js";
2
+ import { HotkeyScopesState } from "@tanstack/hotkeys";
3
+ //#region src/useHotkeyScopesState.d.ts
4
+ /**
5
+ * Reads the hotkey manager's scope state: the active scopes, the exclusive
6
+ * scope that owns the keyboard, and the scopes it allows or suspends. Pass the
7
+ * containing component as owner to release the subscription on destruction.
8
+ */
9
+ export declare function useHotkeyScopesState(owner: object): EmberHotkeyState<HotkeyScopesState>;
10
+ //#endregion
@@ -0,0 +1,15 @@
1
+ import { select } from "./utils.js";
2
+ import { getHotkeyManager } from "@tanstack/hotkeys";
3
+
4
+ //#region src/useHotkeyScopesState.ts
5
+ /**
6
+ * Reads the hotkey manager's scope state: the active scopes, the exclusive
7
+ * scope that owns the keyboard, and the scopes it allows or suspends. Pass the
8
+ * containing component as owner to release the subscription on destruction.
9
+ */
10
+ function useHotkeyScopesState(owner) {
11
+ return select(owner, getHotkeyManager().scopes, (state) => state);
12
+ }
13
+
14
+ //#endregion
15
+ export { useHotkeyScopesState };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ember-hotkeys",
3
- "version": "0.4.1",
3
+ "version": "0.6.0",
4
4
  "description": "Ember adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -17,7 +17,8 @@
17
17
  "keywords": [
18
18
  "ember",
19
19
  "tanstack",
20
- "hotkeys"
20
+ "hotkeys",
21
+ "tanstack-intent"
21
22
  ],
22
23
  "type": "module",
23
24
  "types": "./dist/index.d.ts",
@@ -34,11 +35,12 @@
34
35
  },
35
36
  "files": [
36
37
  "dist",
37
- "addon-main.cjs"
38
+ "addon-main.cjs",
39
+ "skills"
38
40
  ],
39
41
  "dependencies": {
40
42
  "@embroider/addon-shim": "^1.10.3",
41
- "@tanstack/hotkeys": "0.16.1",
43
+ "@tanstack/hotkeys": "0.18.0",
42
44
  "@tanstack/store": "^0.11.2",
43
45
  "ember-modifier": "4.2.2"
44
46
  },
@@ -0,0 +1,92 @@
1
+ ---
2
+ name: ember-hotkeys
3
+ description: >-
4
+ Use when adding or fixing keyboard shortcuts in an Ember app with
5
+ @tanstack/ember-hotkeys: the useHotkey and useHotkeys template helpers,
6
+ onHotkey and onHotkeys element modifiers, createHotkeysScope defaults,
7
+ scopes activated from the hotkey manager, app hotkeys, recorders, and held
8
+ keys in strict-mode .gts components, or replacing keydown event listeners.
9
+ metadata:
10
+ purpose: >-
11
+ Ember bindings for TanStack Hotkeys: template helpers, element modifiers,
12
+ owner-bound state readers, manager-activated scopes, and the
13
+ Ember-specific mistakes to avoid.
14
+ type: framework
15
+ framework: ember
16
+ library: '@tanstack/ember-hotkeys'
17
+ library_version: '0.4.0'
18
+ sources:
19
+ - src/useHotkey.ts
20
+ - src/useHotkeys.ts
21
+ - src/onHotkey.ts
22
+ - src/onHotkeys.ts
23
+ - src/createHotkeysScope.ts
24
+ - src/useAppHotkeys.ts
25
+ - TanStack/hotkeys:docs/framework/ember/quick-start.md
26
+ - TanStack/hotkeys:docs/framework/ember/guides/app-hotkeys.md
27
+ requires:
28
+ - '@tanstack/hotkeys#hotkeys-core'
29
+ ---
30
+
31
+ # TanStack Hotkeys in Ember
32
+
33
+ Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack/intent@latest load @tanstack/hotkeys#hotkeys-core`).
34
+
35
+ ```gts
36
+ import Component from '@glimmer/component'
37
+ import { tracked } from '@glimmer/tracking'
38
+ import { array } from '@ember/helper'
39
+ import { onHotkey, useHotkey } from '@tanstack/ember-hotkeys'
40
+
41
+ export default class Editor extends Component {
42
+ @tracked dirty = false
43
+ save = () => saveDocument()
44
+ close = () => closePanel()
45
+ scrollToTop = () => window.scrollTo({ top: 0 })
46
+
47
+ <template>
48
+ {{useHotkey "Mod+S" this.save enabled=this.dirty}}
49
+ {{useHotkey (array "G" "G") this.scrollToTop}}
50
+ <div tabindex="0" {{onHotkey "Escape" this.close}}>Panel</div>
51
+ </template>
52
+ }
53
+ ```
54
+
55
+ ## API
56
+
57
+ | API | What it's for |
58
+ | --------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
59
+ | `{{useHotkey hotkey callback ...options}}` | Register one chord, sequence, or app hotkey while the template renders the helper. |
60
+ | `{{useHotkeys definitions}}` | Register several hotkeys, or a list built from data, in one helper. |
61
+ | `{{onHotkey hotkey callback ...options}}` | Attach a hotkey to the element the modifier is on, so it fires while focus is inside that element. |
62
+ | `{{onHotkeys definitions}}` | Attach several hotkeys to one element. |
63
+ | `createHotkeysScope(defaults?)` | Create `useHotkey`, `useHotkeys`, `onHotkey`, `onHotkeys`, and `useHotkeyRecorder` that share default options. |
64
+ | `provideAppHotkeys(appHotkeys, options?)` | Declare app hotkeys and load the user's custom hotkeys before the app boots, since Ember has no provider. |
65
+ | `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. |
66
+ | `useAppHotkeys(this)` | Get the declared app hotkeys with the user's custom hotkeys applied. |
67
+ | `useAppHotkeyList(this)` | List every app hotkey with its status, for a cheat sheet or settings screen. |
68
+ | `useCustomHotkeys(this)` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
69
+ | `useHotkeyRecorder(this, options)` | Record a chord or sequence the user presses, for a settings screen. |
70
+ | `useHotkeyRegistrations(this)` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
71
+ | `useHotkeyScopesState(this)` | Get the active scopes and the exclusive scope that owns the keyboard. |
72
+ | `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. |
73
+ | `useKeyHold(this, key)` | Get whether one key is held. |
74
+ | `useHeldKeys(this)` | Get every key that's held right now. |
75
+ | `useHeldKeyCodes(this)` | Map each held key to its physical `event.code`. |
76
+
77
+ The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
78
+
79
+ 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 or newer and strict-mode `.gts` components.
80
+
81
+ ## Pitfalls
82
+
83
+ - **Use arrow-function fields, or `@action`, for callbacks.** The helpers call a callback without the component as `this`, so a plain method throws when the key is pressed.
84
+ - **Build template sequences with `array`.** `{{useHotkey "G G" ...}}` registers a single key named `G G`, which never matches.
85
+ - **`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.
86
+ - **Read app hotkeys through getters.** Template paths can't index ids that contain dots.
87
+
88
+ 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).
89
+
90
+ ## API discovery and docs
91
+
92
+ Signatures: `node_modules/@tanstack/ember-hotkeys/dist/*.d.ts`, with state shapes in `types.d.ts`. Guides: `https://tanstack.com/hotkeys/latest/docs/framework/ember/guides/<page>.md`, such as `hotkeys` or `app-hotkeys`.
@@ -0,0 +1,68 @@
1
+ # App hotkeys in Ember
2
+
3
+ The `@tanstack/hotkeys#app-hotkeys` skill owns the rules. This is the Ember wiring.
4
+
5
+ ```ts
6
+ // app/app-hotkeys.ts
7
+ import { defineAppHotkeys } from '@tanstack/ember-hotkeys'
8
+
9
+ export const appHotkeys = defineAppHotkeys({
10
+ 'doc.save': { hotkey: 'Mod+S', meta: { name: 'Save' } },
11
+ 'help.shortcuts': { hotkey: 'Mod+/', meta: { name: 'Keyboard shortcuts' } },
12
+ })
13
+
14
+ declare module '@tanstack/ember-hotkeys' {
15
+ interface Register {
16
+ appHotkeys: typeof appHotkeys
17
+ }
18
+ }
19
+ ```
20
+
21
+ ```ts
22
+ // app/app.ts, before the application boots
23
+ import { provideAppHotkeys } from '@tanstack/ember-hotkeys'
24
+ import { appHotkeys } from './app-hotkeys'
25
+
26
+ provideAppHotkeys(appHotkeys, {
27
+ customHotkeys: JSON.parse(localStorage.getItem('hotkeys') ?? '{}'),
28
+ onCustomHotkeysChange: (custom) =>
29
+ localStorage.setItem('hotkeys', JSON.stringify(custom)),
30
+ })
31
+ ```
32
+
33
+ The app hotkey utilities throw until `provideAppHotkeys` has run. When saved shortcuts come from a server, await them first. Register through a getter: `get saveHotkey() { return this.appHotkeys.value['doc.save'] }` with `appHotkeys = useAppHotkeys(this)`, then `{{useHotkey this.saveHotkey this.save}}`. Build a cheat sheet from `useAppHotkeyList(this).value`.
34
+
35
+ A settings row records with `useHotkeyRecorder` and saves through `useCustomHotkeys`:
36
+
37
+ ```gts
38
+ import Component from '@glimmer/component'
39
+ import { on } from '@ember/modifier'
40
+ import { formatForDisplay, useAppHotkeys, useCustomHotkeys, useHotkeyRecorder } from '@tanstack/ember-hotkeys'
41
+ import type { AppHotkeyId } from '@tanstack/ember-hotkeys'
42
+
43
+ export default class ShortcutRow extends Component<{ Args: { id: AppHotkeyId } }> {
44
+ appHotkeys = useAppHotkeys(this)
45
+ custom = useCustomHotkeys(this)
46
+ recorder = useHotkeyRecorder(this, () => ({
47
+ maxSteps: Infinity,
48
+ validate: (hotkey) => {
49
+ const [conflict] = this.custom.findConflicts(this.args.id, hotkey)
50
+ return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
51
+ },
52
+ onRecord: (hotkey) => this.custom.setCustomHotkey(this.args.id, hotkey),
53
+ onClear: () => this.custom.setCustomHotkey(this.args.id, null),
54
+ }))
55
+
56
+ get label() {
57
+ const { meta, hotkey } = this.appHotkeys.value[this.args.id]
58
+ return `${meta?.name}: ${hotkey ? formatForDisplay(hotkey) : 'Unassigned'}`
59
+ }
60
+ reset = () => this.custom.resetCustomHotkey(this.args.id)
61
+
62
+ <template>
63
+ {{this.label}}
64
+ <button type="button" {{on "click" this.recorder.startRecording}}>Change</button>
65
+ <button type="button" {{on "click" this.reset}}>Reset</button>
66
+ </template>
67
+ }
68
+ ```