@tanstack/solid-hotkeys 0.22.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,14 @@ 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.
@@ -27,8 +35,9 @@ interface HotkeysContextValue {
27
35
  export interface HotkeysProviderProps extends HotkeysProviderScopeProps {
28
36
  children: JSX.Element;
29
37
  /**
30
- * Every app hotkey, from `defineAppHotkeys`. Read when the provider is
31
- * created; listed by `createAppHotkeyList` whether or not they are registered.
38
+ * Every app hotkey, from `defineAppHotkeys`. Listed by `createAppHotkeyList`
39
+ * whether or not they are registered. It can start as `undefined` and arrive
40
+ * later, such as from a lazy import.
32
41
  */
33
42
  appHotkeys?: AppHotkeys;
34
43
  /**
@@ -1,7 +1,7 @@
1
1
  import { publishAppHotkeysStore } from "@tanstack/hotkeys";
2
2
  import { createComponent } from "solid-js/web";
3
3
  import { activateHotkeysProviderScope, createAppHotkeysStore, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults } from "@tanstack/hotkeys/adapter";
4
- import { createContext, createEffect, createMemo, onCleanup, onMount, useContext } from "solid-js";
4
+ import { createContext, createEffect, createMemo, onCleanup, untrack, useContext } from "solid-js";
5
5
 
6
6
  // src/HotkeysProvider.tsx
7
7
  const HotkeysContext = createContext(null);
@@ -35,23 +35,24 @@ const DEFAULT_OPTIONS = {};
35
35
  */
36
36
  const HotkeysProvider = (props) => {
37
37
  const parent = useContext(HotkeysContext);
38
- const store = props.appHotkeys ? createAppHotkeysStore(props.appHotkeys, {
38
+ const store = createMemo((created) => created ?? (props.appHotkeys ? untrack(() => createAppHotkeysStore(props.appHotkeys, {
39
39
  customHotkeys: props.customHotkeys,
40
40
  onCustomHotkeysChange: (custom) => props.onCustomHotkeysChange?.(custom),
41
41
  platform: props.defaultOptions?.platform ?? parent?.defaultOptions.platform
42
- }) : null;
42
+ })) : null), null);
43
43
  createEffect(() => {
44
- if (props.appHotkeys) store?.setAppHotkeys(props.appHotkeys);
44
+ if (props.appHotkeys) store()?.setAppHotkeys(props.appHotkeys);
45
45
  });
46
- onMount(() => {
47
- if (store) onCleanup(publishAppHotkeysStore(store));
46
+ createEffect(() => {
47
+ const created = store();
48
+ if (created) onCleanup(publishAppHotkeysStore(created));
48
49
  });
49
50
  const active = () => (parent?.enabled ?? true) && props.enabled !== false;
50
51
  const defaultOptions = createMemo(() => mergeHotkeysProviderDefaults(parent?.defaultOptions, props.defaultOptions, props.scope));
51
- if (store) createEffect(() => {
52
+ createEffect(() => {
52
53
  const customHotkeys = props.customHotkeys;
53
54
  const platform = defaultOptions().platform;
54
- store.setOptions({
55
+ store()?.setOptions({
55
56
  customHotkeys,
56
57
  platform
57
58
  });
@@ -66,17 +67,18 @@ const HotkeysProvider = (props) => {
66
67
  const key = scopeKey();
67
68
  if (key) onCleanup(activateHotkeysProviderScope(key));
68
69
  });
69
- const contextValue = {
70
- appHotkeysStore: store ?? parent?.appHotkeysStore ?? null,
71
- get defaultOptions() {
72
- return defaultOptions();
73
- },
74
- get enabled() {
75
- return active();
76
- }
77
- };
78
70
  return createComponent(HotkeysContext.Provider, {
79
- value: contextValue,
71
+ value: {
72
+ get appHotkeysStore() {
73
+ return store() ?? parent?.appHotkeysStore ?? null;
74
+ },
75
+ get defaultOptions() {
76
+ return defaultOptions();
77
+ },
78
+ get enabled() {
79
+ return active();
80
+ }
81
+ },
80
82
  get children() {
81
83
  return props.children;
82
84
  }
@@ -1,4 +1,4 @@
1
- import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RegisteredAppHotkeys } from "@tanstack/hotkeys";
1
+ import { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RecordedKeyPresses, 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.
@@ -22,15 +22,20 @@ export interface UseCustomHotkeysResult {
22
22
  customHotkeys: () => CustomHotkeys;
23
23
  /**
24
24
  * Sets the user's hotkey for an app hotkey. `null` unbinds it.
25
- * Returns conflicting app hotkeys; when any exist, nothing changes.
25
+ * Returns conflicting app hotkeys; when any exist, nothing changes. Pass
26
+ * the recorder's `context` for a recorded hotkey, as for `findConflicts`.
26
27
  */
27
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null) => Array<AppHotkey>;
28
+ setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
28
29
  /** Restores the default hotkey for an app hotkey. */
29
30
  resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
30
31
  /** Restores every default hotkey. */
31
32
  resetCustomHotkeys: () => void;
32
- /** Other app hotkeys that would collide if `id` used `hotkey`. */
33
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey) => Array<AppHotkey>;
33
+ /**
34
+ * Other app hotkeys that would collide if `id` used `hotkey`. Pass the
35
+ * recorder's `context` for a recorded hotkey: it has the key press, which
36
+ * also finds a hotkey written differently that the same press fires.
37
+ */
38
+ findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
34
39
  }
35
40
  /**
36
41
  * Reads and changes the user's custom hotkeys. Changes apply to every
@@ -5,9 +5,10 @@ import { createMemo } from "solid-js";
5
5
  // src/appHotkeys.ts
6
6
  function useRequiredStore(name) {
7
7
  const source = useAppHotkeysStoreSource();
8
- if (!source.store) throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with an appHotkeys prop`);
8
+ const initial = source.store;
9
+ if (!initial) throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with an appHotkeys prop`);
9
10
  return {
10
- store: source.store,
11
+ store: () => source.store ?? initial,
11
12
  state: source.state
12
13
  };
13
14
  }
@@ -25,7 +26,7 @@ function useAppHotkeys() {
25
26
  const { store, state } = useRequiredStore("useAppHotkeys");
26
27
  return createMemo(() => {
27
28
  state();
28
- return store.appHotkeys;
29
+ return store().appHotkeys;
29
30
  });
30
31
  }
31
32
  /**
@@ -38,7 +39,7 @@ function createAppHotkeyList() {
38
39
  const registrations = createHotkeyRegistrations();
39
40
  return createMemo(() => {
40
41
  state();
41
- return store.getList(registrations());
42
+ return store().getList(registrations());
42
43
  });
43
44
  }
44
45
  /**
@@ -51,12 +52,12 @@ function useCustomHotkeys() {
51
52
  return {
52
53
  customHotkeys: createMemo(() => {
53
54
  state();
54
- return store.customHotkeys;
55
+ return store().customHotkeys;
55
56
  }),
56
- setCustomHotkey: (id, hotkey) => store.setCustomHotkey(id, hotkey),
57
- resetCustomHotkey: (id) => store.resetCustomHotkey(id),
58
- resetCustomHotkeys: () => store.resetCustomHotkeys(),
59
- findConflicts: (id, hotkey) => store.findConflicts(id, hotkey)
57
+ setCustomHotkey: (id, hotkey, recordedWith) => store().setCustomHotkey(id, hotkey, recordedWith),
58
+ resetCustomHotkey: (id) => store().resetCustomHotkey(id),
59
+ resetCustomHotkeys: () => store().resetCustomHotkeys(),
60
+ findConflicts: (id, hotkey, recordedWith) => store().findConflicts(id, hotkey, recordedWith)
60
61
  };
61
62
  }
62
63
 
@@ -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,7 @@
1
+ import { createKeyboardLayout } from "./createKeyboardLayout.js";
1
2
  import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
3
  import { formatForDisplay } from "@tanstack/hotkeys";
4
+ import { namesPhysicalKey } from "@tanstack/hotkeys/adapter";
3
5
 
4
6
  // src/createFormatForDisplay.ts
5
7
  /**
@@ -8,22 +10,30 @@ import { formatForDisplay } from "@tanstack/hotkeys";
8
10
  * function works anywhere, and JSX updates when the provider's platform
9
11
  * changes. An explicit `platform` option wins.
10
12
  *
13
+ * Physical bindings such as `[KeyQ]` are labeled with the character the key
14
+ * types for this user in Chromium browsers, once the keyboard layout has
15
+ * loaded, and JSX updates when the layout arrives. See `createKeyboardLayout`.
16
+ * An explicit `layoutMap` option wins. Firefox and Safari report no layout, so
17
+ * labels there keep the key's US character.
18
+ *
11
19
  * Server-rendered apps should set the provider's `defaultOptions.platform`
12
20
  * from the request user agent with `detectPlatform(userAgent)`.
13
21
  *
14
22
  * @example
15
23
  * ```tsx
16
24
  * function SaveButton() {
17
- * const format = createFormatForDisplay()
18
- * return <kbd>{format('Mod+S')}</kbd>
25
+ * const formatForDisplay = createFormatForDisplay()
26
+ * return <kbd>{formatForDisplay('Mod+S')}</kbd>
19
27
  * }
20
28
  * ```
21
29
  */
22
30
  function createFormatForDisplay() {
23
31
  const defaultOptions = useDefaultHotkeysOptionsSource();
32
+ const layoutMap = createKeyboardLayout();
24
33
  return ((hotkey, options) => formatForDisplay(hotkey, {
25
34
  ...options,
26
- platform: options?.platform ?? defaultOptions().platform
35
+ platform: options?.platform ?? defaultOptions().platform,
36
+ layoutMap: options?.layoutMap ?? (namesPhysicalKey(hotkey) ? layoutMap() ?? void 0 : void 0)
27
37
  }));
28
38
  }
29
39
 
@@ -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>
@@ -13,6 +13,7 @@ import { getHotkeyManager, toHotkeyRegistrationView } from "@tanstack/hotkeys";
13
13
  * @example
14
14
  * ```tsx
15
15
  * const registrations = createHotkeyRegistrations()
16
+ * const formatForDisplay = createFormatForDisplay()
16
17
  * <For each={registrations()}>
17
18
  * {(reg) => <li>{reg.options.meta?.name}: {formatForDisplay(reg.hotkey)}</li>}
18
19
  * </For>
@@ -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,52 @@
1
+ import { useDefaultHotkeysOptionsSource } from "./defaultOptions.js";
2
+ import { getKeyboardLayoutStore, loadKeyboardLayout } from "@tanstack/hotkeys";
3
+ import { createEffect, createMemo, createSignal, onCleanup } from "solid-js";
4
+
5
+ // src/createKeyboardLayout.ts
6
+ /**
7
+ * SolidJS primitive that returns a signal of the browser's keyboard layout,
8
+ * or `null` until it loads and in browsers that don't report one. The layout
9
+ * maps each physical code to the character it types, such as `KeyQ` to `a` on
10
+ * a French AZERTY keyboard. Only Chromium browsers report one.
11
+ *
12
+ * The layout loads after the first component that uses this primitive mounts,
13
+ * so server-rendered output hydrates unchanged. `createFormatForDisplay` uses
14
+ * this primitive, so you only need it for labels you draw yourself.
15
+ *
16
+ * To never ask the browser for the layout, set `keyboardLayout: false` in
17
+ * `HotkeysProvider`'s `defaultOptions`. The signal then stays `null`.
18
+ *
19
+ * @returns Signal accessor for the keyboard layout, or `null`
20
+ *
21
+ * @example
22
+ * ```tsx
23
+ * function MoveKeys() {
24
+ * const layoutMap = createKeyboardLayout()
25
+ * const label = (code: string) =>
26
+ * (layoutMap()?.get(code) ?? code).toUpperCase()
27
+ *
28
+ * return <kbd>{label('KeyW')}</kbd>
29
+ * }
30
+ * ```
31
+ */
32
+ function createKeyboardLayout() {
33
+ const defaultOptions = useDefaultHotkeysOptionsSource();
34
+ const enabled = createMemo(() => defaultOptions().keyboardLayout !== false);
35
+ const [layoutMap, setLayoutMap] = createSignal(null);
36
+ createEffect(() => {
37
+ if (!enabled()) return;
38
+ const store = getKeyboardLayoutStore();
39
+ setLayoutMap(() => store.get());
40
+ const { unsubscribe } = store.subscribe((state) => {
41
+ setLayoutMap(() => state);
42
+ });
43
+ const stop = loadKeyboardLayout();
44
+ onCleanup(() => {
45
+ unsubscribe();
46
+ stop();
47
+ });
48
+ });
49
+ return () => enabled() ? layoutMap() : null;
50
+ }
51
+
52
+ export { createKeyboardLayout };
@@ -1,6 +1,6 @@
1
1
  import { useHotkeysContext } from "./HotkeysProvider.js";
2
2
  import { useSelector } from "./_useSelector.js";
3
- import { createStore } from "@tanstack/hotkeys/adapter";
3
+ import { createMemo } from "solid-js";
4
4
 
5
5
  // src/defaultOptions.ts
6
6
  /** Captures the context once and reads current defaults inside reactive computations. */
@@ -8,16 +8,23 @@ function useDefaultHotkeysOptionsSource() {
8
8
  const context = useHotkeysContext();
9
9
  return () => context?.defaultOptions ?? {};
10
10
  }
11
- const NO_APP_HOTKEYS = createStore(null);
12
11
  /**
13
12
  * The provider's app hotkeys store and an accessor that tracks its state, so
14
- * computations re-run when app hotkeys or custom hotkeys change.
13
+ * computations re-run when app hotkeys or custom hotkeys change. `store` is a
14
+ * getter: a provider creates its store when its app hotkeys arrive, which can
15
+ * be after this is called.
15
16
  */
16
17
  function useAppHotkeysStoreSource() {
17
- const store = useHotkeysContext()?.appHotkeysStore ?? null;
18
+ const context = useHotkeysContext();
19
+ const state = createMemo(() => {
20
+ const store = context?.appHotkeysStore;
21
+ return store ? useSelector(store.store, (value) => value) : null;
22
+ });
18
23
  return {
19
- store,
20
- state: useSelector(store?.store ?? NO_APP_HOTKEYS, (value) => value)
24
+ get store() {
25
+ return context?.appHotkeysStore ?? null;
26
+ },
27
+ state: () => state()?.() ?? null
21
28
  };
22
29
  }
23
30
 
@@ -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.0",
3
+ "version": "0.24.0",
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.0"
41
+ "@tanstack/hotkeys": "0.29.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