@tanstack/vue-hotkeys 0.22.0 → 0.22.1

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.
@@ -35,8 +35,9 @@ export interface ProvideHotkeysContextOptions {
35
35
  /** See `HotkeysProviderScopeProps['enabled']`. */
36
36
  enabled?: MaybeRefOrGetter<HotkeysProviderScopeProps['enabled']>;
37
37
  /**
38
- * Every app hotkey, from `defineAppHotkeys`. Read when the context is
39
- * provided; later changes update the declarations.
38
+ * Every app hotkey, from `defineAppHotkeys`. It can start as `undefined` and
39
+ * arrive later, such as from a lazy import. Later changes update the
40
+ * declarations.
40
41
  */
41
42
  appHotkeys?: MaybeRefOrGetter<AppHotkeys | undefined>;
42
43
  /**
@@ -1,6 +1,6 @@
1
1
  import { useLive } from "./_useLive.js";
2
2
  import { publishAppHotkeysStore } from "@tanstack/hotkeys";
3
- import { computed, hasInjectionContext, inject, onMounted, onScopeDispose, provide, toValue, watch } from "vue";
3
+ import { computed, hasInjectionContext, inject, onMounted, provide, shallowRef, toValue, watch } from "vue";
4
4
  import { activateHotkeysProviderScope, createAppHotkeysStore, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
5
5
 
6
6
  // src/HotkeysProviderContext.ts
@@ -28,22 +28,28 @@ const DEFAULT_OPTIONS = {};
28
28
  */
29
29
  function provideHotkeysContext(options = {}) {
30
30
  const parent = inject(HotkeysContext, null);
31
- const appHotkeys = toValue(options.appHotkeys);
32
- const store = appHotkeys ? createAppHotkeysStore(appHotkeys, {
31
+ const createStore = (appHotkeys) => createAppHotkeysStore(appHotkeys, {
33
32
  customHotkeys: toValue(options.customHotkeys),
34
33
  onCustomHotkeysChange: (custom) => options.onCustomHotkeysChange?.(custom),
35
34
  platform: toValue(options.defaultOptions)?.platform ?? parent?.defaultOptions.platform
36
- }) : null;
37
- if (store) {
38
- watch(() => toValue(options.appHotkeys), (next) => {
39
- if (next) store.setAppHotkeys(next);
40
- });
41
- if (typeof document !== "undefined") onScopeDispose(publishAppHotkeysStore(store));
42
- }
35
+ });
36
+ const initial = toValue(options.appHotkeys);
37
+ const store = shallowRef(initial ? createStore(initial) : null);
38
+ watch(() => toValue(options.appHotkeys), (next) => {
39
+ if (!next) return;
40
+ if (store.value) store.value.setAppHotkeys(next);
41
+ else store.value = createStore(next);
42
+ }, { flush: "sync" });
43
+ if (typeof document !== "undefined") watch(store, (created, _, onCleanup) => {
44
+ if (created) onCleanup(publishAppHotkeysStore(created));
45
+ }, {
46
+ immediate: true,
47
+ flush: "sync"
48
+ });
43
49
  const live = useLive();
44
50
  const active = computed(() => live.value && (parent?.enabled ?? true) && toValue(options.enabled) !== false);
45
51
  const defaultOptions = computed(() => mergeHotkeysProviderDefaults(parent?.defaultOptions, toValue(options.defaultOptions), toValue(options.scope)));
46
- if (store) watch([() => toValue(options.customHotkeys), () => defaultOptions.value.platform], ([customHotkeys, platform]) => store.setOptions({
52
+ watch([() => toValue(options.customHotkeys), () => defaultOptions.value.platform], ([customHotkeys, platform]) => store.value?.setOptions({
47
53
  customHotkeys,
48
54
  platform
49
55
  }), { deep: true });
@@ -58,16 +64,17 @@ function provideHotkeysContext(options = {}) {
58
64
  if (key) onCleanup(activateHotkeysProviderScope(key));
59
65
  }, { immediate: true });
60
66
  });
61
- const contextValue = {
62
- appHotkeysStore: store ?? parent?.appHotkeysStore ?? null,
67
+ provide(HotkeysContext, {
68
+ get appHotkeysStore() {
69
+ return store.value ?? parent?.appHotkeysStore ?? null;
70
+ },
63
71
  get defaultOptions() {
64
72
  return defaultOptions.value;
65
73
  },
66
74
  get enabled() {
67
75
  return active.value;
68
76
  }
69
- };
70
- provide(HotkeysContext, contextValue);
77
+ });
71
78
  }
72
79
  /**
73
80
  * The nearest context, or `null`. Outside a component, such as in a bare
@@ -1,6 +1,5 @@
1
1
  import { useHotkeysContext } from "./HotkeysProviderContext.js";
2
- import { useStoreSelector } from "./useStoreSelector.js";
3
- import { shallowRef } from "vue";
2
+ import { shallowRef, watch } from "vue";
4
3
 
5
4
  // src/defaultOptions.ts
6
5
  /** Captures the context once and reads current defaults inside reactive computations. */
@@ -10,13 +9,29 @@ function useDefaultHotkeysOptionsSource() {
10
9
  }
11
10
  /**
12
11
  * The provider's app hotkeys store and a ref that tracks its state, so
13
- * watchers re-run when app hotkeys or custom hotkeys change.
12
+ * watchers re-run when app hotkeys or custom hotkeys change. `store` is a
13
+ * getter: a provider creates its store when its app hotkeys arrive, which can
14
+ * be after this is called.
14
15
  */
15
16
  function useAppHotkeysStoreSource() {
16
- const store = useHotkeysContext()?.appHotkeysStore ?? null;
17
+ const context = useHotkeysContext();
18
+ const state = shallowRef(context?.appHotkeysStore?.store.get() ?? null);
19
+ if (context && typeof document !== "undefined") watch(() => context.appHotkeysStore, (store, _, onCleanup) => {
20
+ state.value = store?.store.get() ?? null;
21
+ if (!store) return;
22
+ const { unsubscribe } = store.store.subscribe((value) => {
23
+ state.value = value;
24
+ });
25
+ onCleanup(unsubscribe);
26
+ }, {
27
+ immediate: true,
28
+ flush: "sync"
29
+ });
17
30
  return {
18
- store,
19
- state: store ? useStoreSelector(store.store, (value) => value) : shallowRef(null)
31
+ get store() {
32
+ return context?.appHotkeysStore ?? null;
33
+ },
34
+ state
20
35
  };
21
36
  }
22
37
 
@@ -5,9 +5,10 @@ import { computed } from "vue";
5
5
  // src/useAppHotkeys.ts
6
6
  function useRequiredStore(name) {
7
7
  const source = useAppHotkeysStoreSource();
8
- if (!source.store) throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with app hotkeys`);
8
+ const initial = source.store;
9
+ if (!initial) throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with app hotkeys`);
9
10
  return {
10
- store: source.store,
11
+ store: () => source.store ?? initial,
11
12
  state: source.state
12
13
  };
13
14
  }
