@tanstack/preact-hotkeys 0.21.1 → 0.23.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.
@@ -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
  /**
@@ -10,21 +10,23 @@ export interface HotkeysProviderOptions {
10
10
  * `detectPlatform(userAgent)`.
11
11
  */
12
12
  platform?: HotkeyPlatform;
13
+ /**
14
+ * Whether labels from `useFormatForDisplay` follow the user's keyboard
15
+ * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
16
+ * loads from the browser when the first label mounts. Only Chromium
17
+ * browsers report one. Set `false` to never ask the browser for it.
18
+ * Default: `true`.
19
+ */
20
+ keyboardLayout?: boolean;
13
21
  /**
14
22
  * Defaults for every hotkey registered by `useHotkey` and `useHotkeys`.
15
23
  * `requireReset` applies to chords and `timeout` applies to sequences.
16
24
  */
17
25
  hotkey?: UseHotkeyCommonOptions;
18
26
  /** Defaults for every `useHotkeyRecorder`. */
19
- hotkeyRecorder?: Partial<Omit<HotkeyChordRecorderOptions & HotkeySequenceRecorderOptions, 'maxSteps' | 'onRecord' | 'validate'>>;
20
- }
21
- interface HotkeysContextValue {
22
- appHotkeysStore: AppHotkeysStore | null;
23
- /** Merged with every ancestor provider's defaults, including `hotkey.scope`. */
24
- defaultOptions: HotkeysProviderOptions;
25
- /** Whether this provider and every ancestor are enabled. */
26
- enabled: boolean;
27
+ hotkeyRecorder?: HotkeyRecorderDefaults;
27
28
  }
