@flare-engine/haptics 0.4.11 → 0.4.12
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 +19 -2
- package/dist/index.d.mts +32 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.d.ts +32 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +49 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +49 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ Pick a provider at the app boundary and put it on the context. The cue surface i
|
|
|
14
14
|
|
|
15
15
|
```tsx no-check
|
|
16
16
|
import * as Haptics from 'expo-haptics';
|
|
17
|
+
import { Platform } from 'react-native';
|
|
17
18
|
import {
|
|
18
19
|
createExpoHapticsProvider,
|
|
19
20
|
createWebVibrationProvider,
|
|
@@ -25,7 +26,7 @@ import {
|
|
|
25
26
|
const provider =
|
|
26
27
|
typeof navigator !== 'undefined' && 'vibrate' in navigator
|
|
27
28
|
? createWebVibrationProvider(navigator.vibrate.bind(navigator))
|
|
28
|
-
: createExpoHapticsProvider(Haptics);
|
|
29
|
+
: createExpoHapticsProvider(Haptics, { platform: Platform.OS });
|
|
29
30
|
|
|
30
31
|
export function App() {
|
|
31
32
|
return (
|
|
@@ -52,11 +53,27 @@ function FireButton() {
|
|
|
52
53
|
|
|
53
54
|
| Factory | Backend | Notes |
|
|
54
55
|
| --- | --- | --- |
|
|
55
|
-
| `createExpoHapticsProvider(expo)` | `expo-haptics` (injected) | `expo` is the structural `{ impactAsync, notificationAsync, selectionAsync }` shape; cue kinds map onto expo's string enums 1:1. Starts enabled. |
|
|
56
|
+
| `createExpoHapticsProvider(expo, options?)` | `expo-haptics` (injected) | `expo` is the structural `{ impactAsync, notificationAsync, selectionAsync, performAndroidHapticsAsync? }` shape; cue kinds map onto expo's string enums 1:1. `options.platform` (pass `Platform.OS`) turns on the Android system effects, see below. Starts enabled. |
|
|
56
57
|
| `createWebVibrationProvider(vibrate?)` | Web Vibration API | `vibrate` is `navigator.vibrate` bound to `navigator`. Impact scales the buzz with intensity; each notification category has its own pulse pattern; selection is a short tick. Absent `vibrate` → inert no-op. |
|
|
57
58
|
| `NoopHapticsProvider` | none | Immutable, always disabled — the context default and the safe fallback. `setEnabled` is inert by design. |
|
|
58
59
|
| `createNoopHapticsProvider(enabled?)` | none | No-op cues but a **real** enabled gate, so a consumer's haptics toggle is unit-testable without a native backend. Defaults to disabled. |
|
|
59
60
|
|
|
61
|
+
### Android
|
|
62
|
+
|
|
63
|
+
With `{ platform: 'android' }` and a module that has `performAndroidHapticsAsync`, cues use the system haptic effects, which follow the device's system touch-feedback setting:
|
|
64
|
+
|
|
65
|
+
| Cue | System effect |
|
|
66
|
+
| --- | --- |
|
|
67
|
+
| `selection()` | `clock-tick` |
|
|
68
|
+
| `impact('light')`, `impact('soft')` | `keyboard-tap` |
|
|
69
|
+
| `impact('medium')`, `impact()` | `virtual-key` |
|
|
70
|
+
| `impact('heavy')`, `impact('rigid')` | `long-press` |
|
|
71
|
+
| `notification('success')`, `notification()` | `confirm` |
|
|
72
|
+
| `notification('warning')` | `long-press` |
|
|
73
|
+
| `notification('error')` | `reject` |
|
|
74
|
+
|
|
75
|
+
`confirm` and `reject` need Android 11. On Android 10 and lower the module rejects them, and those cues fall back to expo's vibrator calls (`notificationAsync`); the provider then stops trying that effect. Without the hint, Android plays expo's vibrator waveforms as before.
|
|
76
|
+
|
|
60
77
|
## Other backends
|
|
61
78
|
|
|
62
79
|
Any SDK works through injection — the same pattern the Expo and Web providers use. Wrap its calls in the `impact` / `notification` / `selection` shape and hand them to the shared gate, so your adapter never re-implements the enabled logic. For a native `Vibration` (from `react-native`) or a Core Haptics bridge, mirror `createExpoHapticsProvider`: accept the module, forward the cues, start enabled.
|
package/dist/index.d.mts
CHANGED
|
@@ -64,6 +64,25 @@ interface ExpoHapticsModule {
|
|
|
64
64
|
impactAsync(style?: string): Promise<void>;
|
|
65
65
|
notificationAsync(type?: string): Promise<void>;
|
|
66
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;
|
|
67
86
|
}
|
|
68
87
|
/**
|
|
69
88
|
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
@@ -73,11 +92,21 @@ interface ExpoHapticsModule {
|
|
|
73
92
|
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
74
93
|
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
75
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
|
+
*
|
|
76
104
|
* @example
|
|
77
105
|
* import * as Haptics from 'expo-haptics';
|
|
78
|
-
*
|
|
106
|
+
* import { Platform } from 'react-native';
|
|
107
|
+
* const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });
|
|
79
108
|
*/
|
|
80
|
-
export declare function createExpoHapticsProvider(expo: ExpoHapticsModule): HapticsProvider;
|
|
109
|
+
export declare function createExpoHapticsProvider(expo: ExpoHapticsModule, options?: ExpoHapticsOptions): HapticsProvider;
|
|
81
110
|
/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */
|
|
82
111
|
type WebVibrate = (pattern: number | number[]) => boolean;
|
|
83
112
|
/**
|
|
@@ -95,5 +124,5 @@ type WebVibrate = (pattern: number | number[]) => boolean;
|
|
|
95
124
|
* pattern per category, selection is a single short tick.
|
|
96
125
|
*/
|
|
97
126
|
export declare function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider;
|
|
98
|
-
export type { ExpoHapticsModule, HapticImpactStyle, HapticNotificationType, HapticsProvider, WebVibrate };
|
|
127
|
+
export type { ExpoHapticsModule, ExpoHapticsOptions, HapticImpactStyle, HapticNotificationType, HapticsProvider, WebVibrate };
|
|
99
128
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"mappings":";KACY;;KAGA;;;;;;;;;UAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB;;;;;qBCnGtD,gCAAc,QAAA;;wBAGX,cAAc;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"mappings":";KACY;;KAGA;;;;;;;;;UAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB;;;;;qBCnGtD,gCAAc,QAAA;;wBAGX,cAAc;;;;;;;;;;;;;UCSb;EACf,YAAY,iBAAiB;EAC7B,kBAAkB,gBAAgB;EAClC,kBAAkB;;;;;;;;EAQlB,4BAA4B,eAAe;;;UAI5B;;;;;;;;EAQf;;;;;;;;;;;;;;;;;;;;;;;;wBAyCc,0BACd,MAAM,mBACN,UAAU,qBACT;;KC7ES,cAAc;;;;;;;;;;;;;;;wBAsCV,2BAA2B,UAAU,aAAa"}
|
package/dist/index.d.ts
CHANGED
|
@@ -64,6 +64,25 @@ interface ExpoHapticsModule {
|
|
|
64
64
|
impactAsync(style?: string): Promise<void>;
|
|
65
65
|
notificationAsync(type?: string): Promise<void>;
|
|
66
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;
|
|
67
86
|
}
|
|
68
87
|
/**
|
|
69
88
|
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
@@ -73,11 +92,21 @@ interface ExpoHapticsModule {
|
|
|
73
92
|
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
74
93
|
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
75
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
|
+
*
|
|
76
104
|
* @example
|
|
77
105
|
* import * as Haptics from 'expo-haptics';
|
|
78
|
-
*
|
|
106
|
+
* import { Platform } from 'react-native';
|
|
107
|
+
* const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });
|
|
79
108
|
*/
|
|
80
|
-
export declare function createExpoHapticsProvider(expo: ExpoHapticsModule): HapticsProvider;
|
|
109
|
+
export declare function createExpoHapticsProvider(expo: ExpoHapticsModule, options?: ExpoHapticsOptions): HapticsProvider;
|
|
81
110
|
/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */
|
|
82
111
|
type WebVibrate = (pattern: number | number[]) => boolean;
|
|
83
112
|
/**
|
|
@@ -95,5 +124,5 @@ type WebVibrate = (pattern: number | number[]) => boolean;
|
|
|
95
124
|
* pattern per category, selection is a single short tick.
|
|
96
125
|
*/
|
|
97
126
|
export declare function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider;
|
|
98
|
-
export type { ExpoHapticsModule, HapticImpactStyle, HapticNotificationType, HapticsProvider, WebVibrate };
|
|
127
|
+
export type { ExpoHapticsModule, ExpoHapticsOptions, HapticImpactStyle, HapticNotificationType, HapticsProvider, WebVibrate };
|
|
99
128
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"mappings":";KACY;;KAGA;;;;;;;;;UAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB;;;;;qBCnGtD,gCAAc,QAAA;;wBAGX,cAAc;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"mappings":";KACY;;KAGA;;;;;;;;;UAUK;;EAEf,OAAO,QAAQ,oBAAoB;;EAEnC,aAAa,OAAO,yBAAyB;;EAE7C,aAAa;;;;;EAKb;;EAEA,WAAW;;;;;;;;;;qBA+DA,qBAAqB;;;;;;;wBAgBlB,0BAA0B,2BAAyB;;;;;qBCnGtD,gCAAc,QAAA;;wBAGX,cAAc;;;;;;;;;;;;;UCSb;EACf,YAAY,iBAAiB;EAC7B,kBAAkB,gBAAgB;EAClC,kBAAkB;;;;;;;;EAQlB,4BAA4B,eAAe;;;UAI5B;;;;;;;;EAQf;;;;;;;;;;;;;;;;;;;;;;;;wBAyCc,0BACd,MAAM,mBACN,UAAU,qBACT;;KC7ES,cAAc;;;;;;;;;;;;;;;wBAsCV,2BAA2B,UAAU,aAAa"}
|
package/dist/index.js
CHANGED
|
@@ -69,6 +69,19 @@ function useHaptics() {
|
|
|
69
69
|
return (0, react.useContext)(HapticsContext);
|
|
70
70
|
}
|
|
71
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
|
+
};
|
|
72
85
|
/**
|
|
73
86
|
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
74
87
|
* Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
|
|
@@ -77,16 +90,49 @@ function useHaptics() {
|
|
|
77
90
|
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
78
91
|
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
79
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
|
+
*
|
|
80
102
|
* @example
|
|
81
103
|
* import * as Haptics from 'expo-haptics';
|
|
82
|
-
*
|
|
104
|
+
* import { Platform } from 'react-native';
|
|
105
|
+
* const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });
|
|
83
106
|
*/
|
|
84
|
-
function createExpoHapticsProvider(expo) {
|
|
85
|
-
return createGatedProvider({
|
|
107
|
+
function createExpoHapticsProvider(expo, options) {
|
|
108
|
+
if (options?.platform !== "android" || typeof expo.performAndroidHapticsAsync !== "function") return createGatedProvider({
|
|
86
109
|
impact: (style) => expo.impactAsync(style),
|
|
87
110
|
notification: (type) => expo.notificationAsync(type),
|
|
88
111
|
selection: () => expo.selectionAsync()
|
|
89
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);
|
|
90
136
|
}
|
|
91
137
|
// src/web.ts
|
|
92
138
|
/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["createContext","useContext"],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n","import { createContext, useContext } from 'react';\nimport { type HapticsProvider, NoopHapticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `HapticsProvider`. Defaults to\n * `NoopHapticsProvider`.\n */\nexport const HapticsContext = createContext<HapticsProvider>(NoopHapticsProvider);\n\n/** Hook returning the current `HapticsProvider`. */\nexport function useHaptics(): HapticsProvider {\n return useContext(HapticsContext);\n}\n","import { createGatedProvider, type HapticsProvider } from './provider.js';\n\n/**\n * Structural shape of the `expo-haptics` module surface this adapter needs — the\n * `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared\n * here so the package never depends on `expo-haptics`; the app injects the real\n * module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).\n *\n * Style / type params are `string` because expo types them as its own string\n * enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal\n * this package's `HapticImpactStyle` / `HapticNotificationType` literals\n * (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through\n * unchanged and the real `Haptics` module stays structurally assignable.\n */\nexport interface ExpoHapticsModule {\n impactAsync(style?: string): Promise<void>;\n notificationAsync(type?: string): Promise<void>;\n selectionAsync(): Promise<void>;\n}\n\n/**\n * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.\n * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,\n * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an\n * omitted style / type forwards `undefined`, letting expo apply its own default.\n * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the\n * `@flare-engine/a11y` reduced-motion signal, or a user setting).\n *\n * @example\n * import * as Haptics from 'expo-haptics';\n * const haptics = createExpoHapticsProvider(Haptics);\n */\nexport function createExpoHapticsProvider(expo: ExpoHapticsModule): HapticsProvider {\n return createGatedProvider(\n {\n impact: (style) => expo.impactAsync(style),\n notification: (type) => expo.notificationAsync(type),\n selection: () => expo.selectionAsync(),\n },\n true,\n );\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":";;;;;;;;;;AAgDA,SAAgB,oBAAoB,MAAkB,gBAA0C;CAC9F,IAAI,UAAU;CACd,OAAO;EACL,MAAM,OAAO,OAA0C;GACrD,IAAI,SAAS,MAAM,KAAK,OAAO,KAAK;EACtC;EACA,MAAM,aAAa,MAA8C;GAC/D,IAAI,SAAS,MAAM,KAAK,aAAa,IAAI;EAC3C;EACA,MAAM,YAA2B;GAC/B,IAAI,SAAS,MAAM,KAAK,UAAU;EACpC;EACA,YAAqB;GACnB,OAAO;EACT;EACA,WAAW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ACA,IACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["createContext","useContext"],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n","import { createContext, useContext } from 'react';\nimport { type HapticsProvider, NoopHapticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `HapticsProvider`. Defaults to\n * `NoopHapticsProvider`.\n */\nexport const HapticsContext = createContext<HapticsProvider>(NoopHapticsProvider);\n\n/** Hook returning the current `HapticsProvider`. */\nexport function useHaptics(): HapticsProvider {\n return useContext(HapticsContext);\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n} from './provider.js';\n\n/**\n * Structural shape of the `expo-haptics` module surface this adapter needs — the\n * `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared\n * here so the package never depends on `expo-haptics`; the app injects the real\n * module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).\n *\n * Style / type params are `string` because expo types them as its own string\n * enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal\n * this package's `HapticImpactStyle` / `HapticNotificationType` literals\n * (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through\n * unchanged and the real `Haptics` module stays structurally assignable.\n */\nexport interface ExpoHapticsModule {\n impactAsync(style?: string): Promise<void>;\n notificationAsync(type?: string): Promise<void>;\n selectionAsync(): Promise<void>;\n /**\n * Android system haptic feedback. Optional, so a module without it still fits.\n * `type` is `string` for the same reason as above: expo types it as its\n * `AndroidHaptics` string enum, and method syntax keeps the real module\n * structurally assignable. The adapter passes that enum's values\n * (`'clock-tick'`, `'confirm'`, …).\n */\n performAndroidHapticsAsync?(type: string): Promise<void>;\n}\n\n/** Options for {@link createExpoHapticsProvider}. */\nexport interface ExpoHapticsOptions {\n /**\n * The running platform — pass `Platform.OS` from `react-native`. With\n * `'android'`, cues use the system haptic effects when the injected module has\n * `performAndroidHapticsAsync`. The hint is needed because expo's function\n * resolves without doing anything on other platforms, so its presence alone\n * cannot pick the route.\n */\n platform?: string;\n}\n\nconst ANDROID_SELECTION = 'clock-tick';\n\nconst ANDROID_IMPACT: Readonly<Record<HapticImpactStyle, string>> = {\n light: 'keyboard-tap',\n soft: 'keyboard-tap',\n medium: 'virtual-key',\n heavy: 'long-press',\n rigid: 'long-press',\n};\n\nconst ANDROID_NOTIFICATION: Readonly<Record<HapticNotificationType, string>> = {\n success: 'confirm',\n warning: 'long-press',\n error: 'reject',\n};\n\n/**\n * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.\n * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,\n * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an\n * omitted style / type forwards `undefined`, letting expo apply its own default.\n * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the\n * `@flare-engine/a11y` reduced-motion signal, or a user setting).\n *\n * With `options.platform === 'android'` and a module that has\n * `performAndroidHapticsAsync`, cues play Android system haptic effects instead:\n * `selection` → `'clock-tick'`; `impact` `light` / `soft` → `'keyboard-tap'`,\n * `medium` (and omitted) → `'virtual-key'`, `heavy` / `rigid` → `'long-press'`;\n * `notification` `success` (and omitted) → `'confirm'`, `warning` →\n * `'long-press'`, `error` → `'reject'`. If the module rejects an effect\n * (`'confirm'` / `'reject'` need Android 11), that cue falls back to the expo\n * call above, and the provider stops trying that effect.\n *\n * @example\n * import * as Haptics from 'expo-haptics';\n * import { Platform } from 'react-native';\n * const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });\n */\nexport function createExpoHapticsProvider(\n expo: ExpoHapticsModule,\n options?: ExpoHapticsOptions,\n): HapticsProvider {\n if (options?.platform !== 'android' || typeof expo.performAndroidHapticsAsync !== 'function') {\n return createGatedProvider(\n {\n impact: (style) => expo.impactAsync(style),\n notification: (type) => expo.notificationAsync(type),\n selection: () => expo.selectionAsync(),\n },\n true,\n );\n }\n\n const perform = expo.performAndroidHapticsAsync.bind(expo);\n const unsupported = new Set<string>();\n\n // Resolves `true` when the system effect played, `false` when the caller must\n // fall back. Any rejection marks the type: the module's error class is not\n // visible from here, and a type that failed once is not worth a retry per cue.\n const playSystemEffect = async (type: string): Promise<boolean> => {\n if (unsupported.has(type)) return false;\n try {\n await perform(type);\n return true;\n } catch {\n unsupported.add(type);\n return false;\n }\n };\n\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n if (!(await playSystemEffect(ANDROID_IMPACT[style ?? 'medium']))) {\n await expo.impactAsync(style);\n }\n },\n async notification(type): Promise<void> {\n if (!(await playSystemEffect(ANDROID_NOTIFICATION[type ?? 'success']))) {\n await expo.notificationAsync(type);\n }\n },\n async selection(): Promise<void> {\n if (!(await playSystemEffect(ANDROID_SELECTION))) await expo.selectionAsync();\n },\n },\n true,\n );\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":";;;;;;;;;;AAgDA,SAAgB,oBAAoB,MAAkB,gBAA0C;CAC9F,IAAI,UAAU;CACd,OAAO;EACL,MAAM,OAAO,OAA0C;GACrD,IAAI,SAAS,MAAM,KAAK,OAAO,KAAK;EACtC;EACA,MAAM,aAAa,MAA8C;GAC/D,IAAI,SAAS,MAAM,KAAK,aAAa,IAAI;EAC3C;EACA,MAAM,YAA2B;GAC/B,IAAI,SAAS,MAAM,KAAK,UAAU;EACpC;EACA,YAAqB;GACnB,OAAO;EACT;EACA,WAAW,MAAqB;GAC9B,UAAU;EACZ;CACF;AACF;;AAGA,MAAM,YAAwB;CAC5B,MAAM,SAAwB,CAE9B;CACA,MAAM,eAA8B,CAEpC;CACA,MAAM,YAA2B,CAEjC;AACF;;;;;;;;;AAUA,MAAa,sBAAuC;CAClD,GAAG;CACH,YAAqB;EACnB,OAAO;CACT;CACA,aAAmB,CAEnB;AACF;;;;;;;AAQA,SAAgB,0BAA0B,iBAAiB,OAAwB;CACjF,OAAO,oBAAoB,WAAW,cAAc;AACtD;;;;;;ACrGA,MAAa,kBAAA,GAAiBA,MAAAA,cAAAA,CAA+B,mBAAmB;;AAGhF,SAAgB,aAA8B;CAC5C,QAAA,GAAOC,MAAAA,WAAAA,CAAW,cAAc;AAClC;;ACiCA,MAAM,oBAAoB;AAE1B,MAAM,iBAA8D;CAClE,OAAO;CACP,MAAM;CACN,QAAQ;CACR,OAAO;CACP,OAAO;AACT;AAEA,MAAM,uBAAyE;CAC7E,SAAS;CACT,SAAS;CACT,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,0BACd,MACA,SACiB;CACjB,IAAI,SAAS,aAAa,aAAa,OAAO,KA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C;EACA,MAAM,aAAa,MAAqB;GACtC,KAAK,qBAAqB,QAAQ,qBAAqB;EACzD;EACA,MAAM,YAA2B;GAC/B,KAAK,YAAY;EACnB;CACF,GACA,IACF;AACF"}
|
package/dist/index.mjs
CHANGED
|
@@ -68,6 +68,19 @@ function useHaptics() {
|
|
|
68
68
|
return useContext(HapticsContext);
|
|
69
69
|
}
|
|
70
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
|
+
};
|
|
71
84
|
/**
|
|
72
85
|
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
73
86
|
* Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
|
|
@@ -76,16 +89,49 @@ function useHaptics() {
|
|
|
76
89
|
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
77
90
|
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
78
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
|
+
*
|
|
79
101
|
* @example
|
|
80
102
|
* import * as Haptics from 'expo-haptics';
|
|
81
|
-
*
|
|
103
|
+
* import { Platform } from 'react-native';
|
|
104
|
+
* const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });
|
|
82
105
|
*/
|
|
83
|
-
function createExpoHapticsProvider(expo) {
|
|
84
|
-
return createGatedProvider({
|
|
106
|
+
function createExpoHapticsProvider(expo, options) {
|
|
107
|
+
if (options?.platform !== "android" || typeof expo.performAndroidHapticsAsync !== "function") return createGatedProvider({
|
|
85
108
|
impact: (style) => expo.impactAsync(style),
|
|
86
109
|
notification: (type) => expo.notificationAsync(type),
|
|
87
110
|
selection: () => expo.selectionAsync()
|
|
88
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);
|
|
89
135
|
}
|
|
90
136
|
// src/web.ts
|
|
91
137
|
/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n","import { createContext, useContext } from 'react';\nimport { type HapticsProvider, NoopHapticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `HapticsProvider`. Defaults to\n * `NoopHapticsProvider`.\n */\nexport const HapticsContext = createContext<HapticsProvider>(NoopHapticsProvider);\n\n/** Hook returning the current `HapticsProvider`. */\nexport function useHaptics(): HapticsProvider {\n return useContext(HapticsContext);\n}\n","import { createGatedProvider, type HapticsProvider } from './provider.js';\n\n/**\n * Structural shape of the `expo-haptics` module surface this adapter needs — the\n * `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared\n * here so the package never depends on `expo-haptics`; the app injects the real\n * module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).\n *\n * Style / type params are `string` because expo types them as its own string\n * enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal\n * this package's `HapticImpactStyle` / `HapticNotificationType` literals\n * (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through\n * unchanged and the real `Haptics` module stays structurally assignable.\n */\nexport interface ExpoHapticsModule {\n impactAsync(style?: string): Promise<void>;\n notificationAsync(type?: string): Promise<void>;\n selectionAsync(): Promise<void>;\n}\n\n/**\n * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.\n * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,\n * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an\n * omitted style / type forwards `undefined`, letting expo apply its own default.\n * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the\n * `@flare-engine/a11y` reduced-motion signal, or a user setting).\n *\n * @example\n * import * as Haptics from 'expo-haptics';\n * const haptics = createExpoHapticsProvider(Haptics);\n */\nexport function createExpoHapticsProvider(expo: ExpoHapticsModule): HapticsProvider {\n return createGatedProvider(\n {\n impact: (style) => expo.impactAsync(style),\n notification: (type) => expo.notificationAsync(type),\n selection: () => expo.selectionAsync(),\n },\n true,\n );\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":";;;;;;;;;AAgDA,SAAgB,oBAAoB,MAAkB,gBAA0C;CAC9F,IAAI,UAAU;CACd,OAAO;EACL,MAAM,OAAO,OAA0C;GACrD,IAAI,SAAS,MAAM,KAAK,OAAO,KAAK;EACtC;EACA,MAAM,aAAa,MAA8C;GAC/D,IAAI,SAAS,MAAM,KAAK,aAAa,IAAI;EAC3C;EACA,MAAM,YAA2B;GAC/B,IAAI,SA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AAK,YAAY;EACnB;CACF,GACA,IACF;AACF"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/provider.ts","../src/context.ts","../src/expo.ts","../src/web.ts"],"sourcesContent":["/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */\nexport type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';\n\n/** Notification feedback categories. */\nexport type HapticNotificationType = 'success' | 'warning' | 'error';\n\n/**\n * Haptics (tactile feedback) provider interface. Consumers supply a concrete\n * adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app\n * boundary. The cue surface is deliberately thin — three semantic calls mirroring\n * the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an\n * enabled gate, so the user's haptics setting and the reduced-motion gate\n * (`@flare-engine/a11y`) live in one place instead of at every call site.\n */\nexport interface HapticsProvider {\n /** A physical impact cue — weapon fire, projectile hit, bomb detonation. */\n impact(style?: HapticImpactStyle): Promise<void>;\n /** A notification cue — victory, medal earned, error / deny. */\n notification(type?: HapticNotificationType): Promise<void>;\n /** A light selection tick — menu focus, toggle, pickup. */\n selection(): Promise<void>;\n /**\n * Whether cues currently fire. Reflects the user setting and the reduced-motion\n * gate; when `false`, `impact` / `notification` / `selection` are no-ops.\n */\n isEnabled(): boolean;\n /** Enable or disable all feedback. When disabled, cue methods resolve without firing. */\n setEnabled(enabled: boolean): void;\n}\n\n/**\n * The three cue implementations a concrete backend supplies. {@link createGatedProvider}\n * wraps them with the shared enabled gate so every provider honours the enabled\n * setting identically, and a backend never has to re-implement the gate.\n */\nexport interface HapticCues {\n impact(style?: HapticImpactStyle): Promise<void>;\n notification(type?: HapticNotificationType): Promise<void>;\n selection(): Promise<void>;\n}\n\n/**\n * Wrap cue implementations with a real enabled gate. When disabled, cue methods\n * resolve without invoking the backend; `setEnabled` / `isEnabled` reflect the\n * live state (held in this closure, so each call is an independent instance) so a\n * consumer's haptics toggle is observable — and unit-testable. Not part of the\n * public barrel; the shipped providers (expo / web / noop factory) build on it.\n */\nexport function createGatedProvider(cues: HapticCues, initialEnabled: boolean): HapticsProvider {\n let enabled = initialEnabled;\n return {\n async impact(style?: HapticImpactStyle): Promise<void> {\n if (enabled) await cues.impact(style);\n },\n async notification(type?: HapticNotificationType): Promise<void> {\n if (enabled) await cues.notification(type);\n },\n async selection(): Promise<void> {\n if (enabled) await cues.selection();\n },\n isEnabled(): boolean {\n return enabled;\n },\n setEnabled(next: boolean): void {\n enabled = next;\n },\n };\n}\n\n/** Stateless no-op cues, shared by both no-op providers below. */\nconst NOOP_CUES: HapticCues = {\n async impact(): Promise<void> {\n // no-op\n },\n async notification(): Promise<void> {\n // no-op\n },\n async selection(): Promise<void> {\n // no-op\n },\n};\n\n/**\n * Immutable no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web without vibration, CI, tests) and the\n * default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the\n * hard \"off\" default, so wiring it into a context can never accidentally report\n * \"enabled\". For a no-op whose enabled toggle is observable (e.g. to unit-test a\n * consumer's haptics setting), use {@link createNoopHapticsProvider}.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\n ...NOOP_CUES,\n isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // inert: hard \"off\" default — see createNoopHapticsProvider for a toggle-able one\n },\n};\n\n/**\n * Create a stateful no-op provider: cue methods never fire, but `setEnabled` /\n * `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle\n * without a native backend. Each call is an independent instance; defaults to\n * disabled (matching {@link NoopHapticsProvider}).\n */\nexport function createNoopHapticsProvider(initialEnabled = false): HapticsProvider {\n return createGatedProvider(NOOP_CUES, initialEnabled);\n}\n","import { createContext, useContext } from 'react';\nimport { type HapticsProvider, NoopHapticsProvider } from './provider.js';\n\n/**\n * React context carrying the active `HapticsProvider`. Defaults to\n * `NoopHapticsProvider`.\n */\nexport const HapticsContext = createContext<HapticsProvider>(NoopHapticsProvider);\n\n/** Hook returning the current `HapticsProvider`. */\nexport function useHaptics(): HapticsProvider {\n return useContext(HapticsContext);\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n} from './provider.js';\n\n/**\n * Structural shape of the `expo-haptics` module surface this adapter needs — the\n * `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared\n * here so the package never depends on `expo-haptics`; the app injects the real\n * module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).\n *\n * Style / type params are `string` because expo types them as its own string\n * enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal\n * this package's `HapticImpactStyle` / `HapticNotificationType` literals\n * (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through\n * unchanged and the real `Haptics` module stays structurally assignable.\n */\nexport interface ExpoHapticsModule {\n impactAsync(style?: string): Promise<void>;\n notificationAsync(type?: string): Promise<void>;\n selectionAsync(): Promise<void>;\n /**\n * Android system haptic feedback. Optional, so a module without it still fits.\n * `type` is `string` for the same reason as above: expo types it as its\n * `AndroidHaptics` string enum, and method syntax keeps the real module\n * structurally assignable. The adapter passes that enum's values\n * (`'clock-tick'`, `'confirm'`, …).\n */\n performAndroidHapticsAsync?(type: string): Promise<void>;\n}\n\n/** Options for {@link createExpoHapticsProvider}. */\nexport interface ExpoHapticsOptions {\n /**\n * The running platform — pass `Platform.OS` from `react-native`. With\n * `'android'`, cues use the system haptic effects when the injected module has\n * `performAndroidHapticsAsync`. The hint is needed because expo's function\n * resolves without doing anything on other platforms, so its presence alone\n * cannot pick the route.\n */\n platform?: string;\n}\n\nconst ANDROID_SELECTION = 'clock-tick';\n\nconst ANDROID_IMPACT: Readonly<Record<HapticImpactStyle, string>> = {\n light: 'keyboard-tap',\n soft: 'keyboard-tap',\n medium: 'virtual-key',\n heavy: 'long-press',\n rigid: 'long-press',\n};\n\nconst ANDROID_NOTIFICATION: Readonly<Record<HapticNotificationType, string>> = {\n success: 'confirm',\n warning: 'long-press',\n error: 'reject',\n};\n\n/**\n * Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.\n * Maps the cue surface onto expo's calls (`impact` → `impactAsync`,\n * `notification` → `notificationAsync`, `selection` → `selectionAsync`); an\n * omitted style / type forwards `undefined`, letting expo apply its own default.\n * Starts enabled — gate it off via `setEnabled(false)` (e.g. from the\n * `@flare-engine/a11y` reduced-motion signal, or a user setting).\n *\n * With `options.platform === 'android'` and a module that has\n * `performAndroidHapticsAsync`, cues play Android system haptic effects instead:\n * `selection` → `'clock-tick'`; `impact` `light` / `soft` → `'keyboard-tap'`,\n * `medium` (and omitted) → `'virtual-key'`, `heavy` / `rigid` → `'long-press'`;\n * `notification` `success` (and omitted) → `'confirm'`, `warning` →\n * `'long-press'`, `error` → `'reject'`. If the module rejects an effect\n * (`'confirm'` / `'reject'` need Android 11), that cue falls back to the expo\n * call above, and the provider stops trying that effect.\n *\n * @example\n * import * as Haptics from 'expo-haptics';\n * import { Platform } from 'react-native';\n * const haptics = createExpoHapticsProvider(Haptics, { platform: Platform.OS });\n */\nexport function createExpoHapticsProvider(\n expo: ExpoHapticsModule,\n options?: ExpoHapticsOptions,\n): HapticsProvider {\n if (options?.platform !== 'android' || typeof expo.performAndroidHapticsAsync !== 'function') {\n return createGatedProvider(\n {\n impact: (style) => expo.impactAsync(style),\n notification: (type) => expo.notificationAsync(type),\n selection: () => expo.selectionAsync(),\n },\n true,\n );\n }\n\n const perform = expo.performAndroidHapticsAsync.bind(expo);\n const unsupported = new Set<string>();\n\n // Resolves `true` when the system effect played, `false` when the caller must\n // fall back. Any rejection marks the type: the module's error class is not\n // visible from here, and a type that failed once is not worth a retry per cue.\n const playSystemEffect = async (type: string): Promise<boolean> => {\n if (unsupported.has(type)) return false;\n try {\n await perform(type);\n return true;\n } catch {\n unsupported.add(type);\n return false;\n }\n };\n\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n if (!(await playSystemEffect(ANDROID_IMPACT[style ?? 'medium']))) {\n await expo.impactAsync(style);\n }\n },\n async notification(type): Promise<void> {\n if (!(await playSystemEffect(ANDROID_NOTIFICATION[type ?? 'success']))) {\n await expo.notificationAsync(type);\n }\n },\n async selection(): Promise<void> {\n if (!(await playSystemEffect(ANDROID_SELECTION))) await expo.selectionAsync();\n },\n },\n true,\n );\n}\n","import {\n createGatedProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n NoopHapticsProvider,\n} from './provider.js';\n\n/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */\nexport type WebVibrate = (pattern: number | number[]) => boolean;\n\n/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */\nconst IMPACT_MS: Record<HapticImpactStyle, number> = {\n light: 10,\n medium: 20,\n heavy: 40,\n soft: 15,\n rigid: 30,\n};\n\n/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */\nconst NOTIFICATION_PATTERN: Record<HapticNotificationType, number[]> = {\n success: [10, 40, 20],\n warning: [20, 60, 20],\n error: [30, 40, 30, 40, 30],\n};\n\n/** Selection tick duration (ms) — a single light buzz. */\nconst SELECTION_MS = 5;\n\nconst DEFAULT_IMPACT: HapticImpactStyle = 'medium';\nconst DEFAULT_NOTIFICATION: HapticNotificationType = 'success';\n\n/**\n * Create a {@link HapticsProvider} backed by the Web Vibration API. Pass\n * `navigator.vibrate` bound to `navigator` (e.g.\n * `navigator.vibrate?.bind(navigator)`).\n *\n * When `vibrate` is absent — the API is unsupported on iOS Safari and most\n * desktop browsers — this returns the inert {@link NoopHapticsProvider}, so\n * `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,\n * and the provider never claims to be enabled when there is no backend). When\n * present, starts enabled; gate via `setEnabled`.\n *\n * Cue durations: impact scales with intensity, notification uses a distinct pulse\n * pattern per category, selection is a single short tick.\n */\nexport function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider {\n if (typeof vibrate !== 'function') {\n return NoopHapticsProvider;\n }\n const buzz = vibrate;\n return createGatedProvider(\n {\n async impact(style): Promise<void> {\n buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);\n },\n async notification(type): Promise<void> {\n buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);\n },\n async selection(): Promise<void> {\n buzz(SELECTION_MS);\n },\n },\n true,\n );\n}\n"],"mappings":";;;;;;;;;AAgDA,SAAgB,oBAAoB,MAAkB,gBAA0C;CAC9F,IAAI,UAAU;CACd,OAAO;EACL,MAAM,OAAO,OAA0C;GACrD,IAAI,SAAS,MAAM,KAAK,OAAO,KAAK;EACtC;EACA,MAAM,aAAa,MAA8C;GAC/D,IAAI,SAAS,MAAM,KAAK,aAAa,IAAI;EAC3C;EACA,MAAM,YAA2B;GAC/B,IAAI,SAAS,MAAM,KAAK,UAAU;EACpC;EACA,YAAqB;GACnB,OAAO;EACT;EACA,WAAW,MAAqB;GAC9B,UAAU;EACZ;CACF;AACF;;AAGA,MAAM,YAAwB;CAC5B,MAAM,SAAwB,CAE9B;CACA,MAAM,eAA8B,CAEpC;CACA,MAAM,YAA2B,CAEjC;AACF;;;;;;;;;AAUA,MAAa,sBAAuC;CAClD,GAAG;CACH,YAAqB;EACnB,OAAO;CACT;CACA,aAAmB,CAEnB;AACF;;;;;;;AAQA,SAAgB,0BAA0B,iBAAiB,OAAwB;CACjF,OAAO,oBAAoB,WAAW,cAAc;AACtD;;;;;;ACrGA,MAAa,iBAAiB,cAA+B,mBAAmB;;AAGhF,SAAgB,aAA8B;CAC5C,OAAO,WAAW,cAAc;AAClC;;ACiCA,MAAM,oBAAoB;AAE1B,MAAM,iBAA8D;CAClE,OAAO;CACP,MAAM;CACN,QAAQ;CACR,OAAO;CACP,OAAO;AACT;AAEA,MAAM,uBAAyE;CAC7E,SAAS;CACT,SAAS;CACT,OAAO;AACT;;;;;;;;;;;;;;;;;;;;;;;AAwBA,SAAgB,0BACd,MACA,SACiB;CACjB,IAAI,SAAS,aAAa,aAAa,OAAO,KAAK,+BAA+B,YAChF,OAAO,oBACL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tC,KAAK,qBAAqB,QAAQ,qBAAqB;EACzD;EACA,MAAM,YAA2B;GAC/B,KAAK,YAAY;EACnB;CACF,GACA,IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/haptics",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.12",
|
|
4
4
|
"description": "Haptics (tactile feedback) provider interface (consumer-supplied adapter)",
|
|
5
5
|
"keywords": ["flare-engine", "haptics", "provider", "interface", "react-native", "typescript"],
|
|
6
6
|
"license": "Apache-2.0",
|