@tanstack/octane-hotkeys 0.12.1 → 0.14.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, AppHotkeysStore, CustomHotkeys, HotkeyPlatform } from "@tanstack/hotkeys";
3
+ import { HotkeyRecorderDefaults, HotkeysContextValue, HotkeysProviderScopeProps } from "@tanstack/hotkeys/adapter";
4
4
  export interface DefaultHotkeysOptions {
5
5
  /**
6
6
  * The user's platform, for labels from `useFormatForDisplay`, hotkeys,
@@ -9,20 +9,21 @@ export interface DefaultHotkeysOptions {
9
9
  * `detectPlatform(userAgent)`.
10
10
  */
11
11
  platform?: HotkeyPlatform;
12
+ /**
13
+ * Whether labels from `useFormatForDisplay` follow the user's keyboard
14
+ * layout, so `[KeyQ]` reads `A` on a French AZERTY keyboard. The layout
15
+ * loads from the browser when the first label mounts. Only Chromium
16
+ * browsers report one. Set `false` to never ask the browser for it.
17
+ * Default: `true`.
18
+ */
19
+ keyboardLayout?: boolean;
12
20
  /**
13
21
  * Defaults for every hotkey registered by `useHotkey` and `useHotkeys`.
14
22
  * `requireReset` applies to chords and `timeout` applies to sequences.
15
23
  */
16
24
  hotkey?: UseHotkeyCommonOptions;
17
25
  /** Defaults for every `useHotkeyRecorder`. */
18
- hotkeyRecorder?: Partial<Omit<HotkeyChordRecorderOptions & HotkeySequenceRecorderOptions, 'maxSteps' | 'onRecord' | 'validate'>>;
19
- }
20
- interface HotkeysContextValue {
21
- /** Merged with every ancestor provider's defaults, including `hotkey.scope`. */
22
- defaultOptions: DefaultHotkeysOptions;
23
- appHotkeysStore: AppHotkeysStore | null;
24
- /** Whether this provider and every ancestor are enabled. */
25
- enabled: boolean;
26
+ hotkeyRecorder?: HotkeyRecorderDefaults;
26
27
  }
27
28
  export type HotkeysProviderOptions = DefaultHotkeysOptions;
