@tanstack/preact-hotkeys 0.23.0 → 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,6 +1,6 @@
1
1
  import { UseHotkeyCommonOptions } from "./useHotkey.js";
2
- import { AppHotkeys, AppHotkeysStore, CustomHotkeys, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
3
- import { HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
2
+ import { AppHotkeys, CustomHotkeys, HotkeyPlatform } from "@tanstack/hotkeys";
3
+ import { HotkeyRecorderDefaults, HotkeysContextValue, HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
4
4
  import { ComponentChildren } from "preact";
5
5
  export interface HotkeysProviderOptions {
6
6
  /**
@@ -24,15 +24,9 @@ export interface HotkeysProviderOptions {
24
24
  */
25
25
  hotkey?: UseHotkeyCommonOptions;
26
26
  /** Defaults for every `useHotkeyRecorder`. */
27
- hotkeyRecorder?: Partial<Omit<HotkeyChordRecorderOptions & HotkeySequenceRecorderOptions, 'maxSteps' | 'onRecord' | 'validate'>>;
28
- }
29
- interface HotkeysContextValue {
30
- appHotkeysStore: AppHotkeysStore | null;
31
- /** Merged with every ancestor provider's defaults, including `hotkey.scope`. */
32
- defaultOptions: HotkeysProviderOptions;
33
- /** Whether this provider and every ancestor are enabled. */
34
- enabled: boolean;
27
+ hotkeyRecorder?: HotkeyRecorderDefaults;
35
28
  }
29
+ type HotkeysContextValue$1 = HotkeysContextValue<HotkeysProviderOptions>;
36
30
  export interface HotkeysProviderProps<T extends AppHotkeys = AppHotkeys> extends HotkeysProviderScopeProps {
37
31
  children: ComponentChildren;
38
32
  /**
@@ -85,6 +79,6 @@ export interface HotkeysProviderProps<T extends AppHotkeys = AppHotkeys> extends
85
79
  * ```
86
80
  */
87
81
  export declare function HotkeysProvider<T extends AppHotkeys = AppHotkeys>({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions, scope, exclusive, enabled }: HotkeysProviderProps<T>): import("preact").JSX.Element;
88
- export declare function useHotkeysContext(): HotkeysContextValue | null;
82
+ export declare function useHotkeysContext(): HotkeysContextValue$1 | null;
89
83
  export declare function useDefaultHotkeysOptions(): HotkeysProviderOptions;
90
84
  export {};
@@ -1,4 +1,4 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RecordedKeyPresses, RegisteredAppHotkeys } from "@tanstack/hotkeys";
1
+ import { CustomHotkeyActions, CustomHotkeys, RegisteredAppHotkeyListItem, RegisteredAppHotkeys } from "@tanstack/hotkeys";
2
2
  /**
3
3
  * Returns the effective app hotkeys from `HotkeysProvider`, typed by
4
4
  * `Register`: each declaration with the user's custom hotkey applied.
@@ -35,27 +35,11 @@ export declare function useAppHotkeys(): RegisteredAppHotkeys;
35
35
  * }
36
36
  * ```
37
37
  */
38
- export declare function useAppHotkeyList(): Array<AppHotkeyListItem<RegisteredAppHotkeys[keyof RegisteredAppHotkeys]>>;
38
+ export declare function useAppHotkeyList(): Array<RegisteredAppHotkeyListItem>;
39
39
  /** Custom hotkey state and actions returned by `useCustomHotkeys`. */
40
- export interface UseCustomHotkeysResult {
40
+ export interface UseCustomHotkeysResult extends CustomHotkeyActions {
41
41
  /** The user's hotkeys that differ from the defaults. */
42
42
  customHotkeys: CustomHotkeys;
43
- /**
44
- * Sets the user's hotkey for an app hotkey. `null` unbinds it.
45
- * Returns conflicting app hotkeys; when any exist, nothing changes. Pass
46
- * the recorder's `context` for a recorded hotkey, as for `findConflicts`.
47
- */
48
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
49
- /** Restores the default hotkey for an app hotkey. */
50
- resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
51
- /** Restores every default hotkey. */
52
- resetCustomHotkeys: () => void;
53
- /**
54
- * Other app hotkeys that would collide if `id` used `hotkey`. Pass the
55
- * recorder's `context` for a recorded hotkey: it has the key press, which
56
- * also finds a hotkey written differently that the same press fires.
57
- */
58
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
59
43
  }
60
44
  /**
61
45
  * Reads and changes the user's custom hotkeys. Changes apply to every
@@ -1,11 +1,10 @@
1
1
  import { useAppHotkeysStoreState } from "./_appHotkeysStore.js";
2
2
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
3
+ import { bindCustomHotkeyActions, requireAppHotkeysStore } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/useAppHotkeys.ts
5
6
  function useRequiredStore(hook) {
6
- const store = useAppHotkeysStoreState();
7
- if (!store) throw new Error(`[hotkeys] ${hook} requires a HotkeysProvider with an appHotkeys prop`);
8
- return store;
7
+ return requireAppHotkeysStore(useAppHotkeysStoreState(), `${hook} requires a HotkeysProvider with an appHotkeys prop`);
9
8
  }
10
9
  /**
11
10
  * Returns the effective app hotkeys from `HotkeysProvider`, typed by
@@ -71,10 +70,7 @@ function useCustomHotkeys() {
71
70
  const store = useRequiredStore("useCustomHotkeys");
72
71
  return {
73
72
  customHotkeys: store.customHotkeys,
74
- setCustomHotkey: (id, hotkey, recordedWith) => store.setCustomHotkey(id, hotkey, recordedWith),
75
- resetCustomHotkey: (id) => store.resetCustomHotkey(id),
76
- resetCustomHotkeys: () => store.resetCustomHotkeys(),
77
- findConflicts: (id, hotkey, recordedWith) => store.findConflicts(id, hotkey, recordedWith)
73
+ ...bindCustomHotkeyActions(() => store)
78
74
  };
79
75
  }
80
76
 
@@ -1,7 +1,7 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
2
  import { useKeyboardLayout } from "./useKeyboardLayout.js";
3
- import { formatForDisplay } from "@tanstack/hotkeys";
4
- import { useCallback } from "preact/hooks";
3
+ import { createFormatForDisplay } from "@tanstack/hotkeys/adapter";
4
+ import { useMemo } from "preact/hooks";
5
5
 
6
6
  // src/useFormatForDisplay.ts
7
7
  /**
@@ -36,11 +36,7 @@ import { useCallback } from "preact/hooks";
36
36
  function useFormatForDisplay() {
37
37
  const platform = useDefaultHotkeysOptions().platform;
38
38
  const layoutMap = useKeyboardLayout();
39
- return useCallback((hotkey, options) => formatForDisplay(hotkey, {
40
- ...options,
41
- platform: options?.platform ?? platform,
42
- layoutMap: options?.layoutMap ?? layoutMap ?? void 0
43
- }), [platform, layoutMap]);
39
+ return useMemo(() => createFormatForDisplay(() => platform, () => layoutMap), [platform, layoutMap]);
44
40
  }
45
41
 
46
42
  export { useFormatForDisplay };
@@ -1,11 +1,11 @@
1
1
  import { AppHotkey, HotkeyCallback, HotkeyCallbackContextFor, RegisterableHotkey } from "@tanstack/hotkeys";
2
- import { AdapterAppHotkeyOptions, AdapterChordOptions, AdapterCommonOptions, AdapterHotkeyOptions, AdapterOptionsFor, AdapterSequenceOptions } from "@tanstack/hotkeys/adapter";
2
+ import { AdapterAppHotkeyOptions, AdapterChordOptions, AdapterCommonOptions, AdapterHotkeyOptions, AdapterOptionsFor, AdapterSequenceOptions, RefTarget } from "@tanstack/hotkeys/adapter";
3
3
  import { RefObject } from "preact";
4
4
  /**
5
5
  * A Preact ref, element, document, or window to listen on. A `null` or
6
6
  * `undefined` target, or a ref that is still unset, waits for the element.
7
7
  */
8
- export type UseHotkeyTarget = RefObject<HTMLElement | null | undefined> | HTMLElement | Document | Window | null;
8
+ export type UseHotkeyTarget = RefTarget<RefObject<HTMLElement | null | undefined>>;
9
9
  /** Options shared by chords and sequences. */
10
10
  export type UseHotkeyOptions = AdapterHotkeyOptions<UseHotkeyTarget>;
11
11
  /** Options for a chord such as `'Mod+S'`. */
@@ -1,20 +1,6 @@
1
- import { Hotkey, HotkeyChord, HotkeyChordRecorderOptions, HotkeySequence, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
2
- export interface PreactHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
3
- /** Whether recording is currently active */
4
- isRecording: boolean;
5
- /** Chords recorded so far in a multi-step recording (for live preview) */
6
- steps: HotkeySequence;
7
- /** The last committed hotkey */
8
- recordedHotkey: THotkey | null;
9
- /** Start recording a new hotkey */
10
- startRecording: () => void;
11
- /** Stop recording without calling onCancel */
12
- stopRecording: () => void;
13
- /** Cancel recording and call onCancel */
14
- cancelRecording: () => void;
15
- /** Commit the recorded steps now (multi-step recordings) */
16
- commitRecording: () => void;
17
- }
1
+ import { Hotkey, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderActions, HotkeyRecorderState, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
2
+ /** The recorder state and controls returned by `useHotkeyRecorder`. */
3
+ export interface PreactHotkeyRecorder<THotkey extends Hotkey = Hotkey> extends HotkeyRecorderState<THotkey>, HotkeyRecorderActions {}
18
4
  /**
19
5
  * Preact hook for recording keyboard shortcuts.
20
6
  *
@@ -1,5 +1,6 @@
1
1
  import { useSelector } from "./_useSelector.js";
2
- import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
2
+ import { getHotkeyManager } from "@tanstack/hotkeys";
3
+ import { getHotkeyRegistrationViews } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/useHotkeyRegistrations.ts
5
6
  /**
@@ -30,7 +31,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
30
31
  * ```
31
32
  */
32
33
  function useHotkeyRegistrations() {
33
- return useSelector(getHotkeyManager().registrations, (state) => Array.from(state.values()).map(toHotkeyRegistrationView));
34
+ return useSelector(getHotkeyManager().registrations, (state) => getHotkeyRegistrationViews(state));
34
35
  }
35
36
 
36
37
  export { useHotkeyRegistrations };
@@ -1,7 +1,6 @@
1
1
  import { useHotkeysContext } from "./HotkeysProvider.js";
2
2
  import { useAppHotkeysStoreState } from "./_appHotkeysStore.js";
3
- import { resolveTarget } from "./utils.js";
4
- import { createHotkeyBindings, prepareHotkeyDefinitions } from "@tanstack/hotkeys/adapter";
3
+ import { createHotkeyBindings, prepareHotkeyDefinitions, resolveRefTarget } from "@tanstack/hotkeys/adapter";
5
4
  import { useLayoutEffect, useRef } from "preact/hooks";
6
5
 
7
6
  // src/useHotkeys.ts
@@ -67,7 +66,7 @@ function useHotkeys(hotkeys, commonOptions = {}) {
67
66
  const { hotkeys, ...context } = latestRef.current;
68
67
  bindingsRef.current.update(prepareHotkeyDefinitions(hotkeys, {
69
68
  ...context,
70
- resolveTarget
69
+ resolveTarget: resolveRefTarget
71
70
  }));
72
71
  });
73
72
  useLayoutEffect(() => {
@@ -1,5 +1,6 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
- import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
2
+ import { getKeyboardLayoutStore } from "@tanstack/hotkeys";
3
+ import { followKeyboardLayout } from "@tanstack/hotkeys/adapter";
3
4
  import { useLayoutEffect, useState } from "preact/hooks";
4
5
 
5
6
  // src/useKeyboardLayout.ts
@@ -31,14 +32,9 @@ function useKeyboardLayout() {
31
32
  const [layoutMap, setLayoutMap] = useState(null);
32
33
  useLayoutEffect(() => {
33
34
  if (!enabled) return;
34
- const store = getKeyboardLayoutStore();
35
- setLayoutMap(store.get());
36
- const { unsubscribe } = store.subscribe(setLayoutMap);
37
- const stop = loadKeyboardLayout();
38
- return () => {
39
- unsubscribe();
40
- stop();
41
- };
35
+ const stop = followKeyboardLayout(setLayoutMap);
36
+ setLayoutMap(getKeyboardLayoutStore().get());
37
+ return stop;
42
38
  }, [enabled]);
43
39
  return enabled ? layoutMap : null;
44
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-hotkeys",
3
- "version": "0.23.0",
3
+ "version": "0.24.0",
4
4
  "description": "Preact adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -35,7 +35,7 @@
35
35
  "skills"
36
36
  ],
37
37
  "dependencies": {
38
- "@tanstack/hotkeys": "0.29.0"
38
+ "@tanstack/hotkeys": "0.31.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@preact/preset-vite": "^2.10.6",
package/dist/utils.js DELETED
@@ -1,16 +0,0 @@
1
- // src/utils.ts
2
- /**
3
- * Type guard to check if a value is a Preact ref-like object.
4
- */
5
- function isRef(value) {
6
- return value !== null && typeof value === "object" && "current" in value;
7
- }
8
- /**
9
- * Resolves a hook target. A null target, or a ref that is still unset, skips
10
- * registration. Omit the target to listen on the document.
11
- */
12
- function resolveTarget(target) {
13
- return isRef(target) ? target.current : target;
14
- }
15
-
16
- export { resolveTarget };