@keybindy/react 1.1.0 → 1.1.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.
- package/dist/index.mjs +232 -0
- package/package.json +3 -5
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
// src/Keybindy.tsx
|
|
4
|
+
import React2 from "react";
|
|
5
|
+
|
|
6
|
+
// src/useKeybindy.tsx
|
|
7
|
+
import React from "react";
|
|
8
|
+
import ShortcutManager from "@keybindy/core";
|
|
9
|
+
var sharedInstance = null;
|
|
10
|
+
var useKeybindy = ({
|
|
11
|
+
logs = false,
|
|
12
|
+
onShortcutFired
|
|
13
|
+
} = {}) => {
|
|
14
|
+
const managerRef = React.useRef(null);
|
|
15
|
+
const registeredIds = React.useRef(/* @__PURE__ */ new Set());
|
|
16
|
+
if (!sharedInstance) {
|
|
17
|
+
sharedInstance = new ShortcutManager({ onShortcutFired, silent: !logs });
|
|
18
|
+
}
|
|
19
|
+
if (!managerRef.current) {
|
|
20
|
+
managerRef.current = sharedInstance;
|
|
21
|
+
}
|
|
22
|
+
const log = (...args) => {
|
|
23
|
+
if (logs) console.log("[Keybindy]", ...args);
|
|
24
|
+
};
|
|
25
|
+
const warn = (...args) => {
|
|
26
|
+
if (logs) console.warn("[Keybindy]", ...args);
|
|
27
|
+
};
|
|
28
|
+
const register = React.useCallback(
|
|
29
|
+
(keys, handler, options) => {
|
|
30
|
+
if (keys.length === 0) {
|
|
31
|
+
warn("No keys provided to register");
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const id = options?.data?.id;
|
|
35
|
+
if (id) registeredIds.current.add(id);
|
|
36
|
+
log("Registered:", id ?? keys);
|
|
37
|
+
managerRef.current?.register(keys, handler, options);
|
|
38
|
+
},
|
|
39
|
+
[]
|
|
40
|
+
);
|
|
41
|
+
const unregister = React.useCallback((keys, scope) => {
|
|
42
|
+
if (keys.length === 0) {
|
|
43
|
+
warn("No keys provided to unregister");
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
managerRef.current?.unregister(keys, scope);
|
|
47
|
+
log("Unregistered:", keys);
|
|
48
|
+
}, []);
|
|
49
|
+
const enable = React.useCallback((keys, scope) => {
|
|
50
|
+
if (keys.length === 0) {
|
|
51
|
+
warn("No keys provided to enable");
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
managerRef.current?.enable(keys, scope);
|
|
55
|
+
log("Enabled:", keys);
|
|
56
|
+
}, []);
|
|
57
|
+
const disable = React.useCallback((keys, scope) => {
|
|
58
|
+
if (keys.length === 0) {
|
|
59
|
+
warn("No keys provided to disable");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
managerRef.current?.disable(keys, scope);
|
|
63
|
+
log("Disabled:", keys);
|
|
64
|
+
}, []);
|
|
65
|
+
const toggle = React.useCallback((keys, scope) => {
|
|
66
|
+
if (keys.length === 0) {
|
|
67
|
+
warn("No keys provided to toggle");
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
managerRef.current?.toggle(keys, scope);
|
|
71
|
+
log("Toggled:", keys);
|
|
72
|
+
}, []);
|
|
73
|
+
const getCheatSheet = React.useCallback((scope) => {
|
|
74
|
+
return managerRef.current?.getCheatSheet(scope);
|
|
75
|
+
}, []);
|
|
76
|
+
const getActiveScope = React.useCallback(() => {
|
|
77
|
+
return managerRef.current?.getActiveScope() ?? "";
|
|
78
|
+
}, []);
|
|
79
|
+
const disableAll = React.useCallback((scope) => {
|
|
80
|
+
managerRef.current?.disableAll(scope);
|
|
81
|
+
log(`Disabled all shortcuts${scope ? ` in scope "${scope}"` : ""}`);
|
|
82
|
+
}, []);
|
|
83
|
+
const enableAll = React.useCallback((scope) => {
|
|
84
|
+
managerRef.current?.enableAll(scope);
|
|
85
|
+
log(`Enabled all shortcuts${scope ? ` in scope "${scope}"` : ""}`);
|
|
86
|
+
}, []);
|
|
87
|
+
const setScope = React.useCallback((scope) => {
|
|
88
|
+
managerRef.current?.setActiveScope(scope);
|
|
89
|
+
log("Scope set to:", scope);
|
|
90
|
+
}, []);
|
|
91
|
+
const resetScope = React.useCallback(() => {
|
|
92
|
+
managerRef.current?.resetScope();
|
|
93
|
+
log("Reset scope");
|
|
94
|
+
}, []);
|
|
95
|
+
const getScopes = React.useCallback(() => {
|
|
96
|
+
return managerRef.current?.getScopes() ?? [];
|
|
97
|
+
}, []);
|
|
98
|
+
const isScopeActive = React.useCallback((scope) => {
|
|
99
|
+
return managerRef.current?.isScopeActive(scope);
|
|
100
|
+
}, []);
|
|
101
|
+
const onTyping = React.useCallback(
|
|
102
|
+
(callback) => {
|
|
103
|
+
managerRef.current?.onTyping(callback);
|
|
104
|
+
},
|
|
105
|
+
[]
|
|
106
|
+
);
|
|
107
|
+
const popScope = React.useCallback(() => {
|
|
108
|
+
managerRef.current?.popScope();
|
|
109
|
+
log("Popped scope, active scope is:", managerRef.current?.getActiveScope());
|
|
110
|
+
}, []);
|
|
111
|
+
const pushScope = React.useCallback((scope) => {
|
|
112
|
+
managerRef.current?.pushScope(scope);
|
|
113
|
+
log("Pushed scope:", scope);
|
|
114
|
+
}, []);
|
|
115
|
+
const getScopeInfo = React.useCallback((scope) => {
|
|
116
|
+
return managerRef.current?.getScopesInfo(scope);
|
|
117
|
+
}, []);
|
|
118
|
+
const destroy = () => {
|
|
119
|
+
managerRef.current?.destroy();
|
|
120
|
+
};
|
|
121
|
+
const clear = () => {
|
|
122
|
+
managerRef.current?.clear();
|
|
123
|
+
};
|
|
124
|
+
return {
|
|
125
|
+
register,
|
|
126
|
+
unregister,
|
|
127
|
+
enable,
|
|
128
|
+
disable,
|
|
129
|
+
toggle,
|
|
130
|
+
setScope,
|
|
131
|
+
getCheatSheet,
|
|
132
|
+
destroy,
|
|
133
|
+
getScopeInfo,
|
|
134
|
+
getActiveScope,
|
|
135
|
+
popScope,
|
|
136
|
+
pushScope,
|
|
137
|
+
resetScope,
|
|
138
|
+
getScopes,
|
|
139
|
+
isScopeActive,
|
|
140
|
+
onTyping,
|
|
141
|
+
enableAll,
|
|
142
|
+
clear,
|
|
143
|
+
disableAll,
|
|
144
|
+
manager: managerRef.current
|
|
145
|
+
};
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
// src/Keybindy.tsx
|
|
149
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
150
|
+
var Keybindy = ({
|
|
151
|
+
scope = "global",
|
|
152
|
+
shortcuts = [],
|
|
153
|
+
children,
|
|
154
|
+
disabled,
|
|
155
|
+
onShortcutFired,
|
|
156
|
+
logs = false
|
|
157
|
+
}) => {
|
|
158
|
+
const { register, unregister, manager, pushScope, popScope, getScopes, setScope } = useKeybindy({
|
|
159
|
+
onShortcutFired,
|
|
160
|
+
logs
|
|
161
|
+
});
|
|
162
|
+
const prevScope = React2.useRef(null);
|
|
163
|
+
React2.useEffect(() => {
|
|
164
|
+
prevScope.current = manager.getActiveScope();
|
|
165
|
+
if (!getScopes()?.includes(scope)) {
|
|
166
|
+
pushScope(scope);
|
|
167
|
+
}
|
|
168
|
+
setScope(scope);
|
|
169
|
+
shortcuts.forEach(({ keys, handler, options }) => {
|
|
170
|
+
register(keys, handler, { ...options, scope });
|
|
171
|
+
});
|
|
172
|
+
if (disabled) {
|
|
173
|
+
manager.disableAll(scope);
|
|
174
|
+
}
|
|
175
|
+
return () => {
|
|
176
|
+
shortcuts.forEach(({ keys }) => {
|
|
177
|
+
if (Array.isArray(keys[0])) {
|
|
178
|
+
keys.forEach((key) => unregister(key, scope));
|
|
179
|
+
} else {
|
|
180
|
+
unregister(keys, scope);
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
popScope();
|
|
184
|
+
};
|
|
185
|
+
}, [scope, JSON.stringify(shortcuts)]);
|
|
186
|
+
return /* @__PURE__ */ jsx(Fragment, { children });
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
// src/ShortcutLabel.tsx
|
|
190
|
+
import React3 from "react";
|
|
191
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
192
|
+
var ShortcutLabel = ({ keys, renderKey, style, ...props }) => {
|
|
193
|
+
const isMac = typeof navigator !== "undefined" && /Mac/.test(navigator.userAgent);
|
|
194
|
+
const defaultRenderKey = (key) => {
|
|
195
|
+
switch (key.toLowerCase()) {
|
|
196
|
+
case "meta":
|
|
197
|
+
return isMac ? "\u2318" : "Ctrl";
|
|
198
|
+
case "ctrl":
|
|
199
|
+
return "Ctrl";
|
|
200
|
+
case "shift":
|
|
201
|
+
return "\u21E7";
|
|
202
|
+
case "alt":
|
|
203
|
+
return isMac ? "\u2325" : "Alt";
|
|
204
|
+
case "enter":
|
|
205
|
+
return "\u21B5";
|
|
206
|
+
default:
|
|
207
|
+
return key.toUpperCase();
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
const rendered = renderKey ? keys.map((key, i) => /* @__PURE__ */ jsx2(React3.Fragment, { children: renderKey(key, i, keys) }, i)) : defaultRenderKey ? [keys.map((key) => defaultRenderKey(key)).join(" + ")] : [];
|
|
211
|
+
return /* @__PURE__ */ jsx2(
|
|
212
|
+
"kbd",
|
|
213
|
+
{
|
|
214
|
+
style: {
|
|
215
|
+
fontFamily: "monospace",
|
|
216
|
+
padding: "2.5px 5px",
|
|
217
|
+
border: "1px solid #cccccc2f",
|
|
218
|
+
backgroundColor: "#2e2e2e",
|
|
219
|
+
borderRadius: "4px",
|
|
220
|
+
userSelect: "none",
|
|
221
|
+
...style
|
|
222
|
+
},
|
|
223
|
+
...props,
|
|
224
|
+
children: rendered
|
|
225
|
+
}
|
|
226
|
+
);
|
|
227
|
+
};
|
|
228
|
+
export {
|
|
229
|
+
Keybindy,
|
|
230
|
+
ShortcutLabel,
|
|
231
|
+
useKeybindy
|
|
232
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@keybindy/react",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Keybindy for React: Simple, scoped keyboard shortcuts that require little setup. designed to smoothly blend in with your React applications, allowing for robust keybinding functionality without the overhead.",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "PRASSamin",
|
|
@@ -27,9 +27,7 @@
|
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
|
-
"dist
|
|
31
|
-
"dist/**/*.d.ts",
|
|
32
|
-
"!dist/**/*.map",
|
|
30
|
+
"dist/**/*",
|
|
33
31
|
"package.json",
|
|
34
32
|
"LICENSE.md",
|
|
35
33
|
"README.md"
|
|
@@ -53,7 +51,7 @@
|
|
|
53
51
|
},
|
|
54
52
|
"dependencies": {
|
|
55
53
|
"react": "^19.1.0",
|
|
56
|
-
"@keybindy/core": "1.1.
|
|
54
|
+
"@keybindy/core": "1.1.1"
|
|
57
55
|
},
|
|
58
56
|
"scripts": {
|
|
59
57
|
"format": "prettier --write \"src/**/*.{ts,tsx}\"",
|