@joknoll/svelte-attach 0.3.1 → 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 CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  Svelte attachments to add keyboard shortcuts, sound effects, vibration, and gamepad controls to Svelte 5 components.
4
4
 
5
+ [Try the examples](https://joknoll.github.io/svelte-attach/)
6
+
5
7
  ## Usage
6
8
 
7
9
  Requires Svelte `5.55.0` or later.
@@ -22,8 +24,6 @@ Requires Svelte `5.55.0` or later.
22
24
 
23
25
  Press `⌘K` on Apple devices or `Ctrl+K` elsewhere to open the popover. The button doesn’t need focus.
24
26
 
25
- [Keyboard API](docs/keyboard.md)
26
-
27
27
  ### Sound
28
28
 
29
29
  ```svelte
@@ -39,8 +39,6 @@ Sound plays on `click` by default. Set `on` to use a different event.
39
39
 
40
40
  Use sound sparingly for meaningful moments, like completing a task. Avoid playing it on every click or keystroke, and let users mute it.
41
41
 
42
- [Sound API](docs/sound.md)
43
-
44
42
  ### Vibration
45
43
 
46
44
  ```svelte
@@ -55,8 +53,6 @@ The button vibrates the device on click if the browser and device support vibrat
55
53
 
56
54
  Use a brief pulse to confirm a deliberate action, like snapping an item into place. Avoid buzzing during repeated actions like scrolling or typing. Keep visible feedback so the interaction works without vibration.
57
55
 
58
- [Vibration API](docs/vibration.md)
59
-
60
56
  ### Gamepad
61
57
 
62
58
  ```svelte
@@ -73,6 +69,4 @@ Use a brief pulse to confirm a deliberate action, like snapping an item into pla
73
69
 
74
70
  Focus the button, then press `A` on a standard-mapped gamepad to open the popover.
75
71
 
76
- [Gamepad API](docs/gamepad.md)
77
-
78
72
  [Full documentation](docs/)
package/dist/gamepad.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Attachment } from "svelte/attachments";
2
- //#region src/gamepad.d.ts
3
2
  /** CONTRACT: Standard Gamepad API indices denote physical button positions. */
4
3
  export declare const GamepadButtonMap: {
5
4
  readonly A: 0;
@@ -26,6 +25,8 @@ export interface GamepadButtonOptions<E extends HTMLElement = HTMLElement> {
26
25
  onTrigger?: (element: E, pad: Gamepad) => void;
27
26
  /** Gamepad slot to read. Defaults to the first occupied slot. */
28
27
  padIndex?: number;
28
+ /** Only activate while the element has focus. Defaults to `true`. */
29
+ requireFocus?: boolean;
29
30
  }
30
31
  /** API availability only. Permissions Policy can still block access. */
31
32
  export declare function isSupported(): boolean;
@@ -33,12 +34,12 @@ export declare function isSupported(): boolean;
33
34
  export declare function isButtonPressed(button: ButtonName, padIndex?: number): boolean;
34
35
  /**
35
36
  * Clicks the element, or calls `onTrigger`, when a standard gamepad button is newly pressed
36
- * while the element itself has focus.
37
+ * while the element itself has focus. Set `requireFocus: false` to activate it from anywhere.
37
38
  *
38
39
  * @example
39
40
  * ```svelte
40
41
  * <button {@attach gamepadButton("A")} onclick={confirm}>Confirm</button>
42
+ * <button {@attach gamepadButton("START", { requireFocus: false })} onclick={pause}>Pause</button>
41
43
  * ```
42
44
  */
43
- export declare function gamepadButton<E extends HTMLElement = HTMLElement>(button: ButtonName, options?: GamepadButtonOptions<E>): Attachment<E>;
44
- //#endregion
45
+ export declare function gamepadButton<E extends HTMLElement = HTMLElement>(button: ButtonName, options?: GamepadButtonOptions<E>): Attachment<E>;
package/dist/gamepad.js CHANGED
@@ -1,4 +1,4 @@
1
- //#region src/gamepad.ts
1
+ // src/gamepad.ts
2
2
  /** CONTRACT: Standard Gamepad API indices denote physical button positions. */
3
3
  const GamepadButtonMap = {
4
4
  A: 0,
@@ -37,30 +37,43 @@ function isButtonPressed(button, padIndex) {
37
37
  function poll() {
38
38
  try {
39
39
  const pads = navigator.getGamepads();
40
+ const visible = document.visibilityState !== "hidden";
41
+ const winners = /* @__PURE__ */ new Map();
40
42
  for (const registration of registrations) {
41
- const { element, button, trigger, padIndex } = registration;
43
+ const { element, button, padIndex, requireFocus } = registration;
42
44
  const pad = connectedPad(pads, padIndex);
43
45
  const pressed = pad?.buttons[GamepadButtonMap[button]]?.pressed ?? false;
44
46
  const newlyPressed = pressed && !registration.pressed;
45
47
  registration.pressed = pressed;
46
- if (newlyPressed && pad && document.visibilityState !== "hidden" && element.isConnected && element === document.activeElement && !element.matches(":disabled") && !element.closest("[inert]")) trigger(pad);
48
+ if (!newlyPressed || !pad || !visible || !element.isConnected || element.matches(":disabled") || element.closest("[inert]")) continue;
49
+ const focused = element === document.activeElement;
50
+ if (requireFocus && !focused) continue;
51
+ const key = `${pad.index}:${button}`;
52
+ const current = winners.get(key);
53
+ if (!current || focused || !current.focused) winners.set(key, {
54
+ registration,
55
+ pad,
56
+ focused
57
+ });
47
58
  }
59
+ for (const { registration, pad } of winners.values()) registration.trigger(pad);
48
60
  } finally {
49
61
  frame = registrations.size > 0 ? requestAnimationFrame(poll) : 0;
50
62
  }
51
63
  }
52
64
  /**
53
65
  * Clicks the element, or calls `onTrigger`, when a standard gamepad button is newly pressed
54
- * while the element itself has focus.
66
+ * while the element itself has focus. Set `requireFocus: false` to activate it from anywhere.
55
67
  *
56
68
  * @example
57
69
  * ```svelte
58
70
  * <button {@attach gamepadButton("A")} onclick={confirm}>Confirm</button>
71
+ * <button {@attach gamepadButton("START", { requireFocus: false })} onclick={pause}>Pause</button>
59
72
  * ```
60
73
  */
61
74
  function gamepadButton(button, options = {}) {
62
75
  if (!Object.hasOwn(GamepadButtonMap, button)) throw new RangeError(`Unknown gamepad button: ${String(button)}`);
63
- const { onTrigger, padIndex } = options;
76
+ const { onTrigger, padIndex, requireFocus = true } = options;
64
77
  if (padIndex !== void 0 && (!Number.isInteger(padIndex) || padIndex < 0)) throw new RangeError("Gamepad padIndex must be a non-negative integer.");
65
78
  return (element) => {
66
79
  if (!isSupported() || typeof requestAnimationFrame !== "function") return;
@@ -69,6 +82,7 @@ function gamepadButton(button, options = {}) {
69
82
  button,
70
83
  trigger: (pad) => onTrigger ? onTrigger(element, pad) : element.click(),
71
84
  padIndex,
85
+ requireFocus,
72
86
  pressed: false
73
87
  };
74
88
  registrations.add(registration);
@@ -79,5 +93,4 @@ function gamepadButton(button, options = {}) {
79
93
  };
80
94
  };
81
95
  }
82
- //#endregion
83
96
  export { GamepadButtonMap, gamepadButton, isButtonPressed, isSupported };
package/dist/haptic.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Attachment } from "svelte/attachments";
2
- //#region src/haptic.d.ts
3
2
  /** CONTRACT: Entries alternate vibration and pause durations in milliseconds, starting with vibration. */
4
3
  export declare const patterns: {
5
4
  readonly light: readonly [10];
@@ -37,5 +36,4 @@ export declare function cancelHaptic(): boolean;
37
36
  * <button {@attach haptic("selection", { on: "pointerdown" })}>Select</button>
38
37
  * ```
39
38
  */
40
- export declare function haptic(pattern?: HapticPattern, options?: HapticOptions): Attachment<HTMLElement>;
41
- //#endregion
39
+ export declare function haptic(pattern?: HapticPattern, options?: HapticOptions): Attachment<HTMLElement>;
package/dist/haptic.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { on } from "svelte/events";
2
- //#region src/haptic.ts
2
+ // src/haptic.ts
3
3
  /** CONTRACT: Entries alternate vibration and pause durations in milliseconds, starting with vibration. */
4
4
  const patterns = {
5
5
  light: [10],
@@ -66,5 +66,4 @@ function haptic(pattern = "medium", options = {}) {
66
66
  const event = options.on ?? "click";
67
67
  return (element) => on(element, event, () => triggerHaptic(durations));
68
68
  }
69
- //#endregion
70
69
  export { cancelHaptic, createPattern, haptic, isSupported, patterns, triggerHaptic };
package/dist/key.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Attachment } from "svelte/attachments";
2
- //#region src/key/shortcut.d.ts
3
2
  declare const MODIFIERS: {
4
3
  readonly ctrl: "ctrl";
5
4
  readonly control: "ctrl";
@@ -105,8 +104,6 @@ type Shortcut<S extends string> = S extends unknown ? string extends S ? S : Che
105
104
  * with ` / `. Throws a `TypeError` for a malformed shortcut.
106
105
  */
107
106
  export declare function formatHint<const S extends string>(keys: Shortcut<S> | readonly Shortcut<S>[]): string;
108
- //#endregion
109
- //#region src/key/index.d.ts
110
107
  export interface HotkeyOptions<E extends HTMLElement = HTMLElement> {
111
108
  /** Replaces the default `element.click()`. */
112
109
  onTrigger?: (element: E, event: KeyboardEvent) => void;
@@ -146,5 +143,4 @@ export declare function hotkey<const S extends string, E extends HTMLElement = H
146
143
  * that also activate.
147
144
  */
148
145
  export declare function pressed<const S extends string>(keys: Shortcut<S> | readonly Shortcut<S>[], options?: PressedOptions): Attachment<HTMLElement>;
149
- //#endregion
150
146
  export type { Shortcut };
package/dist/key.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { on } from "svelte/events";
2
- //#region src/key/shortcut.ts
2
+ // src/key/shortcut.ts
3
3
  const MODIFIERS = {
4
4
  ctrl: "ctrl",
5
5
  control: "ctrl",
@@ -224,8 +224,7 @@ function releases(event, spec) {
224
224
  if (isModifierKey(typed)) return typed === spec.key || spec[typed];
225
225
  return typed === spec.key || physicalKey(event.code) === spec.key;
226
226
  }
227
- //#endregion
228
- //#region src/key/dispatcher.ts
227
+ // src/key/dispatcher.ts
229
228
  const NON_TEXT_INPUTS = /* @__PURE__ */ new Set([
230
229
  "checkbox",
231
230
  "button",
@@ -282,8 +281,7 @@ function register(reg) {
282
281
  }
283
282
  };
284
283
  }
285
- //#endregion
286
- //#region src/key/index.ts
284
+ // src/key/index.ts
287
285
  function trackHeld(element, className) {
288
286
  const active = /* @__PURE__ */ new Map();
289
287
  const release = (event) => {
@@ -369,5 +367,4 @@ function pressed(keys, options = {}) {
369
367
  };
370
368
  };
371
369
  }
372
- //#endregion
373
370
  export { formatHint, hotkey, pressed };
package/dist/sound.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import { Attachment } from "svelte/attachments";
2
- //#region src/sound.d.ts
3
2
  type EventName = keyof HTMLElementEventMap;
4
3
  export interface SoundOptions {
5
4
  /** Between 0 and 1. Defaults to 1. */
@@ -48,5 +47,4 @@ export declare class Sound {
48
47
  * ```
49
48
  */
50
49
  export declare function sound<E extends HTMLElement = HTMLElement>(src: SoundSource<E>, options?: SoundAttachmentOptions): Attachment<E>;
51
- //#endregion
52
50
  export {};
package/dist/sound.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { on } from "svelte/events";
2
- //#region src/sound.ts
2
+ // src/sound.ts
3
3
  const buffers = /* @__PURE__ */ new Map();
4
4
  let sharedContext;
5
5
  function isSupported() {
@@ -143,5 +143,4 @@ function sound(src, options = {}) {
143
143
  };
144
144
  };
145
145
  }
146
- //#endregion
147
146
  export { Sound, isSupported, sound };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@joknoll/svelte-attach",
3
- "version": "0.3.1",
3
+ "version": "1.0.0",
4
4
  "description": "Svelte 5 attachments for keyboard shortcuts, sound effects, vibration, and gamepad buttons.",
5
5
  "keywords": [
6
6
  "attachment",
@@ -52,11 +52,11 @@
52
52
  "access": "public"
53
53
  },
54
54
  "devDependencies": {
55
- "@types/node": "^24",
56
- "jsdom": "^29.0.1",
57
- "svelte": "^5.55.0",
55
+ "@types/node": "^26.6.6",
56
+ "jsdom": "^31.0.1",
57
+ "svelte": "^5.59.1",
58
58
  "typescript": "^7.0.2",
59
- "vite-plus": "1.2.0"
59
+ "vite-plus": "1.8.0"
60
60
  },
61
61
  "peerDependencies": {
62
62
  "svelte": ">=5.55.0"
@@ -69,7 +69,7 @@
69
69
  },
70
70
  "runtime": {
71
71
  "name": "node",
72
- "version": ">=22.18.0"
72
+ "version": "26.13.1"
73
73
  }
74
74
  },
75
75
  "scripts": {