@tanstack/solid-hotkeys 0.22.1 → 0.24.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 { CreateHotkeyCommonOptions } from "./createHotkey.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 { JSX, ParentComponent } from "solid-js";
5
5
  export interface HotkeysProviderOptions {
6
6
  /**
@@ -9,20 +9,21 @@ export interface HotkeysProviderOptions {
9
9
  * from the request with `detectPlatform(userAgent)`.
10
10
  */
11
11
  platform?: HotkeyPlatform;
12
+ /**
13
+ * Whether labels from `createFormatForDisplay` 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 `createHotkey` and `createHotkeys`.
14
22
  * `requireReset` applies to chords and `timeout` applies to sequences.
15
23
  */
16
24
  hotkey?: CreateHotkeyCommonOptions;
17
25
  /** Defaults for every `createHotkeyRecorder`. */
18
- hotkeyRecorder?: Partial<Omit<HotkeyChordRecorderOptions & HotkeySequenceRecorderOptions, 'maxSteps' | 'onRecord' | 'validate'>>;
19
- }
20
- interface HotkeysContextValue {
21
- readonly appHotkeysStore: AppHotkeysStore | null;
22
- /** Merged with every ancestor provider's defaults, including `hotkey.scope`. */
23
- readonly defaultOptions: HotkeysProviderOptions;
24
- /** Whether this provider and every ancestor are enabled. */
25
- readonly enabled: boolean;
26
+ hotkeyRecorder?: HotkeyRecorderDefaults;
26
27
  }
27
28
  export interface HotkeysProviderProps extends HotkeysProviderScopeProps {
28
29
  children: JSX.Element;
@@ -72,6 +73,5 @@ export interface HotkeysProviderProps extends HotkeysProviderScopeProps {
72
73
  * ```
73
74
  */
74
75
  export declare const HotkeysProvider: ParentComponent<HotkeysProviderProps>;
75
- export declare function useHotkeysContext(): HotkeysContextValue | null;
76
- export declare function useDefaultHotkeysOptions(): HotkeysProviderOptions;
77
- export {};
76
+ export declare function useHotkeysContext(): HotkeysContextValue<HotkeysProviderOptions> | null;
77
+ export declare function useDefaultHotkeysOptions(): HotkeysProviderOptions;
@@ -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 an accessor for the effective app hotkeys from `HotkeysProvider`,
4
4
  * typed by `Register`: each declaration with the user's custom hotkey applied.
@@ -15,22 +15,11 @@ export declare function useAppHotkeys(): () => RegisteredAppHotkeys;
15
15
  * cheat sheets and settings screens. App hotkeys whose components have not
16
16
  * mounted are `'inactive'`; those without a hotkey are `'unbound'`.
17
17
  */
18
- export declare function createAppHotkeyList(): () => Array<AppHotkeyListItem<RegisteredAppHotkeys[keyof RegisteredAppHotkeys]>>;
18
+ export declare function createAppHotkeyList(): () => Array<RegisteredAppHotkeyListItem>;
19
19
  /** Custom hotkey state and actions returned by `useCustomHotkeys`. */
20
- export interface UseCustomHotkeysResult {
20
+ export interface UseCustomHotkeysResult extends CustomHotkeyActions {
21
21
  /** The user's hotkeys that differ from the defaults. */
22
22
  customHotkeys: () => CustomHotkeys;
23
- /**
24
- * Sets the user's hotkey for an app hotkey. `null` unbinds it.
25
- * Returns conflicting app hotkeys; when any exist, nothing changes.
26
- */
27
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
28
- /** Restores the default hotkey for an app hotkey. */
29
- resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
30
- /** Restores every default hotkey. */
31
- resetCustomHotkeys: () => void;
32
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
33
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
34
23
  }
35
24
  /**
36
25
  * Reads and changes the user's custom hotkeys. Changes apply to every
@@ -1,14 +1,15 @@
1
1
  import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
2
2
  import { useAppHotkeysStoreSource } from "./defaultOptions.js";
3
+ import { bindCustomHotkeyActions, requireAppHotkeysStore } from "@tanstack/hotkeys/adapter";
3
4
  import { createMemo } from "solid-js";
4
5
 
5
6
  // src/appHotkeys.ts
6
7
  function useRequiredStore(name) {
7
8
  const source = useAppHotkeysStoreSource();
8
- const initial = source.store;
9
- if (!initial) throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with an appHotkeys prop`);
9
+ const message = `${name} requires a HotkeysProvider with an appHotkeys prop`;
10
+ const initial = requireAppHotkeysStore(source.store, message);
10
11
  return {
11
- store: () => source.store ?? initial,
12
+ store: () => requireAppHotkeysStore(source.store ?? initial, message),
12
13
  state: source.state
13
14
  };
14
15
  }
