@tanstack/vue-hotkeys 0.22.1 → 0.24.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.
@@ -1,7 +1,39 @@
1
1
  import { HotkeysProviderOptions } from "./HotkeysProviderContext.js";
2
2
  import { AppHotkeys, CustomHotkeys } from "@tanstack/hotkeys";
3
- import { PropType } from "vue";
3
+ import { ComponentOptionsMixin, DefineComponent, PropType, VNode } from "vue";
4
4
  import { HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
5
+ declare const providerProps: {
6
+ defaultOptions: {
7
+ type: PropType<HotkeysProviderOptions>;
8
+ default: undefined;
9
+ };
10
+ appHotkeys: {
11
+ type: PropType<AppHotkeys>;
12
+ default: undefined;
13
+ };
14
+ customHotkeys: {
15
+ type: PropType<CustomHotkeys | null>;
16
+ default: undefined;
17
+ };
18
+ /** See `HotkeysProviderScopeProps['scope']`. */
19
+ scope: {
20
+ type: StringConstructor;
21
+ default: undefined;
22
+ };
23
+ /** See `HotkeysProviderScopeProps['exclusive']`. */
24
+ exclusive: {
25
+ type: PropType<HotkeysProviderScopeProps["exclusive"]>;
26
+ default: boolean;
27
+ };
28
+ /** See `HotkeysProviderScopeProps['enabled']`. */
29
+ enabled: {
30
+ type: BooleanConstructor;
31
+ default: boolean;
32
+ };
33
+ };
34
+ declare const providerEmits: {
35
+ customHotkeysChange: (_customHotkeys: CustomHotkeys) => true;
36
+ };
5
37
  /**
6
38
  * Vue component that provides default options, app hotkeys, and the user's
7
39
  * custom hotkeys. A new `customHotkeys` value replaces the current custom
@@ -13,6 +45,10 @@ import { HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
13
45
  * while it is mounted, enabled, and not hidden by `<KeepAlive>`; with
14
46
  * `exclusive`, only its hotkeys fire.
15
47
  *
48
+ * Props: `defaultOptions` (a `HotkeysProviderOptions`), `appHotkeys`,
49
+ * `customHotkeys`, and the `scope`, `exclusive`, and `enabled` props of
50
+ * `HotkeysProviderScopeProps`. Event: `custom-hotkeys-change`.
51
+ *
16
52
  * @example
17
53
  * ```vue
18
54
  * <template>
@@ -33,75 +69,5 @@ import { HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
33
69
  * </HotkeysProvider>
34
70
  * ```
35
71
  */
36
- export declare const HotkeysProvider: import("vue").DefineComponent<import("vue").ExtractPropTypes<{
37
- defaultOptions: {
38
- type: PropType<HotkeysProviderOptions>;
39
- default: undefined;
40
- };
41
- appHotkeys: {
42
- type: PropType<AppHotkeys>;
43
- default: undefined;
44
- };
45
- customHotkeys: {
46
- type: PropType<CustomHotkeys | null>;
47
- default: undefined;
48
- };
49
- /** See `HotkeysProviderScopeProps['scope']`. */
50
- scope: {
51
- type: StringConstructor;
52
- default: undefined;
53
- };
54
- /** See `HotkeysProviderScopeProps['exclusive']`. */
55
- exclusive: {
56
- type: PropType<HotkeysProviderScopeProps["exclusive"]>;
57
- default: boolean;
58
- };
59
- /** See `HotkeysProviderScopeProps['enabled']`. */
60
- enabled: {
61
- type: BooleanConstructor;
62
- default: boolean;
63
- };
64
- }>, () => import("vue").VNode<import("vue").RendererNode, import("vue").RendererElement, {
65
- [key: string]: any;
66
- }>[] | undefined, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {
67
- customHotkeysChange: (_customHotkeys: CustomHotkeys) => true;
68
- }, string, import("vue").PublicProps, Readonly<import("vue").ExtractPropTypes<{
69
- defaultOptions: {
70
- type: PropType<HotkeysProviderOptions>;
71
- default: undefined;
72
- };
73
- appHotkeys: {
74
- type: PropType<AppHotkeys>;
75
- default: undefined;
76
- };
77
- customHotkeys: {
78
- type: PropType<CustomHotkeys | null>;
79
- default: undefined;
80
- };
81
- /** See `HotkeysProviderScopeProps['scope']`. */
82
- scope: {
83
- type: StringConstructor;
84
- default: undefined;
85
- };
86
- /** See `HotkeysProviderScopeProps['exclusive']`. */
87
- exclusive: {
88
- type: PropType<HotkeysProviderScopeProps["exclusive"]>;
89
- default: boolean;
90
- };
91
- /** See `HotkeysProviderScopeProps['enabled']`. */
92
- enabled: {
93
- type: BooleanConstructor;
94
- default: boolean;
95
- };
96
- }>> & Readonly<{
97
- onCustomHotkeysChange?: ((_customHotkeys: CustomHotkeys<Readonly<Record<string, import("@tanstack/hotkeys").AppHotkey<string, import("@tanstack/hotkeys").HotkeyChord | import("@tanstack/hotkeys").HotkeySequence | null>>>>) => any) | undefined;
98
- }>, {
99
- exclusive: boolean | {
100
- allow: ReadonlyArray<string>;
101
- } | undefined;
102
- defaultOptions: HotkeysProviderOptions;
103
- appHotkeys: Readonly<Record<string, import("@tanstack/hotkeys").AppHotkey<string, import("@tanstack/hotkeys").HotkeyChord | import("@tanstack/hotkeys").HotkeySequence | null>>>;
104
- customHotkeys: CustomHotkeys<Readonly<Record<string, import("@tanstack/hotkeys").AppHotkey<string, import("@tanstack/hotkeys").HotkeyChord | import("@tanstack/hotkeys").HotkeySequence | null>>>> | null;
105
- scope: string;
106
- enabled: boolean;
107
- }, {}, {}, {}, string, import("vue").ComponentProvideOptions, true, {}, any>;
72
+ export declare const HotkeysProvider: DefineComponent<typeof providerProps, () => Array<VNode> | undefined, {}, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, typeof providerEmits>;
73
+ export {};
@@ -2,38 +2,7 @@ import { provideHotkeysContext } from "./HotkeysProviderContext.js";
2
2
  import { defineComponent } from "vue";
3
3
 
4
4
  // src/HotkeysProvider.tsx
5
- /**
6
- * Vue component that provides default options, app hotkeys, and the user's
7
- * custom hotkeys. A new `customHotkeys` value replaces the current custom
8
- * hotkeys, `undefined` leaves them unchanged, and `null` clears them. Listen
9
- * to `custom-hotkeys-change` to save changes.
10
- *
11
- * Nest providers to create hotkey scopes. A nested provider inherits app
12
- * hotkeys and merges default options. With `scope`, its hotkeys fire only
13
- * while it is mounted, enabled, and not hidden by `<KeepAlive>`; with
14
- * `exclusive`, only its hotkeys fire.
15
- *
16
- * @example
17
- * ```vue
18
- * <template>
19
- * <HotkeysProvider
20
- * :app-hotkeys="appHotkeys"
21
- * :custom-hotkeys="saved"
22
- * @custom-hotkeys-change="save"
23
- * >
24
- * <App />
25
- * </HotkeysProvider>
26
- * </template>
27
- * ```
28
- *
29
- * @example
30
- * ```vue
31
- * <HotkeysProvider scope="edit-dialog" :exclusive="{ allow: ['global'] }">
32
- * <DialogBody />
33
- * </HotkeysProvider>
34
- * ```
35
- */
36
- const HotkeysProvider = defineComponent({
5
+ const component = defineComponent({
37
6
  name: "HotkeysProvider",
38
7
  props: {
39
8
  defaultOptions: {
@@ -78,5 +47,41 @@ const HotkeysProvider = defineComponent({
78
47
  return () => slots.default?.();
79
48
  }
80
49
  });
50
+ /**
51
+ * Vue component that provides default options, app hotkeys, and the user's
52
+ * custom hotkeys. A new `customHotkeys` value replaces the current custom
53
+ * hotkeys, `undefined` leaves them unchanged, and `null` clears them. Listen
54
+ * to `custom-hotkeys-change` to save changes.
55
+ *
56
+ * Nest providers to create hotkey scopes. A nested provider inherits app
57
+ * hotkeys and merges default options. With `scope`, its hotkeys fire only
58
+ * while it is mounted, enabled, and not hidden by `<KeepAlive>`; with
59
+ * `exclusive`, only its hotkeys fire.
60
+ *
61
+ * Props: `defaultOptions` (a `HotkeysProviderOptions`), `appHotkeys`,
62
+ * `customHotkeys`, and the `scope`, `exclusive`, and `enabled` props of
63
+ * `HotkeysProviderScopeProps`. Event: `custom-hotkeys-change`.
64
+ *
65
+ * @example
66
+ * ```vue
67
+ * <template>
68
+ * <HotkeysProvider
69
+ * :app-hotkeys="appHotkeys"
70
+ * :custom-hotkeys="saved"
71
+ * @custom-hotkeys-change="save"
72
+ * >
73
+ * <App />
74
+ * </HotkeysProvider>
75
+ * </template>
76
+ * ```
77
+ *
78
+ * @example
79
+ * ```vue
80
+ * <HotkeysProvider scope="edit-dialog" :exclusive="{ allow: ['global'] }">
81
+ * <DialogBody />
82
+ * </HotkeysProvider>
83
+ * ```
84
+ */
85
+ const HotkeysProvider = component;
81
86
 
82
87
  export { HotkeysProvider };
@@ -9,6 +9,14 @@ export interface HotkeysProviderOptions {
9
9
  * from the request with `detectPlatform(userAgent)`.
10
10
  */
11
11
  platform?: HotkeyPlatform;
12
+ /**
13
+ * Whether labels from `useFormatForDisplay` follow the user's keyboard
14
+ * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
15
+ * loads from the browser when the first label mounts. Only Chromium
16
+ * browsers report one. Set `false` to never ask the browser for it.
17
+ * Default: `true`.
18
+ */
19
+ keyboardLayout?: boolean;
12
20
  /**
13
21
  * Defaults for every hotkey registered by `useHotkey` and `useHotkeys`.
14
22
  * `requireReset` applies to chords and `timeout` applies to sequences.
@@ -0,0 +1,12 @@
1
+ import { formatForDisplay } from "@tanstack/hotkeys";
2
+ /**
3
+ * Formats a hotkey as a label, without the provider's platform or the user's
4
+ * keyboard layout.
5
+ *
6
+ * @deprecated Use `useFormatForDisplay()` in components. It returns this
7
+ * function with the provider's platform and the user's keyboard layout filled
8
+ * in. Outside a component, keep calling this function and pass `platform`
9
+ * 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 provider's platform or the user's
6
+ * keyboard layout.
7
+ *
8
+ * @deprecated Use `useFormatForDisplay()` in components. It returns this
9
+ * function with the provider's platform and the user's keyboard layout filled
10
+ * in. Outside a component, keep calling this function and pass `platform`
11
+ * yourself.
12
+ */
13
+ const formatForDisplay$1 = formatForDisplay;
14
+
15
+ export { formatForDisplay$1 as formatForDisplay };
package/dist/index.d.ts CHANGED
@@ -10,6 +10,8 @@ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
10
10
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
11
11
  import { useHotkeyHint } from "./useHotkeyHint.js";
12
12
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
13
+ import { formatForDisplay } from "./formatForDisplay.js";
14
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
13
15
  import { UseCustomHotkeysResult, useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
14
16
  export * from "@tanstack/hotkeys";
15
- export { HotkeysProvider, HotkeysProviderOptions, ProvideHotkeysContextOptions, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, VueHotkeyRecorder, provideHotkeysContext, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
17
+ export { HotkeysProvider, HotkeysProviderOptions, ProvideHotkeysContextOptions, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, VueHotkeyRecorder, formatForDisplay, provideHotkeysContext, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
package/dist/index.js CHANGED
@@ -10,8 +10,10 @@ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
10
10
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
11
11
  import { useHotkeyHint } from "./useHotkeyHint.js";
12
12
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
13
+ import { formatForDisplay } from "./formatForDisplay.js";
14
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
13
15
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
14
16
 
15
17
  export * from "@tanstack/hotkeys"
16
18
 
17
- export { HotkeysProvider, provideHotkeysContext, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
19
+ export { HotkeysProvider, formatForDisplay, provideHotkeysContext, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
@@ -1,4 +1,4 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RegisteredAppHotkeys } from "@tanstack/hotkeys";
1
+ import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RecordedKeyPresses, RegisteredAppHotkeys } from "@tanstack/hotkeys";
2
2
  import { ComputedRef } from "vue";
3
3
  /**
4
4
  * Returns a computed ref of the effective app hotkeys from `HotkeysProvider`,
@@ -23,15 +23,20 @@ export interface UseCustomHotkeysResult {
23
23
  customHotkeys: ComputedRef<CustomHotkeys>;
24
24
  /**
25
25
  * Sets the user's hotkey for an app hotkey. `null` unbinds it.
26
- * Returns conflicting app hotkeys; when any exist, nothing changes.
26
+ * Returns conflicting app hotkeys; when any exist, nothing changes. Pass
27
+ * the recorder's `context` for a recorded hotkey, as for `findConflicts`.
27
28
  */
28
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
29
+ setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
29
30
  /** Restores the default hotkey for an app hotkey. */
30
31
  resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
31
32
  /** Restores every default hotkey. */
32
33
  resetCustomHotkeys: () => void;
33
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
34
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
34
+ /**
35
+ * Other app hotkeys that would collide if `id` used `hotkey`. Pass the
36
+ * recorder's `context` for a recorded hotkey: it has the key press, which
37
+ * also finds a hotkey written differently that the same press fires.
38
+ */
39
+ findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
35
40
  }
36
41
  /**
37
42
  * Reads and changes the user's custom hotkeys. Changes apply to every
@@ -54,10 +54,10 @@ function useCustomHotkeys() {
54
54
  state.value;
55
55
  return store().customHotkeys;
56
56
  }),
57
- setCustomHotkey: (id, hotkey) => store().setCustomHotkey(id, hotkey),
57
+ setCustomHotkey: (id, hotkey, recordedWith) => store().setCustomHotkey(id, hotkey, recordedWith),
58
58
  resetCustomHotkey: (id) => store().resetCustomHotkey(id),
59
59
  resetCustomHotkeys: () => store().resetCustomHotkeys(),
60
- findConflicts: (id, hotkey) => store().findConflicts(id, hotkey)
60
+ findConflicts: (id, hotkey, recordedWith) => store().findConflicts(id, hotkey, recordedWith)
61
61
  };
62
62
  }
63
63
 
@@ -5,17 +5,23 @@ import { formatForDisplay } from "@tanstack/hotkeys";
5
5
  * works anywhere, and templates update when the provider's platform changes.
6
6
  * An explicit `platform` option wins.
7
7
  *
8
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
9
+ * types for this user in Chromium browsers, once the keyboard layout has
10
+ * loaded, and templates update when it arrives. See `useKeyboardLayout`. A
11
+ * `layoutMap` passed to the returned function wins. Firefox and Safari report
12
+ * no layout, so labels there keep the key's US character.
13
+ *
8
14
  * Server-rendered apps should set the provider's `defaultOptions.platform`
9
15
  * from the request user agent with `detectPlatform(userAgent)`.
10
16
  *
11
17
  * @example
12
18
  * ```vue
13
19
  * <script setup lang="ts">
14
- * const format = useFormatForDisplay()
20
+ * const formatForDisplay = useFormatForDisplay()
15
21
  * </script>
16
22
  *
17
23
  * <template>
18
- * <kbd>{{ format('Mod+S') }}</kbd>
24
+ * <kbd>{{ formatForDisplay('Mod+S') }}</kbd>
19
25
  * </template>
20
26
  * ```
21
27
  */
@@ -1,5 +1,7 @@
1
1
  import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
2
3
  import { formatForDisplay } from "@tanstack/hotkeys";
4
+ import { namesPhysicalKey } from "@tanstack/hotkeys/adapter";
3
5
 
4
6
  // src/useFormatForDisplay.ts
5
7
  /**
@@ -8,25 +10,33 @@ import { formatForDisplay } from "@tanstack/hotkeys";
8
10
  * works anywhere, and templates update when the provider's platform changes.
9
11
  * An explicit `platform` option wins.
10
12
  *
13
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
14
+ * types for this user in Chromium browsers, once the keyboard layout has
15
+ * loaded, and templates update when it arrives. See `useKeyboardLayout`. A
16
+ * `layoutMap` passed to the returned function wins. Firefox and Safari report
17
+ * no layout, so labels there keep the key's US character.
18
+ *
11
19
  * Server-rendered apps should set the provider's `defaultOptions.platform`
12
20
  * from the request user agent with `detectPlatform(userAgent)`.
13
21
  *
14
22
  * @example
15
23
  * ```vue
16
24
  * <script setup lang="ts">
17
- * const format = useFormatForDisplay()
25
+ * const formatForDisplay = useFormatForDisplay()
18
26
  * <\/script>
19
27
  *
20
28
  * <template>
21
- * <kbd>{{ format('Mod+S') }}</kbd>
29
+ * <kbd>{{ formatForDisplay('Mod+S') }}</kbd>
22
30
  * </template>
23
31
  * ```
24
32
  */
25
33
  function useFormatForDisplay() {
26
34
  const defaultOptions = useDefaultHotkeysOptionsSource();
35
+ const layoutMap = useKeyboardLayout();
27
36
  return ((hotkey, options) => formatForDisplay(hotkey, {
28
37
  ...options,
29
- platform: options?.platform ?? defaultOptions().platform
38
+ platform: options?.platform ?? defaultOptions().platform,
39
+ layoutMap: options?.layoutMap ?? (namesPhysicalKey(hotkey) ? layoutMap.value ?? void 0 : void 0)
30
40
  }));
31
41
  }
32
42
 
@@ -25,4 +25,4 @@ import { Ref } from "vue";
25
25
  * </template>
26
26
  * ```
27
27
  */
28
- export declare function useHeldKeyCodes(): Ref<Record<string, string>>;
28
+ export declare function useHeldKeyCodes(): Readonly<Ref<Record<string, string>>>;
@@ -5,7 +5,7 @@ import { Ref } from "vue";
5
5
  * This composable subscribes to the global KeyStateTracker and updates whenever keys are pressed
6
6
  * or released.
7
7
  *
8
- * @returns Reactive ref containing array of currently held key names
8
+ * @returns Read-only reactive ref containing array of currently held key names
9
9
  *
10
10
  * @example
11
11
  * ```vue
@@ -22,4 +22,4 @@ import { Ref } from "vue";
22
22
  * </template>
23
23
  * ```
24
24
  */
25
- export declare function useHeldKeys(): Ref<Array<string>>;
25
+ export declare function useHeldKeys(): Readonly<Ref<Array<string>>>;
@@ -8,7 +8,7 @@ import { getKeyStateTracker } from "@tanstack/hotkeys";
8
8
  * This composable subscribes to the global KeyStateTracker and updates whenever keys are pressed
9
9
  * or released.
10
10
  *
11
- * @returns Reactive ref containing array of currently held key names
11
+ * @returns Read-only reactive ref containing array of currently held key names
12
12
  *
13
13
  * @example
14
14
  * ```vue
@@ -44,6 +44,6 @@ export type UseAppHotkeyOptions<T extends AppHotkey = AppHotkey> = WithReactiveE
44
44
  * useHotkey('Escape', () => close(), { enabled: isOpen })
45
45
  * ```
46
46
  */
47
- export declare function useHotkey<T extends AppHotkey>(hotkey: MaybeRefOrGetter<T | null | undefined>, callback: HotkeyCallback, options?: MaybeRefOrGetter<UseAppHotkeyOptions<T>>): void;
48
47
  export declare function useHotkey<const T extends RegisterableHotkey>(hotkey: MaybeRefOrGetter<T | null | undefined>, callback: HotkeyCallback<HotkeyCallbackContextFor<T>>, options?: MaybeRefOrGetter<UseHotkeyOptionsFor<T>>): void;
48
+ export declare function useHotkey<T extends AppHotkey>(hotkey: MaybeRefOrGetter<T | null | undefined>, callback: HotkeyCallback, options?: MaybeRefOrGetter<UseAppHotkeyOptions<T>>): void;
49
49
  export {};
@@ -12,6 +12,7 @@ import { Ref } from "vue";
12
12
  * ```vue
13
13
  * <script setup>
14
14
  * const registrations = useHotkeyRegistrations()
15
+ * const formatForDisplay = useFormatForDisplay()
15
16
  * </script>
16
17
  * <template>
17
18
  * <li v-for="reg in registrations" :key="reg.id">
@@ -14,6 +14,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
14
14
  * ```vue
15
15
  * <script setup>
16
16
  * const registrations = useHotkeyRegistrations()
17
+ * const formatForDisplay = useFormatForDisplay()
17
18
  * <\/script>
18
19
  * <template>
19
20
  * <li v-for="reg in registrations" :key="reg.id">
@@ -20,4 +20,4 @@ import { Ref } from "vue";
20
20
  * </template>
21
21
  * ```
22
22
  */
23
- export declare function useHotkeyScopesState(): Ref<HotkeyScopesState>;
23
+ export declare function useHotkeyScopesState(): Readonly<Ref<HotkeyScopesState>>;
@@ -1,13 +1,14 @@
1
1
  import { IndividualKey } from "@tanstack/hotkeys";
2
- import { MaybeRefOrGetter, Ref } from "vue";
2
+ import { ComputedRef, MaybeRefOrGetter } from "vue";
3
3
  /**
4
4
  * Vue composable that returns a reactive ref indicating whether a specific key is currently being held.
5
5
  *
6
6
  * Reads the held keys from the global KeyStateTracker. The key may be a ref
7
- * or getter; the result follows it without waiting for a key event.
7
+ * or getter; the result follows it without waiting for a key event. Pressing
8
+ * or releasing another key doesn't notify anything that reads the ref.
8
9
  *
9
10
  * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
10
- * @returns Reactive ref that is true if the key is currently held down
11
+ * @returns Read-only reactive ref that is true if the key is currently held down
11
12
  *
12
13
  * @example
13
14
  * ```vue
@@ -43,4 +44,4 @@ import { MaybeRefOrGetter, Ref } from "vue";
43
44
  * </template>
44
45
  * ```
45
46
  */
46
- export declare function useKeyHold(key: MaybeRefOrGetter<IndividualKey>): Ref<boolean>;
47
+ export declare function useKeyHold(key: MaybeRefOrGetter<IndividualKey>): ComputedRef<boolean>;
@@ -1,16 +1,16 @@
1
- import { useHeldKeys } from "./useHeldKeys.js";
2
1
  import { getKeyStateTracker } from "@tanstack/hotkeys";
3
- import { computed, toValue } from "vue";
2
+ import { computed, onScopeDispose, shallowRef, toValue } from "vue";
4
3
 
5
4
  // src/useKeyHold.ts
6
5
  /**
7
6
  * Vue composable that returns a reactive ref indicating whether a specific key is currently being held.
8
7
  *
9
8
  * Reads the held keys from the global KeyStateTracker. The key may be a ref
10
- * or getter; the result follows it without waiting for a key event.
9
+ * or getter; the result follows it without waiting for a key event. Pressing
10
+ * or releasing another key doesn't notify anything that reads the ref.
11
11
  *
12
12
  * @param key - The key to check (e.g., 'Shift', 'Control', 'A')
13
- * @returns Reactive ref that is true if the key is currently held down
13
+ * @returns Read-only reactive ref that is true if the key is currently held down
14
14
  *
15
15
  * @example
16
16
  * ```vue
@@ -47,10 +47,19 @@ import { computed, toValue } from "vue";
47
47
  * ```
48
48
  */
49
49
  function useKeyHold(key) {
50
- const heldKeys = useHeldKeys();
50
+ const tracker = getKeyStateTracker();
51
+ const isHeld = () => tracker.isKeyHeld(toValue(key));
52
+ const changes = shallowRef(0);
53
+ let last = isHeld();
54
+ if (typeof document !== "undefined") {
55
+ const { unsubscribe } = tracker.store.subscribe(() => {
56
+ if (isHeld() !== last) changes.value++;
57
+ });
58
+ onScopeDispose(unsubscribe);
59
+ }
51
60
  return computed(() => {
52
- heldKeys.value;
53
- return getKeyStateTracker().isKeyHeld(toValue(key));
61
+ changes.value;
62
+ return last = isHeld();
54
63
  });
55
64
  }
56
65
 
@@ -0,0 +1,33 @@
1
+ import { KeyboardLayoutMap } from "@tanstack/hotkeys";
2
+ import { Ref } from "vue";
3
+ /**
4
+ * Vue composable that returns a ref of the browser's keyboard layout, or
5
+ * `null` until it loads and in browsers that don't report one. The layout maps
6
+ * each physical code to the character it types, such as `KeyQ` to `a` on a
7
+ * French AZERTY keyboard. Only Chromium browsers report one.
8
+ *
9
+ * The layout loads after the first component that uses this composable
10
+ * mounts, so server-rendered output hydrates unchanged. `useFormatForDisplay`
11
+ * uses this composable, so you only need it for labels you draw yourself.
12
+ *
13
+ * To never ask the browser for the layout, set `keyboardLayout: false` in
14
+ * `HotkeysProvider`'s `defaultOptions`. The ref then holds `null`.
15
+ *
16
+ * @returns Read-only reactive ref of the keyboard layout, or `null`
17
+ *
18
+ * @example
19
+ * ```vue
20
+ * <script setup lang="ts">
21
+ * import { useKeyboardLayout } from '@tanstack/vue-hotkeys'
22
+ *
23
+ * const layoutMap = useKeyboardLayout()
24
+ * const label = (code: string) =>
25
+ * (layoutMap.value?.get(code) ?? code).toUpperCase()
26
+ * </script>
27
+ *
28
+ * <template>
29
+ * <kbd>{{ label('KeyW') }}</kbd>
30
+ * </template>
31
+ * ```
32
+ */
33
+ export declare function useKeyboardLayout(): Readonly<Ref<KeyboardLayoutMap | null>>;
@@ -0,0 +1,65 @@
1
+ import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
+ import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
3
+ import { getCurrentInstance, onMounted, shallowReadonly, shallowRef, watch } from "vue";
4
+
5
+ // src/useKeyboardLayout.ts
6
+ /**
7
+ * Vue composable that returns a ref of the browser's keyboard layout, or
8
+ * `null` until it loads and in browsers that don't report one. The layout maps
9
+ * each physical code to the character it types, such as `KeyQ` to `a` on a
10
+ * French AZERTY keyboard. Only Chromium browsers report one.
11
+ *
12
+ * The layout loads after the first component that uses this composable
13
+ * mounts, so server-rendered output hydrates unchanged. `useFormatForDisplay`
14
+ * uses this composable, so you only need it for labels you draw yourself.
15
+ *
16
+ * To never ask the browser for the layout, set `keyboardLayout: false` in
17
+ * `HotkeysProvider`'s `defaultOptions`. The ref then holds `null`.
18
+ *
19
+ * @returns Read-only reactive ref of the keyboard layout, or `null`
20
+ *
21
+ * @example
22
+ * ```vue
23
+ * <script setup lang="ts">
24
+ * import { useKeyboardLayout } from '@tanstack/vue-hotkeys'
25
+ *
26
+ * const layoutMap = useKeyboardLayout()
27
+ * const label = (code: string) =>
28
+ * (layoutMap.value?.get(code) ?? code).toUpperCase()
29
+ * <\/script>
30
+ *
31
+ * <template>
32
+ * <kbd>{{ label('KeyW') }}</kbd>
33
+ * </template>
34
+ * ```
35
+ */
36
+ function useKeyboardLayout() {
37
+ const layout = shallowRef(null);
38
+ if (typeof document === "undefined") return shallowReadonly(layout);
39
+ const defaultOptions = useDefaultHotkeysOptionsSource();
40
+ const enabled = () => defaultOptions().keyboardLayout !== false;
41
+ const store = getKeyboardLayoutStore();
42
+ const follow = () => {
43
+ watch(enabled, (on, _, onCleanup) => {
44
+ layout.value = on ? store.get() : null;
45
+ if (!on) return;
46
+ const stop = loadKeyboardLayout();
47
+ const { unsubscribe } = store.subscribe((next) => {
48
+ layout.value = next;
49
+ });
50
+ onCleanup(() => {
51
+ unsubscribe();
52
+ stop();
53
+ });
54
+ }, { immediate: true });
55
+ };
56
+ const instance = getCurrentInstance();
57
+ if (!instance || instance.isMounted) {
58
+ follow();
59
+ return shallowReadonly(layout);
60
+ }
61
+ onMounted(follow);
62
+ return shallowReadonly(layout);
63
+ }
64
+
65
+ export { useKeyboardLayout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/vue-hotkeys",
3
- "version": "0.22.1",
3
+ "version": "0.24.0",
4
4
  "description": "Vue adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "skills"
39
39
  ],
40
40
  "dependencies": {
41
- "@tanstack/hotkeys": "0.27.1"
41
+ "@tanstack/hotkeys": "0.29.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@tanstack/hotkeys-test-utils": "0.0.0",
@@ -33,7 +33,7 @@ Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack
33
33
 
34
34
  ```vue
35
35
  <script setup lang="ts">
36
- import { formatForDisplay, useHotkey } from '@tanstack/vue-hotkeys'
36
+ import { useFormatForDisplay, useHotkey } from '@tanstack/vue-hotkeys'
37
37
  import { ref } from 'vue'
38
38
 
39
39
  const isDirty = ref(false)
@@ -41,6 +41,7 @@ const panel = ref<HTMLDivElement | null>(null)
41
41
 
42
42
  useHotkey('Mod+S', () => save(), { enabled: isDirty })
43
43
  useHotkey('Escape', () => closePanel(), { target: panel })
44
+ const formatForDisplay = useFormatForDisplay()
44
45
  </script>
45
46
 
46
47
  <template>
@@ -52,27 +53,27 @@ useHotkey('Escape', () => closePanel(), { target: panel })
52
53
 
53
54
  ## API
54
55
 
55
- | API | What it's for |
56
- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
57
- | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted; the hotkey and options can be refs or getters. |
58
- | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a ref or getter of a list built from data, in one call. |
59
- | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for its children, and emit `custom-hotkeys-change`. |
60
- | `provideHotkeysContext(options)` | Provide the same options from a component's setup instead of rendering `HotkeysProvider`; they apply to its children. |
61
- | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
62
- | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
63
- | `useFormatForDisplay()` | Get `formatForDisplay` with the provider's `platform` filled in, for labels in server-rendered apps. Call it in `setup`. |
64
- | `useAppHotkeys()` | Get a computed ref of the declared app hotkeys with the user's custom hotkeys applied. |
65
- | `useAppHotkeyList()` | Get a computed ref listing every app hotkey with its status, for a cheat sheet or settings screen. |
66
- | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
67
- | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses; destructure the refs it returns. |
68
- | `useHotkeyRegistrations()` | Get a ref listing every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
69
- | `useHotkeyScopesState()` | Get a ref of the active scopes and the exclusive scope that owns the keyboard. |
70
- | `useHotkeyHint(hotkey, options?)` | Get a computed ref of 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. |
71
- | `useKeyHold(key)` | Get a ref of whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
72
- | `useHeldKeys()` | Get a ref of every key that's held right now. |
73
- | `useHeldKeyCodes()` | Get a ref mapping each held key to its physical `event.code`. |
56
+ | API | What it's for |
57
+ | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
58
+ | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted; the hotkey and options can be refs or getters. |
59
+ | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a ref or getter of a list built from data, in one call. |
60
+ | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for its children, and emit `custom-hotkeys-change`. |
61
+ | `provideHotkeysContext(options)` | Provide the same options from a component's setup instead of rendering `HotkeysProvider`; they apply to its children. |
62
+ | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
63
+ | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
64
+ | `useFormatForDisplay()` | Get the function that formats a hotkey as a label, with the provider's `platform` and the user's keyboard layout filled in. Only Chromium browsers report a layout, so physical keys keep their US labels in Firefox and Safari. Use it for every label in a component: `const formatForDisplay = useFormatForDisplay()` in `setup`, then `formatForDisplay(hotkey)`. |
65
+ | `useAppHotkeys()` | Get a computed ref of the declared app hotkeys with the user's custom hotkeys applied. |
66
+ | `useAppHotkeyList()` | Get a computed ref listing every app hotkey with its status, for a cheat sheet or settings screen. |
67
+ | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
68
+ | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses; destructure the refs it returns. |
69
+ | `useHotkeyRegistrations()` | Get a ref listing every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
70
+ | `useHotkeyScopesState()` | Get a ref of the active scopes and the exclusive scope that owns the keyboard. |
71
+ | `useHotkeyHint(hotkey, options?)` | Get a computed ref of 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. |
72
+ | `useKeyHold(key)` | Get a ref of whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
73
+ | `useHeldKeys()` | Get a ref of every key that's held right now. |
74
+ | `useHeldKeyCodes()` | Get a ref mapping each held key to its physical `event.code`. |
74
75
 
75
- The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
76
+ The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `useFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`.
76
77
 
77
78
  No provider is required. Call the composables synchronously in setup.
78
79
 
@@ -84,7 +85,7 @@ No provider is required. Call the composables synchronously in setup.
84
85
  - **Destructure the recorder.** Templates unwrap only top-level refs, so `v-if="recorder.isRecording"` is always true. Use `const { isRecording, startRecording } = useHotkeyRecorder(...)`.
85
86
  - **The callback isn't tracked.** Read refs and props inside it.
86
87
  - **Call the composables directly.** A `useShortcut(keys: string)` wrapper has to cast strings to hotkeys, which turns off type checking and drops sequences, reactive options, and app hotkeys.
87
- - **Server-rendered labels need the user's platform.** On the server, `formatForDisplay` can't see the user's OS, so it renders Linux labels that differ after hydration for Mac and Windows users. Set the provider's `defaultOptions.platform` to `detectPlatform(userAgent)` from the request's `User-Agent` header, and format labels with `const format = useFormatForDisplay()` in `setup`, then `format(hotkey)`. Keep plain `formatForDisplay` for labels that render only in the browser, when no provider sets `platform`.
88
+ - **Server-rendered labels need the user's platform.** The server can't see the user's OS, so without a platform it renders Linux labels that differ after hydration for Mac and Windows users. Set the provider's `defaultOptions.platform` to `detectPlatform(userAgent)` from the request's `User-Agent` header. `useFormatForDisplay()` reads it.
88
89
 
89
90
  When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Vue](references/app-hotkeys.md).
90
91
 
@@ -49,26 +49,27 @@ A settings row records with `useHotkeyRecorder` and saves through `useCustomHotk
49
49
  ```vue
50
50
  <script setup lang="ts">
51
51
  import {
52
- formatForDisplay,
53
52
  useAppHotkeys,
54
53
  useCustomHotkeys,
54
+ useFormatForDisplay,
55
55
  useHotkeyRecorder,
56
56
  } from '@tanstack/vue-hotkeys'
57
57
  import { computed } from 'vue'
58
58
  import type { AppHotkeyId } from '@tanstack/vue-hotkeys'
59
59
 
60
60
  const props = defineProps<{ id: AppHotkeyId }>()
61
+ const formatForDisplay = useFormatForDisplay()
61
62
  const appHotkeys = useAppHotkeys()
62
63
  const appHotkey = computed(() => appHotkeys.value[props.id])
63
64
  const { findConflicts, setCustomHotkey, resetCustomHotkey } = useCustomHotkeys()
64
65
 
65
66
  const { startRecording } = useHotkeyRecorder({
66
67
  maxSteps: Infinity,
67
- validate: (hotkey) => {
68
- const [conflict] = findConflicts(props.id, hotkey)
68
+ validate: (hotkey, context) => {
69
+ const [conflict] = findConflicts(props.id, hotkey, context)
69
70
  return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
70
71
  },
71
- onRecord: (hotkey) => setCustomHotkey(props.id, hotkey),
72
+ onRecord: (hotkey, context) => setCustomHotkey(props.id, hotkey, context),
72
73
  onClear: () => setCustomHotkey(props.id, null),
73
74
  })
74
75
  </script>