@joknoll/svelte-attach 0.3.0 → 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 +27 -110
- package/dist/gamepad.d.ts +5 -4
- package/dist/gamepad.js +19 -6
- package/dist/haptic.d.ts +1 -3
- package/dist/haptic.js +1 -2
- package/dist/key.d.ts +4 -5
- package/dist/key.js +42 -19
- package/dist/sound.d.ts +0 -2
- package/dist/sound.js +1 -2
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -1,155 +1,72 @@
|
|
|
1
1
|
# @joknoll/svelte-attach
|
|
2
2
|
|
|
3
|
-
Svelte
|
|
3
|
+
Svelte attachments to add keyboard shortcuts, sound effects, vibration, and gamepad controls to Svelte 5 components.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
vp add @joknoll/svelte-attach
|
|
7
|
-
```
|
|
5
|
+
[Try the examples](https://joknoll.github.io/svelte-attach/)
|
|
8
6
|
|
|
9
|
-
|
|
10
|
-
| -------------------------------- | ----------------------------------------------- |
|
|
11
|
-
| `@joknoll/svelte-attach/key` | Keyboard shortcuts and held state |
|
|
12
|
-
| `@joknoll/svelte-attach/sound` | Web Audio playback bound to DOM events |
|
|
13
|
-
| `@joknoll/svelte-attach/haptic` | Web Vibration feedback bound to DOM events |
|
|
14
|
-
| `@joknoll/svelte-attach/gamepad` | Standard gamepad buttons that activate elements |
|
|
7
|
+
## Usage
|
|
15
8
|
|
|
16
|
-
|
|
9
|
+
Requires Svelte `5.55.0` or later.
|
|
17
10
|
|
|
18
|
-
|
|
11
|
+
### Keyboard
|
|
19
12
|
|
|
20
13
|
```svelte
|
|
21
14
|
<script lang="ts">
|
|
22
|
-
import { hotkey
|
|
15
|
+
import { hotkey } from "@joknoll/svelte-attach/key";
|
|
23
16
|
</script>
|
|
24
17
|
|
|
25
|
-
<button
|
|
26
|
-
|
|
18
|
+
<button popovertarget="shortcuts" {@attach hotkey("mod+k")}>
|
|
19
|
+
Show shortcuts
|
|
27
20
|
</button>
|
|
28
21
|
|
|
29
|
-
<
|
|
22
|
+
<div id="shortcuts" popover>Press Escape to close.</div>
|
|
30
23
|
```
|
|
31
24
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
- **`onTrigger(element, event)`:** called instead of the click.
|
|
35
|
-
- **`pressedClass`:** a class kept on the element from activation until the shortcut is released.
|
|
36
|
-
- **`preventDefault`, `stopPropagation`, `ignoreInputs`, `ignoreRepeat`:** control dispatch. `preventDefault`, `ignoreInputs` and `ignoreRepeat` default to `true`.
|
|
37
|
-
|
|
38
|
-
`hotkey` also sets `aria-keyshortcuts` and restores the previous value on cleanup. When two attached elements claim the same shortcut, the most recently attached one wins, and removing it restores the previous one.
|
|
39
|
-
|
|
40
|
-
A shortcut is zero or more modifiers followed by one key, joined with `+`, such as `"ctrl+shift+k"`. Pass an array for alternatives, such as `["j", "arrowdown"]`.
|
|
41
|
-
|
|
42
|
-
- **Modifiers:** `ctrl`, `shift`, `alt`, `meta`, and `mod`, which means Meta on Apple devices and Control elsewhere. `control`, `option`, `cmd`, `command`, and `win` are accepted as aliases.
|
|
43
|
-
- **Keys:**
|
|
44
|
-
- any single character
|
|
45
|
-
- `f1` to `f12`
|
|
46
|
-
- `escape`, `enter`, `tab`, `space`, `plus`, `backspace`, `delete`, `insert`, `home`, `end`, `pageup`, `pagedown`
|
|
47
|
-
- the arrows `arrowup` / `up`, `arrowdown` / `down`, `arrowleft` / `left`, `arrowright` / `right`
|
|
48
|
-
- `esc`, `return` and `del` as aliases
|
|
49
|
-
- **Errors:** shortcut literals are checked by TypeScript, so `hotkey("ctrl+esacpe")` fails to compile with `Unknown key "esacpe"`. Shortcuts held in a `string` variable are checked at runtime and throw a `TypeError`.
|
|
50
|
-
|
|
51
|
-
`pressed(keys, { className = "is-pressed", ignoreInputs = true })` adds a class while a shortcut is held, without triggering anything. A lone modifier such as `"mod"` or `"shift"` is a valid shortcut here, which suits key caps in a hint.
|
|
25
|
+
Press `⌘K` on Apple devices or `Ctrl+K` elsewhere to open the popover. The button doesn’t need focus.
|
|
52
26
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
Hotkeys ignore repeated keys, IME composition, focused form fields and editable elements, disabled elements, and elements inside an inert subtree. `data-keyshortcuts-ignore` on a container suppresses hotkeys while focus is inside it.
|
|
56
|
-
|
|
57
|
-
## Sound
|
|
27
|
+
### Sound
|
|
58
28
|
|
|
59
29
|
```svelte
|
|
60
30
|
<script lang="ts">
|
|
61
|
-
import { sound
|
|
31
|
+
import { sound } from "@joknoll/svelte-attach/sound";
|
|
62
32
|
import click from "./click.opus";
|
|
63
|
-
import switchOn from "./switch-on.opus";
|
|
64
|
-
import switchOff from "./switch-off.opus";
|
|
65
|
-
|
|
66
|
-
const quietClick = sound(click, { volume: 0.4 });
|
|
67
|
-
const notification = new Sound(click);
|
|
68
33
|
</script>
|
|
69
34
|
|
|
70
|
-
<button {@attach sound(click)}>
|
|
71
|
-
<button {@attach quietClick}>Quiet click</button>
|
|
72
|
-
<button onclick={() => notification.play()}>Play manually</button>
|
|
73
|
-
<input
|
|
74
|
-
type="checkbox"
|
|
75
|
-
role="switch"
|
|
76
|
-
aria-label="Sound switch"
|
|
77
|
-
{@attach sound((input: HTMLInputElement) => (input.checked ? switchOn : switchOff), {
|
|
78
|
-
on: "change",
|
|
79
|
-
})}
|
|
80
|
-
/>
|
|
35
|
+
<button {@attach sound(click)}>Play sound</button>
|
|
81
36
|
```
|
|
82
37
|
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
- **`src`:** a URL, or `(element, event) => URL` to choose a sound when the event fires. Return `null` or `undefined` to skip playback.
|
|
86
|
-
- **Stopping:** the `stopOn` event and attachment cleanup stop every playback the attachment started and cancel pending ones.
|
|
87
|
-
- **Errors:** selection, load, and playback errors reach `onError`; without it they are logged.
|
|
88
|
-
- **Other options:** `volume` (0 to 1), `loop`, and `rate` (greater than 0).
|
|
89
|
-
- **Reuse:** one `sound(...)` result can be attached to many elements.
|
|
90
|
-
- **Caching:** decoded audio is cached by URL across all attachments and `Sound` instances, and a failed load leaves the cache so a later play can retry.
|
|
91
|
-
|
|
92
|
-
`new Sound(src, options?)` is for imperative playback.
|
|
38
|
+
Sound plays on `click` by default. Set `on` to use a different event.
|
|
93
39
|
|
|
94
|
-
|
|
95
|
-
- **`stop()`:** stops all current playbacks and cancels pending ones.
|
|
96
|
-
- **`preload()`:** resolves to `true` when decoding succeeds and to `false` when Web Audio is unavailable.
|
|
40
|
+
Use sound sparingly for meaningful moments, like completing a task. Avoid playing it on every click or keystroke, and let users mute it.
|
|
97
41
|
|
|
98
|
-
|
|
42
|
+
### Vibration
|
|
99
43
|
|
|
100
44
|
```svelte
|
|
101
45
|
<script lang="ts">
|
|
102
|
-
import { haptic
|
|
46
|
+
import { haptic } from "@joknoll/svelte-attach/haptic";
|
|
103
47
|
</script>
|
|
104
48
|
|
|
105
|
-
<button {@attach haptic("
|
|
106
|
-
<button onclick={() => triggerHaptic("success")}>Saved</button>
|
|
49
|
+
<button {@attach haptic("light")}>Try vibration</button>
|
|
107
50
|
```
|
|
108
51
|
|
|
109
|
-
|
|
52
|
+
The button vibrates the device on click if the browser and device support vibration.
|
|
110
53
|
|
|
111
|
-
|
|
112
|
-
- **Single duration:** one number, in milliseconds.
|
|
113
|
-
- **Sequence:** an array of alternating vibration and pause durations.
|
|
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.
|
|
114
55
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
The Web Vibration API controls timing, not physical intensity. Support varies by browser and device, so use vibration as an optional complement to visible feedback.
|
|
118
|
-
|
|
119
|
-
## Gamepad
|
|
56
|
+
### Gamepad
|
|
120
57
|
|
|
121
58
|
```svelte
|
|
122
59
|
<script lang="ts">
|
|
123
60
|
import { gamepadButton } from "@joknoll/svelte-attach/gamepad";
|
|
124
61
|
</script>
|
|
125
62
|
|
|
126
|
-
<button {@attach gamepadButton("A")}
|
|
127
|
-
|
|
63
|
+
<button popovertarget="gamepad-help" {@attach gamepadButton("A")}>
|
|
64
|
+
Show controls
|
|
128
65
|
</button>
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
`gamepadButton(button, options?)` clicks the attached element on a new press, **only while it has focus**.
|
|
132
|
-
|
|
133
|
-
- **`onTrigger(element, pad)`:** replaces the click.
|
|
134
|
-
- **`padIndex`:** selects a gamepad slot. By default the first connected pad is used.
|
|
135
|
-
- **Polling:** disabled elements are ignored, all attachments share one animation-frame poller, and polling stops when the last attachment is removed.
|
|
136
66
|
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
- the face buttons `A`, `B`, `X`, `Y`
|
|
140
|
-
- the shoulders and triggers `LB`, `RB`, `LT`, `RT`
|
|
141
|
-
- the stick presses `L3` and `R3`
|
|
142
|
-
- the directional buttons `UP`, `DOWN`, `LEFT`, `RIGHT`
|
|
143
|
-
- `SELECT`, `START`, and `HOME`
|
|
144
|
-
|
|
145
|
-
## Development
|
|
146
|
-
|
|
147
|
-
The example site lives in [`examples/website`](examples/website).
|
|
148
|
-
|
|
149
|
-
```bash
|
|
150
|
-
vp install
|
|
151
|
-
vp run ready
|
|
152
|
-
vp run dev
|
|
67
|
+
<div id="gamepad-help" popover>Press Escape to close.</div>
|
|
153
68
|
```
|
|
154
69
|
|
|
155
|
-
|
|
70
|
+
Focus the button, then press `A` on a standard-mapped gamepad to open the popover.
|
|
71
|
+
|
|
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
|
-
|
|
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,
|
|
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
|
|
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
|
-
|
|
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;
|
|
@@ -115,7 +112,10 @@ export interface HotkeyOptions<E extends HTMLElement = HTMLElement> {
|
|
|
115
112
|
/** Defaults to true. */
|
|
116
113
|
preventDefault?: boolean;
|
|
117
114
|
stopPropagation?: boolean;
|
|
118
|
-
/**
|
|
115
|
+
/**
|
|
116
|
+
* Defaults to true: shortcuts are ignored while a text field, select, radio, range or editable
|
|
117
|
+
* element has focus. Checkboxes and buttons do not block them.
|
|
118
|
+
*/
|
|
119
119
|
ignoreInputs?: boolean;
|
|
120
120
|
/** Defaults to true. */
|
|
121
121
|
ignoreRepeat?: boolean;
|
|
@@ -143,5 +143,4 @@ export declare function hotkey<const S extends string, E extends HTMLElement = H
|
|
|
143
143
|
* that also activate.
|
|
144
144
|
*/
|
|
145
145
|
export declare function pressed<const S extends string>(keys: Shortcut<S> | readonly Shortcut<S>[], options?: PressedOptions): Attachment<HTMLElement>;
|
|
146
|
-
//#endregion
|
|
147
146
|
export type { Shortcut };
|
package/dist/key.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { on } from "svelte/events";
|
|
2
|
-
|
|
2
|
+
// src/key/shortcut.ts
|
|
3
3
|
const MODIFIERS = {
|
|
4
4
|
ctrl: "ctrl",
|
|
5
5
|
control: "ctrl",
|
|
@@ -197,26 +197,51 @@ function formatHint(keys) {
|
|
|
197
197
|
return parts.join(mac ? " " : " + ");
|
|
198
198
|
}).join(" / ");
|
|
199
199
|
}
|
|
200
|
+
const PHYSICAL_KEY = /^(?:Key([A-Z])|Digit([0-9]))$/;
|
|
201
|
+
/** The letter or digit at this key's position on a US layout, read from `KeyboardEvent.code`. */
|
|
202
|
+
function physicalKey(code) {
|
|
203
|
+
const match = PHYSICAL_KEY.exec(code);
|
|
204
|
+
return (match?.[1] ?? match?.[2])?.toLowerCase();
|
|
205
|
+
}
|
|
206
|
+
function isSymbol(key) {
|
|
207
|
+
return key.length === 1 && key !== " " && key.toLowerCase() === key.toUpperCase();
|
|
208
|
+
}
|
|
200
209
|
function matches(event, spec) {
|
|
201
|
-
|
|
210
|
+
if (event.metaKey !== spec.meta) return false;
|
|
211
|
+
const typed = eventKey(event.key);
|
|
212
|
+
if (typed === spec.key && isSymbol(typed)) {
|
|
213
|
+
const altGraph = event.getModifierState("AltGraph");
|
|
214
|
+
const altTypes = altGraph || isMac();
|
|
215
|
+
return (event.ctrlKey && !altGraph) === spec.ctrl && (!spec.shift || event.shiftKey) && (spec.alt ? event.altKey : altTypes || !event.altKey);
|
|
216
|
+
}
|
|
217
|
+
if (event.ctrlKey !== spec.ctrl || event.shiftKey !== spec.shift || event.altKey !== spec.alt) return false;
|
|
218
|
+
if (typed === spec.key) return true;
|
|
219
|
+
return !/^[a-z0-9]$/.test(typed) && physicalKey(event.code) === spec.key;
|
|
202
220
|
}
|
|
203
221
|
/** True when releasing this key ends the shortcut: its key or any of its modifiers. */
|
|
204
222
|
function releases(event, spec) {
|
|
205
|
-
const
|
|
206
|
-
return
|
|
223
|
+
const typed = eventKey(event.key);
|
|
224
|
+
if (isModifierKey(typed)) return typed === spec.key || spec[typed];
|
|
225
|
+
return typed === spec.key || physicalKey(event.code) === spec.key;
|
|
207
226
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
"
|
|
212
|
-
"
|
|
213
|
-
"
|
|
227
|
+
// src/key/dispatcher.ts
|
|
228
|
+
const NON_TEXT_INPUTS = /* @__PURE__ */ new Set([
|
|
229
|
+
"checkbox",
|
|
230
|
+
"button",
|
|
231
|
+
"submit",
|
|
232
|
+
"reset",
|
|
233
|
+
"image",
|
|
234
|
+
"color",
|
|
235
|
+
"file"
|
|
214
236
|
]);
|
|
215
237
|
const registrations = /* @__PURE__ */ new Set();
|
|
216
238
|
let stopListener = null;
|
|
217
239
|
function isTyping(event) {
|
|
218
|
-
const target = event.
|
|
219
|
-
|
|
240
|
+
const target = event.composedPath()[0];
|
|
241
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
242
|
+
if (target.isContentEditable) return true;
|
|
243
|
+
if (target instanceof HTMLInputElement) return !NON_TEXT_INPUTS.has(target.type);
|
|
244
|
+
return target instanceof HTMLTextAreaElement || target instanceof HTMLSelectElement;
|
|
220
245
|
}
|
|
221
246
|
function inIgnoreZone(event) {
|
|
222
247
|
const target = event.target;
|
|
@@ -229,7 +254,7 @@ function inIgnoreZone(event) {
|
|
|
229
254
|
return false;
|
|
230
255
|
}
|
|
231
256
|
function handleKeydown(event) {
|
|
232
|
-
if (event.isComposing || event.key === "Dead") return;
|
|
257
|
+
if (event.isComposing || event.key === "Dead" && !event.altKey) return;
|
|
233
258
|
const ignoreZone = inIgnoreZone(event);
|
|
234
259
|
const eligible = (reg) => {
|
|
235
260
|
if (reg.ignoreRepeat && event.repeat) return false;
|
|
@@ -256,20 +281,19 @@ function register(reg) {
|
|
|
256
281
|
}
|
|
257
282
|
};
|
|
258
283
|
}
|
|
259
|
-
|
|
260
|
-
//#region src/key/index.ts
|
|
284
|
+
// src/key/index.ts
|
|
261
285
|
function trackHeld(element, className) {
|
|
262
|
-
const active = /* @__PURE__ */ new
|
|
286
|
+
const active = /* @__PURE__ */ new Map();
|
|
263
287
|
const release = (event) => {
|
|
264
288
|
if (active.size === 0) return;
|
|
265
|
-
for (const shortcut of active) if (!event || releases(event, shortcut)) active.delete(shortcut);
|
|
289
|
+
for (const [shortcut, code] of active) if (!event || code !== "" && event.code === code || releases(event, shortcut)) active.delete(shortcut);
|
|
266
290
|
if (active.size === 0) element.classList.remove(className);
|
|
267
291
|
};
|
|
268
292
|
const offUp = on(window, "keyup", release);
|
|
269
293
|
const offBlur = on(window, "blur", () => release());
|
|
270
294
|
return {
|
|
271
295
|
press(event, shortcuts) {
|
|
272
|
-
for (const shortcut of shortcuts) if (matches(event, shortcut)) active.
|
|
296
|
+
for (const shortcut of shortcuts) if (matches(event, shortcut)) active.set(shortcut, event.code);
|
|
273
297
|
element.classList.add(className);
|
|
274
298
|
},
|
|
275
299
|
stop() {
|
|
@@ -343,5 +367,4 @@ function pressed(keys, options = {}) {
|
|
|
343
367
|
};
|
|
344
368
|
};
|
|
345
369
|
}
|
|
346
|
-
//#endregion
|
|
347
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
|
-
|
|
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
|
+
"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": "^
|
|
56
|
-
"jsdom": "^
|
|
57
|
-
"svelte": "^5.
|
|
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.
|
|
59
|
+
"vite-plus": "1.8.0"
|
|
60
60
|
},
|
|
61
61
|
"peerDependencies": {
|
|
62
62
|
"svelte": ">=5.55.0"
|
|
@@ -69,13 +69,13 @@
|
|
|
69
69
|
},
|
|
70
70
|
"runtime": {
|
|
71
71
|
"name": "node",
|
|
72
|
-
"version": "
|
|
72
|
+
"version": "26.13.1"
|
|
73
73
|
}
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
76
|
"build": "vp pack",
|
|
77
77
|
"test": "vp test",
|
|
78
|
-
"ready": "vp check && vp run build && vp test && vp run
|
|
79
|
-
"dev": "vp run
|
|
78
|
+
"ready": "vp check && vp run build && vp test && vp run example#check && vp run example#build",
|
|
79
|
+
"dev": "vp run example#dev"
|
|
80
80
|
}
|
|
81
81
|
}
|