@tanstack/svelte-hotkeys 0.25.6 → 0.27.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.
@@ -1,6 +1,6 @@
1
1
  import { createAppHotkeysStore, getHotkeysProviderScopeKey, mergeHotkeysProviderDefaults, } from '@tanstack/hotkeys/adapter';
2
2
  import { createContext } from 'svelte';
3
- import { activateScopeWhileMounted, syncAppHotkeysStore, } from './internal.svelte.js';
3
+ import { activateScopeWhileMounted, suspendCharacterKeysWhileMounted, syncAppHotkeysStore, } from './internal.svelte.js';
4
4
  export const DEFAULT_OPTIONS = {};
5
5
  const [useHotkeysContext, setHotkeysContextValue] = createContext();
6
6
  /**
@@ -77,6 +77,8 @@ export function setHotkeysContextSource(source) {
77
77
  parent: parent?.defaultOptions.hotkey?.scope,
78
78
  });
79
79
  });
80
+ // Suspend character key shortcuts while mounted, as the user's setting asks.
81
+ suspendCharacterKeysWhileMounted(() => source().suspendCharacterKeys);
80
82
  return context;
81
83
  }
82
84
  export function getHotkeysContext() {
@@ -11,6 +11,7 @@
11
11
  scope,
12
12
  exclusive,
13
13
  enabled,
14
+ suspendCharacterKeys,
14
15
  }: HotkeysProviderProps = $props()
15
16
 
16
17
  setHotkeysContextSource(() => ({
@@ -21,6 +22,7 @@
21
22
  scope,
22
23
  exclusive,
23
24
  enabled,
25
+ suspendCharacterKeys,
24
26
  }))
25
27
  </script>
26
28
 
@@ -0,0 +1,11 @@
1
+ import { formatForAria as coreFormatForAria } from '@tanstack/hotkeys';
2
+ /**
3
+ * Formats a hotkey for `aria-keyshortcuts`, without the provider's platform or
4
+ * the user's keyboard layout.
5
+ *
6
+ * @deprecated Use `getFormatForAria()` 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
+ export declare const formatForAria: typeof coreFormatForAria;
@@ -0,0 +1,11 @@
1
+ import { formatForAria as coreFormatForAria } from '@tanstack/hotkeys';
2
+ /**
3
+ * Formats a hotkey for `aria-keyshortcuts`, without the provider's platform or
4
+ * the user's keyboard layout.
5
+ *
6
+ * @deprecated Use `getFormatForAria()` 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
+ export const formatForAria = coreFormatForAria;
@@ -0,0 +1,31 @@
1
+ import { formatForAria } from '@tanstack/hotkeys';
2
+ /**
3
+ * Returns `formatForAria` with the provider's `platform` as the default. Use
4
+ * its value for `aria-keyshortcuts`, so screen readers announce the shortcut.
5
+ * It returns `undefined` for a multi-step sequence, no hotkey, or a hotkey
6
+ * that can't fire right now (one that is disabled, in a suspended scope, a
7
+ * suspended character key, or not registered), so Svelte omits the attribute.
8
+ * Call during component initialization, like `getContext`; the returned
9
+ * function works anywhere and reads the provider's current defaults. An
10
+ * explicit `platform` option wins.
11
+ *
12
+ * Physical bindings such as `[KeyQ]` name the key the user presses in
13
+ * Chromium browsers, once the keyboard layout has loaded. See
14
+ * `getKeyboardLayout`. A `layoutMap` passed to the returned function wins.
15
+ *
16
+ * Server-rendered apps should set the provider's `defaultOptions.platform`
17
+ * from the request user agent with `detectPlatform(userAgent)`.
18
+ *
19
+ * @example
20
+ * ```svelte
21
+ * <script lang="ts">
22
+ * const appHotkeys = getAppHotkeys()
23
+ * const formatForAria = getFormatForAria()
24
+ * </script>
25
+ *
26
+ * <button aria-keyshortcuts={formatForAria(appHotkeys.current['search.open'].hotkey)}>
27
+ * Search
28
+ * </button>
29
+ * ```
30
+ */
31
+ export declare function getFormatForAria(): typeof formatForAria;
@@ -0,0 +1,43 @@
1
+ import { formatForAria } from '@tanstack/hotkeys';
2
+ import { announceLiveHotkeys, createFormatter } from '@tanstack/hotkeys/adapter';
3
+ import { getHotkeysContext } from './HotkeysCtx.js';
4
+ import { getHotkeyRegistrations } from './getHotkeyRegistrations.svelte.js';
5
+ import { getKeyboardLayout } from './getKeyboardLayout.svelte.js';
6
+ /**
7
+ * Returns `formatForAria` with the provider's `platform` as the default. Use
8
+ * its value for `aria-keyshortcuts`, so screen readers announce the shortcut.
9
+ * It returns `undefined` for a multi-step sequence, no hotkey, or a hotkey
10
+ * that can't fire right now (one that is disabled, in a suspended scope, a
11
+ * suspended character key, or not registered), so Svelte omits the attribute.
12
+ * Call during component initialization, like `getContext`; the returned
13
+ * function works anywhere and reads the provider's current defaults. An
14
+ * explicit `platform` option wins.
15
+ *
16
+ * Physical bindings such as `[KeyQ]` name the key the user presses in
17
+ * Chromium browsers, once the keyboard layout has loaded. See
18
+ * `getKeyboardLayout`. A `layoutMap` passed to the returned function wins.
19
+ *
20
+ * Server-rendered apps should set the provider's `defaultOptions.platform`
21
+ * from the request user agent with `detectPlatform(userAgent)`.
22
+ *
23
+ * @example
24
+ * ```svelte
25
+ * <script lang="ts">
26
+ * const appHotkeys = getAppHotkeys()
27
+ * const formatForAria = getFormatForAria()
28
+ * </script>
29
+ *
30
+ * <button aria-keyshortcuts={formatForAria(appHotkeys.current['search.open'].hotkey)}>
31
+ * Search
32
+ * </button>
33
+ * ```
34
+ */
35
+ export function getFormatForAria() {
36
+ const context = getHotkeysContext();
37
+ const keyboard = getKeyboardLayout();
38
+ const registrations = getHotkeyRegistrations();
39
+ // Only a physical key reads the layout, so other values don't render again
40
+ // when it loads. Every value reads the registrations, so it follows whether
41
+ // the hotkey can fire.
42
+ return announceLiveHotkeys(createFormatter(formatForAria, () => context?.defaultOptions.platform, () => keyboard.layout), () => registrations.hotkeys);
43
+ }
@@ -0,0 +1,35 @@
1
+ import type { LockKey } from '@tanstack/hotkeys';
2
+ export interface SvelteKeyLockState {
3
+ readonly value: boolean | null;
4
+ }
5
+ /**
6
+ * Svelte function that returns reactive access to whether a lock key, such as
7
+ * Caps Lock, is on.
8
+ *
9
+ * Lock keys toggle instead of being held, so this reads the lock state that
10
+ * the browser reports on key and mouse events. Its `value` is `null` until the
11
+ * user presses a key, clicks, or moves the mouse, after the window loses
12
+ * focus or the page is hidden, and always for Num Lock and Scroll Lock on macOS and iOS.
13
+ *
14
+ * @param key - `'CapsLock'`, `'NumLock'`, or `'ScrollLock'`
15
+ * @returns Object with a reactive `value` property: `true` when the lock is
16
+ * on, `false` when it is off, or `null`
17
+ *
18
+ * @example
19
+ * ```svelte
20
+ * <script>
21
+ * import { getKeyLockState } from '@tanstack/svelte-hotkeys'
22
+ *
23
+ * const capsLock = getKeyLockState('CapsLock')
24
+ * </script>
25
+ *
26
+ * <label>
27
+ * Password
28
+ * <input type="password" />
29
+ * {#if capsLock.value}
30
+ * <span role="status">Caps Lock is on</span>
31
+ * {/if}
32
+ * </label>
33
+ * ```
34
+ */
35
+ export declare function getKeyLockState(key: LockKey): SvelteKeyLockState;
@@ -0,0 +1,47 @@
1
+ import { getKeyStateTracker } from '@tanstack/hotkeys';
2
+ import { createStoreSubscriber } from './internal.svelte.js';
3
+ class KeyLockState {
4
+ #tracker = getKeyStateTracker();
5
+ #key;
6
+ #subscribe = createStoreSubscriber(this.#tracker.store);
7
+ constructor(key) {
8
+ this.#key = key;
9
+ }
10
+ get value() {
11
+ this.#subscribe();
12
+ return this.#tracker.store.state.locks[this.#key];
13
+ }
14
+ }
15
+ /**
16
+ * Svelte function that returns reactive access to whether a lock key, such as
17
+ * Caps Lock, is on.
18
+ *
19
+ * Lock keys toggle instead of being held, so this reads the lock state that
20
+ * the browser reports on key and mouse events. Its `value` is `null` until the
21
+ * user presses a key, clicks, or moves the mouse, after the window loses
22
+ * focus or the page is hidden, and always for Num Lock and Scroll Lock on macOS and iOS.
23
+ *
24
+ * @param key - `'CapsLock'`, `'NumLock'`, or `'ScrollLock'`
25
+ * @returns Object with a reactive `value` property: `true` when the lock is
26
+ * on, `false` when it is off, or `null`
27
+ *
28
+ * @example
29
+ * ```svelte
30
+ * <script>
31
+ * import { getKeyLockState } from '@tanstack/svelte-hotkeys'
32
+ *
33
+ * const capsLock = getKeyLockState('CapsLock')
34
+ * </script>
35
+ *
36
+ * <label>
37
+ * Password
38
+ * <input type="password" />
39
+ * {#if capsLock.value}
40
+ * <span role="status">Caps Lock is on</span>
41
+ * {/if}
42
+ * </label>
43
+ * ```
44
+ */
45
+ export function getKeyLockState(key) {
46
+ return new KeyLockState(key);
47
+ }
package/dist/index.d.ts CHANGED
@@ -7,11 +7,14 @@ export * from './getHotkeyRegistrations.svelte.js';
7
7
  export * from './getHotkeyScopesState.svelte.js';