@@ -25,7 +26,7 @@ function useAppHotkeys() {
25
26
  const { store, state } = useRequiredStore("useAppHotkeys");
26
27
  return computed(() => {
27
28
  state.value;
28
- return store.appHotkeys;
29
+ return store().appHotkeys;
29
30
  });
30
31
  }
31
32
  /**
@@ -38,7 +39,7 @@ function useAppHotkeyList() {
38
39
  const registrations = useHotkeyRegistrations();
39
40
  return computed(() => {
40
41
  state.value;
41
- return store.getList(registrations.value);
42
+ return store().getList(registrations.value);
42
43
  });
43
44
  }
44
45
  /**
@@ -51,12 +52,12 @@ function useCustomHotkeys() {
51
52
  return {
52
53
  customHotkeys: computed(() => {
53
54
  state.value;
54
- return store.customHotkeys;
55
+ return store().customHotkeys;
55
56
  }),
56
- setCustomHotkey: (id, hotkey) => store.setCustomHotkey(id, hotkey),
57
- resetCustomHotkey: (id) => store.resetCustomHotkey(id),
58
- resetCustomHotkeys: () => store.resetCustomHotkeys(),
59
- findConflicts: (id, hotkey) => store.findConflicts(id, hotkey)
57
+ setCustomHotkey: (id, hotkey) => store().setCustomHotkey(id, hotkey),
58
+ resetCustomHotkey: (id) => store().resetCustomHotkey(id),
59
+ resetCustomHotkeys: () => store().resetCustomHotkeys(),
60
+ findConflicts: (id, hotkey) => store().findConflicts(id, hotkey)
60
61
  };
61
62
  }
62
63
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/vue-hotkeys",
3
- "version": "0.22.0",
3
+ "version": "0.22.1",
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.0"
41
+ "@tanstack/hotkeys": "0.27.1"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@tanstack/hotkeys-test-utils": "0.0.0",