@joknoll/svelte-attach-gamepad 0.1.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,37 @@
1
+ # @joknoll/svelte-attach-gamepad
2
+
3
+ A small Svelte 5 helper for reading [Gamepad API](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API) button state.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ npm install @joknoll/svelte-attach-gamepad
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```ts
14
+ import { isSupported, isButtonPressed, GamepadButtonMap } from "@joknoll/svelte-attach-gamepad";
15
+
16
+ if (isButtonPressed("A")) {
17
+ // first connected gamepad, "A" button held
18
+ }
19
+ ```
20
+
21
+ ## API
22
+
23
+ ### `isSupported`
24
+
25
+ Boolean indicating whether `navigator.getGamepads` is available.
26
+
27
+ ### `isButtonPressed(button, padIndex = 0)`
28
+
29
+ Returns whether the named button is currently pressed on the gamepad at `padIndex`.
30
+ No-ops to `false` when the Gamepad API is unavailable (e.g. SSR) or no pad is
31
+ connected at that index.
32
+
33
+ ### `GamepadButtonMap` / `ButtonName`
34
+
35
+ The standard gamepad button name → index map, and the union of valid button names
36
+ (`"A"`, `"B"`, `"X"`, `"Y"`, `"LB"`, `"RB"`, `"LT"`, `"RT"`, `"SELECT"`, `"START"`,
37
+ `"UP"`, `"DOWN"`, `"LEFT"`, `"RIGHT"`).
@@ -0,0 +1,30 @@
1
+ //#region src/buttons.d.ts
2
+ declare const GamepadButtonMap: {
3
+ readonly A: 0;
4
+ readonly B: 1;
5
+ readonly X: 2;
6
+ readonly Y: 3;
7
+ readonly LB: 4;
8
+ readonly RB: 5;
9
+ readonly LT: 6;
10
+ readonly RT: 7;
11
+ readonly SELECT: 8;
12
+ readonly START: 9;
13
+ readonly UP: 12;
14
+ readonly DOWN: 13;
15
+ readonly LEFT: 14;
16
+ readonly RIGHT: 15;
17
+ };
18
+ type ButtonName = keyof typeof GamepadButtonMap;
19
+ //#endregion
20
+ //#region src/index.d.ts
21
+ /** Whether the Gamepad API is available in the current environment. */
22
+ declare const isSupported: boolean;
23
+ /**
24
+ * Returns whether the named button is currently pressed on the gamepad at
25
+ * `padIndex`. No-ops to `false` when the Gamepad API is unavailable (e.g. SSR)
26
+ * or no pad is connected at that index.
27
+ */
28
+ declare function isButtonPressed(buttonName: ButtonName, padIndex?: number): boolean;
29
+ //#endregion
30
+ export { type ButtonName, GamepadButtonMap, isButtonPressed, isSupported };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ const e={A:0,B:1,X:2,Y:3,LB:4,RB:5,LT:6,RT:7,SELECT:8,START:9,UP:12,DOWN:13,LEFT:14,RIGHT:15},t=typeof navigator<`u`&&typeof navigator.getGamepads==`function`;function n(n,r=0){if(!t)return!1;let i=navigator.getGamepads()[r];return i?i.buttons[e[n]]?.pressed??!1:!1}export{e as GamepadButtonMap,n as isButtonPressed,t as isSupported};
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@joknoll/svelte-attach-gamepad",
3
+ "version": "0.1.0",
4
+ "description": "A Svelte helper for reading gamepad button state.",
5
+ "keywords": [
6
+ "attachment",
7
+ "gamepad",
8
+ "svelte",
9
+ "svelte5"
10
+ ],
11
+ "homepage": "https://github.com/joknoll/svelte-attach/tree/main/packages/gamepad#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/joknoll/svelte-attach/issues"
14
+ },
15
+ "license": "MIT",
16
+ "author": "joknoll",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/joknoll/svelte-attach.git",
20
+ "directory": "packages/gamepad"
21
+ },
22
+ "files": [
23
+ "dist"
24
+ ],
25
+ "type": "module",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ ".": "./dist/index.js",
29
+ "./package.json": "./package.json"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^25.6.2",
36
+ "@typescript/native-preview": "7.0.0-dev.20260509.2",
37
+ "bumpp": "^11.1.0",
38
+ "svelte": "^5.55.0",
39
+ "typescript": "^6.0.3",
40
+ "vite-plus": "latest"
41
+ },
42
+ "peerDependencies": {
43
+ "svelte": ">=5.55.0"
44
+ },
45
+ "scripts": {
46
+ "build": "vp pack",
47
+ "dev": "vp pack --watch",
48
+ "test": "vp test --passWithNoTests",
49
+ "check": "vp check"
50
+ }
51
+ }