bzzz 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/LICENSE +21 -0
- package/README.md +30 -0
- package/dist/chunk-RLTRZ2WN.mjs +398 -0
- package/dist/index.d.mts +124 -0
- package/dist/index.d.ts +124 -0
- package/dist/index.js +430 -0
- package/dist/index.mjs +18 -0
- package/dist/react.d.mts +34 -0
- package/dist/react.d.ts +34 -0
- package/dist/react.js +447 -0
- package/dist/react.mjs +45 -0
- package/dist/types-BRn0rP4m.d.mts +47 -0
- package/dist/types-BRn0rP4m.d.ts +47 -0
- package/package.json +72 -0
package/dist/react.js
ADDED
|
@@ -0,0 +1,447 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/react.ts
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
useCreateHaptics: () => useCreateHaptics,
|
|
24
|
+
useHaptics: () => useHaptics
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(react_exports);
|
|
27
|
+
var import_react = require("react");
|
|
28
|
+
|
|
29
|
+
// src/audio.ts
|
|
30
|
+
var CLICK_DURATION = 5e-3;
|
|
31
|
+
var CLICK_GAIN = 0.3;
|
|
32
|
+
var FILTER_FREQ = 2200;
|
|
33
|
+
var FILTER_Q = 3;
|
|
34
|
+
var engine = null;
|
|
35
|
+
function getAudioEngine() {
|
|
36
|
+
if (engine) return engine;
|
|
37
|
+
try {
|
|
38
|
+
let createClickBuffer2 = function() {
|
|
39
|
+
const buffer = ctx.createBuffer(1, bufferLength, ctx.sampleRate);
|
|
40
|
+
const data = buffer.getChannelData(0);
|
|
41
|
+
for (let i = 0; i < data.length; i++) {
|
|
42
|
+
const t = i / data.length;
|
|
43
|
+
const envelope = (1 - t) * (1 - t);
|
|
44
|
+
data[i] = (Math.random() * 2 - 1) * envelope;
|
|
45
|
+
}
|
|
46
|
+
return buffer;
|
|
47
|
+
}, playTap2 = function(startTime, intensity = 1) {
|
|
48
|
+
if (ctx.state === "suspended") {
|
|
49
|
+
ctx.resume().catch(() => void 0);
|
|
50
|
+
}
|
|
51
|
+
const level = Math.max(0, Math.min(1, intensity));
|
|
52
|
+
if (level === 0) return;
|
|
53
|
+
const buffer = createClickBuffer2();
|
|
54
|
+
const source = ctx.createBufferSource();
|
|
55
|
+
source.buffer = buffer;
|
|
56
|
+
const filter = ctx.createBiquadFilter();
|
|
57
|
+
filter.type = "lowpass";
|
|
58
|
+
filter.frequency.setValueAtTime(
|
|
59
|
+
FILTER_FREQ + (Math.random() - 0.5) * 400,
|
|
60
|
+
startTime
|
|
61
|
+
);
|
|
62
|
+
filter.Q.setValueAtTime(FILTER_Q, startTime);
|
|
63
|
+
const gain = ctx.createGain();
|
|
64
|
+
gain.gain.setValueAtTime(CLICK_GAIN * level, startTime);
|
|
65
|
+
source.connect(filter);
|
|
66
|
+
filter.connect(gain);
|
|
67
|
+
gain.connect(ctx.destination);
|
|
68
|
+
source.start(startTime);
|
|
69
|
+
source.stop(startTime + CLICK_DURATION);
|
|
70
|
+
};
|
|
71
|
+
var createClickBuffer = createClickBuffer2, playTap = playTap2;
|
|
72
|
+
const AC = globalThis.AudioContext ?? globalThis.webkitAudioContext;
|
|
73
|
+
if (!AC) return null;
|
|
74
|
+
const ctx = new AC();
|
|
75
|
+
const bufferLength = Math.ceil(CLICK_DURATION * ctx.sampleRate);
|
|
76
|
+
engine = { playTap: playTap2, context: ctx };
|
|
77
|
+
return engine;
|
|
78
|
+
} catch {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function resetAudioEngine() {
|
|
83
|
+
if (engine) {
|
|
84
|
+
try {
|
|
85
|
+
engine.context.close().catch(() => {
|
|
86
|
+
});
|
|
87
|
+
} catch {
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
engine = null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/capabilities.ts
|
|
94
|
+
function getAudioContextConstructor() {
|
|
95
|
+
const runtime = globalThis;
|
|
96
|
+
return runtime.AudioContext ?? runtime.webkitAudioContext;
|
|
97
|
+
}
|
|
98
|
+
function isIOS() {
|
|
99
|
+
if (typeof navigator === "undefined") return false;
|
|
100
|
+
const ua = navigator.userAgent;
|
|
101
|
+
if (/iPad|iPhone|iPod/.test(ua)) return true;
|
|
102
|
+
if (navigator.maxTouchPoints > 1) {
|
|
103
|
+
if (navigator.platform === "MacIntel") return true;
|
|
104
|
+
const uad = navigator.userAgentData;
|
|
105
|
+
if (uad?.platform === "macOS") return true;
|
|
106
|
+
}
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
function hasVibrationHardware() {
|
|
110
|
+
if (typeof navigator === "undefined") return false;
|
|
111
|
+
if (typeof navigator.vibrate !== "function") return false;
|
|
112
|
+
if (typeof matchMedia !== "undefined" && matchMedia("(pointer: coarse)").matches) {
|
|
113
|
+
return true;
|
|
114
|
+
}
|
|
115
|
+
return false;
|
|
116
|
+
}
|
|
117
|
+
function prefersReducedMotion() {
|
|
118
|
+
if (typeof matchMedia === "undefined") return false;
|
|
119
|
+
return matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
120
|
+
}
|
|
121
|
+
function getCapabilityState() {
|
|
122
|
+
return {
|
|
123
|
+
haptics: hasVibrationHardware(),
|
|
124
|
+
audio: Boolean(getAudioContextConstructor()),
|
|
125
|
+
ios: isIOS(),
|
|
126
|
+
reducedMotion: prefersReducedMotion()
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/patterns.ts
|
|
131
|
+
var defaultPatterns = {
|
|
132
|
+
selection: [
|
|
133
|
+
{ type: "pulse", duration: 8, intensity: 0.4 },
|
|
134
|
+
{ type: "gap", duration: 12 },
|
|
135
|
+
{ type: "pulse", duration: 10, intensity: 0.6 }
|
|
136
|
+
],
|
|
137
|
+
success: [
|
|
138
|
+
{ type: "pulse", duration: 15, intensity: 0.4 },
|
|
139
|
+
{ type: "gap", duration: 40 },
|
|
140
|
+
{ type: "pulse", duration: 25, intensity: 0.7 },
|
|
141
|
+
{ type: "gap", duration: 60 },
|
|
142
|
+
{ type: "pulse", duration: 35, intensity: 1 }
|
|
143
|
+
],
|
|
144
|
+
error: [
|
|
145
|
+
{ type: "pulse", duration: 30, intensity: 0.7 },
|
|
146
|
+
{ type: "gap", duration: 30 },
|
|
147
|
+
{ type: "pulse", duration: 30, intensity: 0.7 },
|
|
148
|
+
{ type: "gap", duration: 30 },
|
|
149
|
+
{ type: "pulse", duration: 35, intensity: 0.9 },
|
|
150
|
+
{ type: "gap", duration: 30 },
|
|
151
|
+
{ type: "pulse", duration: 40, intensity: 1 }
|
|
152
|
+
],
|
|
153
|
+
toggle: [
|
|
154
|
+
{ type: "pulse", duration: 12, intensity: 0.5 },
|
|
155
|
+
{ type: "gap", duration: 24 },
|
|
156
|
+
{ type: "pulse", duration: 18, intensity: 0.8 },
|
|
157
|
+
{ type: "gap", duration: 24 },
|
|
158
|
+
{ type: "pulse", duration: 12, intensity: 0.5 }
|
|
159
|
+
],
|
|
160
|
+
snap: [
|
|
161
|
+
{ type: "pulse", duration: 8, intensity: 0.3 },
|
|
162
|
+
{ type: "gap", duration: 8 },
|
|
163
|
+
{ type: "pulse", duration: 10, intensity: 0.5 },
|
|
164
|
+
{ type: "gap", duration: 8 },
|
|
165
|
+
{ type: "pulse", duration: 12, intensity: 0.7 },
|
|
166
|
+
{ type: "gap", duration: 8 },
|
|
167
|
+
{ type: "pulse", duration: 14, intensity: 0.9 },
|
|
168
|
+
{ type: "gap", duration: 8 },
|
|
169
|
+
{ type: "pulse", duration: 16, intensity: 1 }
|
|
170
|
+
]
|
|
171
|
+
};
|
|
172
|
+
for (const pattern of Object.values(defaultPatterns)) {
|
|
173
|
+
Object.freeze(pattern);
|
|
174
|
+
for (const block of pattern) {
|
|
175
|
+
Object.freeze(block);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
Object.freeze(defaultPatterns);
|
|
179
|
+
function clonePattern(pattern) {
|
|
180
|
+
return pattern.map((block) => ({ ...block }));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// src/safari-haptics.ts
|
|
184
|
+
var TAP_INTERVAL = 26;
|
|
185
|
+
var label = null;
|
|
186
|
+
function ensureDOM() {
|
|
187
|
+
if (label) return;
|
|
188
|
+
if (typeof document === "undefined") return;
|
|
189
|
+
label = document.createElement("label");
|
|
190
|
+
label.ariaHidden = "true";
|
|
191
|
+
Object.assign(label.style, {
|
|
192
|
+
position: "fixed",
|
|
193
|
+
left: "-9999px",
|
|
194
|
+
top: "-9999px",
|
|
195
|
+
opacity: "0.01",
|
|
196
|
+
pointerEvents: "none"
|
|
197
|
+
});
|
|
198
|
+
const input = document.createElement("input");
|
|
199
|
+
input.type = "checkbox";
|
|
200
|
+
input.setAttribute("switch", "");
|
|
201
|
+
input.ariaHidden = "true";
|
|
202
|
+
input.tabIndex = -1;
|
|
203
|
+
label.appendChild(input);
|
|
204
|
+
document.body.appendChild(label);
|
|
205
|
+
}
|
|
206
|
+
function triggerSafariHaptic() {
|
|
207
|
+
if (typeof document === "undefined") return;
|
|
208
|
+
ensureDOM();
|
|
209
|
+
label?.click();
|
|
210
|
+
}
|
|
211
|
+
var pendingTimers = [];
|
|
212
|
+
function playSafariPattern(pattern) {
|
|
213
|
+
for (const id of pendingTimers) clearTimeout(id);
|
|
214
|
+
pendingTimers = [];
|
|
215
|
+
let cursorMs = 0;
|
|
216
|
+
const pulseTimes = [];
|
|
217
|
+
for (const block of pattern) {
|
|
218
|
+
if (block.type === "pulse" && block.duration >= 5) {
|
|
219
|
+
pulseTimes.push(cursorMs);
|
|
220
|
+
}
|
|
221
|
+
cursorMs += block.duration;
|
|
222
|
+
}
|
|
223
|
+
if (pulseTimes.length === 0) return;
|
|
224
|
+
triggerSafariHaptic();
|
|
225
|
+
for (let i = 1; i < pulseTimes.length; i++) {
|
|
226
|
+
const delay = Math.max(pulseTimes[i] - pulseTimes[0], i * TAP_INTERVAL);
|
|
227
|
+
pendingTimers.push(setTimeout(() => triggerSafariHaptic(), delay));
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
function destroySafariHaptic() {
|
|
231
|
+
for (const id of pendingTimers) clearTimeout(id);
|
|
232
|
+
pendingTimers = [];
|
|
233
|
+
if (label?.parentNode) label.parentNode.removeChild(label);
|
|
234
|
+
label = null;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// src/playback.ts
|
|
238
|
+
var PWM_CYCLE = 20;
|
|
239
|
+
function modulateVibration(duration, intensity) {
|
|
240
|
+
if (intensity >= 1) return [duration];
|
|
241
|
+
if (intensity <= 0) return [];
|
|
242
|
+
if (duration <= PWM_CYCLE) return [duration];
|
|
243
|
+
const onTime = Math.max(1, Math.round(PWM_CYCLE * intensity));
|
|
244
|
+
const offTime = PWM_CYCLE - onTime;
|
|
245
|
+
const result = [];
|
|
246
|
+
let remaining = duration;
|
|
247
|
+
while (remaining >= PWM_CYCLE) {
|
|
248
|
+
result.push(onTime, offTime);
|
|
249
|
+
remaining -= PWM_CYCLE;
|
|
250
|
+
}
|
|
251
|
+
if (remaining > 0) {
|
|
252
|
+
const remainOn = Math.min(remaining, onTime);
|
|
253
|
+
result.push(remainOn);
|
|
254
|
+
const remainOff = remaining - remainOn;
|
|
255
|
+
if (remainOff > 0) {
|
|
256
|
+
result.push(remainOff);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return result;
|
|
260
|
+
}
|
|
261
|
+
function toVibrationPattern(pattern) {
|
|
262
|
+
const result = [];
|
|
263
|
+
for (const block of pattern) {
|
|
264
|
+
if (block.type === "pulse") {
|
|
265
|
+
const intensity = block.intensity ?? 1;
|
|
266
|
+
const modulated = modulateVibration(block.duration, intensity);
|
|
267
|
+
if (modulated.length > 0) {
|
|
268
|
+
if (result.length > 0 && result.length % 2 === 1) {
|
|
269
|
+
result.push(0, ...modulated);
|
|
270
|
+
} else {
|
|
271
|
+
result.push(...modulated);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
} else if (block.type === "gap") {
|
|
275
|
+
if (result.length === 0) continue;
|
|
276
|
+
if (result.length % 2 === 0) {
|
|
277
|
+
result[result.length - 1] += block.duration;
|
|
278
|
+
} else {
|
|
279
|
+
result.push(block.duration);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
return result;
|
|
284
|
+
}
|
|
285
|
+
function playAudioClicks(pattern) {
|
|
286
|
+
const engine2 = getAudioEngine();
|
|
287
|
+
if (!engine2) return false;
|
|
288
|
+
const startTime = engine2.context.currentTime + 0.01;
|
|
289
|
+
let cursorMs = 0;
|
|
290
|
+
let scheduled = false;
|
|
291
|
+
for (const block of pattern) {
|
|
292
|
+
if (block.type === "pulse" && block.duration >= 5) {
|
|
293
|
+
engine2.playTap(startTime + cursorMs / 1e3, block.intensity ?? 1);
|
|
294
|
+
scheduled = true;
|
|
295
|
+
}
|
|
296
|
+
cursorMs += block.duration;
|
|
297
|
+
}
|
|
298
|
+
return scheduled;
|
|
299
|
+
}
|
|
300
|
+
function validatePattern(pattern) {
|
|
301
|
+
return pattern.map((b) => {
|
|
302
|
+
const duration = Math.max(0, Number.isFinite(b.duration) ? b.duration : 0);
|
|
303
|
+
if (b.type === "pulse") {
|
|
304
|
+
return {
|
|
305
|
+
type: "pulse",
|
|
306
|
+
duration,
|
|
307
|
+
intensity: b.intensity != null ? Math.max(0, Math.min(1, b.intensity)) : void 0
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
return { type: "gap", duration };
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
function playPattern(pattern, options) {
|
|
314
|
+
const capabilities = getCapabilityState();
|
|
315
|
+
if (capabilities.reducedMotion || options?.enabled === false) return { mode: "none" };
|
|
316
|
+
const validated = validatePattern(pattern);
|
|
317
|
+
const vibrationPattern = toVibrationPattern(validated);
|
|
318
|
+
if (capabilities.ios && validated.some((b) => b.type === "pulse" && b.duration >= 5)) {
|
|
319
|
+
playSafariPattern(validated);
|
|
320
|
+
try {
|
|
321
|
+
playAudioClicks(validated);
|
|
322
|
+
} catch {
|
|
323
|
+
}
|
|
324
|
+
return { mode: "haptics" };
|
|
325
|
+
}
|
|
326
|
+
if (vibrationPattern.length > 0 && capabilities.haptics && navigator.vibrate(vibrationPattern)) {
|
|
327
|
+
try {
|
|
328
|
+
playAudioClicks(validated);
|
|
329
|
+
} catch {
|
|
330
|
+
}
|
|
331
|
+
return { mode: "haptics" };
|
|
332
|
+
}
|
|
333
|
+
if (capabilities.audio) {
|
|
334
|
+
try {
|
|
335
|
+
const played = playAudioClicks(validated);
|
|
336
|
+
return { mode: played ? "audio" : "none" };
|
|
337
|
+
} catch {
|
|
338
|
+
return { mode: "none" };
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return { mode: "none" };
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// src/runtime.ts
|
|
345
|
+
function createRegistry(overrides) {
|
|
346
|
+
const registry = /* @__PURE__ */ new Map();
|
|
347
|
+
for (const [name, pattern] of Object.entries(defaultPatterns)) {
|
|
348
|
+
registry.set(name, clonePattern(pattern));
|
|
349
|
+
}
|
|
350
|
+
if (overrides) {
|
|
351
|
+
for (const [name, pattern] of Object.entries(overrides)) {
|
|
352
|
+
registry.set(name, clonePattern(pattern));
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return registry;
|
|
356
|
+
}
|
|
357
|
+
function resolvePattern(registry, nameOrPattern) {
|
|
358
|
+
if (typeof nameOrPattern !== "string") {
|
|
359
|
+
return clonePattern(nameOrPattern);
|
|
360
|
+
}
|
|
361
|
+
const pattern = registry.get(nameOrPattern);
|
|
362
|
+
if (!pattern) {
|
|
363
|
+
throw new Error(`Unknown haptics pattern: ${nameOrPattern}`);
|
|
364
|
+
}
|
|
365
|
+
return clonePattern(pattern);
|
|
366
|
+
}
|
|
367
|
+
var singletonEnabled = true;
|
|
368
|
+
function playNamedPattern(name) {
|
|
369
|
+
return playPattern(clonePattern(defaultPatterns[name]), { enabled: singletonEnabled });
|
|
370
|
+
}
|
|
371
|
+
var haptics = {
|
|
372
|
+
selection: () => playNamedPattern("selection"),
|
|
373
|
+
success: () => playNamedPattern("success"),
|
|
374
|
+
error: () => playNamedPattern("error"),
|
|
375
|
+
toggle: () => playNamedPattern("toggle"),
|
|
376
|
+
snap: () => playNamedPattern("snap"),
|
|
377
|
+
play: (pattern) => playPattern(clonePattern(pattern), { enabled: singletonEnabled }),
|
|
378
|
+
getCapabilities: () => getCapabilityState(),
|
|
379
|
+
setEnabled: (enabled) => {
|
|
380
|
+
singletonEnabled = enabled;
|
|
381
|
+
},
|
|
382
|
+
isEnabled: () => singletonEnabled,
|
|
383
|
+
dispose: () => {
|
|
384
|
+
resetAudioEngine();
|
|
385
|
+
destroySafariHaptic();
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
function createHaptics(options = {}) {
|
|
389
|
+
const registry = createRegistry(options.patterns);
|
|
390
|
+
let enabled = true;
|
|
391
|
+
return {
|
|
392
|
+
play: (nameOrPattern) => playPattern(resolvePattern(registry, nameOrPattern), { enabled }),
|
|
393
|
+
register: (name, pattern) => {
|
|
394
|
+
registry.set(name, clonePattern(pattern));
|
|
395
|
+
},
|
|
396
|
+
getCapabilities: () => getCapabilityState(),
|
|
397
|
+
setEnabled: (value) => {
|
|
398
|
+
enabled = value;
|
|
399
|
+
},
|
|
400
|
+
isEnabled: () => enabled,
|
|
401
|
+
dispose: () => {
|
|
402
|
+
resetAudioEngine();
|
|
403
|
+
destroySafariHaptic();
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// src/react.ts
|
|
409
|
+
var hapticsReturn = {
|
|
410
|
+
selection: haptics.selection,
|
|
411
|
+
success: haptics.success,
|
|
412
|
+
error: haptics.error,
|
|
413
|
+
toggle: haptics.toggle,
|
|
414
|
+
snap: haptics.snap,
|
|
415
|
+
play: haptics.play,
|
|
416
|
+
getCapabilities: haptics.getCapabilities,
|
|
417
|
+
isEnabled: haptics.isEnabled,
|
|
418
|
+
setEnabled: haptics.setEnabled
|
|
419
|
+
};
|
|
420
|
+
function useHaptics() {
|
|
421
|
+
return hapticsReturn;
|
|
422
|
+
}
|
|
423
|
+
function useCreateHaptics(options) {
|
|
424
|
+
const instanceRef = (0, import_react.useRef)(null);
|
|
425
|
+
if (!instanceRef.current) {
|
|
426
|
+
instanceRef.current = createHaptics(options);
|
|
427
|
+
}
|
|
428
|
+
(0, import_react.useEffect)(() => {
|
|
429
|
+
return () => {
|
|
430
|
+
instanceRef.current?.dispose();
|
|
431
|
+
instanceRef.current = null;
|
|
432
|
+
};
|
|
433
|
+
}, []);
|
|
434
|
+
const instance = instanceRef.current;
|
|
435
|
+
return (0, import_react.useMemo)(() => ({
|
|
436
|
+
play: instance.play,
|
|
437
|
+
register: instance.register,
|
|
438
|
+
getCapabilities: instance.getCapabilities,
|
|
439
|
+
isEnabled: instance.isEnabled,
|
|
440
|
+
setEnabled: instance.setEnabled
|
|
441
|
+
}), [instance]);
|
|
442
|
+
}
|
|
443
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
444
|
+
0 && (module.exports = {
|
|
445
|
+
useCreateHaptics,
|
|
446
|
+
useHaptics
|
|
447
|
+
});
|
package/dist/react.mjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createHaptics,
|
|
3
|
+
haptics
|
|
4
|
+
} from "./chunk-RLTRZ2WN.mjs";
|
|
5
|
+
|
|
6
|
+
// src/react.ts
|
|
7
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
8
|
+
var hapticsReturn = {
|
|
9
|
+
selection: haptics.selection,
|
|
10
|
+
success: haptics.success,
|
|
11
|
+
error: haptics.error,
|
|
12
|
+
toggle: haptics.toggle,
|
|
13
|
+
snap: haptics.snap,
|
|
14
|
+
play: haptics.play,
|
|
15
|
+
getCapabilities: haptics.getCapabilities,
|
|
16
|
+
isEnabled: haptics.isEnabled,
|
|
17
|
+
setEnabled: haptics.setEnabled
|
|
18
|
+
};
|
|
19
|
+
function useHaptics() {
|
|
20
|
+
return hapticsReturn;
|
|
21
|
+
}
|
|
22
|
+
function useCreateHaptics(options) {
|
|
23
|
+
const instanceRef = useRef(null);
|
|
24
|
+
if (!instanceRef.current) {
|
|
25
|
+
instanceRef.current = createHaptics(options);
|
|
26
|
+
}
|
|
27
|
+
useEffect(() => {
|
|
28
|
+
return () => {
|
|
29
|
+
instanceRef.current?.dispose();
|
|
30
|
+
instanceRef.current = null;
|
|
31
|
+
};
|
|
32
|
+
}, []);
|
|
33
|
+
const instance = instanceRef.current;
|
|
34
|
+
return useMemo(() => ({
|
|
35
|
+
play: instance.play,
|
|
36
|
+
register: instance.register,
|
|
37
|
+
getCapabilities: instance.getCapabilities,
|
|
38
|
+
isEnabled: instance.isEnabled,
|
|
39
|
+
setEnabled: instance.setEnabled
|
|
40
|
+
}), [instance]);
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
useCreateHaptics,
|
|
44
|
+
useHaptics
|
|
45
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
type PulseBlock = {
|
|
2
|
+
type: "pulse";
|
|
3
|
+
duration: number;
|
|
4
|
+
intensity?: number;
|
|
5
|
+
};
|
|
6
|
+
type GapBlock = {
|
|
7
|
+
type: "gap";
|
|
8
|
+
duration: number;
|
|
9
|
+
};
|
|
10
|
+
type PatternBlock = PulseBlock | GapBlock;
|
|
11
|
+
type NamedPattern = "selection" | "success" | "error" | "toggle" | "snap";
|
|
12
|
+
type PlaybackMode = "haptics" | "audio" | "none";
|
|
13
|
+
type PlaybackResult = {
|
|
14
|
+
mode: PlaybackMode;
|
|
15
|
+
};
|
|
16
|
+
type CapabilityState = {
|
|
17
|
+
haptics: boolean;
|
|
18
|
+
audio: boolean;
|
|
19
|
+
ios: boolean;
|
|
20
|
+
reducedMotion: boolean;
|
|
21
|
+
};
|
|
22
|
+
type PatternRegistry = Record<string, PatternBlock[]>;
|
|
23
|
+
type CreateHapticsOptions = {
|
|
24
|
+
patterns?: PatternRegistry;
|
|
25
|
+
};
|
|
26
|
+
type HapticsApi = {
|
|
27
|
+
selection: () => PlaybackResult;
|
|
28
|
+
success: () => PlaybackResult;
|
|
29
|
+
error: () => PlaybackResult;
|
|
30
|
+
toggle: () => PlaybackResult;
|
|
31
|
+
snap: () => PlaybackResult;
|
|
32
|
+
play: (pattern: readonly PatternBlock[]) => PlaybackResult;
|
|
33
|
+
getCapabilities: () => CapabilityState;
|
|
34
|
+
setEnabled: (enabled: boolean) => void;
|
|
35
|
+
isEnabled: () => boolean;
|
|
36
|
+
dispose: () => void;
|
|
37
|
+
};
|
|
38
|
+
type HapticsInstance = {
|
|
39
|
+
play: (nameOrPattern: string | readonly PatternBlock[]) => PlaybackResult;
|
|
40
|
+
register: (name: string, pattern: readonly PatternBlock[]) => void;
|
|
41
|
+
getCapabilities: () => CapabilityState;
|
|
42
|
+
setEnabled: (enabled: boolean) => void;
|
|
43
|
+
isEnabled: () => boolean;
|
|
44
|
+
dispose: () => void;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type { CapabilityState as C, GapBlock as G, HapticsInstance as H, NamedPattern as N, PatternBlock as P, CreateHapticsOptions as a, HapticsApi as b, PatternRegistry as c, PlaybackMode as d, PlaybackResult as e, PulseBlock as f };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
type PulseBlock = {
|
|
2
|
+
type: "pulse";
|
|
3
|
+
duration: number;
|
|
4
|
+
intensity?: number;
|
|
5
|
+
};
|
|
6
|
+
type GapBlock = {
|
|
7
|
+
type: "gap";
|
|
8
|
+
duration: number;
|
|
9
|
+
};
|
|
10
|
+
type PatternBlock = PulseBlock | GapBlock;
|
|
11
|
+
type NamedPattern = "selection" | "success" | "error" | "toggle" | "snap";
|
|
12
|
+
type PlaybackMode = "haptics" | "audio" | "none";
|
|
13
|
+
type PlaybackResult = {
|
|
14
|
+
mode: PlaybackMode;
|
|
15
|
+
};
|
|
16
|
+
type CapabilityState = {
|
|
17
|
+
haptics: boolean;
|
|
18
|
+
audio: boolean;
|
|
19
|
+
ios: boolean;
|
|
20
|
+
reducedMotion: boolean;
|
|
21
|
+
};
|
|
22
|
+
type PatternRegistry = Record<string, PatternBlock[]>;
|
|
23
|
+
type CreateHapticsOptions = {
|
|
24
|
+
patterns?: PatternRegistry;
|
|
25
|
+
};
|
|
26
|
+
type HapticsApi = {
|
|
27
|
+
selection: () => PlaybackResult;
|
|
28
|
+
success: () => PlaybackResult;
|
|
29
|
+
error: () => PlaybackResult;
|
|
30
|
+
toggle: () => PlaybackResult;
|
|
31
|
+
snap: () => PlaybackResult;
|
|
32
|
+
play: (pattern: readonly PatternBlock[]) => PlaybackResult;
|
|
33
|
+
getCapabilities: () => CapabilityState;
|
|
34
|
+
setEnabled: (enabled: boolean) => void;
|
|
35
|
+
isEnabled: () => boolean;
|
|
36
|
+
dispose: () => void;
|
|
37
|
+
};
|
|
38
|
+
type HapticsInstance = {
|
|
39
|
+
play: (nameOrPattern: string | readonly PatternBlock[]) => PlaybackResult;
|
|
40
|
+
register: (name: string, pattern: readonly PatternBlock[]) => void;
|
|
41
|
+
getCapabilities: () => CapabilityState;
|
|
42
|
+
setEnabled: (enabled: boolean) => void;
|
|
43
|
+
isEnabled: () => boolean;
|
|
44
|
+
dispose: () => void;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type { CapabilityState as C, GapBlock as G, HapticsInstance as H, NamedPattern as N, PatternBlock as P, CreateHapticsOptions as a, HapticsApi as b, PatternRegistry as c, PlaybackMode as d, PlaybackResult as e, PulseBlock as f };
|
package/package.json
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bzzz",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Haptic feedback for the web. Native vibration + audio fallback.",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"engines": {
|
|
10
|
+
"node": ">=18"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"README.md"
|
|
15
|
+
],
|
|
16
|
+
"exports": {
|
|
17
|
+
".": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"import": "./dist/index.mjs",
|
|
20
|
+
"require": "./dist/index.js"
|
|
21
|
+
},
|
|
22
|
+
"./react": {
|
|
23
|
+
"types": "./dist/react.d.ts",
|
|
24
|
+
"import": "./dist/react.mjs",
|
|
25
|
+
"require": "./dist/react.js"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "tsup src/index.ts src/react.ts --format esm,cjs --dts --clean --external react",
|
|
30
|
+
"dev": "tsup src/index.ts src/react.ts --format esm,cjs --dts --watch --external react",
|
|
31
|
+
"dev:website": "npm --prefix website run dev",
|
|
32
|
+
"build:website": "npm --prefix website run build",
|
|
33
|
+
"dev:all": "concurrently \"npm run dev\" \"npm run dev:website\"",
|
|
34
|
+
"lint": "tsc --noEmit",
|
|
35
|
+
"test": "vitest run",
|
|
36
|
+
"pack:check": "npm pack --dry-run --cache /tmp/bzzz-npm-cache"
|
|
37
|
+
},
|
|
38
|
+
"keywords": [
|
|
39
|
+
"haptics",
|
|
40
|
+
"audio",
|
|
41
|
+
"feedback",
|
|
42
|
+
"interaction",
|
|
43
|
+
"web"
|
|
44
|
+
],
|
|
45
|
+
"license": "MIT",
|
|
46
|
+
"author": "Pavle Lucic",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/pavlito/bzzz"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://pavlito.github.io/bzzz",
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/pavlito/bzzz/issues"
|
|
54
|
+
},
|
|
55
|
+
"peerDependencies": {
|
|
56
|
+
"react": ">=18"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"react": {
|
|
60
|
+
"optional": true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@types/react": "^19.0.0",
|
|
65
|
+
"concurrently": "^9.2.0",
|
|
66
|
+
"jsdom": "^29.0.0",
|
|
67
|
+
"react": "^19.0.0",
|
|
68
|
+
"tsup": "^8.5.0",
|
|
69
|
+
"typescript": "^5.9.3",
|
|
70
|
+
"vitest": "^3.2.4"
|
|
71
|
+
}
|
|
72
|
+
}
|