@solid-primitives/keyboard 0.0.100

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Solid Primitives Working Group
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ <p>
2
+ <img width="100%" src="https://assets.solidjs.com/banner?type=Primitives&background=tiles&project=keyboard" alt="Solid Primitives keyboard">
3
+ </p>
4
+
5
+ # @solid-primitives/keyboard
6
+
7
+ [![lerna](https://img.shields.io/badge/maintained%20with-lerna-cc00ff.svg?style=for-the-badge)](https://lerna.js.org/)
8
+ [![size](https://img.shields.io/bundlephobia/minzip/@solid-primitives/keyboard?style=for-the-badge&label=size)](https://bundlephobia.com/package/@solid-primitives/keyboard)
9
+ [![version](https://img.shields.io/npm/v/@solid-primitives/keyboard?style=for-the-badge)](https://www.npmjs.com/package/@solid-primitives/keyboard)
10
+ [![stage](https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Fraw.githubusercontent.com%2Fsolidjs-community%2Fsolid-primitives%2Fmain%2Fassets%2Fbadges%2Fstage-0.json)](https://github.com/solidjs-community/solid-primitives#contribution-process)
11
+
12
+ A library of reactive promitives helping handling user's keyboard input.
13
+
14
+ - [`makeKeyHoldListener`](#makeKeyHoldListener) - Attaches keyboard event-listeners, and triggers callback whenever user holds or stops holding specified key.
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @solid-primitives/keyboard
20
+ # or
21
+ yarn add @solid-primitives/keyboard
22
+ ```
23
+
24
+ ## `makeKeyHoldListener`
25
+
26
+ Attaches keyboard event-listeners to `window`, and calls provided callback whenever user holds or stops holding specified key.
27
+
28
+ Event listeners are automatically cleaned on root dispose.
29
+
30
+ ### How to use it
31
+
32
+ `makeKeyHoldListener` takes three arguments:
33
+
34
+ - `key` keyboard key or modifier to listen for
35
+ - `onHoldChange` callback fired when the hold state changes
36
+ - `options` additional configuration:
37
+ - `preventDefault` — call `e.preventDefault()` on the keyboard event, when the specified key is pressed. _(Defaults to `false`)_
38
+ - `allowOtherKeys` — Should the user be allowed to press other keys while holding the specified one _(Defaults to `false`)_
39
+
40
+ ```tsx
41
+ import { makeKeyHoldListener } from "@solid-primitives/keyboard";
42
+
43
+ const [pressing, setPressing] = createSignal(false);
44
+
45
+ makeKeyHoldListener("altKey", setPressing, {
46
+ preventDefault: true
47
+ });
48
+
49
+ <p>Is pressing Alt? {pressing() ? "YES" : "NO"}</p>;
50
+ ```
51
+
52
+ ## Changelog
53
+
54
+ <details>
55
+ <summary><b>Expand Changelog</b></summary>
56
+
57
+ 0.0.100
58
+
59
+ Initial release as a Stage-0 primitive.
60
+
61
+ </details>
package/dist/index.cjs ADDED
@@ -0,0 +1,73 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/index.ts
20
+ var src_exports = {};
21
+ __export(src_exports, {
22
+ makeKeyHoldListener: () => makeKeyHoldListener
23
+ });
24
+ module.exports = __toCommonJS(src_exports);
25
+
26
+ // src/holdKeyListener.ts
27
+ var import_event_listener = require("@solid-primitives/event-listener");
28
+ var keyModifiers = ["altKey", "ctrlKey", "metaKey", "shiftKey"];
29
+ function makeKeyHoldListener(key, onHoldChange, options = {}) {
30
+ const { preventDefault, allowOtherKeys } = options;
31
+ const modifier = keyModifiers.includes(key) ? key : void 0;
32
+ let state = false;
33
+ let actualPressed = false;
34
+ const updateState = (newState) => {
35
+ newState !== state && onHoldChange(state = newState);
36
+ };
37
+ (0, import_event_listener.makeEventListener)(window, "keydown", modifier ? (e) => {
38
+ if (e.repeat)
39
+ return;
40
+ if (e[modifier]) {
41
+ if (!state && !actualPressed) {
42
+ preventDefault && e.preventDefault();
43
+ onHoldChange(state = actualPressed = true);
44
+ } else if (!allowOtherKeys)
45
+ updateState(false);
46
+ } else
47
+ updateState(actualPressed = false);
48
+ } : (e) => {
49
+ if (e.key !== key) {
50
+ allowOtherKeys || updateState(false);
51
+ return;
52
+ }
53
+ if (e.repeat)
54
+ return;
55
+ updateState(true);
56
+ });
57
+ (0, import_event_listener.makeEventListener)(window, "keyup", modifier ? (e) => {
58
+ if (e[modifier])
59
+ allowOtherKeys || updateState(false);
60
+ else
61
+ updateState(actualPressed = false);
62
+ } : (e) => {
63
+ if (e.key !== key)
64
+ allowOtherKeys || updateState(false);
65
+ else
66
+ updateState(false);
67
+ });
68
+ (0, import_event_listener.makeEventListener)(document, "visibilitychange", () => document.visibilityState !== "visible" && updateState(false));
69
+ }
70
+ // Annotate the CommonJS export names for ESM import in node:
71
+ 0 && (module.exports = {
72
+ makeKeyHoldListener
73
+ });
@@ -0,0 +1,19 @@
1
+ declare type KeyModifier = "altKey" | "ctrlKey" | "metaKey" | "shiftKey";
2
+ declare type KeyToHold = KeyModifier | (string & {});
3
+ /**
4
+ * Attaches keyboard event-listeners to `window`, and calls {@link onHoldChange} callback whenever user holds or stops holding specified {@link key}.
5
+ *
6
+ * Event listeners are automatically cleaned on root dispose.
7
+ *
8
+ * @param key keyboard key or modifier to listen for
9
+ * @param onHoldChange callback fired when the hold state changes
10
+ * @param options additional configuration
11
+ * - `options.preventDefault` — call `e.preventDefault()` on the keyboard event, when the specified {@link key} is pressed. *(Defaults to `false`)*
12
+ * - `options.allowOtherKeys` — Should the user be allowed to press other keys while holding the specified one *(Defaults to `false`)*
13
+ */
14
+ declare function makeKeyHoldListener(key: KeyToHold, onHoldChange: (isHolding: boolean) => void, options?: {
15
+ preventDefault?: boolean;
16
+ allowOtherKeys?: boolean;
17
+ }): void;
18
+
19
+ export { KeyModifier, KeyToHold, makeKeyHoldListener };
package/dist/index.js ADDED
@@ -0,0 +1,47 @@
1
+ // src/holdKeyListener.ts
2
+ import { makeEventListener } from "@solid-primitives/event-listener";
3
+ var keyModifiers = ["altKey", "ctrlKey", "metaKey", "shiftKey"];
4
+ function makeKeyHoldListener(key, onHoldChange, options = {}) {
5
+ const { preventDefault, allowOtherKeys } = options;
6
+ const modifier = keyModifiers.includes(key) ? key : void 0;
7
+ let state = false;
8
+ let actualPressed = false;
9
+ const updateState = (newState) => {
10
+ newState !== state && onHoldChange(state = newState);
11
+ };
12
+ makeEventListener(window, "keydown", modifier ? (e) => {
13
+ if (e.repeat)
14
+ return;
15
+ if (e[modifier]) {
16
+ if (!state && !actualPressed) {
17
+ preventDefault && e.preventDefault();
18
+ onHoldChange(state = actualPressed = true);
19
+ } else if (!allowOtherKeys)
20
+ updateState(false);
21
+ } else
22
+ updateState(actualPressed = false);
23
+ } : (e) => {
24
+ if (e.key !== key) {
25
+ allowOtherKeys || updateState(false);
26
+ return;
27
+ }
28
+ if (e.repeat)
29
+ return;
30
+ updateState(true);
31
+ });
32
+ makeEventListener(window, "keyup", modifier ? (e) => {
33
+ if (e[modifier])
34
+ allowOtherKeys || updateState(false);
35
+ else
36
+ updateState(actualPressed = false);
37
+ } : (e) => {
38
+ if (e.key !== key)
39
+ allowOtherKeys || updateState(false);
40
+ else
41
+ updateState(false);
42
+ });
43
+ makeEventListener(document, "visibilitychange", () => document.visibilityState !== "visible" && updateState(false));
44
+ }
45
+ export {
46
+ makeKeyHoldListener
47
+ };
@@ -0,0 +1,30 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+
19
+ // src/server.ts
20
+ var server_exports = {};
21
+ __export(server_exports, {
22
+ makeKeyHoldListener: () => makeKeyHoldListener
23
+ });
24
+ module.exports = __toCommonJS(server_exports);
25
+ var makeKeyHoldListener = () => {
26
+ };
27
+ // Annotate the CommonJS export names for ESM import in node:
28
+ 0 && (module.exports = {
29
+ makeKeyHoldListener
30
+ });
package/dist/server.js ADDED
@@ -0,0 +1,6 @@
1
+ // src/server.ts
2
+ var makeKeyHoldListener = () => {
3
+ };
4
+ export {
5
+ makeKeyHoldListener
6
+ };
package/package.json ADDED
@@ -0,0 +1,75 @@
1
+ {
2
+ "name": "@solid-primitives/keyboard",
3
+ "version": "0.0.100",
4
+ "description": "A library of reactive promitives helping handling user's keyboard input.",
5
+ "author": "Damian Tarnwski <gthetarnav@gmail.com>",
6
+ "contributors": [],
7
+ "license": "MIT",
8
+ "homepage": "https://github.com/solidjs-community/solid-primitives/tree/main/packages/keyboard#readme",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/solidjs-community/solid-primitives.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/solidjs-community/solid-primitives/issues"
15
+ },
16
+ "primitive": {
17
+ "name": "keyboard",
18
+ "stage": 0,
19
+ "list": [
20
+ "makeKeyHoldListener"
21
+ ],
22
+ "category": "Inputs"
23
+ },
24
+ "private": false,
25
+ "sideEffects": false,
26
+ "type": "module",
27
+ "main": "./dist/server.cjs",
28
+ "module": "./dist/index.js",
29
+ "types": "./dist/index.d.ts",
30
+ "exports": {
31
+ "node": {
32
+ "import": "./dist/server.js",
33
+ "require": "./dist/server.cjs"
34
+ },
35
+ "import": "./dist/index.js",
36
+ "require": "./dist/index.cjs"
37
+ },
38
+ "files": [
39
+ "dist"
40
+ ],
41
+ "scripts": {
42
+ "start": "vite serve dev --host",
43
+ "dev": "npm run start",
44
+ "build": "tsup",
45
+ "test": "uvu -r solid-register",
46
+ "test:watch": "watchlist src test -- npm test"
47
+ },
48
+ "keywords": [
49
+ "solid",
50
+ "primitives",
51
+ "keyboard",
52
+ "keystroke",
53
+ "hotkey"
54
+ ],
55
+ "devDependencies": {
56
+ "jsdom": "^19.0.0",
57
+ "prettier": "^2.7.1",
58
+ "solid-register": "^0.2.5",
59
+ "tslib": "^2.4.0",
60
+ "tsup": "^6.1.2",
61
+ "typescript": "^4.7.3",
62
+ "unocss": "0.39.0",
63
+ "uvu": "^0.5.3",
64
+ "vite": "2.9.12",
65
+ "vite-plugin-solid": "2.2.6",
66
+ "watchlist": "^0.3.1",
67
+ "solid-js": "^1.4.4"
68
+ },
69
+ "peerDependencies": {
70
+ "solid-js": "^1.4.0"
71
+ },
72
+ "dependencies": {
73
+ "@solid-primitives/event-listener": "^2.2.0"
74
+ }
75
+ }