nycklar 1.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 ADDED
@@ -0,0 +1,40 @@
1
+ # 🔑 `nycklar`
2
+
3
+ > A tiny proptype for keybindings, inspired by the [`tinykeys`](https://jamiebuilds.github.io/tinykeys/) lib
4
+
5
+ This is a tiny version of what `tinykeys` can do, and is more of a personal project than something you should use.
6
+
7
+ ## Usage
8
+
9
+ ```js
10
+ import { nycklar } from "nycklar";
11
+
12
+ nycklar(window, {
13
+ "Shift+D": () => {
14
+ console.log("pressed shift and d");
15
+ },
16
+ abc: () => {
17
+ console.log("pressed the abc keys in order");
18
+ },
19
+ });
20
+ ```
21
+
22
+ ### React Hooks Example
23
+
24
+ If you're using nycklar within a component, you should also make use of the returned `cleanup()` function.
25
+
26
+ ```js
27
+ import { useEffect } from "react";
28
+ import { nycklar } from "nycklar";
29
+
30
+ const myKeybindingsHook = () => {
31
+ useEffect(() => {
32
+ let cleanup = nycklar(window, {
33
+ // ...
34
+ });
35
+ return () => {
36
+ cleanup();
37
+ };
38
+ });
39
+ };
40
+ ```
@@ -0,0 +1,33 @@
1
+ //#region src/index.d.ts
2
+ type TEventsMap = Readonly<Record<string, (event: KeyboardEvent) => void>>;
3
+ declare const eventType: readonly ["keydown", "keyup"];
4
+ type TEventType = (typeof eventType)[number];
5
+ interface TOptions {
6
+ timeout?: number;
7
+ event?: TEventType;
8
+ }
9
+ /**
10
+ * A basic prototype of the tinykeys lib
11
+ * Takes a target and a map of key events to listen to an a function to call when matched
12
+ * Returns an unsubscribe method.
13
+ * @see https://github.com/jamiebuilds/tinykeys
14
+ * @example
15
+ * ```js
16
+ * import { nycklar } from "../src/nycklar"
17
+ *
18
+ * nycklar(window, {
19
+ * "d": () => {
20
+ * console.log("pressed d");
21
+ * },
22
+ * "abc": () => {
23
+ * console.log("pressed abc");
24
+ * },
25
+ * "Shift+D": () => {
26
+ * console.log("pressed shift d");
27
+ * },
28
+ * })
29
+ * ```
30
+ */
31
+ declare const nycklar: (target: Window | HTMLElement, keyEvents: TEventsMap, options?: TOptions) => () => void;
32
+ //#endregion
33
+ export { nycklar };
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ const e=(e,t,n)=>{e in t&&t[e](n)},t=(t,n,r={})=>{let i=[],{timeout:a=1e3,event:o=`keydown`}=r,s=null,c=t=>{if(!(t instanceof KeyboardEvent)||t.target instanceof HTMLInputElement||t.target instanceof HTMLTextAreaElement||t.target instanceof HTMLElement&&(t.target.isContentEditable||t.target.contentEditable===`true`))return;let r=t.altKey||t.ctrlKey||t.shiftKey;i.push(t.key);let o=i.join(r?`+`:``);s!==null&&clearTimeout(s),s=setTimeout(()=>{e(o,n,t),i=[]},a)};return t.addEventListener(o,c),()=>{t.removeEventListener(o,c)}};export{t as nycklar};
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "nycklar",
3
+ "version": "1.0.0",
4
+ "author": "Daniel Reed",
5
+ "keywords": [],
6
+ "description": "",
7
+ "license": "MIT",
8
+ "type": "module",
9
+ "exports": {
10
+ ".": {
11
+ "import": "./dist/index.mjs",
12
+ "types": "./dist/index.d.mts"
13
+ }
14
+ },
15
+ "types": "./dist/index.d.mts",
16
+ "files": ["dist"],
17
+ "scripts": {
18
+ "build": "tsdown",
19
+ "dev": "tsdown --watch",
20
+ "lint": "biome check src",
21
+ "lint:fix": "biome check --write .",
22
+ "format": "biome format --write .",
23
+ "typecheck": "tsc --noEmit",
24
+ "test": "vitest run",
25
+ "test:watch": "vitest",
26
+ "playground": "cd playground && pnpm dev"
27
+ },
28
+ "devDependencies": {
29
+ "@biomejs/biome": "^2.3.15",
30
+ "jsdom": "^28.0.0",
31
+ "tsdown": "^0.20.3",
32
+ "typescript": "^5.9.3",
33
+ "vitest": "^4.0.18"
34
+ }
35
+ }