28
29
  export interface HotkeysProviderProps extends HotkeysProviderScopeProps {
@@ -46,7 +47,7 @@ export interface HotkeysProviderProps extends HotkeysProviderScopeProps {
46
47
  onCustomHotkeysChange?: (customHotkeys: CustomHotkeys) => void;
47
48
  }
48
49
  /** Reads the nearest provider, or null outside a provider. */
49
- export declare function useHotkeysContext(): HotkeysContextValue | null;
50
+ export declare function useHotkeysContext(): HotkeysContextValue<DefaultHotkeysOptions> | null;
50
51
  /** Reads the nearest provider's defaults, or an empty object. */
51
52
  export declare function useDefaultHotkeysOptions(): DefaultHotkeysOptions;
52
53
  /**
@@ -63,5 +64,4 @@ export declare function HotkeysProvider({ children, defaultOptions, appHotkeys,
63
64
  enabled: boolean;
64
65
  };
65
66
  children: unknown;
66
- }>;
67
- export {};
67
+ }>;
@@ -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,10 +6,12 @@ import { useHeldKeyCodes } from "./useHeldKeyCodes.js";
6
6
  import { useKeyHold } from "./useKeyHold.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 { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
10
12
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
11
13
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
12
14
  import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotkeys.js";
13
15
  import { HotkeyRegistrationsResult, OctaneHotkeyRecorder, UseCustomHotkeysResult } from "./types.js";
14
16
  export * from "@tanstack/hotkeys";
15
- export { type DefaultHotkeysOptions, HotkeyRegistrationsResult, HotkeysProvider, type HotkeysProviderOptions, type HotkeysProviderProps, OctaneHotkeyRecorder, UseAppHotkeyOptions, UseCustomHotkeysResult, UseHotkeyChordOptions, UseHotkeyCommonOptions, UseHotkeyDefinition, UseHotkeyOptions, UseHotkeyOptionsFor, UseHotkeySequenceOptions, UseHotkeyTarget, useAppHotkeyList, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useFormatForDisplay, useHeldKeyCodes, useHeldKeys, useHotkey, useHotkeyHint, useHotkeyRecorder, useHotkeyRegistrations, useHotkeyScopesState, useHotkeys, useHotkeysContext, useKeyHold };
17
+ export { type DefaultHotkeysOptions, HotkeyRegistrationsResult, HotkeysProvider, type HotkeysProviderOptions, type HotkeysProviderProps, OctaneHotkeyRecorder, 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 { useHeldKeyCodes } from "./useHeldKeyCodes.js";
6
6
  import { useKeyHold } from "./useKeyHold.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 { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
10
12
  import { useHotkeyScopesState } from "./useHotkeyScopesState.js";
11
13
  import { useHotkeyRecorder } from "./useHotkeyRecorder.js";
@@ -13,4 +15,4 @@ import { useAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./useAppHotke
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 };
package/dist/types.d.ts CHANGED
@@ -1,39 +1,14 @@
1
- import { AppHotkey, CustomHotkeys, Hotkey, HotkeyRegistrationView, HotkeySequence, RegisteredAppHotkeys } from "@tanstack/hotkeys";
1
+ import { CustomHotkeyActions, CustomHotkeys, Hotkey, HotkeyRecorderActions, HotkeyRecorderState, HotkeyRegistrationView, HotkeyRegistrationsResult } from "@tanstack/hotkeys";
2
2
  /** Recording state and controls owned by the containing component. */
3
- export interface OctaneHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
4
- /** Whether the recorder is listening for a shortcut. */
5
- isRecording: boolean;
6
- /** Chords captured so far in a multi-step recording. */
7
- steps: HotkeySequence;
8
- /** The last committed hotkey, including brackets for physical codes. */
9
- recordedHotkey: THotkey | null;
10
- /** Start a new recording session. */
11
- startRecording: () => void;
12
- /** Stop and clear recorder state without calling onRecord or onCancel. */
13
- stopRecording: () => void;
14
- /** Stop, clear recorder state, and call onCancel. */
15
- cancelRecording: () => void;
16
- /** Commit the current steps (multi-step recordings). */
17
- commitRecording: () => void;
18
- }
3
+ export interface OctaneHotkeyRecorder<THotkey extends Hotkey = Hotkey> extends HotkeyRecorderState<THotkey>, HotkeyRecorderActions {}
19
4
  /** Live registration views from the shared hotkey manager. */