8
8
  export * from './getHeldKeyCodesMap.svelte.js';
9
9
  export * from './getIsKeyHeld.svelte.js';
10
+ export * from './getKeyLockState.svelte.js';
10
11
  export * from './getAppHotkeys.svelte.js';
11
12
  export { default as HotkeysProvider } from './HotkeysProvider.svelte';
12
13
  export { DEFAULT_OPTIONS, getDefaultHotkeysOptions, getHotkeysContext, setHotkeysContext, } from './HotkeysCtx.js';
13
14
  export type { HotkeysContextOptions, HotkeysProviderOptions, HotkeysProviderProps, } from './HotkeysCtx.js';
14
15
  export * from './getHotkeyHint.svelte.js';
15
16
  export * from './getFormatForDisplay.js';
17
+ export * from './getFormatForAria.js';
16
18
  export { formatForDisplay } from './formatForDisplay.js';
19
+ export { formatForAria } from './formatForAria.js';
17
20
  export * from './getKeyboardLayout.svelte.js';
package/dist/index.js CHANGED
@@ -8,11 +8,14 @@ export * from './getHotkeyRegistrations.svelte.js';
8
8
  export * from './getHotkeyScopesState.svelte.js';
9
9
  export * from './getHeldKeyCodesMap.svelte.js';
