@flare-engine/haptics 0.4.18 → 0.4.19

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/dist/index.d.ts CHANGED
@@ -1,128 +1,5 @@
1
- /** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */
2
- type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';
3
- /** Notification feedback categories. */
4
- type HapticNotificationType = 'success' | 'warning' | 'error';
5
- /**
6
- * Haptics (tactile feedback) provider interface. Consumers supply a concrete
7
- * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app
8
- * boundary. The cue surface is deliberately thin — three semantic calls mirroring
9
- * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an
10
- * enabled gate, so the user's haptics setting and the reduced-motion gate
11
- * (`@flare-engine/a11y`) live in one place instead of at every call site.
12
- */
13
- interface HapticsProvider {
14
- /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */
15
- impact(style?: HapticImpactStyle): Promise<void>;
16
- /** A notification cue — victory, medal earned, error / deny. */
17
- notification(type?: HapticNotificationType): Promise<void>;
18
- /** A light selection tick — menu focus, toggle, pickup. */
19
- selection(): Promise<void>;
20
- /**
21
- * Whether cues currently fire. Reflects the user setting and the reduced-motion
22
- * gate; when `false`, `impact` / `notification` / `selection` are no-ops.
23
- */
24
- isEnabled(): boolean;
25
- /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */
26
- setEnabled(enabled: boolean): void;
27
- }
28
- /**
29
- * Immutable no-op provider. Reports disabled and fires nothing — the safe default
30
- * where there is no haptics backend (web without vibration, CI, tests) and the
31
- * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
32
- * hard "off" default, so wiring it into a context can never accidentally report
33
- * "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
34
- * consumer's haptics setting), use {@link createNoopHapticsProvider}.
35
- */
36
- export declare const NoopHapticsProvider: HapticsProvider;
37
- /**
38
- * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
39
- * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
40
- * without a native backend. Each call is an independent instance; defaults to
41
- * disabled (matching {@link NoopHapticsProvider}).
42
- */
43
- export declare function createNoopHapticsProvider(initialEnabled?: boolean): HapticsProvider;
44
- /**
45
- * React context carrying the active `HapticsProvider`. Defaults to
46
- * `NoopHapticsProvider`.
47
- */
48
- export declare const HapticsContext: import("react").Context<HapticsProvider>;
49
- /** Hook returning the current `HapticsProvider`. */
50
- export declare function useHaptics(): HapticsProvider;
51
- /**
52
- * Structural shape of the `expo-haptics` module surface this adapter needs — the
53
- * `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared
54
- * here so the package never depends on `expo-haptics`; the app injects the real
55
- * module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).
56
- *
57
- * Style / type params are `string` because expo types them as its own string
58
- * enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal
59
- * this package's `HapticImpactStyle` / `HapticNotificationType` literals
60
- * (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through
61
- * unchanged and the real `Haptics` module stays structurally assignable.
62
- */
63
- interface ExpoHapticsModule {
64
- impactAsync(style?: string): Promise<void>;
65
- notificationAsync(type?: string): Promise<void>;
66
- selectionAsync(): Promise<void>;
67
- /**
68
- * Android system haptic feedback. Optional, so a module without it still fits.
69
- * `type` is `string` for the same reason as above: expo types it as its
70
- * `AndroidHaptics` string enum, and method syntax keeps the real module
71
- * structurally assignable. The adapter passes that enum's values
72
- * (`'clock-tick'`, `'confirm'`, …).
73
- */
74
- performAndroidHapticsAsync?(type: string): Promise<void>;
75
- }
76
- /** Options for {@link createExpoHapticsProvider}. */
77
- interface ExpoHapticsOptions {
78
- /**
79
- * The running platform — pass `Platform.OS` from `react-native`. With
80
- * `'android'`, cues use the system haptic effects when the injected module has
81
- * `performAndroidHapticsAsync`. The hint is needed because expo's function
82
- * resolves without doing anything on other platforms, so its presence alone
83
- * cannot pick the route.
84
- */
85
- platform?: string;
86
- }
87
- /**
88
- * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
89
- * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
90
- * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an
91
- * omitted style / type forwards `undefined`, letting expo apply its own default.
92
- * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
93
- * `@flare-engine/a11y` reduced-motion signal, or a user setting).
94
- *
95
- * With `options.platform === 'android'` and a module that has
96
- * `performAndroidHapticsAsync`, cues play Android system haptic effects instead:
97
- * `selection` → `'clock-tick'`; `impact` `light` / `soft` → `'keyboard-tap'`,
98
- * `medium` (and omitted) → `'virtual-key'`, `heavy` / `rigid` → `'long-press'`;
99
- * `notification` `success` (and omitted) → `'confirm'`, `warning` →
100
- * `'long-press'`, `error` → `'reject'`. If the module rejects an effect
101
- * (`'confirm'` / `'reject'` need Android 11), that cue falls back to the expo
102
- * call above, and the provider stops trying that effect.
103
- *
104
- * @example
105
- * import * as Haptics from 'expo-haptics';
106
- * import { Platform } from 'react-native';
107
- * const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });
108
- */
109
- export declare function createExpoHapticsProvider(expo: ExpoHapticsModule, options?: ExpoHapticsOptions): HapticsProvider;
110
- /** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */
111
- type WebVibrate = (pattern: number | number[]) => boolean;
112
- /**
113
- * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
114
- * `navigator.vibrate` bound to `navigator` (e.g.
115
- * `navigator.vibrate?.bind(navigator)`).
116
- *
117
- * When `vibrate` is absent — the API is unsupported on iOS Safari and most
118
- * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
119
- * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
120
- * and the provider never claims to be enabled when there is no backend). When
121
- * present, starts enabled; gate via `setEnabled`.
122
- *
123
- * Cue durations: impact scales with intensity, notification uses a distinct pulse
124
- * pattern per category, selection is a single short tick.
125
- */
126
- export declare function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider;
127
- export type { ExpoHapticsModule, ExpoHapticsOptions, HapticImpactStyle, HapticNotificationType, HapticsProvider, WebVibrate };
128
- //# sourceMappingURL=index.d.ts.map
1
+ import { HapticsContext, useHaptics } from "./context.js";
2
+ import { ExpoHapticsModule, ExpoHapticsOptions, createExpoHapticsProvider } from "./expo.js";
3
+ import { HapticImpactStyle, HapticNotificationType, HapticsProvider, NoopHapticsProvider, createNoopHapticsProvider } from "./provider.js";
4
+ import { WebVibrate, createWebVibrationProvider } from "./web.js";
5
+ export { type ExpoHapticsModule, type ExpoHapticsOptions, type HapticImpactStyle, type HapticNotificationType, HapticsContext, type HapticsProvider, NoopHapticsProvider, type WebVibrate, createExpoHapticsProvider, createNoopHapticsProvider, createWebVibrationProvider, useHaptics };
package/dist/index.js CHANGED
@@ -1,206 +1,11 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- let react = require("react");
3
- // src/provider.ts
4
- /**
5
- * Wrap cue implementations with a real enabled gate. When disabled, cue methods
6
- * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the
7
- * live state (held in this closure, so each call is an independent instance) so a
8
- * consumer's haptics toggle is observable — and unit-testable. Not part of the
9
- * public barrel; the shipped providers (expo / web / noop factory) build on it.
10
- */
11
- function createGatedProvider(cues, initialEnabled) {
12
- let enabled = initialEnabled;
13
- return {
14
- async impact(style) {
15
- if (enabled) await cues.impact(style);
16
- },
17
- async notification(type) {
18
- if (enabled) await cues.notification(type);
19
- },
20
- async selection() {
21
- if (enabled) await cues.selection();
22
- },
23
- isEnabled() {
24
- return enabled;
25
- },
26
- setEnabled(next) {
27
- enabled = next;
28
- }
29
- };
30
- }
31
- /** Stateless no-op cues, shared by both no-op providers below. */
32
- const NOOP_CUES = {
33
- async impact() {},
34
- async notification() {},
35
- async selection() {}
36
- };
37
- /**
38
- * Immutable no-op provider. Reports disabled and fires nothing — the safe default
39
- * where there is no haptics backend (web without vibration, CI, tests) and the
40
- * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
41
- * hard "off" default, so wiring it into a context can never accidentally report
42
- * "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
43
- * consumer's haptics setting), use {@link createNoopHapticsProvider}.
44
- */
45
- const NoopHapticsProvider = {
46
- ...NOOP_CUES,
47
- isEnabled() {
48
- return false;
49
- },
50
- setEnabled() {}
51
- };
52
- /**
53
- * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
54
- * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
55
- * without a native backend. Each call is an independent instance; defaults to
56
- * disabled (matching {@link NoopHapticsProvider}).
57
- */
58
- function createNoopHapticsProvider(initialEnabled = false) {
59
- return createGatedProvider(NOOP_CUES, initialEnabled);
60
- }
61
- // src/context.ts
62
- /**
63
- * React context carrying the active `HapticsProvider`. Defaults to
64
- * `NoopHapticsProvider`.
65
- */
66
- const HapticsContext = (0, react.createContext)(NoopHapticsProvider);
67
- /** Hook returning the current `HapticsProvider`. */
68
- function useHaptics() {
69
- return (0, react.useContext)(HapticsContext);
70
- }
71
- // src/expo.ts
72
- const ANDROID_SELECTION = "clock-tick";
73
- const ANDROID_IMPACT = {
74
- light: "keyboard-tap",
75
- soft: "keyboard-tap",
76
- medium: "virtual-key",
77
- heavy: "long-press",
78
- rigid: "long-press"
79
- };
80
- const ANDROID_NOTIFICATION = {
81
- success: "confirm",
82
- warning: "long-press",
83
- error: "reject"
84
- };
85
- /**
86
- * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
87
- * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
88
- * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an
89
- * omitted style / type forwards `undefined`, letting expo apply its own default.
90
- * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
91
- * `@flare-engine/a11y` reduced-motion signal, or a user setting).
92
- *
93
- * With `options.platform === 'android'` and a module that has
94
- * `performAndroidHapticsAsync`, cues play Android system haptic effects instead:
95
- * `selection` → `'clock-tick'`; `impact` `light` / `soft` → `'keyboard-tap'`,
96
- * `medium` (and omitted) → `'virtual-key'`, `heavy` / `rigid` → `'long-press'`;
97
- * `notification` `success` (and omitted) → `'confirm'`, `warning` →
98
- * `'long-press'`, `error` → `'reject'`. If the module rejects an effect
99
- * (`'confirm'` / `'reject'` need Android 11), that cue falls back to the expo
100
- * call above, and the provider stops trying that effect.
101
- *
102
- * @example
103
- * import * as Haptics from 'expo-haptics';
104
- * import { Platform } from 'react-native';
105
- * const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });
106
- */
107
- function createExpoHapticsProvider(expo, options) {
108
- if (options?.platform !== "android" || typeof expo.performAndroidHapticsAsync !== "function") return createGatedProvider({
109
- impact: (style) => expo.impactAsync(style),
110
- notification: (type) => expo.notificationAsync(type),
111
- selection: () => expo.selectionAsync()
112
- }, true);
113
- const perform = expo.performAndroidHapticsAsync.bind(expo);
114
- const unsupported = /* @__PURE__ */ new Set();
115
- const playSystemEffect = async (type) => {
116
- if (unsupported.has(type)) return false;
117
- try {
118
- await perform(type);
119
- return true;
120
- } catch {
121
- unsupported.add(type);
122
- return false;
123
- }
124
- };
125
- return createGatedProvider({
126
- async impact(style) {
127
- if (!await playSystemEffect(ANDROID_IMPACT[style ?? "medium"])) await expo.impactAsync(style);
128
- },
129
- async notification(type) {
130
- if (!await playSystemEffect(ANDROID_NOTIFICATION[type ?? "success"])) await expo.notificationAsync(type);
131
- },
132
- async selection() {
133
- if (!await playSystemEffect(ANDROID_SELECTION)) await expo.selectionAsync();
134
- }
135
- }, true);
136
- }
137
- // src/web.ts
138
- /** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
139
- const IMPACT_MS = {
140
- light: 10,
141
- medium: 20,
142
- heavy: 40,
143
- soft: 15,
144
- rigid: 30
145
- };
146
- /** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */
147
- const NOTIFICATION_PATTERN = {
148
- success: [
149
- 10,
150
- 40,
151
- 20
152
- ],
153
- warning: [
154
- 20,
155
- 60,
156
- 20
157
- ],
158
- error: [
159
- 30,
160
- 40,
161
- 30,
162
- 40,
163
- 30
164
- ]
165
- };
166
- /** Selection tick duration (ms) — a single light buzz. */
167
- const SELECTION_MS = 5;
168
- const DEFAULT_IMPACT = "medium";
169
- const DEFAULT_NOTIFICATION = "success";
170
- /**
171
- * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
172
- * `navigator.vibrate` bound to `navigator` (e.g.
173
- * `navigator.vibrate?.bind(navigator)`).
174
- *
175
- * When `vibrate` is absent — the API is unsupported on iOS Safari and most
176
- * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
177
- * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
178
- * and the provider never claims to be enabled when there is no backend). When
179
- * present, starts enabled; gate via `setEnabled`.
180
- *
181
- * Cue durations: impact scales with intensity, notification uses a distinct pulse
182
- * pattern per category, selection is a single short tick.
183
- */
184
- function createWebVibrationProvider(vibrate) {
185
- if (typeof vibrate !== "function") return NoopHapticsProvider;
186
- const buzz = vibrate;
187
- return createGatedProvider({
188
- async impact(style) {
189
- buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);
190
- },
191
- async notification(type) {
192
- buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);
193
- },
194
- async selection() {
195
- buzz(SELECTION_MS);
196
- }
197
- }, true);
198
- }
199
- exports.HapticsContext = HapticsContext;
200
- exports.NoopHapticsProvider = NoopHapticsProvider;
201
- exports.createExpoHapticsProvider = createExpoHapticsProvider;
202
- exports.createNoopHapticsProvider = createNoopHapticsProvider;
203
- exports.createWebVibrationProvider = createWebVibrationProvider;
204
- exports.useHaptics = useHaptics;
205
-
206
- //# sourceMappingURL=index.js.map
2
+ const require_context = require("./context.js");
3
+ const require_expo = require("./expo.js");
4
+ const require_provider = require("./provider.js");
5
+ const require_web = require("./web.js");
6
+ exports.HapticsContext = require_context.HapticsContext;
7
+ exports.NoopHapticsProvider = require_provider.NoopHapticsProvider;
8
+ exports.createExpoHapticsProvider = require_expo.createExpoHapticsProvider;
9
+ exports.createNoopHapticsProvider = require_provider.createNoopHapticsProvider;
10
+ exports.createWebVibrationProvider = require_web.createWebVibrationProvider;
11
+ exports.useHaptics = require_context.useHaptics;
package/dist/index.mjs CHANGED
@@ -1,200 +1,5 @@
1
- import { createContext, useContext } from "react";
2
- // src/provider.ts
3
- /**
4
- * Wrap cue implementations with a real enabled gate. When disabled, cue methods
5
- * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the
6
- * live state (held in this closure, so each call is an independent instance) so a
7
- * consumer's haptics toggle is observable — and unit-testable. Not part of the
8
- * public barrel; the shipped providers (expo / web / noop factory) build on it.
9
- */
10
- function createGatedProvider(cues, initialEnabled) {
11
- let enabled = initialEnabled;
12
- return {
13
- async impact(style) {
14
- if (enabled) await cues.impact(style);
15
- },
16
- async notification(type) {
17
- if (enabled) await cues.notification(type);
18
- },
19
- async selection() {
20
- if (enabled) await cues.selection();
21
- },
22
- isEnabled() {
23
- return enabled;
24
- },
25
- setEnabled(next) {
26
- enabled = next;
27
- }
28
- };
29
- }
30
- /** Stateless no-op cues, shared by both no-op providers below. */
31
- const NOOP_CUES = {
32
- async impact() {},
33
- async notification() {},
34
- async selection() {}
35
- };
36
- /**
37
- * Immutable no-op provider. Reports disabled and fires nothing — the safe default
38
- * where there is no haptics backend (web without vibration, CI, tests) and the
39
- * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
40
- * hard "off" default, so wiring it into a context can never accidentally report
41
- * "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
42
- * consumer's haptics setting), use {@link createNoopHapticsProvider}.
43
- */
44
- const NoopHapticsProvider = {
45
- ...NOOP_CUES,
46
- isEnabled() {
47
- return false;
48
- },
49
- setEnabled() {}
50
- };
51
- /**
52
- * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
53
- * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
54
- * without a native backend. Each call is an independent instance; defaults to
55
- * disabled (matching {@link NoopHapticsProvider}).
56
- */
57
- function createNoopHapticsProvider(initialEnabled = false) {
58
- return createGatedProvider(NOOP_CUES, initialEnabled);
59
- }
60
- // src/context.ts
61
- /**
62
- * React context carrying the active `HapticsProvider`. Defaults to
63
- * `NoopHapticsProvider`.
64
- */
65
- const HapticsContext = createContext(NoopHapticsProvider);
66
- /** Hook returning the current `HapticsProvider`. */
67
- function useHaptics() {
68
- return useContext(HapticsContext);
69
- }
70
- // src/expo.ts
71
- const ANDROID_SELECTION = "clock-tick";
72
- const ANDROID_IMPACT = {
73
- light: "keyboard-tap",
74
- soft: "keyboard-tap",
75
- medium: "virtual-key",
76
- heavy: "long-press",
77
- rigid: "long-press"
78
- };
79
- const ANDROID_NOTIFICATION = {
80
- success: "confirm",
81
- warning: "long-press",
82
- error: "reject"
83
- };
84
- /**
85
- * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
86
- * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
87
- * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an
88
- * omitted style / type forwards `undefined`, letting expo apply its own default.
89
- * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
90
- * `@flare-engine/a11y` reduced-motion signal, or a user setting).
91
- *
92
- * With `options.platform === 'android'` and a module that has
93
- * `performAndroidHapticsAsync`, cues play Android system haptic effects instead:
94
- * `selection` → `'clock-tick'`; `impact` `light` / `soft` → `'keyboard-tap'`,
95
- * `medium` (and omitted) → `'virtual-key'`, `heavy` / `rigid` → `'long-press'`;
96
- * `notification` `success` (and omitted) → `'confirm'`, `warning` →
97
- * `'long-press'`, `error` → `'reject'`. If the module rejects an effect
98
- * (`'confirm'` / `'reject'` need Android 11), that cue falls back to the expo
99
- * call above, and the provider stops trying that effect.
100
- *
101
- * @example
102
- * import * as Haptics from 'expo-haptics';
103
- * import { Platform } from 'react-native';
104
- * const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });
105
- */
106
- function createExpoHapticsProvider(expo, options) {
107
- if (options?.platform !== "android" || typeof expo.performAndroidHapticsAsync !== "function") return createGatedProvider({
108
- impact: (style) => expo.impactAsync(style),
109
- notification: (type) => expo.notificationAsync(type),
110
- selection: () => expo.selectionAsync()
111
- }, true);
112
- const perform = expo.performAndroidHapticsAsync.bind(expo);
113
- const unsupported = /* @__PURE__ */ new Set();
114
- const playSystemEffect = async (type) => {
115
- if (unsupported.has(type)) return false;
116
- try {
117
- await perform(type);
118
- return true;
119
- } catch {
120
- unsupported.add(type);
121
- return false;
122
- }
123
- };
124
- return createGatedProvider({
125
- async impact(style) {
126
- if (!await playSystemEffect(ANDROID_IMPACT[style ?? "medium"])) await expo.impactAsync(style);
127
- },
128
- async notification(type) {
129
- if (!await playSystemEffect(ANDROID_NOTIFICATION[type ?? "success"])) await expo.notificationAsync(type);
130
- },
131
- async selection() {
132
- if (!await playSystemEffect(ANDROID_SELECTION)) await expo.selectionAsync();
133
- }
134
- }, true);
135
- }
136
- // src/web.ts
137
- /** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
138
- const IMPACT_MS = {
139
- light: 10,
140
- medium: 20,
141
- heavy: 40,
142
- soft: 15,
143
- rigid: 30
144
- };
145
- /** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */
146
- const NOTIFICATION_PATTERN = {
147
- success: [
148
- 10,
149
- 40,
150
- 20
151
- ],
152
- warning: [
153
- 20,
154
- 60,
155
- 20
156
- ],
157
- error: [
158
- 30,
159
- 40,
160
- 30,
161
- 40,
162
- 30
163
- ]
164
- };
165
- /** Selection tick duration (ms) — a single light buzz. */
166
- const SELECTION_MS = 5;
167
- const DEFAULT_IMPACT = "medium";
168
- const DEFAULT_NOTIFICATION = "success";
169
- /**
170
- * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
171
- * `navigator.vibrate` bound to `navigator` (e.g.
172
- * `navigator.vibrate?.bind(navigator)`).
173
- *
174
- * When `vibrate` is absent — the API is unsupported on iOS Safari and most
175
- * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
176
- * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
177
- * and the provider never claims to be enabled when there is no backend). When
178
- * present, starts enabled; gate via `setEnabled`.
179
- *
180
- * Cue durations: impact scales with intensity, notification uses a distinct pulse
181
- * pattern per category, selection is a single short tick.
182
- */
183
- function createWebVibrationProvider(vibrate) {
184
- if (typeof vibrate !== "function") return NoopHapticsProvider;
185
- const buzz = vibrate;
186
- return createGatedProvider({
187
- async impact(style) {
188
- buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);
189
- },
190
- async notification(type) {
191
- buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);
192
- },
193
- async selection() {
194
- buzz(SELECTION_MS);
195
- }
196
- }, true);
197
- }
1
+ import { HapticsContext, useHaptics } from "./context.mjs";
2
+ import { createExpoHapticsProvider } from "./expo.mjs";
3
+ import { NoopHapticsProvider, createNoopHapticsProvider } from "./provider.mjs";
4
+ import { createWebVibrationProvider } from "./web.mjs";
198
5
  export { HapticsContext, NoopHapticsProvider, createExpoHapticsProvider, createNoopHapticsProvider, createWebVibrationProvider, useHaptics };