20
- export interface HotkeyRegistrationsResult {
5
+ interface HotkeyRegistrationsResult$1 extends HotkeyRegistrationsResult {
21
6
  /** Every registration, chords and sequences. Narrow each view with `kind`. */
22
7
  hotkeys: Array<HotkeyRegistrationView>;
23
8
  }
24
9
  /** Custom hotkey state and actions returned by `useCustomHotkeys`. */
25
- export interface UseCustomHotkeysResult {
10
+ export interface UseCustomHotkeysResult extends CustomHotkeyActions {
26
11
  /** The user's hotkeys that differ from the defaults. */
27
12
  customHotkeys: CustomHotkeys;
28
- /**
29
- * Sets the user's hotkey for an app hotkey. `null` unbinds it.
30
- * Returns conflicting app hotkeys; when any exist, nothing changes.
31
- */
32
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
33
- /** Restores the default hotkey for an app hotkey. */
34
- resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
35
- /** Restores every default hotkey. */
36
- resetCustomHotkeys: () => void;
37
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
38
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
39
- }
13
+ }
14
+ export { HotkeyRegistrationsResult$1 as HotkeyRegistrationsResult };
@@ -1,5 +1,5 @@
1
1
  import { UseCustomHotkeysResult } from "./types.js";
2
- import { AppHotkeyListItem, RegisteredAppHotkeys } from "@tanstack/hotkeys";
2
+ import { RegisteredAppHotkeyListItem, RegisteredAppHotkeys } from "@tanstack/hotkeys";
3
3
  /**
4
4
  * Returns the effective app hotkeys from `HotkeysProvider`, typed by
5
5
  * `Register`: each declaration with the user's custom hotkey applied.
@@ -10,7 +10,7 @@ export declare function useAppHotkeys(): RegisteredAppHotkeys;
10
10
  * cheat sheets and settings screens. App hotkeys whose components have not
11
11
  * mounted are `'inactive'`; those without a hotkey are `'unbound'`.
12
12
  */
13
- export declare function useAppHotkeyList(): Array<AppHotkeyListItem<RegisteredAppHotkeys[keyof RegisteredAppHotkeys]>>;
13
+ export declare function useAppHotkeyList(): Array<RegisteredAppHotkeyListItem>;
14
14
  /**
15
15
  * Reads and changes the user's custom hotkeys. Changes apply to every
16
16
  * registered app hotkey immediately and are passed to the provider's
@@ -1,12 +1,11 @@
1
1
  import { useHotkeysContext } from "./HotkeysProvider.js";
2
2
  import { subSlot, useAppHotkeysStore } from "./internal.js";
3
3
  import { useHotkeyRegistrations } from "./useHotkeyRegistrations.js";
4
+ import { bindCustomHotkeyActions, requireAppHotkeysStore } from "@tanstack/hotkeys/adapter";
4
5
 
5
6
  // src/useAppHotkeys.ts
6
7
  function useRequiredStore(name, slot) {
7
- const store = useAppHotkeysStore(useHotkeysContext()?.appHotkeysStore ?? null, subSlot(slot, `${name}-store`));
8
- if (!store) throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with appHotkeys`);
9
- return store;
8
+ return requireAppHotkeysStore(useAppHotkeysStore(useHotkeysContext()?.appHotkeysStore ?? null, subSlot(slot, `${name}-store`)), `${name} requires a HotkeysProvider with appHotkeys`);
10
9
  }
11
10
  function useAppHotkeys(slot) {
12
11
  return useRequiredStore("useAppHotkeys", slot).appHotkeys;
@@ -20,10 +19,7 @@ function useCustomHotkeys(slot) {
20
19
  const store = useRequiredStore("useCustomHotkeys", slot);
21
20
  return {
22
21
  customHotkeys: store.customHotkeys,
23
- setCustomHotkey: (id, hotkey) => store.setCustomHotkey(id, hotkey),
24
- resetCustomHotkey: (id) => store.resetCustomHotkey(id),
25
- resetCustomHotkeys: () => store.resetCustomHotkeys(),
26
- findConflicts: (id, hotkey) => store.findConflicts(id, hotkey)
22
+ ...bindCustomHotkeyActions(() => store)
27
23
  };
28
24
  }
29
25
 
@@ -5,6 +5,12 @@ import { formatForDisplay } from "@tanstack/hotkeys";
5
5
  * wins. Without a provider platform, server rendering and hydration use
6
6
  * 'linux', then the client re-renders with the detected platform.
7
7
  *
8
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
9
+ * types for this user in Chromium browsers, once the keyboard layout has
10
+ * loaded. See `useKeyboardLayout`. A `layoutMap` passed to the returned
11
+ * function wins. Firefox and Safari report no layout, so labels there keep the
12
+ * key's US character.
13
+ *
8
14
  * Call the hook once at the top of the component; the returned function can
9
15
  * be called anywhere, such as in a loop or a condition. Server-rendered apps
10
16
  * should set the provider's `defaultOptions.platform` from the request with
@@ -13,10 +19,10 @@ import { formatForDisplay } from "@tanstack/hotkeys";
13
19
  * @example
14
20
  * ```tsx
15
21
  * function SaveButton() @{
16
- * const format = useFormatForDisplay()
22
+ * const formatForDisplay = useFormatForDisplay()
17
23
  *
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,7 +1,8 @@
1
1
  import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
2
  import { subSlot } from "./internal.js";
3
- import { detectPlatform, formatForDisplay } from "@tanstack/hotkeys";
4
- import { getServerPlatformSnapshot } from "@tanstack/hotkeys/adapter";
3
+ import { useKeyboardLayout } from "./useKeyboardLayout.js";
4
+ import { detectPlatform } from "@tanstack/hotkeys";
5
+ import { createFormatForDisplay, getServerPlatformSnapshot } from "@tanstack/hotkeys/adapter";
5
6
  import { useCallback, useSyncExternalStore } from "octane";
6
7
 
7
8
  // src/useFormatForDisplay.ts
@@ -9,10 +10,8 @@ const subscribe = () => () => {};
9
10
  function useFormatForDisplay(slot) {
10
11
  const provided = useDefaultHotkeysOptions().platform;
11
12
  const platform = useSyncExternalStore(subscribe, () => provided ?? detectPlatform(), () => provided ?? getServerPlatformSnapshot(), subSlot(slot, "platform"));
12
- return useCallback((hotkey, options) => formatForDisplay(hotkey, {
13
- ...options,
14
- platform: options?.platform ?? platform
15
- }), [platform], subSlot(slot, "format"));
13
+ const layoutMap = useKeyboardLayout(subSlot(slot, "layout"));
14
+ return useCallback(createFormatForDisplay(() => platform, () => layoutMap), [platform, layoutMap], subSlot(slot, "format"));
16
15
  }
17
16
 
18
17
  export { useFormatForDisplay };
@@ -1,12 +1,10 @@
1
- import { AppHotkey, HotkeyCallback, HotkeyCallbackContextFor, RegisterableHotkey, Target } from "@tanstack/hotkeys";
2
- import { AdapterAppHotkeyOptions, AdapterChordOptions, AdapterCommonOptions, AdapterHotkeyOptions, AdapterOptionsFor, AdapterSequenceOptions } from "@tanstack/hotkeys/adapter";
1
+ import { AppHotkey, HotkeyCallback, HotkeyCallbackContextFor, RegisterableHotkey } from "@tanstack/hotkeys";
2
+ import { AdapterAppHotkeyOptions, AdapterChordOptions, AdapterCommonOptions, AdapterHotkeyOptions, AdapterOptionsFor, AdapterSequenceOptions, RefTarget } from "@tanstack/hotkeys/adapter";
3
3
  /**
4
4
  * A ref object, element, document, or window to listen on. A `null` or
5
5
  * `undefined` target, or a ref that is still unset, waits for the element.
6
6
  */
7
- export type UseHotkeyTarget = {
8
- current: HTMLElement | null | undefined;
9
- } | Target | null;
7
+ export type UseHotkeyTarget = RefTarget;
10
8
  /** Options shared by chords and sequences. */
11
9
  export type UseHotkeyOptions = AdapterHotkeyOptions<UseHotkeyTarget>;
12
10
  /** Options for a chord such as `'Mod+S'`. */
@@ -1,16 +1,16 @@
1
1
  import { useHotkeysContext } from "./HotkeysProvider.js";
2
2
  import { select, splitSlot, subSlot, useAppHotkeysStore } from "./internal.js";
3
- import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
3
+ import { getHotkeyManager, getKeyStateTracker } from "@tanstack/hotkeys";
4
+ import { isHotkeyHintVisible } from "@tanstack/hotkeys/adapter";
4
5
 
5
6
  // src/useHotkeyHint.ts
6
7
  function useHotkeyHint(hotkey, ...rest) {
7
8
  const [args, slot] = splitSlot(rest);
8
9
  const store = useAppHotkeysStore(useHotkeysContext()?.appHotkeysStore ?? null, subSlot(slot, "app-hotkeys"));
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), subSlot(slot, "hint-active"));
12
- const held = select(getKeyStateTracker().store, (state) => resolved !== null && matchesHeldModifiers(resolved, state.heldKeys, args[0]), subSlot(slot, "hint"));
13
- return active && held;
11
+ const active = select(manager.registrations, () => manager.hasActiveRegistration(hotkey), subSlot(slot, "hint-active"));
12
+ const visible = select(getKeyStateTracker().store, (state) => isHotkeyHintVisible(hotkey, state.heldKeys, args[0], store), subSlot(slot, "hint"));
13
+ return active && visible;
14
14
  }