@@ -54,10 +55,7 @@ function useCustomHotkeys() {
54
55
  state();
55
56
  return store().customHotkeys;
56
57
  }),
57
- setCustomHotkey: (id, hotkey) => store().setCustomHotkey(id, hotkey),
58
- resetCustomHotkey: (id) => store().resetCustomHotkey(id),
59
- resetCustomHotkeys: () => store().resetCustomHotkeys(),
60
- findConflicts: (id, hotkey) => store().findConflicts(id, hotkey)
58
+ ...bindCustomHotkeyActions(store)
61
59
  };
62
60
  }
63
61
 
@@ -5,14 +5,20 @@ import { formatForDisplay } from "@tanstack/hotkeys";
5
5
  * function works anywhere, and JSX updates when the provider's platform
6
6
  * changes. An explicit `platform` option wins.
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, and JSX updates when the layout arrives. See `createKeyboardLayout`.
11
+ * An explicit `layoutMap` option wins. Firefox and Safari report no layout, so
12
+ * labels there keep the key's US character.
13
+ *
8
14
  * Server-rendered apps should set the provider's `defaultOptions.platform`
9
15
  * from the request user agent with `detectPlatform(userAgent)`.
10
16
  *
11
17
  * @example
12
18
  * ```tsx
13
19
  * function SaveButton() {
14
- * const format = createFormatForDisplay()
15
- * return <kbd>{format('Mod+S')}</kbd>
20
+ * const formatForDisplay = createFormatForDisplay()
21
+ * return <kbd>{formatForDisplay('Mod+S')}</kbd>
16
22
  * }
17
23
  * ```
18
24
  */
@@ -1,5 +1,6 @@
1
+ import { createKeyboardLayout } from "./createKeyboardLayout.js";
1
2
  import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
- import { formatForDisplay } from "@tanstack/hotkeys";
3
+ import { createFormatForDisplay } from "@tanstack/hotkeys/adapter";
3
4
 
4
5
  // src/createFormatForDisplay.ts
5
6
  /**
@@ -8,23 +9,27 @@ import { formatForDisplay } from "@tanstack/hotkeys";
8
9
  * function works anywhere, and JSX updates when the provider's platform
9
10
  * changes. An explicit `platform` option 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, and JSX updates when the layout arrives. See `createKeyboardLayout`.
15
+ * An explicit `layoutMap` option wins. Firefox and Safari report no layout, so
16
+ * labels there keep the key's US character.
17
+ *
11
18
  * Server-rendered apps should set the provider's `defaultOptions.platform`
12
19
  * from the request user agent with `detectPlatform(userAgent)`.
13
20
  *
14
21
  * @example
15
22
  * ```tsx
16
23
  * function SaveButton() {
17
- * const format = createFormatForDisplay()
18
- * return <kbd>{format('Mod+S')}</kbd>
24
+ * const formatForDisplay = createFormatForDisplay()
25
+ * return <kbd>{formatForDisplay('Mod+S')}</kbd>
19
26
  * }
20
27
  * ```
21
28
  */
