@inklu/keys 1.4.1 → 2.0.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.
- package/README.md +161 -8
- package/dist/{attributes-O6bXNKT9.d.ts → attributes-DotLr2MW.d.mts} +142 -23
- package/dist/{attributes.d.ts → attributes.d.mts} +1 -1
- package/dist/{attributes.js → attributes.mjs} +12 -5
- package/dist/{index-B5iiRwCy.d.ts → index-BTgscYdI.d.mts} +25 -2
- package/dist/index.d.mts +3 -0
- package/dist/index.mjs +3 -0
- package/dist/react.d.mts +82 -0
- package/dist/{react.js → react.mjs} +24 -13
- package/dist/src-DgYjA1oS.mjs +1697 -0
- package/package.json +15 -42
- package/dist/index.d.ts +0 -3
- package/dist/index.js +0 -3
- package/dist/react.d.ts +0 -77
- package/dist/src-DWmUPZSL.js +0 -1078
package/dist/react.d.mts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { A as HotkeySequence, C as Command, D as Conflict, E as CommandSource, F as SequenceBinding, I as SerializedBinding, M as KeymapStorage, N as Platform, O as ConflictKind, P as ScopeOptions, S as Chord, T as CommandDefinition, _ as bindingTokens, a as Commands, b as Binding, c as PendingSequence, d as createCommands, f as CommandKeymap, g as ariaKeyShortcuts, h as DisplayOptions, i as CommandId, j as KeymapPersistence, k as Hotkey, l as RunBlockReason, m as SerializedOverride, n as observe, o as CommandsOptions, p as SerializedKeymap, r as CommandHandle, s as ConflictOptions, t as ObserveOptions, u as RunOptions, v as formatBinding, w as CommandContext, x as BindingInput, y as spokenBinding } from "./attributes-DotLr2MW.mjs";
|
|
2
|
+
import { a as RebindState, c as BindingValidation, d as validateBinding, i as RebindOptions, l as isSingleKeyBinding, n as ALWAYS_SCOPE, o as Rebinder, r as RebindMode, s as createRebinder, t as matchCommands, u as seq } from "./index-BTgscYdI.mjs";
|
|
3
|
+
//#region src/react/hooks.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Mounts a commands registry for the lifecycle of the component.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```tsx
|
|
9
|
+
* function App() {
|
|
10
|
+
* useMountCommands(commands);
|
|
11
|
+
* return <Shortcuts />;
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
export declare function useMountCommands<Id extends string>(registry: Commands<Id>): void;
|
|
16
|
+
/** Returns all registered commands, re-rendering when the registry changes. */
|
|
17
|
+
export declare function useCommands<Id extends string>(registry: Commands<Id>): ReadonlyArray<Command<Id>>;
|
|
18
|
+
/** Returns a specific command snapshot by ID. */
|
|
19
|
+
export declare function useCommand<Id extends string>(registry: Commands<Id>, id: NoInfer<Id>): Command<Id> | undefined;
|
|
20
|
+
/**
|
|
21
|
+
* Returns display and accessibility shortcuts for a command's primary
|
|
22
|
+
* binding: `tokens` for `<kbd>` elements, `aria` for `aria-keyshortcuts`
|
|
23
|
+
* (chords only), and `spoken` for screen-reader text such as `"G then I"`.
|
|
24
|
+
*/
|
|
25
|
+
export declare function useShortcut<Id extends string>(registry: Commands<Id>, id: NoInfer<Id>): {
|
|
26
|
+
display: string;
|
|
27
|
+
tokens: ReadonlyArray<string>;
|
|
28
|
+
aria: string;
|
|
29
|
+
spoken: string;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Activates a scope for the lifecycle of the component.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```tsx
|
|
36
|
+
* useScope(commands, "dialog", isOpen, { exclusive: true });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function useScope<Id extends string>(registry: Commands<Id>, name: string, active?: boolean, options?: ScopeOptions): void;
|
|
40
|
+
/** Registers a dynamic command for the lifecycle of the component. */
|
|
41
|
+
export declare function useRegisterCommand<Id extends string>(registry: Commands<Id>, id: NoInfer<Id> | (string & {}), definition: CommandDefinition<NoInfer<Id>>): void;
|
|
42
|
+
/** State and actions for rebinding a command. */
|
|
43
|
+
interface UseRebindResult<Id extends string = string> extends RebindState<Id> {
|
|
44
|
+
/** Active display string for the shortcut. */
|
|
45
|
+
display: string;
|
|
46
|
+
/** Display tokens for rendering `<kbd>` elements. */
|
|
47
|
+
tokens: ReadonlyArray<string>;
|
|
48
|
+
/** Whether the shortcut has been customized by the user. */
|
|
49
|
+
overridden: boolean;
|
|
50
|
+
start: () => void;
|
|
51
|
+
stop: () => void;
|
|
52
|
+
cancel: () => void;
|
|
53
|
+
commit: () => void;
|
|
54
|
+
clear: () => void;
|
|
55
|
+
reset: () => void;
|
|
56
|
+
}
|
|
57
|
+
/** Hook for recording and configuring a command's keybindings. */
|
|
58
|
+
export declare function useRebind<Id extends string>(registry: Commands<Id>, id: NoInfer<Id>, options?: RebindOptions<Id>): UseRebindResult<Id>;
|
|
59
|
+
/** Returns an array of keys currently held down. */
|
|
60
|
+
export declare function useHeldKeys(): ReadonlyArray<string>;
|
|
61
|
+
/**
|
|
62
|
+
* Checks whether a specific key is currently held down.
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* ```tsx
|
|
66
|
+
* const shift = useKeyHold("Shift");
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare function useKeyHold(key: string): boolean;
|
|
70
|
+
/** Returns a map of held key identifiers to physical event codes. */
|
|
71
|
+
export declare function useHeldKeyCodes(): Readonly<Record<string, string>>;
|
|
72
|
+
/**
|
|
73
|
+
* Returns currently active partially-typed sequences.
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* ```tsx
|
|
77
|
+
* const pending = usePendingSequences(commands);
|
|
78
|
+
* ```
|
|
79
|
+
*/
|
|
80
|
+
export declare function usePendingSequences<Id extends string>(registry: Commands<Id>): ReadonlyArray<PendingSequence<Id>>;
|
|
81
|
+
//#endregion
|
|
82
|
+
export { ALWAYS_SCOPE, type Binding, type BindingInput, type BindingValidation, type Chord, type Command, type CommandContext, type CommandDefinition, type CommandHandle, type CommandId, type CommandKeymap, type CommandSource, type Commands, type CommandsOptions, type Conflict, type ConflictKind, type ConflictOptions, type DisplayOptions, type Hotkey, type HotkeySequence, type KeymapPersistence, type KeymapStorage, type ObserveOptions, type PendingSequence, type Platform, type RebindMode, type RebindOptions, type RebindState, type Rebinder, type RunBlockReason, type RunOptions, type ScopeOptions, type SequenceBinding, type SerializedBinding, type SerializedKeymap, type SerializedOverride, type UseRebindResult, ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, formatBinding, isSingleKeyBinding, matchCommands, observe, seq, spokenBinding, validateBinding };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { observe } from "./attributes.
|
|
3
|
-
import { a as
|
|
2
|
+
import { observe } from "./attributes.mjs";
|
|
3
|
+
import { a as ariaKeyShortcuts, c as spokenBinding, d as seq, f as toBindings, i as createRebinder, l as bindingSignature, n as createCommands, o as bindingTokens, p as validateBinding, r as ALWAYS_SCOPE, s as formatBinding, t as matchCommands, u as isSingleKeyBinding } from "./src-DgYjA1oS.mjs";
|
|
4
4
|
import { getKeyStateTracker } from "@tanstack/hotkeys";
|
|
5
5
|
import { useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
|
6
6
|
//#region src/react/hooks.ts
|
|
@@ -28,7 +28,8 @@ function useMountCommands(registry) {
|
|
|
28
28
|
function useCommandList(registry) {
|
|
29
29
|
const subscribe = useCallback((onChange) => registry.subscribe(onChange), [registry]);
|
|
30
30
|
const getSnapshot = useCallback(() => registry.list(), [registry]);
|
|
31
|
-
|
|
31
|
+
const getServerSnapshot = useCallback(() => registry.serverList(), [registry]);
|
|
32
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
32
33
|
}
|
|
33
34
|
/** Returns all registered commands, re-rendering when the registry changes. */
|
|
34
35
|
function useCommands(registry) {
|
|
@@ -37,19 +38,20 @@ function useCommands(registry) {
|
|
|
37
38
|
/** Returns a specific command snapshot by ID. */
|
|
38
39
|
function useCommand(registry, id) {
|
|
39
40
|
const commands = useCommandList(registry);
|
|
40
|
-
return useMemo(() =>
|
|
41
|
-
registry,
|
|
42
|
-
commands,
|
|
43
|
-
id
|
|
44
|
-
]);
|
|
41
|
+
return useMemo(() => commands.find((command) => command.id === id), [commands, id]);
|
|
45
42
|
}
|
|
46
|
-
/**
|
|
43
|
+
/**
|
|
44
|
+
* Returns display and accessibility shortcuts for a command's primary
|
|
45
|
+
* binding: `tokens` for `<kbd>` elements, `aria` for `aria-keyshortcuts`
|
|
46
|
+
* (chords only), and `spoken` for screen-reader text such as `"G then I"`.
|
|
47
|
+
*/
|
|
47
48
|
function useShortcut(registry, id) {
|
|
48
49
|
const command = useCommand(registry, id);
|
|
49
50
|
return useMemo(() => ({
|
|
50
51
|
display: command?.display[0] ?? "",
|
|
51
52
|
tokens: command?.tokens[0] ?? EMPTY_TOKENS,
|
|
52
|
-
aria: command?.aria ?? ""
|
|
53
|
+
aria: command?.aria ?? "",
|
|
54
|
+
spoken: command?.spoken ?? ""
|
|
53
55
|
}), [command]);
|
|
54
56
|
}
|
|
55
57
|
/**
|
|
@@ -62,14 +64,19 @@ function useShortcut(registry, id) {
|
|
|
62
64
|
*/
|
|
63
65
|
function useScope(registry, name, active = true, options = {}) {
|
|
64
66
|
const exclusive = options.exclusive === true;
|
|
67
|
+
const strict = options.strict === true;
|
|
65
68
|
useEffect(() => {
|
|
66
69
|
if (!active) return;
|
|
67
|
-
return registry.pushScope(name, {
|
|
70
|
+
return registry.pushScope(name, {
|
|
71
|
+
exclusive,
|
|
72
|
+
strict
|
|
73
|
+
});
|
|
68
74
|
}, [
|
|
69
75
|
registry,
|
|
70
76
|
name,
|
|
71
77
|
active,
|
|
72
|
-
exclusive
|
|
78
|
+
exclusive,
|
|
79
|
+
strict
|
|
73
80
|
]);
|
|
74
81
|
}
|
|
75
82
|
/** Registers a dynamic command for the lifecycle of the component. */
|
|
@@ -95,6 +102,10 @@ function definitionSignature(definition) {
|
|
|
95
102
|
toBindings(definition.keys).map((binding) => bindingSignature(binding)).join("|"),
|
|
96
103
|
definition.scope ?? "",
|
|
97
104
|
definition.title,
|
|
105
|
+
definition.description ?? "",
|
|
106
|
+
definition.group ?? "",
|
|
107
|
+
(definition.keywords ?? []).join(","),
|
|
108
|
+
definition.hidden === true ? "1" : "0",
|
|
98
109
|
definition.enabled === false ? "0" : "1",
|
|
99
110
|
definition.preventDefault === false ? "0" : "1",
|
|
100
111
|
definition.ignoreInputs === void 0 ? "" : String(definition.ignoreInputs),
|
|
@@ -211,4 +222,4 @@ function usePendingSequences(registry) {
|
|
|
211
222
|
return useSyncExternalStore(subscribe, getSnapshot, getEmptyPending);
|
|
212
223
|
}
|
|
213
224
|
//#endregion
|
|
214
|
-
export { ALWAYS_SCOPE, ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, formatBinding, observe, seq, useCommand, useCommands, useHeldKeyCodes, useHeldKeys, useKeyHold, useMountCommands, usePendingSequences, useRebind, useRegisterCommand, useScope, useShortcut, validateBinding };
|
|
225
|
+
export { ALWAYS_SCOPE, ariaKeyShortcuts, bindingTokens, createCommands, createRebinder, formatBinding, isSingleKeyBinding, matchCommands, observe, seq, spokenBinding, useCommand, useCommands, useHeldKeyCodes, useHeldKeys, useKeyHold, useMountCommands, usePendingSequences, useRebind, useRegisterCommand, useScope, useShortcut, validateBinding };
|