10
10
  export * from './getIsKeyHeld.svelte.js';
11
+ export * from './getKeyLockState.svelte.js';
11
12
  export * from './getAppHotkeys.svelte.js';
12
13
  export { default as HotkeysProvider } from './HotkeysProvider.svelte';
13
14
  export { DEFAULT_OPTIONS, getDefaultHotkeysOptions, getHotkeysContext, setHotkeysContext, } from './HotkeysCtx.js';
14
15
  export * from './getHotkeyHint.svelte.js';
15
16
  export * from './getFormatForDisplay.js';
17
+ export * from './getFormatForAria.js';
16
18
  // Replaces the core export, to point components to `getFormatForDisplay`
17
19
  export { formatForDisplay } from './formatForDisplay.js';
20
+ export { formatForAria } from './formatForAria.js';
18
21
  export * from './getKeyboardLayout.svelte.js';
@@ -35,3 +35,8 @@ 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
+ /**
39
+ * Suspends character key shortcuts while the component is mounted and
40
+ * `getSuspend` returns true.
41
+ */
42
+ export declare function suspendCharacterKeysWhileMounted(getSuspend: () => boolean | undefined): void;
@@ -1,4 +1,4 @@
1
- import { publishAppHotkeysStore } from '@tanstack/hotkeys';
1
+ import { getHotkeyManager, publishAppHotkeysStore } from '@tanstack/hotkeys';
2
2
  import { activateHotkeysProviderScope } from '@tanstack/hotkeys/adapter';
