@tanstack/vue-hotkeys 0.19.0 → 0.20.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.
@@ -11,7 +11,8 @@ import { HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
11
11
  *
12
12
  * Nest providers to create hotkey scopes. A nested provider inherits app
13
13
  * hotkeys and merges default options. With `scope`, its hotkeys fire only
14
- * while it is mounted and enabled; with `exclusive`, only its hotkeys fire.
14
+ * while it is mounted, enabled, and not hidden by `<KeepAlive>`; with
15
+ * `exclusive`, only its hotkeys fire.
15
16
  *
16
17
  * @example
17
18
  * ```vue
@@ -10,7 +10,8 @@ import { defineComponent } from "vue";
10
10
  *
11
11
  * Nest providers to create hotkey scopes. A nested provider inherits app
12
12
  * hotkeys and merges default options. With `scope`, its hotkeys fire only
13
- * while it is mounted and enabled; with `exclusive`, only its hotkeys fire.
13
+ * while it is mounted, enabled, and not hidden by `<KeepAlive>`; with
14
+ * `exclusive`, only its hotkeys fire.
14
15
  *
15
16
  * @example
16
17
  * ```vue
@@ -51,8 +51,8 @@ export interface ProvideHotkeysContextOptions {
51
51
  *
52
52
  * Nest contexts to create hotkey scopes. A nested context inherits app
53
53
  * hotkeys and merges default options. With `scope`, its hotkeys fire only
54
- * while the component is mounted and enabled; with `exclusive`, only its
55
- * hotkeys fire.
54
+ * while the component is mounted, enabled, and not hidden by `<KeepAlive>`;
55
+ * with `exclusive`, only its hotkeys fire.
56
56
  *
57
57
  * @example
58
58
  * ```ts
@@ -1,3 +1,4 @@
1
+ import { useLive } from "./_useLive.js";
1
2
  import { publishAppHotkeysStore } from "@tanstack/hotkeys";
2
3
  import { computed, hasInjectionContext, inject, onMounted, onScopeDispose, provide, toValue, watch } from "vue";
3
4
  import { activateHotkeysProviderScope, createAppHotkeysStore, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
@@ -12,8 +13,8 @@ const DEFAULT_OPTIONS = {};
12
13
  *
13
14
  * Nest contexts to create hotkey scopes. A nested context inherits app
14
15
  * hotkeys and merges default options. With `scope`, its hotkeys fire only
15
- * while the component is mounted and enabled; with `exclusive`, only its
16
- * hotkeys fire.
16
+ * while the component is mounted, enabled, and not hidden by `<KeepAlive>`;
17
+ * with `exclusive`, only its hotkeys fire.
17
18
  *
18
19
  * @example
19
20
  * ```ts
@@ -39,7 +40,8 @@ function provideHotkeysContext(options = {}) {
39
40
  watch(() => toValue(options.customHotkeys), (customHotkeys) => store.setOptions({ customHotkeys }), { deep: true });
40
41
  if (typeof document !== "undefined") onScopeDispose(publishAppHotkeysStore(store));
41
42
  }
42
- const active = computed(() => (parent?.enabled ?? true) && toValue(options.enabled) !== false);
43
+ const live = useLive();
44
+ const active = computed(() => live.value && (parent?.enabled ?? true) && toValue(options.enabled) !== false);
43
45
  const defaultOptions = computed(() => mergeHotkeysProviderDefaults(parent?.defaultOptions, toValue(options.defaultOptions), toValue(options.scope)));
44
46
  const scopeKey = computed(() => getHotkeysProviderScopeKey({
45
47
  scope: toValue(options.scope),
@@ -0,0 +1,19 @@
1
+ import { getCurrentInstance, onActivated, onDeactivated, ref } from "vue";
2
+
3
+ //#region src/_useLive.ts
4
+ /**
5
+ * Whether the component is shown. A component in a page cached by
6
+ * `<KeepAlive>` is not live while the page is hidden. Always true outside a
7
+ * component, where the hooks would warn.
8
+ */
9
+ function useLive() {
10
+ const live = ref(true);
11
+ if (getCurrentInstance()) {
12
+ onActivated(() => live.value = true);
13
+ onDeactivated(() => live.value = false);
14
+ }
15
+ return live;
16
+ }
17
+
18
+ //#endregion
19
+ export { useLive };
@@ -1,7 +1,8 @@
1
+ import { useLive } from "./_useLive.js";
1
2
  import { useStoreSelector } from "./useStoreSelector.js";
2
3
  import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
3
4
  import { HotkeyRecorder } from "@tanstack/hotkeys";
4
- import { onScopeDispose, toValue, watch } from "vue";
5
+ import { getCurrentInstance, onScopeDispose, toValue, watch } from "vue";
5
6
  import { withDefaults } from "@tanstack/hotkeys/adapter";
6
7
 
7
8
  //#region src/useHotkeyRecorder.ts
@@ -13,6 +14,12 @@ function useHotkeyRecorder(options) {
13
14
  const steps = useStoreSelector(recorder.store, (state) => state.steps);
14
15
  const recordedHotkey = useStoreSelector(recorder.store, (state) => state.recordedHotkey);
15
16
  const stopWatcher = watch(resolve, (resolved) => recorder.setOptions(resolved));
17
+ const instance = getCurrentInstance();
18
+ const hidden = () => {
19
+ for (let i = instance; i; i = i.parent) if (i.isDeactivated) return true;
20
+ return false;
21
+ };
22
+ watch(useLive(), (live) => live || recorder.store.state.isRecording && recorder.stop());
16
23
  onScopeDispose(() => {
17
24
  stopWatcher();
18
25
  recorder.destroy();
@@ -21,7 +28,9 @@ function useHotkeyRecorder(options) {
21
28
  isRecording,
22
29
  steps,
23
30
  recordedHotkey,
24
- startRecording: () => recorder.start(),
31
+ startRecording: () => {
32
+ if (!hidden()) recorder.start();
33
+ },
25
34
  stopRecording: () => recorder.stop(),
26
35
  cancelRecording: () => recorder.cancel(),
27
36
  commitRecording: () => recorder.commit()
@@ -1,3 +1,4 @@
1
+ import { useLive } from "./_useLive.js";
1
2
  import { useAppHotkeysStoreSource, useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
3
  import { onScopeDispose, toValue, watch } from "vue";
3
4
  import { createHotkeyBindings, prepareHotkeyDefinitions } from "@tanstack/hotkeys/adapter";
@@ -34,7 +35,9 @@ function useHotkeys(hotkeys, commonOptions = {}) {
34
35
  const defaultOptions = useDefaultHotkeysOptionsSource();
35
36
  const appHotkeys = useAppHotkeysStoreSource();
36
37
  const bindings = createHotkeyBindings();
38
+ const live = useLive();
37
39
  const stopWatcher = watch(() => {
40
+ if (!live.value) return [];
38
41
  appHotkeys.state.value;
39
42
  const definitions = toValue(hotkeys).map((definition) => ({
40
43
  hotkey: toValue(definition.hotkey),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/vue-hotkeys",
3
- "version": "0.19.0",
3
+ "version": "0.20.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.21.0"
41
+ "@tanstack/hotkeys": "0.22.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@vitejs/plugin-vue": "^6.0.9",