solid-drift 0.22.0 → 0.23.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
@@ -1445,6 +1445,34 @@ createInfiniteScroll(() => sentinel, {
1445
1445
  - `createScrollLock()` returns `{ locked, lock, unlock }`: sets `document.body.style.overflow = "hidden"`, restores the previous value when the last lock releases, and reference-counts nested locks so stacked modals cannot unlock each other early. Unmounting releases the locks.
1446
1446
  - `createInfiniteScroll(ref, options)` observes a sentinel with IntersectionObserver and calls `onLoadMore` as it approaches the viewport (`threshold` px prefetch via `rootMargin`, default `200`). `disabled` is a reactive kill switch (e.g. `() => !hasMore()`).
1447
1447
 
1448
+ ### Haptics
1449
+
1450
+ Tactile feedback through the Vibration API: `createHaptic` wraps `navigator.vibrate` with an iOS-style vocabulary (light/medium/heavy, success/warning/error), one-shot presets, and morse-code encoding; `createHapticBeat` is a 16-step haptic sequencer (heartbeat pulses, metronome ticks, breathing guides) running on the shared animation clock.
1451
+
1452
+ ```tsx
1453
+ import { createHaptic, createHapticBeat, hapticBeatPresets } from "solid-drift"
1454
+
1455
+ const haptic = createHaptic()
1456
+ // Buttons get a physical click:
1457
+ <button onClick={() => { haptic.light(); confirm() }}>Confirm</button>
1458
+ // Morse code: dots, dashes, letter gaps, word gaps.
1459
+ <button onClick={() => haptic.morse("... --- ...")}>SOS</button>
1460
+
1461
+ // A heartbeat pulse the user can toggle:
1462
+ const beat = createHapticBeat(haptic, {
1463
+ bpm: 60,
1464
+ pattern: hapticBeatPresets.heartbeat,
1465
+ onStep: (i) => setFlash(i === 0),
1466
+ })
1467
+ <button onClick={() => beat.toggle()}>
1468
+ {beat.playing() ? "Stop pulse" : "Start pulse"}
1469
+ </button>
1470
+ ```
1471
+
1472
+ - `createHaptic(options?)` returns `{ supported, vibrate, light, medium, heavy, success, warning, error, morse }`. `vibrate(pattern)` fires a raw ms pattern; `morse(code, unit?)` encodes `"."`, `"-"`, `" "` (letter gap), `"/"` (word gap) with a configurable dot length (default 60ms). `hapticPatterns` holds the one-shot presets (`tap`, `doubleTap`, `longPress`, `tick`, `heartbeat`, `success`, `warning`, `error`). `options.enabled` is a boolean or a signal master switch (wire it to `useLowPowerMode()`).
1473
+ - `createHapticBeat(haptic, options?)` returns `{ playing, bpm, step, start, stop, toggle, setBpm }`. The 16-step pattern uses `"x"` for a hit, `"X"` for an accent, anything else for a rest; steps run as 16th notes at `bpm` (live-changeable via `setBpm`), the downbeat fires immediately on `start()`, and `onStep(i)` reports each step index. `hapticBeatPresets` ships `heartbeat`, `metronome`, `ticks`, and `pulse`.
1474
+ - Haptics are tactile, not visual, so they fire under reduced motion too; the `enabled` switch is the way to offer quiet. Everything is a no-op where vibration is unsupported, and SSR-safe.
1475
+
1448
1476
  ### Easings
1449
1477
 
1450
1478
  Named easings: `linear`, `easeInQuad`, `easeOutQuad`, `easeInOutQuad`, `easeInCubic`, `easeOutCubic`, `easeInOutCubic`, `easeInQuart`, `easeOutQuart`, `easeInOutQuart`, `easeOutExpo`, `easeOutBack`, plus the cartoon set: `easeInBack` (anticipation dip before movement), `easeInOutBack` (wind-up, overshoot, settle), `easeOutElastic` (decaying rubber-band oscillation), `easeOutBounce` (shrinking cartoon bounces). Also `cubicBezier(x1, y1, x2, y2)` for CSS-style curves. Pass a name or a custom `(t) => number` function anywhere an easing is accepted.
@@ -0,0 +1,115 @@
1
+ import { type Accessor } from "solid-js";
2
+ export type HapticPattern = number | number[];
3
+ export interface HapticOptions {
4
+ /**
5
+ * Master switch. Accepts a plain boolean or a signal, so it can be
6
+ * wired to `useLowPowerMode()`. Default true.
7
+ */
8
+ enabled?: Accessor<boolean> | boolean;
9
+ }
10
+ export interface HapticControls {
11
+ /** True when the device can vibrate and haptics are enabled. */
12
+ supported: Accessor<boolean>;
13
+ /** Fire a raw vibration pattern (ms). No-op when unsupported. */
14
+ vibrate: (pattern: HapticPattern) => void;
15
+ /** Short tap. */
16
+ light: () => void;
17
+ /** Firmer tap. */
18
+ medium: () => void;
19
+ /** Strong tap. */
20
+ heavy: () => void;
21
+ /** Rising two-tap confirmation. */
22
+ success: () => void;
23
+ /** Double low tap. */
24
+ warning: () => void;
25
+ /** Triple strong tap. */
26
+ error: () => void;
27
+ /**
28
+ * Vibrate morse code: "." dot, "-" dash, " " letter gap, "/" word
29
+ * gap. `unit` is the dot length in ms (default 60).
30
+ */
31
+ morse: (code: string, unit?: number) => void;
32
+ }
33
+ /**
34
+ * One-shot vibration presets, in milliseconds. Pass any of these to
35
+ * `vibrate`, or use the named helpers on the controls.
36
+ */
37
+ export declare const hapticPatterns: Record<string, HapticPattern>;
38
+ /**
39
+ * Tactile feedback through the Vibration API (`navigator.vibrate`).
40
+ * Buttons, toggles, and confirmations get a physical click; the
41
+ * presets mirror the iOS haptic vocabulary (light/medium/heavy,
42
+ * success/warning/error) using vibration timing.
43
+ *
44
+ * Haptics are tactile, not visual, so they still fire under reduced
45
+ * motion. Gate them with `enabled` (a boolean or a signal) when the
46
+ * user asks for quiet, e.g. wired to `useLowPowerMode()`.
47
+ *
48
+ * SSR-safe and unsupported-device safe: everything is a no-op and
49
+ * `supported()` is false.
50
+ *
51
+ * ```tsx
52
+ * const haptic = createHaptic()
53
+ * <button onClick={() => { haptic.light(); confirm() }}>Confirm</button>
54
+ * <button onClick={() => haptic.morse("... --- ...")}>SOS</button>
55
+ * ```
56
+ */
57
+ export declare function createHaptic(options?: HapticOptions): HapticControls;
58
+ /**
59
+ * 16-step haptic sequencer presets. "x" is a hit, "X" an accent, and
60
+ * anything else a rest.
61
+ */
62
+ export declare const hapticBeatPresets: Record<string, string>;
63
+ export interface HapticBeatOptions {
64
+ /** Beats per minute (quarter notes). Default 60. */
65
+ bpm?: number;
66
+ /**
67
+ * 16-step pattern string: "x" hit, "X" accent, anything else rest.
68
+ * Shorter strings are padded with rests, longer ones are cut.
69
+ * Default is the heartbeat preset.
70
+ */
71
+ pattern?: string;
72
+ /** Vibration ms for a normal step. Default 12. */
73
+ stepMs?: number;
74
+ /** Vibration ms for an accent step. Default 30. */
75
+ accentMs?: number;
76
+ /** Start immediately. Default false. */
77
+ autostart?: boolean;
78
+ /** Called on every step with its index (0-15). */
79
+ onStep?: (step: number) => void;
80
+ }
81
+ export interface HapticBeatControls {
82
+ /** Whether the sequencer is running. */
83
+ playing: Accessor<boolean>;
84
+ /** Current tempo. Change it live with setBpm. */
85
+ bpm: Accessor<number>;
86
+ /** Current step index (0-15), or -1 when stopped. */
87
+ step: Accessor<number>;
88
+ start: () => void;
89
+ stop: () => void;
90
+ toggle: () => void;
91
+ setBpm: (bpm: number) => void;
92
+ }
93
+ /**
94
+ * A 16-step haptic sequencer on the shared animation clock: heartbeat
95
+ * pulses, metronome ticks, breathing guides, game countdowns. The
96
+ * steps run as 16th notes at `bpm`, so a 60 BPM heartbeat preset
97
+ * pulses once per second with the accent on the downbeat.
98
+ *
99
+ * Takes the haptic controls from `createHaptic` (so one `enabled`
100
+ * switch gates everything) and drives `vibrate` per step. Tempo
101
+ * changes apply live. SSR-safe: `start()` is a no-op on the server.
102
+ *
103
+ * ```tsx
104
+ * const haptic = createHaptic()
105
+ * const beat = createHapticBeat(haptic, {
106
+ * bpm: 60,
107
+ * pattern: hapticBeatPresets.heartbeat,
108
+ * onStep: (i) => setFlash(i === 0),
109
+ * })
110
+ * <button onClick={() => beat.toggle()}>
111
+ * {beat.playing() ? "Stop pulse" : "Start pulse"}
112
+ * </button>
113
+ * ```
114
+ */
115
+ export declare function createHapticBeat(haptic: Pick<HapticControls, "vibrate">, options?: HapticBeatOptions): HapticBeatControls;
package/dist/haptic.js ADDED
@@ -0,0 +1,200 @@
1
+ import { createSignal, onCleanup } from "solid-js";
2
+ import { now, schedule } from "./engine.js";
3
+ /**
4
+ * One-shot vibration presets, in milliseconds. Pass any of these to
5
+ * `vibrate`, or use the named helpers on the controls.
6
+ */
7
+ export const hapticPatterns = {
8
+ tap: 10,
9
+ doubleTap: [15, 50, 15],
10
+ longPress: 60,
11
+ tick: 8,
12
+ heartbeat: [25, 150, 40],
13
+ success: [10, 40, 25],
14
+ warning: [30, 50, 30],
15
+ error: [50, 50, 50, 50, 80],
16
+ };
17
+ /**
18
+ * Tactile feedback through the Vibration API (`navigator.vibrate`).
19
+ * Buttons, toggles, and confirmations get a physical click; the
20
+ * presets mirror the iOS haptic vocabulary (light/medium/heavy,
21
+ * success/warning/error) using vibration timing.
22
+ *
23
+ * Haptics are tactile, not visual, so they still fire under reduced
24
+ * motion. Gate them with `enabled` (a boolean or a signal) when the
25
+ * user asks for quiet, e.g. wired to `useLowPowerMode()`.
26
+ *
27
+ * SSR-safe and unsupported-device safe: everything is a no-op and
28
+ * `supported()` is false.
29
+ *
30
+ * ```tsx
31
+ * const haptic = createHaptic()
32
+ * <button onClick={() => { haptic.light(); confirm() }}>Confirm</button>
33
+ * <button onClick={() => haptic.morse("... --- ...")}>SOS</button>
34
+ * ```
35
+ */
36
+ export function createHaptic(options = {}) {
37
+ const enabled = typeof options.enabled === "function"
38
+ ? options.enabled
39
+ : () => options.enabled !== false;
40
+ const canVibrate = typeof navigator !== "undefined" &&
41
+ typeof navigator.vibrate === "function";
42
+ const supported = () => canVibrate && enabled();
43
+ const vibrate = (pattern) => {
44
+ if (!supported())
45
+ return;
46
+ try {
47
+ navigator.vibrate(pattern);
48
+ }
49
+ catch {
50
+ // Haptics are best-effort: never crash the interaction.
51
+ }
52
+ };
53
+ const morse = (code, unit = 60) => {
54
+ const u = Math.max(1, Math.round(unit));
55
+ const pattern = [];
56
+ for (const ch of code) {
57
+ if (ch === ".") {
58
+ pattern.push(u, u);
59
+ }
60
+ else if (ch === "-") {
61
+ pattern.push(3 * u, u);
62
+ }
63
+ else if (ch === " " || ch === "/") {
64
+ // Extend the trailing pause: a letter gap is 3 units total, a
65
+ // word gap 7, and one unit is already there after each element.
66
+ const last = pattern.length - 1;
67
+ if (last >= 0)
68
+ pattern[last] += ch === " " ? 2 * u : 6 * u;
69
+ }
70
+ }
71
+ // Drop the trailing pause: nothing to separate after the last element.
72
+ if (pattern.length % 2 === 0)
73
+ pattern.pop();
74
+ if (pattern.length > 0)
75
+ vibrate(pattern);
76
+ };
77
+ return {
78
+ supported,
79
+ vibrate,
80
+ light: () => vibrate(hapticPatterns.tap),
81
+ medium: () => vibrate(20),
82
+ heavy: () => vibrate(30),
83
+ success: () => vibrate(hapticPatterns.success),
84
+ warning: () => vibrate(hapticPatterns.warning),
85
+ error: () => vibrate(hapticPatterns.error),
86
+ morse,
87
+ };
88
+ }
89
+ /* ------------------------------------------------------------------ */
90
+ /* createHapticBeat */
91
+ /* ------------------------------------------------------------------ */
92
+ /**
93
+ * 16-step haptic sequencer presets. "x" is a hit, "X" an accent, and
94
+ * anything else a rest.
95
+ */
96
+ export const hapticBeatPresets = {
97
+ /** Lub-dub heartbeat. */
98
+ heartbeat: "X.......x.......",
99
+ /** Four-on-the-floor metronome. */
100
+ metronome: "X...x...x...x...",
101
+ /** Steady eighth-note ticks. */
102
+ ticks: "x.x.x.x.x.x.x.x.",
103
+ /** One pulse per bar. */
104
+ pulse: "X...............",
105
+ };
106
+ function parseSteps(pattern) {
107
+ const steps = [];
108
+ for (let i = 0; i < 16; i++) {
109
+ const ch = pattern[i];
110
+ steps.push(ch === "X" ? 2 : ch === "x" ? 1 : 0);
111
+ }
112
+ return steps;
113
+ }
114
+ /**
115
+ * A 16-step haptic sequencer on the shared animation clock: heartbeat
116
+ * pulses, metronome ticks, breathing guides, game countdowns. The
117
+ * steps run as 16th notes at `bpm`, so a 60 BPM heartbeat preset
118
+ * pulses once per second with the accent on the downbeat.
119
+ *
120
+ * Takes the haptic controls from `createHaptic` (so one `enabled`
121
+ * switch gates everything) and drives `vibrate` per step. Tempo
122
+ * changes apply live. SSR-safe: `start()` is a no-op on the server.
123
+ *
124
+ * ```tsx
125
+ * const haptic = createHaptic()
126
+ * const beat = createHapticBeat(haptic, {
127
+ * bpm: 60,
128
+ * pattern: hapticBeatPresets.heartbeat,
129
+ * onStep: (i) => setFlash(i === 0),
130
+ * })
131
+ * <button onClick={() => beat.toggle()}>
132
+ * {beat.playing() ? "Stop pulse" : "Start pulse"}
133
+ * </button>
134
+ * ```
135
+ */
136
+ export function createHapticBeat(haptic, options = {}) {
137
+ const { bpm: initialBpm = 60, pattern = hapticBeatPresets.heartbeat, stepMs = 12, accentMs = 30, autostart = false, onStep, } = options;
138
+ const steps = parseSteps(pattern);
139
+ const [playing, setPlaying] = createSignal(false);
140
+ const [bpm, setBpm] = createSignal(Math.max(1, initialBpm));
141
+ const [step, setStep] = createSignal(-1);
142
+ let cancel = null;
143
+ const stepDuration = () => 60000 / bpm() / 4;
144
+ const fire = (index) => {
145
+ setStep(index);
146
+ const kind = steps[index];
147
+ if (kind === 2)
148
+ haptic.vibrate(accentMs);
149
+ else if (kind === 1)
150
+ haptic.vibrate(stepMs);
151
+ onStep?.(index);
152
+ };
153
+ const start = () => {
154
+ if (typeof window === "undefined")
155
+ return; // SSR: no-op
156
+ if (playing())
157
+ return;
158
+ setPlaying(true);
159
+ let index = 0;
160
+ fire(0); // no latency: the downbeat lands immediately
161
+ let nextTime = now() + stepDuration();
162
+ cancel = schedule((t) => {
163
+ if (!playing())
164
+ return false;
165
+ const dur = stepDuration();
166
+ while (t >= nextTime) {
167
+ index = (index + 1) % 16;
168
+ fire(index);
169
+ nextTime += dur;
170
+ if (nextTime < t - 250) {
171
+ // Long main-thread stall: skip ahead instead of
172
+ // machine-gunning the missed steps.
173
+ nextTime = t + dur;
174
+ break;
175
+ }
176
+ }
177
+ return true;
178
+ });
179
+ };
180
+ const stop = () => {
181
+ if (!playing())
182
+ return;
183
+ setPlaying(false);
184
+ setStep(-1);
185
+ cancel?.();
186
+ cancel = null;
187
+ };
188
+ onCleanup(stop);
189
+ if (autostart)
190
+ start();
191
+ return {
192
+ playing,
193
+ bpm,
194
+ step,
195
+ start,
196
+ stop,
197
+ toggle: () => (playing() ? stop() : start()),
198
+ setBpm: (next) => setBpm(Math.max(1, next)),
199
+ };
200
+ }
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export { isLowPowerMode, useLowPowerMode, type LowPowerOptions, } from "./power.
15
15
  export { createSlotMachine, type SlotMachineControls, type SlotMachineOptions, type SlotMachineStatus, createRedPacket, type RedPacketCoin, type RedPacketControls, type RedPacketOptions, type RedPacketStatus, createConfetti, type ConfettiControls, type ConfettiOptions, createEmojiBurst, type EmojiBurstControls, type EmojiBurstOptions, createScratch, type ScratchControls, type ScratchOptions, } from "./fun.js";
16
16
  export { createStagger } from "./stagger.js";
17
17
  export { createDebounced, createThrottled, createLocalStorage, type LocalStorageOptions, type LocalStorageControls, createMediaQuery, createClickOutside, type ClickOutsideOptions, createScrollLock, type ScrollLockControls, createInfiniteScroll, type InfiniteScrollOptions, } from "./dom.js";
18
+ export { createHaptic, hapticPatterns, type HapticOptions, type HapticControls, type HapticPattern, createHapticBeat, hapticBeatPresets, type HapticBeatOptions, type HapticBeatControls, } from "./haptic.js";
18
19
  export { createHorizontalScroll, type HorizontalScrollOptions, type HorizontalScrollResult, } from "./horizontal.js";
19
20
  export { createScrub, type ScrubKeyframe, type ScrubOptions } from "./scrub.js";
20
21
  export { createScrollColor, type ScrollColorStop, type ScrollColorOptions, type ScrollColorFormat, createScrollTracking, type ScrollTrackingOptions, createScrollLine, type ScrollLineOptions, type ScrollLineStyle, type ScrollLineAxis, type ScrollLineOrigin, } from "./scrollfx.js";
package/dist/index.js CHANGED
@@ -15,6 +15,7 @@ export { isLowPowerMode, useLowPowerMode, } from "./power.js";
15
15
  export { createSlotMachine, createRedPacket, createConfetti, createEmojiBurst, createScratch, } from "./fun.js";
16
16
  export { createStagger } from "./stagger.js";
17
17
  export { createDebounced, createThrottled, createLocalStorage, createMediaQuery, createClickOutside, createScrollLock, createInfiniteScroll, } from "./dom.js";
18
+ export { createHaptic, hapticPatterns, createHapticBeat, hapticBeatPresets, } from "./haptic.js";
18
19
  export { createHorizontalScroll, } from "./horizontal.js";
19
20
  export { createScrub } from "./scrub.js";
20
21
  export { createScrollColor, createScrollTracking, createScrollLine, } from "./scrollfx.js";
package/package.json CHANGED
@@ -43,5 +43,5 @@
43
43
  },
44
44
  "type": "module",
45
45
  "types": "./dist/index.d.ts",
46
- "version": "0.22.0"
46
+ "version": "0.23.0"
47
47
  }