22
- function createFormatForDisplay() {
29
+ function createFormatForDisplay$1() {
23
30
  const defaultOptions = useDefaultHotkeysOptionsSource();
24
- return ((hotkey, options) => formatForDisplay(hotkey, {
25
- ...options,
26
- platform: options?.platform ?? defaultOptions().platform
27
- }));
31
+ const layoutMap = createKeyboardLayout();
32
+ return createFormatForDisplay(() => defaultOptions().platform, layoutMap);
28
33
  }
29
34
 
30
- export { createFormatForDisplay };
35
+ export { createFormatForDisplay$1 as createFormatForDisplay };
@@ -1,4 +1,4 @@
1
- import { AppHotkey, HotkeyCallback, HotkeyCallbackContextFor, RegisterableHotkey, Target } from "@tanstack/hotkeys";
1
+ import { AppHotkey, HotkeyCallback, HotkeyCallbackContextFor, MaybeGetter, RegisterableHotkey, Target } from "@tanstack/hotkeys";
2
2
  import { AdapterAppHotkeyOptions, AdapterChordOptions, AdapterCommonOptions, AdapterHotkeyOptions, AdapterOptionsFor, AdapterSequenceOptions } from "@tanstack/hotkeys/adapter";
3
3
  /**
4
4
  * The element to listen on. A `null` or `undefined` target skips
@@ -18,7 +18,6 @@ export type CreateHotkeyCommonOptions = AdapterCommonOptions<CreateHotkeyTarget>
18
18
  export type CreateHotkeyOptionsFor<T extends RegisterableHotkey> = AdapterOptionsFor<T, CreateHotkeyTarget>;
19
19
  /** Call-site options for an app hotkey, typed by its declared default kind. */
20
20
  export type CreateAppHotkeyOptions<T extends AppHotkey = AppHotkey> = AdapterAppHotkeyOptions<T, CreateHotkeyTarget>;
21
- type MaybeAccessor<T> = T | (() => T);
22
21
  /**
23
22
  * Solid primitive for registering a hotkey: a chord such as `'Mod+S'`, a
24
23
  * sequence such as `['G', 'G']`, or an app hotkey from `defineAppHotkeys`.
@@ -37,6 +36,5 @@ type MaybeAccessor<T> = T | (() => T);
37
36
  * createHotkey('Escape', () => close(), () => ({ enabled: isOpen() }))
38
37
  * ```
39
38
  */
40
- export declare function createHotkey<T extends AppHotkey>(hotkey: MaybeAccessor<T | null | undefined>, callback: HotkeyCallback, options?: MaybeAccessor<CreateAppHotkeyOptions<T>>): void;
41
- export declare function createHotkey<const T extends RegisterableHotkey>(hotkey: MaybeAccessor<T | null | undefined>, callback: HotkeyCallback<HotkeyCallbackContextFor<T>>, options?: MaybeAccessor<CreateHotkeyOptionsFor<T>>): void;
42
- export {};
39
+ export declare function createHotkey<T extends AppHotkey>(hotkey: MaybeGetter<T | null | undefined>, callback: HotkeyCallback, options?: MaybeGetter<CreateAppHotkeyOptions<T>>): void;
40
+ export declare function createHotkey<const T extends RegisterableHotkey>(hotkey: MaybeGetter<T | null | undefined>, callback: HotkeyCallback<HotkeyCallbackContextFor<T>>, options?: MaybeGetter<CreateHotkeyOptionsFor<T>>): void;
@@ -1,11 +1,12 @@
1
1
  import { createHotkeys } from "./createHotkeys.js";
2
+ import { resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
2
3
 
3
4
  // src/createHotkey.ts
4
5
  function createHotkey(hotkey, callback, options = {}) {
5
6
  createHotkeys(() => [{
6
- hotkey: typeof hotkey === "function" ? hotkey() : hotkey,
7
+ hotkey: resolveMaybeGetter(hotkey),
7
8
  callback,
8
- options: typeof options === "function" ? options() : options
9
+ options: resolveMaybeGetter(options)
9
10
  }]);
10
11
  }
11
12
 
@@ -1,4 +1,4 @@
1
- import { AppHotkey, HeldModifierOptions, RegisterableHotkey } from "@tanstack/hotkeys";
1
+ import { AppHotkey, HeldModifierOptions, MaybeGetter, RegisterableHotkey } from "@tanstack/hotkeys";
2
2
  /**
3
3
  * Returns an accessor indicating whether held modifiers reveal this shortcut.
4
4
  * The hint shows only while the hotkey can fire: some registration of it is
@@ -7,4 +7,4 @@ import { AppHotkey, HeldModifierOptions, RegisterableHotkey } from "@tanstack/ho
7
7
  * effective hotkey (false while unbound). No hotkey (`undefined`, `null`, or
8
8
  * `''`) returns false. This helper does not register a shortcut. A hotkey on an element counts only while focus is inside it.
9
9
  */
10
- export declare function createHotkeyHint(hotkey: RegisterableHotkey | AppHotkey | null | undefined | (() => RegisterableHotkey | AppHotkey | null | undefined), options?: HeldModifierOptions | (() => HeldModifierOptions)): () => boolean;
10
+ export declare function createHotkeyHint(hotkey: MaybeGetter<RegisterableHotkey | AppHotkey | null | undefined>, options?: MaybeGetter<HeldModifierOptions>): () => boolean;
@@ -1,6 +1,7 @@
1
1
  import { useSelector } from "./_useSelector.js";
2
2
  import { useAppHotkeysStoreSource } from "./defaultOptions.js";
3
- import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey } from "@tanstack/hotkeys";
3
+ import { getHotkeyManager, getKeyStateTracker } from "@tanstack/hotkeys";
4
+ import { isHotkeyHintVisible, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
4
5
  import { createMemo } from "solid-js";
5
6
 
6
7
  // src/createHotkeyHint.ts
@@ -14,14 +15,12 @@ import { createMemo } from "solid-js";
14
15
  */
15
16
  function createHotkeyHint(hotkey, options = {}) {
16
17
  const appHotkeys = useAppHotkeysStoreSource();
17
- const manager = getHotkeyManager();
18
- const registrations = useSelector(manager.registrations, (state) => state);
18
+ const registrations = useSelector(getHotkeyManager().registrations, (state) => state);
19
19
  const held = useSelector(getKeyStateTracker().store, (state) => state.heldKeys);
20
20
  return createMemo(() => {
21
21
  appHotkeys.state();
22
- const value = typeof hotkey === "function" ? hotkey() : hotkey;
23
- const resolved = isAppHotkey(value) ? resolveAppHotkey(value, appHotkeys.store).hotkey : value;
24
- return resolved !== null && matchesHeldModifiers(resolved, held(), typeof options === "function" ? options() : options) && registrations().size > 0 && manager.hasActiveRegistration(value);
22
+ registrations();
23
+ return isHotkeyHintVisible(resolveMaybeGetter(hotkey), held(), resolveMaybeGetter(options), appHotkeys.store);
25
24
  });
26
25
  }
27
26
 
@@ -1,19 +1,11 @@
1
- import { Hotkey, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderOptions, HotkeySequence, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
2
- export interface SolidHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
1
+ import { Hotkey, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderActions, HotkeyRecorderOptions, HotkeySequence, HotkeySequenceRecorderOptions } from "@tanstack/hotkeys";
2
+ export interface SolidHotkeyRecorder<THotkey extends Hotkey = Hotkey> extends HotkeyRecorderActions {
3
3
  /** Whether recording is currently active */
4
4
  isRecording: () => boolean;
5
5
  /** Chords recorded so far in a multi-step recording (for live preview) */
6
6
  steps: () => HotkeySequence;
7
7
  /** The last committed hotkey */
8
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
9
  }
18
10
  /**
19
11
  * Solid primitive for recording keyboard shortcuts. Options may be an accessor.
@@ -1,13 +1,13 @@
1
1
  import { useSelector } from "./_useSelector.js";
2
2
  import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
3
3
  import { HotkeyRecorder } from "@tanstack/hotkeys";
4
- import { withDefaults } from "@tanstack/hotkeys/adapter";
4
+ import { resolveMaybeGetter, withDefaults } from "@tanstack/hotkeys/adapter";
5
5
  import { createEffect, onCleanup } from "solid-js";
6
6
 
7
7
  // src/createHotkeyRecorder.ts
8
8
  function createHotkeyRecorder(options) {
9
9
  const defaultOptions = useDefaultHotkeysOptionsSource();
10
- const resolve = () => withDefaults(defaultOptions().hotkeyRecorder, typeof options === "function" ? options() : options);
10
+ const resolve = () => withDefaults(defaultOptions().hotkeyRecorder, resolveMaybeGetter(options));
11
11
  const recorder = new HotkeyRecorder(resolve());
12
12
  const isRecording = useSelector(recorder.store, (state) => state.isRecording);
13
13
  const steps = useSelector(recorder.store, (state) => state.steps);
@@ -10,6 +10,7 @@ import { HotkeyRegistrationView } from "@tanstack/hotkeys";
10
10
  * @example
11
11
  * ```tsx
12
12
  * const registrations = createHotkeyRegistrations()
13
+ * const formatForDisplay = createFormatForDisplay()
13
14
  * <For each={registrations()}>
14
15
  * {(reg) => <li>{reg.options.meta?.name}: {formatForDisplay(reg.hotkey)}</li>}
15
16
  * </For>
@@ -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/createHotkeyRegistrations.ts
5
6
  /**
@@ -13,13 +14,14 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
13
14
  * @example
14
15
  * ```tsx
15
16
  * const registrations = createHotkeyRegistrations()
17
+ * const formatForDisplay = createFormatForDisplay()
16
18
  * <For each={registrations()}>
17
19
  * {(reg) => <li>{reg.options.meta?.name}: {formatForDisplay(reg.hotkey)}</li>}
18
20
  * </For>
19
21
  * ```
20
22
  */
21
23
  function createHotkeyRegistrations() {
22
- return useSelector(getHotkeyManager().registrations, (state) => Array.from(state.values()).map(toHotkeyRegistrationView));
24
+ return useSelector(getHotkeyManager().registrations, getHotkeyRegistrationViews);
23
25
  }
24
26
 
25
27
  export { createHotkeyRegistrations };
@@ -1,4 +1,5 @@
1
1
  import { CreateHotkeyCommonOptions, CreateHotkeyTarget } from "./createHotkey.js";
2
+ import { MaybeGetter } from "@tanstack/hotkeys";
2
3
  import { AdapterHotkeyDefinition } from "@tanstack/hotkeys/adapter";
3
4
  /**
4
5
  * One hotkey for `createHotkeys`. Chords accept `requireReset`; sequences
@@ -23,4 +24,4 @@ export type CreateHotkeyDefinition = AdapterHotkeyDefinition<CreateHotkeyTarget>
23
24
  * ])
24
25
  * ```
25
26
  */
26
- export declare function createHotkeys(hotkeys: ReadonlyArray<CreateHotkeyDefinition> | (() => ReadonlyArray<CreateHotkeyDefinition>), commonOptions?: CreateHotkeyCommonOptions | (() => CreateHotkeyCommonOptions)): void;
27
+ export declare function createHotkeys(hotkeys: MaybeGetter<ReadonlyArray<CreateHotkeyDefinition>>, commonOptions?: MaybeGetter<CreateHotkeyCommonOptions>): void;
@@ -1,6 +1,6 @@
1
1
  import { useAppHotkeysStoreSource, useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
2
  import { delegateEvents } from "solid-js/web";
3
- import { createHotkeyBindings, prepareHotkeyDefinitions } from "@tanstack/hotkeys/adapter";
3
+ import { createHotkeyBindings, prepareHotkeyDefinitions, resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
4
4
  import { createEffect, onCleanup, untrack } from "solid-js";
5
5
 
6
6
  // src/createHotkeys.ts
@@ -29,10 +29,10 @@ function createHotkeys(hotkeys, commonOptions = {}) {
29
29
  onCleanup(() => bindings.destroy());
30
30
  createEffect(() => {
31
31
  appHotkeys.state();
32
- const definitions = prepareHotkeyDefinitions(typeof hotkeys === "function" ? hotkeys() : hotkeys, {
32
+ const definitions = prepareHotkeyDefinitions(resolveMaybeGetter(hotkeys), {
33
33
  appHotkeysStore: appHotkeys.store,
34
34
  defaults: defaultOptions().hotkey,
35
- commonOptions: typeof commonOptions === "function" ? commonOptions() : commonOptions,
35
+ commonOptions: resolveMaybeGetter(commonOptions),
36
36
  resolveTarget: (target) => target
37
37
  });
38
38
  delegateEvents(["keydown", "keyup"]);
@@ -1,4 +1,4 @@
1
- import { IndividualKey } from "@tanstack/hotkeys";
1
+ import { IndividualKey, MaybeGetter } from "@tanstack/hotkeys";
2
2
  /**
3
3
  * SolidJS primitive that returns whether a specific key is currently being held.
4
4
  *
@@ -38,4 +38,4 @@ import { IndividualKey } from "@tanstack/hotkeys";
38
38
  * }
39
39
  * ```
40
40
  */
41
- export declare function createKeyHold(key: IndividualKey | (() => IndividualKey)): () => boolean;
41
+ export declare function createKeyHold(key: MaybeGetter<IndividualKey>): () => boolean;
@@ -1,5 +1,6 @@
1
1
  import { useSelector } from "./_useSelector.js";
2
2
  import { getKeyStateTracker } from "@tanstack/hotkeys";
3
+ import { resolveMaybeGetter } from "@tanstack/hotkeys/adapter";
3
4
  import { createMemo } from "solid-js";
4
5
 
5
6
  // src/createKeyHold.ts
@@ -47,7 +48,7 @@ function createKeyHold(key) {
47
48
  const heldKeysSelector = useSelector(tracker.store, (state) => state.heldKeys);
48
49
  return createMemo(() => {
49
50
  heldKeysSelector();
50
- return tracker.isKeyHeld(typeof key === "function" ? key() : key);
51
+ return tracker.isKeyHeld(resolveMaybeGetter(key));
51
52
  });
52
53
  }
53
54
 
@@ -0,0 +1,28 @@
1
+ import { KeyboardLayoutMap } from "@tanstack/hotkeys";
2
+ /**
3
+ * SolidJS primitive that returns a signal of the browser's keyboard layout,
4
+ * or `null` until it loads and in browsers that don't report one. The layout
5
+ * maps each physical code to the character it types, such as `KeyQ` to `a` on
6
+ * a French AZERTY keyboard. Only Chromium browsers report one.
7
+ *
8
+ * The layout loads after the first component that uses this primitive mounts,
9
+ * so server-rendered output hydrates unchanged. `createFormatForDisplay` uses
10
+ * this primitive, 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`. The signal then stays `null`.
14
+ *
15
+ * @returns Signal accessor for the keyboard layout, or `null`
16
+ *
17
+ * @example
18
+ * ```tsx
19
+ * function MoveKeys() {
20
+ * const layoutMap = createKeyboardLayout()
21
+ * const label = (code: string) =>
22
+ * (layoutMap()?.get(code) ?? code).toUpperCase()
23
+ *
24
+ * return <kbd>{label('KeyW')}</kbd>
25
+ * }
26
+ * ```
27
+ */
28
+ export declare function createKeyboardLayout(): () => KeyboardLayoutMap | null;
@@ -0,0 +1,45 @@
1
+ import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
+ import { getKeyboardLayoutStore } from "@tanstack/hotkeys";
3
+ import { followKeyboardLayout } from "@tanstack/hotkeys/adapter";
4
+ import { createEffect, createMemo, createSignal, onCleanup } from "solid-js";
5
+
6
+ // src/createKeyboardLayout.ts
7
+ /**
8
+ * SolidJS primitive that returns a signal of the browser's keyboard layout,
9
+ * or `null` until it loads and in browsers that don't report one. The layout
10
+ * maps each physical code to the character it types, such as `KeyQ` to `a` on
11
+ * a French AZERTY keyboard. Only Chromium browsers report one.
12
+ *
13
+ * The layout loads after the first component that uses this primitive mounts,
14
+ * so server-rendered output hydrates unchanged. `createFormatForDisplay` uses
15
+ * this primitive, 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`. The signal then stays `null`.
19
+ *
20
+ * @returns Signal accessor for the keyboard layout, or `null`
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * function MoveKeys() {
25
+ * const layoutMap = createKeyboardLayout()
26
+ * const label = (code: string) =>
27
+ * (layoutMap()?.get(code) ?? code).toUpperCase()
28
+ *
29
+ * return <kbd>{label('KeyW')}</kbd>
30
+ * }
31
+ * ```
32
+ */
33
+ function createKeyboardLayout() {
34
+ const defaultOptions = useDefaultHotkeysOptionsSource();
35
+ const enabled = createMemo(() => defaultOptions().keyboardLayout !== false);
36
+ const [layoutMap, setLayoutMap] = createSignal(null);
37
+ createEffect(() => {
38
+ if (!enabled()) return;
39
+ onCleanup(followKeyboardLayout((state) => setLayoutMap(() => state)));
40
+ setLayoutMap(() => getKeyboardLayoutStore().get());
41
+ });
42
+ return () => enabled() ? layoutMap() : null;
43
+ }
44
+
45
+ export { createKeyboardLayout };
@@ -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 `createFormatForDisplay()` 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 `createFormatForDisplay()` 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 { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
6
6
  import { createHotkeyScopesState } from "./createHotkeyScopesState.js";
7
7
  import { createHotkeyHint } from "./createHotkeyHint.js";
8
8
  import { createFormatForDisplay } from "./createFormatForDisplay.js";
9
+ import { formatForDisplay } from "./formatForDisplay.js";
10
+ import { createKeyboardLayout } from "./createKeyboardLayout.js";
9
11
  import { UseCustomHotkeysResult, createAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./appHotkeys.js";
10
12
  import { createHeldKeys } from "./createHeldKeys.js";
11
13
  import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
12
14
  import { createKeyHold } from "./createKeyHold.js";
13
15
  export * from "@tanstack/hotkeys";
14
- export { CreateAppHotkeyOptions, CreateHotkeyChordOptions, CreateHotkeyCommonOptions, CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeyOptionsFor, CreateHotkeySequenceOptions, CreateHotkeyTarget, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, UseCustomHotkeysResult, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
16
+ export { CreateAppHotkeyOptions, CreateHotkeyChordOptions, CreateHotkeyCommonOptions, CreateHotkeyDefinition, CreateHotkeyOptions, CreateHotkeyOptionsFor, CreateHotkeySequenceOptions, CreateHotkeyTarget, HotkeysProvider, HotkeysProviderOptions, HotkeysProviderProps, SolidHotkeyRecorder, UseCustomHotkeysResult, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, createKeyboardLayout, formatForDisplay, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
package/dist/index.js CHANGED
@@ -6,6 +6,8 @@ import { createHotkeyRegistrations } from "./createHotkeyRegistrations.js";
6
6
  import { createHotkeyScopesState } from "./createHotkeyScopesState.js";
7
7
  import { createHotkeyHint } from "./createHotkeyHint.js";
8
8
  import { createFormatForDisplay } from "./createFormatForDisplay.js";
9
+ import { formatForDisplay } from "./formatForDisplay.js";
10
+ import { createKeyboardLayout } from "./createKeyboardLayout.js";
9
11
  import { createAppHotkeyList, useAppHotkeys, useCustomHotkeys } from "./appHotkeys.js";
10
12
  import { createHeldKeys } from "./createHeldKeys.js";
11
13
  import { createHeldKeyCodes } from "./createHeldKeyCodes.js";
@@ -13,4 +15,4 @@ import { createKeyHold } from "./createKeyHold.js";
13
15
 
14
16
  export * from "@tanstack/hotkeys"
15
17
 
16
- export { HotkeysProvider, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
18
+ export { HotkeysProvider, createAppHotkeyList, createFormatForDisplay, createHeldKeyCodes, createHeldKeys, createHotkey, createHotkeyHint, createHotkeyRecorder, createHotkeyRegistrations, createHotkeyScopesState, createHotkeys, createKeyHold, createKeyboardLayout, formatForDisplay, useAppHotkeys, useCustomHotkeys, useDefaultHotkeysOptions, useHotkeysContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/solid-hotkeys",
3
- "version": "0.22.1",
3
+ "version": "0.24.1",
4
4
  "description": "SolidJS adapter for TanStack Hotkeys",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -38,7 +38,7 @@
38
38
  "skills"
39
39
  ],
40
40
  "dependencies": {
41
- "@tanstack/hotkeys": "0.27.1"
41
+ "@tanstack/hotkeys": "0.30.0"
42
42
  },
43
43
  "devDependencies": {
44
44
  "@solidjs/testing-library": "^0.8.10",
@@ -54,26 +54,26 @@ export function Editor() {
54
54
 
55
55
  ## API
56
56
 
57
- | API | What it's for |
58
- | -------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
59
- | `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey for the owning component's lifetime; the hotkey and options can be accessors. |
60
- | `createHotkeys(definitions, commonOptions?)` | Register several hotkeys, or an accessor of a list built from data, in one call. |
61
- | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
62
- | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
63
- | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
64
- | `createFormatForDisplay()` | Get `formatForDisplay` with the provider's `platform` filled in, for labels in server-rendered apps. |
65
- | `useAppHotkeys()` | Get an accessor of the declared app hotkeys with the user's custom hotkeys applied. |
66
- | `createAppHotkeyList()` | Get an accessor listing every app hotkey with its status, for a cheat sheet or settings screen. |
67
- | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
68
- | `createHotkeyRecorder(options)` | Record a chord or sequence the user presses; its state fields are accessors. |
69
- | `createHotkeyRegistrations()` | Get an accessor listing every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
70
- | `createHotkeyScopesState()` | Get an accessor of the active scopes and the exclusive scope that owns the keyboard. |
71
- | `createHotkeyHint(hotkey, options?)` | Get an accessor of whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
72
- | `createKeyHold(key)` | Get an accessor of whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
73
- | `createHeldKeys()` | Get an accessor of every key that's held right now. |
74
- | `createHeldKeyCodes()` | Get an accessor mapping each held key to its physical `event.code`. |
57
+ | API | What it's for |
58
+ | -------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
59
+ | `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey for the owning component's lifetime; the hotkey and options can be accessors. |
60
+ | `createHotkeys(definitions, commonOptions?)` | Register several hotkeys, or an accessor of a list built from data, in one call. |
61
+ | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
62
+ | `useHotkeysContext()` | Read the nearest provider's merged defaults and enabled state, or `null` outside a provider. |
63
+ | `useDefaultHotkeysOptions()` | Read the default options merged from every provider above. |
64
+ | `createFormatForDisplay()` | 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 = createFormatForDisplay()` in the component, then `formatForDisplay(hotkey)`. |
65
+ | `useAppHotkeys()` | Get an accessor of the declared app hotkeys with the user's custom hotkeys applied. |
66
+ | `createAppHotkeyList()` | Get an accessor listing every app hotkey with its status, for a cheat sheet or settings screen. |
67
+ | `useCustomHotkeys()` | Read, change, and reset the user's custom hotkeys, and check a new one for conflicts. |
68
+ | `createHotkeyRecorder(options)` | Record a chord or sequence the user presses; its state fields are accessors. |
69
+ | `createHotkeyRegistrations()` | Get an accessor listing every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
70
+ | `createHotkeyScopesState()` | Get an accessor of the active scopes and the exclusive scope that owns the keyboard. |
71
+ | `createHotkeyHint(hotkey, options?)` | Get an accessor of whether held modifiers should reveal a shortcut's hint. False unless a registration of the hotkey can fire: enabled, in an active scope, and with focus inside its element target. |
72
+ | `createKeyHold(key)` | Get an accessor of whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
73
+ | `createHeldKeys()` | Get an accessor of every key that's held right now. |
74
+ | `createHeldKeyCodes()` | Get an accessor mapping each held key to its physical `event.code`. |
75
75
 
76
- The package also re-exports `@tanstack/hotkeys`, such as `formatForDisplay`, `matchesKeyboardEvent`, and `defineAppHotkeys`.
76
+ The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `createFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`.
77
77
 
78
78
  The state these return is accessors, including the recorder's fields. No provider is required.
79
79
 
@@ -85,7 +85,7 @@ The state these return is accessors, including the recorder's fields. No provide
85
85
  - **The callback isn't tracked.** Read props inside it, as `() => props.onClose()`, rather than passing `props.onClose` itself.
86
86
  - **Register a dialog's hotkeys in a child of its provider.** A primitive uses the nearest `HotkeysProvider` above its component, so a dialog that renders `<HotkeysProvider scope={...} exclusive>` and creates `Escape` itself puts it in the suspended outer scope. Give each open dialog its own scope name, such as `` `dialog-${createUniqueId()}` ``.
87
87
  - **Call the primitives directly.** A `createShortcut(keys: string)` wrapper has to cast strings to hotkeys, which turns off type checking and drops sequences, accessor options, and app hotkeys.
88
- - **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 = createFormatForDisplay()` in the component, then `format(hotkey)`. Keep plain `formatForDisplay` for labels that render only in the browser, when no provider sets `platform`.
88
+ - **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, and Solid keeps them after hydration for Mac and Windows users. Set the provider's `defaultOptions.platform` to `detectPlatform(userAgent)` from the request's `User-Agent` header. `createFormatForDisplay()` reads it.
89
89
 
90
90
  When declaring app hotkeys, building a cheat sheet, or letting users rebind shortcuts, load `@tanstack/hotkeys#app-hotkeys` and read [app hotkeys in Solid](references/app-hotkeys.md).
91
91
 
@@ -47,25 +47,26 @@ A settings row records with `createHotkeyRecorder` and saves through `useCustomH
47
47
 
48
48
  ```tsx
49
49
  import {
50
+ createFormatForDisplay,
50
51
  createHotkeyRecorder,
51
- formatForDisplay,
52
52
  useAppHotkeys,
53
53
  useCustomHotkeys,
54
54
  } from '@tanstack/solid-hotkeys'
55
55
  import type { AppHotkeyId } from '@tanstack/solid-hotkeys'
56
56
 
57
57
  export function ShortcutRow(props: { id: AppHotkeyId }) {
58
+ const formatForDisplay = createFormatForDisplay()
58
59
  const appHotkeys = useAppHotkeys()
59
60
  const appHotkey = () => appHotkeys()[props.id]
60
61
  const { findConflicts, setCustomHotkey, resetCustomHotkey } =
61
62
  useCustomHotkeys()
62
63
  const recorder = createHotkeyRecorder({
63
64
  maxSteps: Infinity,
64
- validate: (hotkey) => {
65
- const [conflict] = findConflicts(props.id, hotkey)
65
+ validate: (hotkey, context) => {
66
+ const [conflict] = findConflicts(props.id, hotkey, context)
66
67
  return conflict ? `Used by ${conflict.meta?.name ?? conflict.id}` : true
67
68
  },
68
- onRecord: (hotkey) => setCustomHotkey(props.id, hotkey),
69
+ onRecord: (hotkey, context) => setCustomHotkey(props.id, hotkey, context),
69
70
  onClear: () => setCustomHotkey(props.id, null),
70
71
  })
71
72