15
15
 
16
16
  export { useHotkeyHint };
@@ -1,9 +1,10 @@
1
1
  import { select, subSlot } from "./internal.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
  function useHotkeyRegistrations(slot) {
6
- return { hotkeys: select(getHotkeyManager().registrations, (state) => Array.from(state.values()).map(toHotkeyRegistrationView), subSlot(slot, "hotkeys")) };
7
+ return { hotkeys: select(getHotkeyManager().registrations, getHotkeyRegistrationViews, subSlot(slot, "hotkeys")) };
7
8
  }
8
9
 
9
10
  export { useHotkeyRegistrations };
@@ -1,6 +1,6 @@
1
1
  import { useHotkeysContext } from "./HotkeysProvider.js";
2
2
  import { splitSlot, subSlot, useAppHotkeysStore } from "./internal.js";
3
- import { createHotkeyBindings, prepareHotkeyDefinitions } from "@tanstack/hotkeys/adapter";
3
+ import { createHotkeyBindings, prepareHotkeyDefinitions, resolveRefTarget } from "@tanstack/hotkeys/adapter";
4
4
  import { useLayoutEffect, useRef } from "octane";
5
5
 
6
6
  // src/useHotkeys.ts
@@ -17,7 +17,7 @@ function useHotkeys(definitions, ...rest) {
17
17
  appHotkeysStore,
18
18
  defaults: context?.defaultOptions.hotkey,
19
19
  commonOptions,
20
- resolveTarget: (target) => target && "current" in target ? target.current : target
20
+ resolveTarget: resolveRefTarget
21
21
  }));