29
+ type HotkeysContextValue$1 = HotkeysContextValue<HotkeysProviderOptions>;
28
30
  export interface HotkeysProviderProps<T extends AppHotkeys = AppHotkeys> extends HotkeysProviderScopeProps {
29
31
  children: ComponentChildren;
30
32
  /**
@@ -77,6 +79,6 @@ export interface HotkeysProviderProps<T extends AppHotkeys = AppHotkeys> extends
77
79
  * ```
78
80
  */
79
81
  export declare function HotkeysProvider<T extends AppHotkeys = AppHotkeys>({ children, appHotkeys, customHotkeys, onCustomHotkeysChange, defaultOptions, scope, exclusive, enabled }: HotkeysProviderProps<T>): import("preact").JSX.Element;
80
- export declare function useHotkeysContext(): HotkeysContextValue | null;
82
+ export declare function useHotkeysContext(): HotkeysContextValue$1 | null;
81
83
  export declare function useDefaultHotkeysOptions(): HotkeysProviderOptions;
82
84
  export {};
@@ -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
@@ -6,9 +6,11 @@ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
6
6
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
7
7
  import { useHotkeyHint } from "./useHotkeyHint.js";
8
8
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
9
+ import { formatForDisplay } from "./formatForDisplay.js";
10
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
9
11
  import { UseCustomHotkeysResult, useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
10
12
  import { useHeldKeys } from "./useHeldKeys.js";
11
13
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
12
14
  import { useKeyHold } from "./useKeyHold.js";
13
15
  export * from "@tanstack/hotkeys";
14
- export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
16
+ export { HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, PreactHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, formatForDisplay, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold, useKeyboardLayout };
package/dist/index.js CHANGED
@@ -6,6 +6,8 @@ import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
6
6
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
7
7
  import { useHotkeyHint } from "./useHotkeyHint.js";
8
8
  import { useFormatForDisplay } from "./useFormatForDisplay.js";
9
+ import { formatForDisplay } from "./formatForDisplay.js";
10
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
9
11
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
10
12
  import { useHeldKeys } from "./useHeldKeys.js";
11
13
  import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
@@ -13,4 +15,4 @@ import { useKeyHold } from "./useKeyHold.js";
13
15
 
14
16
  export * from "@tanstack/hotkeys"
15
17
 
16
- export { HotkeysProvider, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
18
+ export { HotkeysProvider, formatForDisplay, 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 { 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.
@@ -19,7 +19,9 @@ export declare function useAppHotkeys(): RegisteredAppHotkeys;
19
19
  * @example
20
20
  * ```tsx
21
21
  * function CheatSheet() {
22
+ * const formatForDisplay = useFormatForDisplay()
22
23
  * const items = useAppHotkeyList()
24
+ *
23
25
  * return (
24
26
  * <ul>
25
27
  * {items.map(({ appHotkey, status }) => (
@@ -33,22 +35,11 @@ export declare function useAppHotkeys(): RegisteredAppHotkeys;
33
35
  * }
34
36
  * ```
35
37
  */
36
- export declare function useAppHotkeyList(): Array<AppHotkeyListItem<RegisteredAppHotkeys[keyof RegisteredAppHotkeys]>>;
38
+ export declare function useAppHotkeyList(): Array<RegisteredAppHotkeyListItem>;
37
39
  /** Custom hotkey state and actions returned by `useCustomHotkeys`. */
38
- export interface UseCustomHotkeysResult {
40
+ export interface UseCustomHotkeysResult extends CustomHotkeyActions {
39
41
  /** The user's hotkeys that differ from the defaults. */
40
42
  customHotkeys: CustomHotkeys;
41
- /**
42
- * Sets the user's hotkey for an app hotkey. `null` unbinds it.
43
- * Returns conflicting app hotkeys; when any exist, nothing changes.
44
- */
45
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
46
- /** Restores the default hotkey for an app hotkey. */
47
- resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
48
- /** Restores every default hotkey. */
49
- resetCustomHotkeys: () => void;
50
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
51
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
52
43
  }
53
44
  /**
54
45
  * Reads and changes the user's custom hotkeys. Changes apply to every
@@ -60,9 +51,10 @@ export interface UseCustomHotkeysResult {
60
51
  * const { setCustomHotkey, findConflicts } = useCustomHotkeys()
61
52
  * const recorder = useHotkeyRecorder({
62
53
  * maxSteps: Infinity,
63
- * validate: (hotkey) =>
64
- * findConflicts('doc.save', hotkey).length === 0 || 'Already in use',
65
- * onRecord: (hotkey) => setCustomHotkey('doc.save', hotkey),
54
+ * validate: (hotkey, context) =>
55
+ * findConflicts('doc.save', hotkey, context).length === 0 ||
56
+ * 'Already in use',
57
+ * onRecord: (hotkey, context) => setCustomHotkey('doc.save', hotkey, context),
66
58
  * })
67
59
  * ```
68
60
  */
@@ -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
@@ -29,7 +28,9 @@ function useAppHotkeys() {
29
28
  * @example
30
29
  * ```tsx
31
30
  * function CheatSheet() {
31
+ * const formatForDisplay = useFormatForDisplay()
32
32
  * const items = useAppHotkeyList()
33
+ *
33
34
  * return (
34
35
  * <ul>
35
36
  * {items.map(({ appHotkey, status }) => (
@@ -58,9 +59,10 @@ function useAppHotkeyList() {
58
59
  * const { setCustomHotkey, findConflicts } = useCustomHotkeys()
59
60
  * const recorder = useHotkeyRecorder({
60
61
  * maxSteps: Infinity,
61
- * validate: (hotkey) =>
62
- * findConflicts('doc.save', hotkey).length === 0 || 'Already in use',
63
- * onRecord: (hotkey) => setCustomHotkey('doc.save', hotkey),
62
+ * validate: (hotkey, context) =>
63
+ * findConflicts('doc.save', hotkey, context).length === 0 ||
64
+ * 'Already in use',
65
+ * onRecord: (hotkey, context) => setCustomHotkey('doc.save', hotkey, context),
64
66
  * })
65
67
  * ```
66
68
  */
@@ -68,10 +70,7 @@ function useCustomHotkeys() {
68
70
  const store = useRequiredStore("useCustomHotkeys");
69
71
  return {
70
72
  customHotkeys: store.customHotkeys,
71
- setCustomHotkey: (id, hotkey) => store.setCustomHotkey(id, hotkey),
72
- resetCustomHotkey: (id) => store.resetCustomHotkey(id),
73
- resetCustomHotkeys: () => store.resetCustomHotkeys(),
74
- findConflicts: (id, hotkey) => store.findConflicts(id, hotkey)
73
+ ...bindCustomHotkeyActions(() => store)
75
74
  };
76
75
  }
77
76
 
@@ -4,6 +4,12 @@ import { formatForDisplay } from "@tanstack/hotkeys";
4
4
  * detected platform without one. A `platform` passed to the returned function
5
5
  * wins.
6
6
  *
7
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
8
+ * types for this user in Chromium browsers, once the keyboard layout has
9
+ * loaded. See `useKeyboardLayout`. A `layoutMap` passed to the returned
10
+ * function wins. Firefox and Safari report no layout, so labels there keep the
11
+ * key's US character.
12
+ *
7
13
  * Call the hook once at the top of the component; the returned function can
8
14
  * be called anywhere, such as in a loop or a condition. Server-rendered apps
9
15
  * should set the provider's `defaultOptions.platform` from the request with
@@ -12,11 +18,11 @@ import { formatForDisplay } from "@tanstack/hotkeys";
12
18
  * @example
13
19
  * ```tsx
14
20
  * function SaveButton() {
15
- * const format = useFormatForDisplay()
21
+ * const formatForDisplay = useFormatForDisplay()
16
22
  *
17
23
  * return (
18
24
  * <button>
19
- * Save <kbd>{format('Mod+S')}</kbd>
25
+ * Save <kbd>{formatForDisplay('Mod+S')}</kbd>
20
26
  * </button>
21
27
  * )
22
28
  * }
@@ -1,6 +1,7 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
- import { formatForDisplay } from "@tanstack/hotkeys";
3
- import { useCallback } from "preact/hooks";
2
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
3
+ import { createFormatForDisplay } from "@tanstack/hotkeys/adapter";
4
+ import { useMemo } from "preact/hooks";
4
5
 
5
6
  // src/useFormatForDisplay.ts
6
7
  /**
@@ -8,6 +9,12 @@ import { useCallback } from "preact/hooks";
8
9
  * detected platform without one. A `platform` passed to the returned function
9
10
  * wins.
10
11
  *
12
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
13
+ * types for this user in Chromium browsers, once the keyboard layout has
14
+ * loaded. See `useKeyboardLayout`. A `layoutMap` passed to the returned
15
+ * function wins. Firefox and Safari report no layout, so labels there keep the
16
+ * key's US character.
17
+ *
11
18
  * Call the hook once at the top of the component; the returned function can
12
19
  * be called anywhere, such as in a loop or a condition. Server-rendered apps
13
20
  * should set the provider's `defaultOptions.platform` from the request with
@@ -16,11 +23,11 @@ import { useCallback } from "preact/hooks";
16
23
  * @example
17
24
  * ```tsx
18
25
  * function SaveButton() {
19
- * const format = useFormatForDisplay()
26
+ * const formatForDisplay = useFormatForDisplay()
20
27
  *
21
28
  * return (
22
29
  * <button>
23
- * Save <kbd>{format('Mod+S')}</kbd>
30
+ * Save <kbd>{formatForDisplay('Mod+S')}</kbd>
24
31
  * </button>
25
32
  * )
26
33
  * }
@@ -28,10 +35,8 @@ import { useCallback } from "preact/hooks";
28
35
  */
29
36
  function useFormatForDisplay() {
30
37
  const platform = useDefaultHotkeysOptions().platform;
31
- return useCallback((hotkey, options) => formatForDisplay(hotkey, {
32
- ...options,
33
- platform: options?.platform ?? platform
34
- }), [platform]);
38
+ const layoutMap = useKeyboardLayout();
39
+ return useMemo(() => createFormatForDisplay(() => platform, () => layoutMap), [platform, layoutMap]);
35
40
  }
36
41
 
37
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
  *
@@ -34,6 +20,7 @@ export interface PreactHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
34
20
  * @example
35
21
  * ```tsx
36
22
  * function ShortcutInput() {
23
+ * const formatForDisplay = useFormatForDisplay()
37
24
  * const [shortcut, setShortcut] = useState<Hotkey>('Mod+S')
38
25
  * const recorder = useHotkeyRecorder({ onRecord: setShortcut })
39
26
  *
@@ -11,6 +11,7 @@ import { HotkeyRegistrationView } from "@tanstack/hotkeys";
11
11
  * @example
12
12
  * ```tsx
13
13
  * function ShortcutPalette() {
14
+ * const formatForDisplay = useFormatForDisplay()
14
15
  * const registrations = useHotkeyRegistrations()
15
16
  *
16
17
  * return (
@@ -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
  /**
@@ -14,6 +15,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
14
15
  * @example
15
16
  * ```tsx
16
17
  * function ShortcutPalette() {
18
+ * const formatForDisplay = useFormatForDisplay()
17
19
  * const registrations = useHotkeyRegistrations()
18
20
  *
19
21
  * return (
@@ -29,7 +31,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
29
31
  * ```
30
32
  */
31
33
  function useHotkeyRegistrations() {
32
- return useSelector(getHotkeyManager().registrations, (state) => Array.from(state.values()).map(toHotkeyRegistrationView));
34
+ return useSelector(getHotkeyManager().registrations, (state) => getHotkeyRegistrationViews(state));
33
35
  }
34
36
 
35
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(() => {
@@ -0,0 +1,25 @@
1
+ import { KeyboardLayoutMap } from "@tanstack/hotkeys";
2
+ /**
3
+ * The browser's keyboard layout, or `null` until it loads and in browsers
4
+ * that don't report one. The layout maps each physical code to the character
5
+ * it types, such as `KeyQ` to `a` on a French AZERTY keyboard. Only Chromium
6
+ * browsers report one.
7
+ *
8
+ * The layout loads after the first component that uses this hook mounts, so
9
+ * server-rendered output hydrates unchanged. `useFormatForDisplay` uses this
10
+ * hook, so you only need it for labels you draw yourself.
11
+ *
12
+ * To never ask the browser for the layout, set `keyboardLayout: false` in
13
+ * `HotkeysProvider`'s `defaultOptions`. This hook then returns `null`.
14
+ *
15
+ * @example
16
+ * ```tsx
17
+ * function MoveKeys() {
18
+ * const layoutMap = useKeyboardLayout()
19
+ * const label = (code: string) => (layoutMap?.get(code) ?? code).toUpperCase()
20
+ *
21
+ * return <kbd>{label('KeyW')}</kbd>
22
+ * }
23
+ * ```
24
+ */
25
+ export declare function useKeyboardLayout(): KeyboardLayoutMap | null;
@@ -0,0 +1,42 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { getKeyboardLayoutStore } from "@tanstack/hotkeys";
3
+ import { followKeyboardLayout } from "@tanstack/hotkeys/adapter";
4
+ import { useLayoutEffect, useState } from "preact/hooks";
5
+
6
+ // src/useKeyboardLayout.ts
7
+ /**
8
+ * The browser's keyboard layout, or `null` until it loads and in browsers
9
+ * that don't report one. The layout maps each physical code to the character
10
+ * it types, such as `KeyQ` to `a` on a French AZERTY keyboard. Only Chromium
11
+ * browsers report one.
12
+ *
13
+ * The layout loads after the first component that uses this hook mounts, so
14
+ * server-rendered output hydrates unchanged. `useFormatForDisplay` uses this
15
+ * hook, so you only need it for labels you draw yourself.
16
+ *
17
+ * To never ask the browser for the layout, set `keyboardLayout: false` in
18
+ * `HotkeysProvider`'s `defaultOptions`. This hook then returns `null`.
19
+ *
20
+ * @example
21
+ * ```tsx
22
+ * function MoveKeys() {
23
+ * const layoutMap = useKeyboardLayout()
24
+ * const label = (code: string) => (layoutMap?.get(code) ?? code).toUpperCase()
25
+ *
26
+ * return <kbd>{label('KeyW')}</kbd>
27
+ * }
28
+ * ```
29
+ */
30
+ function useKeyboardLayout() {
31
+ const enabled = useDefaultHotkeysOptions().keyboardLayout !== false;
32
+ const [layoutMap, setLayoutMap] = useState(null);
33
+ useLayoutEffect(() => {
34
+ if (!enabled) return;
35
+ const stop = followKeyboardLayout(setLayoutMap);
36
+ setLayoutMap(getKeyboardLayoutStore().get());
37
+ return stop;
38
+ }, [enabled]);
39
+ return enabled ? layoutMap : null;
40
+ }
41
+
42
+ export { useKeyboardLayout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/preact-hotkeys",
3
- "version": "0.21.1",
3
+ "version": "0.23.1",
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.27.1"
38
+ "@tanstack/hotkeys": "0.30.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@preact/preset-vite": "^2.10.6",
@@ -30,10 +30,11 @@ requires:
30
30
  Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack/intent@latest load @tanstack/hotkeys#hotkeys-core`).
31
31
 
32
32
  ```tsx
33
- import { formatForDisplay, useHotkey } from '@tanstack/preact-hotkeys'
33
+ import { useFormatForDisplay, useHotkey } from '@tanstack/preact-hotkeys'
34
34
 
35
35
  export function SaveButton({ onSave }: { onSave: () => void }) {
36
36
  useHotkey('Mod+S', () => onSave(), { meta: { name: 'Save' } })
37
+ const formatForDisplay = useFormatForDisplay()
37
38
 
38
39
  return (
39
40
  <button onClick={onSave}>
@@ -45,26 +46,26 @@ export function SaveButton({ onSave }: { onSave: () => void }) {
45
46
 
46
47
  ## API
47
48
 
48
- | API | What it's for |
49
- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
50
- | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
51
- | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
52
- | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
53
- | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
54
- | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
55
- | `useFormatForDisplay()` | Get `formatForDisplay` with the provider's `platform` filled in. Use it for labels in server-rendered apps; call it at the top of the component. |
56
- | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
57
- | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
58
- | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
59
- | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
60
- | `useHotkeyRegistrations()` | List every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope), for a cheat sheet of what's mounted. |
61
- | `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
62
- | `useHotkeyHint(hotkey, options?)` | Get whether held modifiers should reveal a shortcut's hint, such as labels that appear while Mod is held. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
63
- | `useKeyHold(key)` | Get whether one key is held, such as Shift for a range-select mode. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
64
- | `useHeldKeys()` | Get every key that's held right now. |
65
- | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
66
-
67
- The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
49
+ | API | What it's for |
50
+ | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
51
+ | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
52
+ | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
53
+ | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
54
+ | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
55
+ | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
56
+ | `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()` at the top, then `formatForDisplay(hotkey)`. |
57
+ | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
58
+ | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
59
+ | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
60
+ | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
61
+ | `useHotkeyRegistrations()` | List every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope), for a cheat sheet of what's mounted. |
62
+ | `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
63
+ | `useHotkeyHint(hotkey, options?)` | Get whether held modifiers should reveal a shortcut's hint, such as labels that appear while Mod is held. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
64
+ | `useKeyHold(key)` | Get whether one key is held, such as Shift for a range-select mode. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
65
+ | `useHeldKeys()` | Get every key that's held right now. |
66
+ | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
67
+
68
+ 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`.
68
69
 
69
70
  No provider is required.
70
71
 
@@ -107,7 +108,7 @@ A hook uses the nearest `HotkeysProvider` above its component. Registering `Esca
107
108
  - **No dependency arrays or `useCallback`.** The hooks read the hotkey, callback, and options on every render, so inline callbacks always see current state.
108
109
  - **Call the hooks directly.** A `useShortcut(keys: string)` wrapper has to cast strings to hotkeys, which turns off type checking and drops sequences, options, app hotkeys, and the typed callback context.
109
110
  - **In tests,** set `<HotkeysProvider defaultOptions={{ platform: 'mac' }}>`. It applies to hotkeys, recorders, labels, and app hotkey conflict checks.
110
- - **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()` at the top of the component, then `format(hotkey)`. Keep plain `formatForDisplay` for labels that render only in the browser, when no provider sets `platform`.
111
+ - **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.
111
112
 
112
113
  When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Preact](references/app-hotkeys.md).
113
114
 
@@ -47,24 +47,25 @@ A settings row records with `useHotkeyRecorder` and saves through `useCustomHotk
47
47
 
48
48
  ```tsx
49
49
  import {
50
- formatForDisplay,
51
50
  useAppHotkeys,
52
51
  useCustomHotkeys,
52
+ useFormatForDisplay,
53
53
  useHotkeyRecorder,
54
54
  } from '@tanstack/preact-hotkeys'
55
55
  import type { AppHotkeyId } from '@tanstack/preact-hotkeys'
56
56
 
57
57
  export function ShortcutRow({ id }: { id: AppHotkeyId }) {
58
+ const formatForDisplay = useFormatForDisplay()
58
59
  const appHotkey = useAppHotkeys()[id]
59
60
  const { findConflicts, setCustomHotkey, resetCustomHotkey } =
60
61
  useCustomHotkeys()
61
62
  const recorder = useHotkeyRecorder({
62
63
  maxSteps: Infinity,
63
- validate: (hotkey) => {
64
- const [conflict] = findConflicts(id, hotkey)
64
+ validate: (hotkey, context) => {
65
+ const [conflict] = findConflicts(id, hotkey, context)
65
66
  return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
66
67
  },
67
- onRecord: (hotkey) => setCustomHotkey(id, hotkey),
68
+ onRecord: (hotkey, context) => setCustomHotkey(id, hotkey, context),
68
69
  onClear: () => setCustomHotkey(id, null),
69
70
  })
70
71
 
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 };