199
-
200
- //# sourceMappingURL=index.mjs.map
@@ -0,0 +1,44 @@
1
+ /** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */
2
+ export type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';
3
+ /** Notification feedback categories. */
4
+ export type HapticNotificationType = 'success' | 'warning' | 'error';
5
+ /**
6
+ * Haptics (tactile feedback) provider interface. Consumers supply a concrete
7
+ * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app
8
+ * boundary. The cue surface is deliberately thin — three semantic calls mirroring
9
+ * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an
10
+ * enabled gate, so the user's haptics setting and the reduced-motion gate
11
+ * (`@flare-engine/a11y`) live in one place instead of at every call site.
12
+ */
13
+ export interface HapticsProvider {
14
+ /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */
15
+ impact(style?: HapticImpactStyle): Promise<void>;
16
+ /** A notification cue — victory, medal earned, error / deny. */
17
+ notification(type?: HapticNotificationType): Promise<void>;
18
+ /** A light selection tick — menu focus, toggle, pickup. */
19
+ selection(): Promise<void>;
20
+ /**
21
+ * Whether cues currently fire. Reflects the user setting and the reduced-motion
22
+ * gate; when `false`, `impact` / `notification` / `selection` are no-ops.
23
+ */
24
+ isEnabled(): boolean;
25
+ /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */
26
+ setEnabled(enabled: boolean): void;
27
+ }
28
+ /**
29
+ * Immutable no-op provider. Reports disabled and fires nothing — the safe default
30
+ * where there is no haptics backend (web without vibration, CI, tests) and the
31
+ * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
32
+ * hard "off" default, so wiring it into a context can never accidentally report
33
+ * "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
34
+ * consumer's haptics setting), use {@link createNoopHapticsProvider}.
35
+ */
36
+ export declare const NoopHapticsProvider: HapticsProvider;
37
+ /**
38
+ * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
39
+ * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
40
+ * without a native backend. Each call is an independent instance; defaults to
41
+ * disabled (matching {@link NoopHapticsProvider}).
42
+ */
43
+ export declare function createNoopHapticsProvider(initialEnabled?: boolean): HapticsProvider;
44
+ //# sourceMappingURL=provider.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.mts","names":[],"sources":["../src/provider.ts"],"mappings":";YACY;;YAGA;;;;;;;;;iBAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB"}
@@ -0,0 +1,44 @@
1
+ /** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */
2
+ export type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';
3
+ /** Notification feedback categories. */
4
+ export type HapticNotificationType = 'success' | 'warning' | 'error';
5
+ /**
6
+ * Haptics (tactile feedback) provider interface. Consumers supply a concrete
7
+ * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app
8
+ * boundary. The cue surface is deliberately thin — three semantic calls mirroring
9
+ * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an
10
+ * enabled gate, so the user's haptics setting and the reduced-motion gate
11
+ * (`@flare-engine/a11y`) live in one place instead of at every call site.
12
+ */
13
+ export interface HapticsProvider {
14
+ /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */
15
+ impact(style?: HapticImpactStyle): Promise<void>;
16
+ /** A notification cue — victory, medal earned, error / deny. */
17
+ notification(type?: HapticNotificationType): Promise<void>;
18
+ /** A light selection tick — menu focus, toggle, pickup. */
19
+ selection(): Promise<void>;
20
+ /**
21
+ * Whether cues currently fire. Reflects the user setting and the reduced-motion
22
+ * gate; when `false`, `impact` / `notification` / `selection` are no-ops.
23
+ */
24
+ isEnabled(): boolean;
25
+ /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */
26
+ setEnabled(enabled: boolean): void;
27
+ }
28
+ /**
29
+ * Immutable no-op provider. Reports disabled and fires nothing — the safe default
30
+ * where there is no haptics backend (web without vibration, CI, tests) and the
31
+ * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
32
+ * hard "off" default, so wiring it into a context can never accidentally report
33
+ * "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
34
+ * consumer's haptics setting), use {@link createNoopHapticsProvider}.
35
+ */
36
+ export declare const NoopHapticsProvider: HapticsProvider;
37
+ /**
38
+ * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
39
+ * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
40
+ * without a native backend. Each call is an independent instance; defaults to
41
+ * disabled (matching {@link NoopHapticsProvider}).
42
+ */
43
+ export declare function createNoopHapticsProvider(initialEnabled?: boolean): HapticsProvider;
44
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","names":[],"sources":["../src/provider.ts"],"mappings":";YACY;;YAGA;;;;;;;;;iBAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB"}