@tanstack/svelte-hotkeys 0.24.0 → 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,5 +1,5 @@
1
- import type { AppHotkeys, AppHotkeysStore, CustomHotkeys, HotkeyChordRecorderOptions, HotkeyPlatform, HotkeySequenceRecorderOptions } from '@tanstack/hotkeys';
2
- import type { HotkeysProviderScopeProps } from '@tanstack/hotkeys/adapter';
1
+ import type { AppHotkeys, CustomHotkeys, HotkeyPlatform } from '@tanstack/hotkeys';
2
+ import type { HotkeyRecorderDefaults, HotkeysContextValue, HotkeysProviderScopeProps } from '@tanstack/hotkeys/adapter';
3
3
  import type { Snippet } from 'svelte';
4
4
  import type { CreateHotkeyCommonOptions } from './createHotkey.svelte.js';
5
5
  export interface HotkeysProviderOptions {
@@ -23,7 +23,7 @@ export interface HotkeysProviderOptions {
23
23
  */
24
24
  hotkey?: CreateHotkeyCommonOptions;
25
25
  /** Defaults for every `createHotkeyRecorder`. */
26
- hotkeyRecorder?: Partial<Omit<HotkeyChordRecorderOptions & HotkeySequenceRecorderOptions, 'maxSteps' | 'onRecord' | 'validate'>>;
26
+ hotkeyRecorder?: HotkeyRecorderDefaults;
27
27
  }
28
28
  /** Options for `setHotkeysContext` and props for `HotkeysProvider`. */
29
29
  export interface HotkeysContextOptions extends HotkeysProviderScopeProps {
@@ -49,13 +49,6 @@ export interface HotkeysProviderProps extends HotkeysContextOptions {
49
49
  children: Snippet;
50
50
  }
51
51
  export declare const DEFAULT_OPTIONS: HotkeysProviderOptions;
52
- interface HotkeysContextValue {
53
- readonly appHotkeysStore: AppHotkeysStore | null;
54
- /** Merged with every ancestor's defaults, including `hotkey.scope`. */
55
- readonly defaultOptions: HotkeysProviderOptions;
56
- /** Whether this context and every ancestor are enabled. */
57
- readonly enabled: boolean;
58
- }
59
52
  /**
60
53
  * Sets the hotkeys context for descendants. `HotkeysProvider` calls this; call
61
54
  * it directly in a component's script instead of rendering the provider.
@@ -77,9 +70,8 @@ interface HotkeysContextValue {
77
70
  * </script>
78
71
  * ```
79
72
  */
80
- export declare function setHotkeysContext(options?: HotkeysContextOptions): HotkeysContextValue;
73
+ export declare function setHotkeysContext(options?: HotkeysContextOptions): HotkeysContextValue<HotkeysProviderOptions>;
81
74
  /** Sets the context from a getter, so default options stay reactive. */
82
- export declare function setHotkeysContextSource(source: () => HotkeysContextOptions): HotkeysContextValue;
83
- export declare function getHotkeysContext(): HotkeysContextValue | null;
75
+ export declare function setHotkeysContextSource(source: () => HotkeysContextOptions): HotkeysContextValue<HotkeysProviderOptions>;
76
+ export declare function getHotkeysContext(): HotkeysContextValue<HotkeysProviderOptions> | null;
84
77
  export declare function getDefaultHotkeysOptions(): HotkeysProviderOptions;
85
- export {};
@@ -1,19 +1,5 @@
1
- import type { Hotkey, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderOptions, HotkeySequence, HotkeySequenceRecorderOptions } from '@tanstack/hotkeys';
2
- export interface SvelteHotkeyRecorder<THotkey extends Hotkey = Hotkey> {
3
- /** Whether recording is currently active */
4
- readonly isRecording: boolean;
5
- /** Chords recorded so far in a multi-step recording (for live preview) */
6
- readonly steps: HotkeySequence;
7
- /** The last committed hotkey */
8
- readonly 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;
1
+ import type { Hotkey, HotkeyChord, HotkeyChordRecorderOptions, HotkeyRecorderActions, HotkeyRecorderOptions, HotkeyRecorderState, HotkeySequenceRecorderOptions } from '@tanstack/hotkeys';
2
+ export interface SvelteHotkeyRecorder<THotkey extends Hotkey = Hotkey> extends Readonly<HotkeyRecorderState<THotkey>>, HotkeyRecorderActions {
17
3
  }
18
4
  /**
19
5
  * Svelte function for recording keyboard shortcuts. Options may be a getter.
@@ -1,4 +1,4 @@
1
- import type { AppHotkey, AppHotkeyListItem, CustomHotkeys, Hotkey, RecordedKeyPresses, RegisteredAppHotkeys } from '@tanstack/hotkeys';
1
+ import type { CustomHotkeyActions, CustomHotkeys, RegisteredAppHotkeyListItem, RegisteredAppHotkeys } from '@tanstack/hotkeys';
2
2
  export interface SvelteAppHotkeys {
3
3
  /** Effective app hotkeys: each declaration with the user's custom hotkey applied. */
4
4
  readonly current: RegisteredAppHotkeys;
@@ -18,7 +18,7 @@ export interface SvelteAppHotkeys {
18
18
  export declare function getAppHotkeys(): SvelteAppHotkeys;
19
19
  export interface SvelteAppHotkeyList {
20
20
  /** Every app hotkey with its live status, in declaration order. */
21
- readonly current: Array<AppHotkeyListItem<RegisteredAppHotkeys[keyof RegisteredAppHotkeys]>>;
21
+ readonly current: Array<RegisteredAppHotkeyListItem>;
22
22
  }
23
23
  /**
24
24
  * Lists every app hotkey with its live status, for cheat sheets and settings
@@ -26,25 +26,9 @@ export interface SvelteAppHotkeyList {
26
26
  * those without a hotkey are `'unbound'`.
27
27
  */
28
28
  export declare function getAppHotkeyList(): SvelteAppHotkeyList;
29
- export interface SvelteCustomHotkeys {
29
+ export interface SvelteCustomHotkeys extends CustomHotkeyActions {
30
30
  /** The user's hotkeys that differ from the defaults. */
31
31
  readonly current: CustomHotkeys;
32
- /**
33
- * Sets the user's hotkey for an app hotkey. `null` unbinds it.
34
- * Returns conflicting app hotkeys; when any exist, nothing changes. Pass
35
- * the recorder's `context` for a recorded hotkey, as for `findConflicts`.
36
- */
37
- setCustomHotkey: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey | null, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
38
- /** Restores the default hotkey for an app hotkey. */
39
- resetCustomHotkey: (id: keyof RegisteredAppHotkeys & string) => void;
40
- /** Restores every default hotkey. */
41
- resetCustomHotkeys: () => void;
42
- /**
43
- * Other app hotkeys that would collide if `id` used `hotkey`. Pass the
44
- * recorder's `context` for a recorded hotkey: it has the key press, which
45
- * also finds a hotkey written differently that the same press fires.
46
- */
47
- findConflicts: (id: keyof RegisteredAppHotkeys & string, hotkey: Hotkey, recordedWith?: RecordedKeyPresses) => Array<AppHotkey>;
48
32
  }
49
33
  /**
50
34
  * Reads and changes the user's custom hotkeys. Changes apply to every
@@ -1,17 +1,16 @@
1
+ import { bindCustomHotkeyActions, requireAppHotkeysStore, } from '@tanstack/hotkeys/adapter';
1
2
  import { getHotkeysContext } from './HotkeysCtx.js';
2
3
  import { getHotkeyRegistrations } from './getHotkeyRegistrations.svelte.js';
3
4
  import { createAppHotkeysSource } from './internal.svelte.js';
4
5
  function requireStore(name) {
5
6
  const source = createAppHotkeysSource(getHotkeysContext());
7
+ const message = `${name} requires a HotkeysProvider with appHotkeys`;
6
8
  const initial = source.store;
7
- if (!initial) {
8
- throw new Error(`[hotkeys] ${name} requires a HotkeysProvider with appHotkeys`);
9
- }
9
+ requireAppHotkeysStore(initial, message);
10
10
  return {
11
11
  // The nearest provider's store, which changes if a nested provider gets
12
12
  // its app hotkeys later.
13
- store: () => (source.store ??
14
- initial),
13
+ store: () => requireAppHotkeysStore(source.store ?? initial, message),
15
14
  subscribe: source.subscribe,
16
15
  };
17
16
  }
@@ -63,9 +62,6 @@ export function getCustomHotkeys() {
63
62
  subscribe();
64
63
  return store().customHotkeys;
65
64
  },
66
- setCustomHotkey: (id, hotkey, recordedWith) => store().setCustomHotkey(id, hotkey, recordedWith),
67
- resetCustomHotkey: (id) => store().resetCustomHotkey(id),
68
- resetCustomHotkeys: () => store().resetCustomHotkeys(),
69
- findConflicts: (id, hotkey, recordedWith) => store().findConflicts(id, hotkey, recordedWith),
65
+ ...bindCustomHotkeyActions(store),
70
66
  };
71
67
  }
@@ -1,4 +1,4 @@
1
- import { formatForDisplay } from '@tanstack/hotkeys';
1
+ import type { formatForDisplay } from '@tanstack/hotkeys';
2
2
  /**
3
3
  * Returns `formatForDisplay` with the provider's `platform` as the default,
4
4
  * so labels match the user's platform. Call during component initialization,
@@ -1,5 +1,4 @@
1
- import { formatForDisplay } from '@tanstack/hotkeys';
2
- import { namesPhysicalKey } from '@tanstack/hotkeys/adapter';
1
+ import { createFormatForDisplay } from '@tanstack/hotkeys/adapter';
3
2
  import { getHotkeysContext } from './HotkeysCtx.js';
4
3
  import { getKeyboardLayout } from './getKeyboardLayout.svelte.js';
5
4
  /**
@@ -29,12 +28,7 @@ import { getKeyboardLayout } from './getKeyboardLayout.svelte.js';
29
28
  export function getFormatForDisplay() {
30
29
  const context = getHotkeysContext();
31
30
  const keyboard = getKeyboardLayout();
32
- return ((hotkey, options) => formatForDisplay(hotkey, {
33
- ...options,
34
- platform: options?.platform ?? context?.defaultOptions.platform,
35
- // Only a position's label follows the layout. Other labels don't read
36
- // it, so they don't render again when it loads.
37
- layoutMap: options?.layoutMap ??
38
- (namesPhysicalKey(hotkey) ? (keyboard.layout ?? undefined) : undefined),
39
- }));
31
+ // Only a position's label reads the layout, so other labels don't render
32
+ // again when it loads.
33
+ return createFormatForDisplay(() => context?.defaultOptions.platform, () => keyboard.layout);
40
34
  }
@@ -1,4 +1,5 @@
1
- import { getHotkeyManager, getKeyStateTracker, isAppHotkey, matchesHeldModifiers, resolveAppHotkey, } from '@tanstack/hotkeys';
1
+ import { getHotkeyManager, getKeyStateTracker } from '@tanstack/hotkeys';
2
+ import { isHotkeyHintVisible } from '@tanstack/hotkeys/adapter';
2
3
  import { createSubscriber } from 'svelte/reactivity';
3
4
  import { getHotkeysContext } from './HotkeysCtx.js';
4
5
  import { resolveMaybeGetter } from './internal.svelte.js';
@@ -18,15 +19,7 @@ export function getHotkeyHint(hotkey, options = {}) {
18
19
  // Read each time: a provider creates its store when its app hotkeys arrive.
19
20
  const getStore = () => context?.appHotkeysStore ?? null;
20
21
  // Resolve current reactive arguments without caching a stale hotkey.
21
- const read = () => {
22
- const value = resolveMaybeGetter(hotkey);
23
- const resolved = isAppHotkey(value)
24
- ? resolveAppHotkey(value, getStore()).hotkey
25
- : value;
26
- return (resolved !== null &&
27
- matchesHeldModifiers(resolved, tracker.store.state.heldKeys, resolveMaybeGetter(options)) &&
28
- manager.hasActiveRegistration(value));
29
- };
22
+ const read = () => isHotkeyHintVisible(resolveMaybeGetter(hotkey), tracker.store.state.heldKeys, resolveMaybeGetter(options), getStore());
30
23
  let previous = false;
31
24
  // Notify Svelte only when visibility changes, not for every unrelated held key.
32
25
  const track = (store) => createSubscriber((update) => {
@@ -1,7 +1,5 @@
1
- import type { HotkeyRegistrationView } from '@tanstack/hotkeys';
2
- export interface SvelteHotkeyRegistrations {
3
- /** Every registration, chords and sequences. Narrow each view with `kind`. */
4
- readonly hotkeys: Array<HotkeyRegistrationView>;
1
+ import type { HotkeyRegistrationsResult } from '@tanstack/hotkeys';
2
+ export interface SvelteHotkeyRegistrations extends HotkeyRegistrationsResult {
5
3
  }
6
4
  /**
7
5
  * Svelte function that returns reactive access to every hotkey registration,
@@ -1,11 +1,12 @@
1
- import { getHotkeyManager, toHotkeyRegistrationView } from '@tanstack/hotkeys';
1
+ import { getHotkeyManager } from '@tanstack/hotkeys';
2
+ import { getHotkeyRegistrationViews } from '@tanstack/hotkeys/adapter';
2
3
  import { createStoreSubscriber } from './internal.svelte.js';
3
4
  class HotkeyRegistrationsState {
4
5
  #manager = getHotkeyManager();
5
6
  #subscribe = createStoreSubscriber(this.#manager.registrations);
6
7
  get hotkeys() {
7
8
  this.#subscribe();
8
- return Array.from(this.#manager.registrations.state.values()).map(toHotkeyRegistrationView);
9
+ return getHotkeyRegistrationViews(this.#manager.registrations.state);
9
10
  }
10
11
  }
11
12
  /**
@@ -1,4 +1,5 @@
1
- import { getKeyboardLayoutStore, loadKeyboardLayout } from '@tanstack/hotkeys';
1
+ import { getKeyboardLayoutStore } from '@tanstack/hotkeys';
2
+ import { followKeyboardLayout } from '@tanstack/hotkeys/adapter';
2
3
  import { createSubscriber } from 'svelte/reactivity';
3
4
  import { getHotkeysContext } from './HotkeysCtx.js';
4
5
  class KeyboardLayoutState {
@@ -6,14 +7,7 @@ class KeyboardLayoutState {
6
7
  #store = getKeyboardLayoutStore();
7
8
  // Loads the layout while an effect or markup reads it. Calls are counted,
8
9
  // so every reader can load it.
9
- #subscribe = createSubscriber((update) => {
10
- const stop = loadKeyboardLayout();
11
- const { unsubscribe } = this.#store.subscribe(update);
12
- return () => {
13
- unsubscribe();
14
- stop();
15
- };
16
- });
10
+ #subscribe = createSubscriber((update) => followKeyboardLayout(update));
17
11
  // Server HTML never has a layout, so a component that hydrates starts
18
12
  // without one, even when the layout has already loaded.
19
13
  #mounted = $state(false);
@@ -1,12 +1,12 @@
1
1
  import type { AppHotkeys, AppHotkeysStore, CustomHotkeys, HotkeyPlatform } from '@tanstack/hotkeys';
2
- export type MaybeGetter<T> = T | (() => T);
2
+ export type { MaybeGetter } from '@tanstack/hotkeys';
3
+ export { resolveMaybeGetter } from '@tanstack/hotkeys/adapter';
3
4
  interface SubscribableStore<TState> {
4
5
  state: TState;
5
6
  subscribe: (listener: () => void) => (() => void) | {
6
7
  unsubscribe: () => void;
7
8
  };
8
9
  }
9
- export declare function resolveMaybeGetter<T>(value: MaybeGetter<T>): T;
10
10
  export declare function createStoreSubscriber<TState>(store: SubscribableStore<TState>): () => void;
11
11
  /**
12
12
  * The nearest provider's app hotkeys store, read from the context each time.
@@ -35,4 +35,3 @@ export declare function syncAppHotkeysStore(getStore: () => AppHotkeysStore | nu
35
35
  * whenever the key from `getHotkeysProviderScopeKey` changes.
36
36
  */
37
37
  export declare function activateScopeWhileMounted(getKey: () => string | null): void;
38
- export {};
@@ -1,9 +1,7 @@
1
1
  import { publishAppHotkeysStore } from '@tanstack/hotkeys';
2
2
  import { activateHotkeysProviderScope } from '@tanstack/hotkeys/adapter';
3
3
  import { createSubscriber } from 'svelte/reactivity';
4
- export function resolveMaybeGetter(value) {
5
- return typeof value === 'function' ? value() : value;
6
- }
4
+ export { resolveMaybeGetter } from '@tanstack/hotkeys/adapter';
7
5
  export function createStoreSubscriber(store) {
8
6
  return createSubscriber((update) => {
9
7
  const subscription = store.subscribe(update);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-hotkeys",
3
- "version": "0.24.0",
3
+ "version": "0.24.1",
4
4
  "description": "Svelte adapter for TanStack Hotkeys",
5
5
  "author": "Kunal Rao",
6
6
  "license": "MIT",
@@ -41,7 +41,7 @@
41
41
  "skills"
42
42
  ],
43
43
  "dependencies": {
44
- "@tanstack/hotkeys": "0.29.0"
44
+ "@tanstack/hotkeys": "0.30.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@sveltejs/package": "^3.0.2",