@tanstack/octane-hotkeys 0.7.1 → 0.9.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.
@@ -29,8 +29,10 @@ export interface HotkeysProviderProps extends HotkeysProviderScopeProps {
29
29
  */
30
30
  appHotkeys?: AppHotkeys;
31
31
  /**
32
- * Starting custom hotkeys, such as saved data. Read once; the provider owns
33
- * the state afterwards. Invalid entries are ignored with a warning.
32
+ * The user's saved custom hotkeys. When the value changes, such as after
33
+ * saved data loads or the user signs in, it replaces the current custom
34
+ * hotkeys. `undefined` leaves them unchanged; `null` clears them. Invalid
35
+ * entries are ignored with a warning.
34
36
  */
35
37
  customHotkeys?: CustomHotkeys | null;
36
38
  /** Called with the custom hotkeys after every change, ready to save. */
@@ -1,7 +1,7 @@
1
1
  import { subSlot } from "./internal.js";
2
- import { AppHotkeysStore, publishAppHotkeysStore } from "@tanstack/hotkeys";
2
+ import { publishAppHotkeysStore } from "@tanstack/hotkeys";
3
3
  import { createContext, createElement, useContext, useLayoutEffect, useRef } from "octane";
4
- import { activateHotkeysProviderScope, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
4
+ import { activateHotkeysProviderScope, createAppHotkeysStore, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
5
5
 
6
6
  //#region src/HotkeysProvider.ts
7
7
  const HotkeysContext = createContext(null);
@@ -25,7 +25,7 @@ function HotkeysProvider({ children, defaultOptions, appHotkeys, customHotkeys,
25
25
  const callbackRef = useRef(onCustomHotkeysChange, subSlot(void 0, "provider-callback"));
26
26
  callbackRef.current = onCustomHotkeysChange;
27
27
  const storeRef = useRef(void 0, subSlot(void 0, "provider-store"));
28
- storeRef.current ??= appHotkeys ? new AppHotkeysStore(appHotkeys, {
28
+ storeRef.current ??= appHotkeys ? createAppHotkeysStore(appHotkeys, {
29
29
  customHotkeys,
30
30
  onCustomHotkeysChange: (custom) => callbackRef.current?.(custom)
31
31
  }) : null;
@@ -33,6 +33,9 @@ function HotkeysProvider({ children, defaultOptions, appHotkeys, customHotkeys,
33
33
  useLayoutEffect(() => {
34
34
  if (appHotkeys) store?.setAppHotkeys(appHotkeys);
35
35
  }, [store, appHotkeys], subSlot(void 0, "provider-declarations"));
36
+ useLayoutEffect(() => {
37
+ store?.setOptions({ customHotkeys });
38
+ }, [store, customHotkeys], subSlot(void 0, "provider-custom-hotkeys"));
36
39
  useLayoutEffect(() => store ? publishAppHotkeysStore(store) : void 0, [store], subSlot(void 0, "provider-publish"));
37
40
  const active = (parent?.enabled ?? true) && enabled;
38
41
  const scopeKey = getHotkeysProviderScopeKey({
package/dist/internal.js CHANGED
@@ -1,4 +1,5 @@
1
- import { Store, useSelector } from "@tanstack/octane-store";
1
+ import { useCallback, useRef, useSyncExternalStore } from "octane";
2
+ import { createStore } from "@tanstack/hotkeys/adapter";
2
3
 
3
4
  //#region src/internal.ts
4
5
  const subSlotCache = /* @__PURE__ */ new Map();
@@ -29,14 +30,34 @@ function splitSlot(args) {
29
30
  const slot = typeof tail === "symbol" ? tail : void 0;
30
31
  return [slot === void 0 ? args : args.slice(0, -1), slot];
31
32
  }
32
- const select = useSelector;
33
- const NO_APP_HOTKEYS = new Store(null);
33
+ /**
34
+ * Subscribes to a store and returns the selected value, re-rendering when it
35
+ * changes (by `Object.is`). The selector runs again only when the store's
36
+ * state or the selector itself changes.
37
+ */
38
+ function select(store, selector, slot) {
39
+ const subscribe = useCallback((onChange) => store.subscribe(onChange).unsubscribe, [store], subSlot(slot, "select:subscribe"));
40
+ const memo = useRef(null, subSlot(slot, "select:memo"));
41
+ const getSelection = () => {
42
+ const state = store.get();
43
+ const last = memo.current;
44
+ if (last && last.selector === selector && Object.is(last.state, state)) return last.selected;
45
+ memo.current = {
46
+ state,
47
+ selector,
48
+ selected: selector(state)
49
+ };
50
+ return memo.current.selected;
51
+ };
52
+ return useSyncExternalStore(subscribe, getSelection, getSelection, subSlot(slot, "select:store"));
53
+ }
54
+ const NO_APP_HOTKEYS = createStore(null);
34
55
  /**
35
56
  * Re-renders when the provider's app hotkeys or custom hotkeys change and
36
57
  * returns the provider's store.
37
58
  */
38
59
  function useAppHotkeysStore(store, slot) {
39
- select(store?.store ?? NO_APP_HOTKEYS, (value) => value, void 0, slot);
60
+ select(store?.store ?? NO_APP_HOTKEYS, (value) => value, slot);
40
61
  return store;
41
62
  }
42
63
 
@@ -3,7 +3,7 @@ import { getKeyStateTracker } from "@tanstack/hotkeys";
3
3
 
4
4
  //#region src/useHeldKeyCodes.ts
5
5
  function useHeldKeyCodes(slot) {
6
- return select(getKeyStateTracker().store, (state) => state.heldCodes, void 0, subSlot(slot, "held-codes"));
6
+ return select(getKeyStateTracker().store, (state) => state.heldCodes, subSlot(slot, "held-codes"));
7
7
  }
8
8
 
9
9
  //#endregion
@@ -3,7 +3,7 @@ import { getKeyStateTracker } from "@tanstack/hotkeys";
3
3
 
4
4
  //#region src/useHeldKeys.ts
5
5
  function useHeldKeys(slot) {
6
- return select(getKeyStateTracker().store, (state) => state.heldKeys, void 0, subSlot(slot, "held-keys"));
6
+ return select(getKeyStateTracker().store, (state) => state.heldKeys, subSlot(slot, "held-keys"));
7
7
  }
8
8
 
9
9
  //#endregion
@@ -18,8 +18,9 @@ export type UseAppHotkeyOptions<T extends AppHotkey = AppHotkey> = AdapterAppHot
18
18
  /**
19
19
  * Registers one hotkey: a chord such as `'Mod+S'`, a sequence such as
20
20
  * `['G', 'G']`, or an app hotkey from `defineAppHotkeys`. The callback and
21
- * options are refreshed after every commit.
21
+ * options are refreshed after every commit. `undefined`, `null`, or `''`
22
+ * registers nothing, for an optional shortcut.
22
23
  */
23
- export declare function useHotkey<T extends AppHotkey>(hotkey: T, callback: HotkeyCallback, options?: UseAppHotkeyOptions<T>): void;
24
- export declare function useHotkey<const T extends RegisterableHotkey>(hotkey: T, callback: HotkeyCallback<HotkeyCallbackContextFor<T>>, options?: UseHotkeyOptionsFor<T>): void;
24
+ export declare function useHotkey<T extends AppHotkey>(hotkey: T | null | undefined, callback: HotkeyCallback, options?: UseAppHotkeyOptions<T>): void;
25
+ export declare function useHotkey<const T extends RegisterableHotkey>(hotkey: T | null | undefined, callback: HotkeyCallback<HotkeyCallbackContextFor<T>>, options?: UseHotkeyOptionsFor<T>): void;
25
26
  //#endregion
@@ -5,7 +5,8 @@ import { AppHotkey, HeldModifierOptions, RegisterableHotkey } from "@tanstack/ho
5
5
  * while the hotkey can fire: some registration of it is enabled and in an
6
6
  * active scope. A sequence reveals its hint from its first step, and an app
7
7
  * hotkey matches its registrations by id and uses its effective hotkey (false
8
- * while unbound). A hotkey on an element counts only while focus is inside it.
8
+ * while unbound). No hotkey (`undefined`, `null`, or `''`) returns false. A
9
+ * hotkey on an element counts only while focus is inside it.
9
10
  */
10
- export declare function useHotkeyHint(hotkey: RegisterableHotkey | AppHotkey, options?: HeldModifierOptions): boolean;
11
+ export declare function useHotkeyHint(hotkey: RegisterableHotkey | AppHotkey | null | undefined, options?: HeldModifierOptions): boolean;
11
12
  //#endregion
@@ -8,8 +8,8 @@ function useHotkeyHint(hotkey, ...rest) {
8
8
  const store = useAppHotkeysStore(useHotkeysContext()?.appHotkeysStore ?? null, subSlot(slot, "app-hotkeys"));
9
9
  const resolved = isAppHotkey(hotkey) ? resolveAppHotkey(hotkey, store).hotkey : hotkey;
10
10
  const manager = getHotkeyManager();
11
- const active = select(manager.registrations, () => resolved !== null && manager.hasActiveRegistration(hotkey), void 0, subSlot(slot, "hint-active"));
12
- const held = select(getKeyStateTracker().store, (state) => resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, args[0]), void 0, subSlot(slot, "hint"));
11
+ const active = select(manager.registrations, () => resolved !== null && manager.hasActiveRegistration(hotkey), subSlot(slot, "hint-active"));
12
+ const held = select(getKeyStateTracker().store, (state) => resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, args[0]), subSlot(slot, "hint"));
13
13
  return active && held;
14
14
  }
15
15
 
@@ -2,13 +2,12 @@ import { select, subSlot } from "./internal.js";
2
2
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
3
3
  import { HotkeyRecorder } from "@tanstack/hotkeys";
4
4
  import { useLayoutEffect, useRef } from "octane";
5
+ import { withDefaults } from "@tanstack/hotkeys/adapter";
5
6
 
6
7
  //#region src/useHotkeyRecorder.ts
7
8
  function useHotkeyRecorder(options, slot) {
8
- const merged = {
9
- ...useDefaultHotkeysOptions().hotkeyRecorder,
10
- ...options
11
- };
9
+ const defaults = useDefaultHotkeysOptions();
10
+ const merged = withDefaults(defaults.hotkeyRecorder, options);
12
11
  const ref = useRef(null, subSlot(slot, "recorder"));
13
12
  ref.current ??= new HotkeyRecorder(merged);
14
13
  const recorder = ref.current;
@@ -17,7 +16,7 @@ function useHotkeyRecorder(options, slot) {
17
16
  }, null, subSlot(slot, "options"));
18
17
  useLayoutEffect(() => () => recorder.destroy(), [], subSlot(slot, "cleanup"));
19
18
  return {
20
- ...select(recorder.store, (value) => value, void 0, subSlot(slot, "state")),
19
+ ...select(recorder.store, (value) => value, subSlot(slot, "state")),
21
20
  startRecording: () => recorder.start(),
22
21
  stopRecording: () => recorder.stop(),
23
22
  cancelRecording: () => recorder.cancel(),
@@ -3,7 +3,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
3
3
 
4
4
  //#region src/useHotkeyRegistrations.ts
5
5
  function useHotkeyRegistrations(slot) {
6
- return { hotkeys: select(getHotkeyManager().registrations, (state) => Array.from(state.values()).map(toHotkeyRegistrationView), void 0, subSlot(slot, "hotkeys")) };
6
+ return { hotkeys: select(getHotkeyManager().registrations, (state) => Array.from(state.values()).map(toHotkeyRegistrationView), subSlot(slot, "hotkeys")) };
7
7
  }
8
8
 
9
9
  //#endregion
@@ -3,7 +3,7 @@ import { getHotkeyManager } from "@tanstack/hotkeys";
3
3
 
4
4
  //#region src/useHotkeyScopesState.ts
5
5
  function useHotkeyScopesState(slot) {
6
- return select(getHotkeyManager().scopes, (state) => state, void 0, subSlot(slot, "hotkey-scopes-state"));
6
+ return select(getHotkeyManager().scopes, (state) => state, subSlot(slot, "hotkey-scopes-state"));
7
7
  }
8
8
 
9
9
  //#endregion
@@ -3,7 +3,7 @@ import { getKeyStateTracker } from "@tanstack/hotkeys";
3
3
 
4
4
  //#region src/useKeyHold.ts
5
5
  function useKeyHold(key, slot) {
6
- return select(getKeyStateTracker().store, (state) => state.heldKeys.includes(key), void 0, subSlot(slot, "key-hold"));
6
+ return select(getKeyStateTracker().store, (state) => state.heldKeys.includes(key), subSlot(slot, "key-hold"));
7
7
  }
8
8
 
9
9
  //#endregion
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/octane-hotkeys",
3
- "version": "0.7.1",
3
+ "version": "0.9.0",
4
4
  "description": "Octane adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -35,8 +35,7 @@
35
35
  "skills"
36
36
  ],
37
37
  "dependencies": {
38
- "@tanstack/hotkeys": "0.19.0",
39
- "@tanstack/octane-store": "^0.13.0"
38
+ "@tanstack/hotkeys": "0.21.0"
40
39
  },
41
40
  "devDependencies": {
42
41
  "@tsrx/oxc": "0.20.0",