22
22
  }, null, subSlot(slot, "update"));
23
23
  useLayoutEffect(() => () => bindings.destroy(), [], subSlot(slot, "cleanup"));
@@ -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
+ * <kbd>{label('KeyW')}</kbd>
22
+ * }
23
+ * ```
24
+ */
25
+ export declare function useKeyboardLayout(): KeyboardLayoutMap | null;
@@ -0,0 +1,18 @@
1
+ import { useDefaultHotkeysOptions } from "./HotkeysProvider.js";
2
+ import { subSlot } from "./internal.js";
3
+ import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
4
+ import { useEffect, useSyncExternalStore } from "octane";
5
+
6
+ // src/useKeyboardLayout.ts
7
+ const subscribe = (onChange) => getKeyboardLayoutStore().subscribe(onChange).unsubscribe;
8
+ const getSnapshot = () => getKeyboardLayoutStore().get();
9
+ const subscribeToNothing = () => () => {};
10
+ const getNothing = () => null;
11
+ const getServerSnapshot = () => null;
12
+ function useKeyboardLayout(slot) {
13
+ const enabled = useDefaultHotkeysOptions().keyboardLayout !== false;
14
+ useEffect(() => enabled ? loadKeyboardLayout() : void 0, [enabled], subSlot(slot, "keyboard-layout:load"));
15
+ return useSyncExternalStore(enabled ? subscribe : subscribeToNothing, enabled ? getSnapshot : getNothing, getServerSnapshot, subSlot(slot, "keyboard-layout"));
16
+ }
17
+
18
+ export { useKeyboardLayout };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/octane-hotkeys",
3
- "version": "0.12.1",
3
+ "version": "0.14.1",
4
4
  "description": "Octane adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -35,14 +35,14 @@
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
  "@tanstack/hotkeys-test-utils": "0.0.0",
42
42
  "@tsrx/oxc": "0.23.0",
43
43
  "@tsrx/typescript-plugin": "^0.6.3",
44
44
  "octane": "0.16.7",
45
- "vite": "^8.3.6"
45
+ "vite": "^8.3.7"
46
46
  },
47
47
  "peerDependencies": {
48
48
  "octane": ">=0.12.0"
@@ -32,13 +32,14 @@ Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack
32
32
 
33
33
  ```tsrx
34
34
  import { useState } from 'octane'
35
- import { formatForDisplay, useHotkey } from '@tanstack/octane-hotkeys'
35
+ import { useFormatForDisplay, useHotkey } from '@tanstack/octane-hotkeys'
36
36
 