3
3
  import { createSubscriber } from 'svelte/reactivity';
4
4
  export { resolveMaybeGetter } from '@tanstack/hotkeys/adapter';
@@ -68,3 +68,11 @@ export function activateScopeWhileMounted(getKey) {
68
68
  const key = $derived(getKey());
69
69
  $effect(() => (key ? activateHotkeysProviderScope(key) : undefined));
70
70
  }
71
+ /**
72
+ * Suspends character key shortcuts while the component is mounted and
73
+ * `getSuspend` returns true.
74
+ */
75
+ export function suspendCharacterKeysWhileMounted(getSuspend) {
76
+ const suspend = $derived(!!getSuspend());
77
+ $effect(() => suspend ? getHotkeyManager().suspendCharacterKeys() : undefined);
78
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/svelte-hotkeys",
3
- "version": "0.25.6",
3
+ "version": "0.27.0",
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.36.0"
44
+ "@tanstack/hotkeys": "0.38.0"
45
45
  },
46
46
  "devDependencies": {
47
47
  "@sveltejs/package": "^3.0.2",
@@ -55,29 +55,31 @@ Before starting, load the `@tanstack/hotkeys#hotkeys-core` skill (`npx @tanstack
55
55
 
56
56
  ## API
57
57
 
58
- | API | What it's for |
59
- | ------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
60
- | `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey on the document while the component is mounted; pass getters for values that change. |
61
- | `createHotkeys(definitions, commonOptions?)` | Register several hotkeys on the document, or a getter of a list built from data. |
62
- | `createHotkeyAttachment(hotkey, callback, options?)` | Create an attachment for `{@attach}` that listens on one element. |
63
- | `createHotkeysAttachment(definitions, commonOptions?)` | Create an attachment that registers several hotkeys on one element. |
64
- | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, and app hotkeys for the components inside it. |
65
- | `setHotkeysContext(options)` | Set the same options from a component's script instead of rendering `HotkeysProvider`; they apply to that component too. |
66
- | `getHotkeysContext()` | Read the nearest context's merged defaults and enabled state, or `null` outside one. |
67
- | `getDefaultHotkeysOptions()` | Read the default options merged from every context above. |
68
- | `getFormatForDisplay()` | 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 = getFormatForDisplay()` during component initialization, then `formatForDisplay(hotkey)`. |
69
- | `getAppHotkeys()` | Read `.current`, the declared app hotkeys with the user's custom hotkeys applied. |
70
- | `getAppHotkeyList()` | Read `.current`, every app hotkey with its status, for a cheat sheet or settings screen. |
71
- | `getCustomHotkeys()` | Read the user's custom hotkeys from `.current`, change or reset them, and check a new one for conflicts. |
72
- | `createHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
73
- | `getHotkeyRegistrations()` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
74
- | `getHotkeyScopesState()` | Read `.state`, the active scopes and the exclusive scope that owns the keyboard. |
75
- | `getHotkeyHint(hotkey, options?)` | Read `.visible`, 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. |
76
- | `getIsKeyHeld(key)` | Read `.held`, whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
77
- | `getHeldKeys()` | Read `.keys`, every key that's held right now. |
78
- | `getHeldKeyCodesMap()` | Read `.codes`, which maps each held key to its physical `event.code`. |
58
+ | API | What it's for |
59
+ | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
60
+ | `createHotkey(hotkey, callback, options?)` | Register one chord, sequence, or app hotkey on the document while the component is mounted; pass getters for values that change. |
61
+ | `createHotkeys(definitions, commonOptions?)` | Register several hotkeys on the document, or a getter of a list built from data. |
62
+ | `createHotkeyAttachment(hotkey, callback, options?)` | Create an attachment for `{@attach}` that listens on one element. |
63
+ | `createHotkeysAttachment(definitions, commonOptions?)` | Create an attachment that registers several hotkeys on one element. |
64
+ | `<HotkeysProvider>` | Set default options, a scope (`exclusive` for dialogs), `enabled`, `suspendCharacterKeys` (set from a user setting that turns off single-key shortcuts; it applies app-wide while the provider is mounted), and app hotkeys for the components inside it. |
65
+ | `setHotkeysContext(options)` | Set the same options from a component's script instead of rendering `HotkeysProvider`; they apply to that component too. |
66
+ | `getHotkeysContext()` | Read the nearest context's merged defaults and enabled state, or `null` outside one. |
67
+ | `getDefaultHotkeysOptions()` | Read the default options merged from every context above. |
68
+ | `getFormatForDisplay()` | 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 = getFormatForDisplay()` during component initialization, then `formatForDisplay(hotkey)`. |
69
+ | `getFormatForAria()` | Get the function that formats a hotkey for `aria-keyshortcuts`, with the provider's `platform` and the user's keyboard layout filled in. Returns `undefined` for a multi-step sequence, no hotkey, or a hotkey that can't fire right now, such as a disabled or suspended one, so the attribute is omitted. Set `aria-keyshortcuts` on every control that has a shortcut, next to the visible label. |
70
+ | `getAppHotkeys()` | Read `.current`, the declared app hotkeys with the user's custom hotkeys applied. |
71
+ | `getAppHotkeyList()` | Read `.current`, every app hotkey with its status, for a cheat sheet or settings screen. |
72
+ | `getCustomHotkeys()` | Read the user's custom hotkeys from `.current`, change or reset them, and check a new one for conflicts. |
73
+ | `createHotkeyRecorder(options)` | Record a chord or sequence the user presses, for a settings screen. |
74
+ | `getHotkeyRegistrations()` | Read `.hotkeys`, every registered hotkey with its options, `meta`, and `status` (whether it's enabled and in an active scope). |
75
+ | `getHotkeyScopesState()` | Read `.state`, the active scopes and the exclusive scope that owns the keyboard. |
76
+ | `getHotkeyHint(hotkey, options?)` | Read `.visible`, 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. |
77
+ | `getIsKeyHeld(key)` | Read `.held`, whether one key is held. Letters match in any case; other names, such as `Shift`, are case-sensitive. |
78
+ | `getKeyLockState(key)` | Read `.value`, whether `'CapsLock'`, `'NumLock'`, or `'ScrollLock'` is on: `true`, `false`, or `null` until a key press, click, or mouse move reports it. Resets to `null` when the window loses focus. Num Lock and Scroll Lock are always `null` on macOS and iOS. Use it for lock state, not `getIsKeyHeld`, which is true only while the key is down. |
79
+ | `getHeldKeys()` | Read `.keys`, every key that's held right now. |
80
+ | `getHeldKeyCodesMap()` | Read `.codes`, which maps each held key to its physical `event.code`. |
79
81
 
80
- The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `getFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`.
82
+ The package also re-exports `@tanstack/hotkeys`, such as `matchesKeyboardEvent` and `defineAppHotkeys`. Its plain `formatForDisplay` export is deprecated: use `getFormatForDisplay()` in components. Outside a component, call the plain function and pass `platform`. The plain `formatForAria` export is deprecated the same way, in favor of `getFormatForAria()`.
81
83
 
82
84
  No provider is required. Call these at the top level of a component's `<script>`.
83
85