37
37
  export function Panel({ onSave }: { onSave: () => void }) @{
38
38
  const [panel, setPanel] = useState<HTMLDivElement | null>(null)
39
39
 
40
40
  useHotkey('Mod+S', () => onSave(), { meta: { name: 'Save' } })
41
41
  useHotkey('Escape', () => closePanel(), { target: panel })
42
+ const formatForDisplay = useFormatForDisplay()
42
43
 
43
44
  <div ref={setPanel} tabIndex={0}>
44
45
  Save <kbd>{formatForDisplay('Mod+S')}</kbd>
@@ -48,26 +49,26 @@ export function Panel({ onSave }: { onSave: () => void }) @{
48
49
 
49
50
  ## API
50
51
 
51
- | API | What it's for |
52
- | ----------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
53
- | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
54
- | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
55
- | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
56
- | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
57
- | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
58
- | `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. |
59
- | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
60
- | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
61
- | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
62
- | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
63
- | `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. |
64
- | `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
65
- | `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. |
66
- | `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. |
67
- | `useHeldKeys()` | Get every key that's held right now. |
68
- | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
52
+ | API | What it's for |
53
+ | ----------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
54
+ | `useHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey while the component is mounted. |
55
+ | `useHotkeys(definitions, commonOptions?)` | Register several hotkeys, or a list built from data, in one call. |
56
+ | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
57
+ | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
58
+ | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
59
+ | `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)`. |
60
+ | `useAppHotkeys()` | Get the declared app hotkeys with the user's custom hotkeys applied, to pass to the register API. |
61
+ | `useAppHotkeyList()` | List every app hotkey with its status, including ones whose components aren't mounted, for a cheat sheet or settings screen. |
62
+ | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
63
+ | `useHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
64
+ | `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. |
65
+ | `useHotkeyScopesState()` | Get the active scopes and the exclusive scope that owns the keyboard. |
66
+ | `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. |
67
+ | `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. |
68
+ | `useHeldKeys()` | Get every key that's held right now. |
69
+ | `useHeldKeyCodes()` | Map each held key to its physical `event.code`. |
69
70
 
70
- The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
71
+ 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`.
71
72
 
72
73
  No provider is required. Use Octane 0.12 or newer, with hooks in compiler-enabled `.tsrx` components.
73
74
 
@@ -78,7 +79,7 @@ No provider is required. Use Octane 0.12 or newer, with hooks in compiler-enable
78
79
  - **Hooks are tracked by call site.** Use `useHotkeys(items.map(...))` for lists, not `useHotkey` in a loop.
79
80
  - **Custom hooks in plain `.ts` and `.js` files need the compiler.** Calls from a `.ts` or `.js` file that the compiler skips share one slot per hook in each component, even across different custom hooks, so only the last `useHotkey` call works. The compiler handles a file that imports a value from `octane` or starts with `/** @jsxImportSource octane */`. A type-only import doesn't count. `.tsrx` and `.tsx` files are fine.
80
81
  - **Call the hooks directly.** A `useShortcut(keys: string)` wrapper has to cast strings to hotkeys, which turns off type checking and drops sequences, options, and app hotkeys.
81
- - **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`.
82
+ - **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.
82
83
 
83
84
  When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Octane](references/app-hotkeys.md).
84
85
 
@@ -42,19 +42,20 @@ Register with `useHotkey(appHotkeys['doc.save'], save)`, using the import or `us
42
42
  A settings row records with `useHotkeyRecorder` and saves through `useCustomHotkeys`:
43
43
 
44
44
  ```tsrx
45
- import { formatForDisplay, useAppHotkeys, useCustomHotkeys, useHotkeyRecorder } from '@tanstack/octane-hotkeys'
45
+ import { useAppHotkeys, useCustomHotkeys, useFormatForDisplay, useHotkeyRecorder } from '@tanstack/octane-hotkeys'
46
46
  import type { AppHotkeyId } from '@tanstack/octane-hotkeys'
47
47
 
48
48
  export function ShortcutRow({ id }: { id: AppHotkeyId }) @{
49
+ const formatForDisplay = useFormatForDisplay()
49
50
  const appHotkey = useAppHotkeys()[id]
50
51
  const { findConflicts, setCustomHotkey, resetCustomHotkey } = useCustomHotkeys()
51
52
  const recorder = useHotkeyRecorder({
52
53
  maxSteps: Infinity,
53
- validate: (hotkey) => {
54
- const [conflict] = findConflicts(id, hotkey)
54
+ validate: (hotkey, context) => {
55
+ const [conflict] = findConflicts(id, hotkey, context)
55
56
  return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
56
57
  },
57
- onRecord: (hotkey) => setCustomHotkey(id, hotkey),
58
+ onRecord: (hotkey, context) => setCustomHotkey(id, hotkey, context),
58
59
  onClear: () => setCustomHotkey(id, null),
59
60
  })
60
61