@flare-engine/haptics 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -21
- package/NOTICE +2 -0
- package/README.md +37 -8
- package/dist/index.d.mts +89 -27
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +89 -27
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +159 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +153 -22
- package/dist/index.mjs.map +1 -1
- package/package.json +17 -9
package/LICENSE
CHANGED
|
@@ -1,21 +1,201 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Grzegorz Otto
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @flare-engine/haptics
|
|
2
2
|
|
|
3
|
-
Typed `HapticsProvider` interface
|
|
3
|
+
Typed `HapticsProvider` interface, a React context, and concrete providers you wire at the app boundary via **injection** — no `expo-haptics` or `react-native` dependency. Ships an Expo Haptics adapter, a Web Vibration adapter, and no-op fallbacks.
|
|
4
4
|
|
|
5
5
|
## Install
|
|
6
6
|
|
|
@@ -10,24 +10,53 @@ bun add @flare-engine/haptics
|
|
|
10
10
|
|
|
11
11
|
## Usage
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
Pick a provider at the app boundary and put it on the context. The cue surface is three semantic calls — `impact(style?)` for weapon / hit / bomb feel, `notification(type?)` for victory / error cues, and `selection()` for light UI ticks — plus an `isEnabled()` / `setEnabled(enabled)` gate. The gate keeps the user's haptics setting and the reduced-motion gate (`@flare-engine/a11y`) in one place; when disabled, cue methods are no-ops.
|
|
14
|
+
|
|
15
|
+
```tsx no-check
|
|
16
|
+
import * as Haptics from 'expo-haptics';
|
|
14
17
|
import {
|
|
18
|
+
createExpoHapticsProvider,
|
|
19
|
+
createWebVibrationProvider,
|
|
15
20
|
HapticsContext,
|
|
16
|
-
useHaptics,
|
|
17
|
-
NoopHapticsProvider,
|
|
18
21
|
} from '@flare-engine/haptics';
|
|
19
22
|
|
|
23
|
+
// Native: inject the expo-haptics module. Web: inject navigator.vibrate.
|
|
24
|
+
// Neither is a dependency of this package — the platform module is passed in.
|
|
25
|
+
const provider =
|
|
26
|
+
typeof navigator !== 'undefined' && 'vibrate' in navigator
|
|
27
|
+
? createWebVibrationProvider(navigator.vibrate.bind(navigator))
|
|
28
|
+
: createExpoHapticsProvider(Haptics);
|
|
29
|
+
|
|
20
30
|
export function App() {
|
|
21
31
|
return (
|
|
22
|
-
<HapticsContext.Provider value={
|
|
32
|
+
<HapticsContext.Provider value={provider}>
|
|
23
33
|
{/* ... */}
|
|
24
34
|
</HapticsContext.Provider>
|
|
25
35
|
);
|
|
26
36
|
}
|
|
27
37
|
```
|
|
28
38
|
|
|
29
|
-
|
|
39
|
+
```tsx
|
|
40
|
+
import { Button } from 'react-native';
|
|
41
|
+
import { useHaptics } from '@flare-engine/haptics';
|
|
42
|
+
|
|
43
|
+
function FireButton() {
|
|
44
|
+
const haptics = useHaptics();
|
|
45
|
+
return <Button title="Fire" onPress={() => haptics.impact('heavy')} />;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
`HapticsContext` defaults to `NoopHapticsProvider`, so `useHaptics()` is always safe to call. `createWebVibrationProvider(undefined)` (no Vibration API) returns that same inert no-op, so web code needs no feature-detect.
|
|
50
|
+
|
|
51
|
+
## Providers
|
|
52
|
+
|
|
53
|
+
| Factory | Backend | Notes |
|
|
54
|
+
| --- | --- | --- |
|
|
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
|
+
| `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
|
+
| `NoopHapticsProvider` | none | Immutable, always disabled — the context default and the safe fallback. `setEnabled` is inert by design. |
|
|
58
|
+
| `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. |
|
|
30
59
|
|
|
31
|
-
##
|
|
60
|
+
## Other backends
|
|
32
61
|
|
|
33
|
-
|
|
62
|
+
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
|
@@ -1,45 +1,107 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/provider.d.ts
|
|
3
2
|
/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */
|
|
4
3
|
type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';
|
|
5
4
|
/** Notification feedback categories. */
|
|
6
5
|
type HapticNotificationType = 'success' | 'warning' | 'error';
|
|
7
6
|
/**
|
|
8
7
|
* Haptics (tactile feedback) provider interface. Consumers supply a concrete
|
|
9
|
-
* adapter (Expo Haptics,
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
8
|
+
* adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app
|
|
9
|
+
* boundary. The cue surface is deliberately thin — three semantic calls mirroring
|
|
10
|
+
* the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an
|
|
11
|
+
* enabled gate, so the user's haptics setting and the reduced-motion gate
|
|
12
|
+
* (`@flare-engine/a11y`) live in one place instead of at every call site.
|
|
14
13
|
*/
|
|
15
14
|
interface HapticsProvider {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
/** A physical impact cue — weapon fire, projectile hit, bomb detonation. */
|
|
16
|
+
impact(style?: HapticImpactStyle): Promise<void>;
|
|
17
|
+
/** A notification cue — victory, medal earned, error / deny. */
|
|
18
|
+
notification(type?: HapticNotificationType): Promise<void>;
|
|
19
|
+
/** A light selection tick — menu focus, toggle, pickup. */
|
|
20
|
+
selection(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Whether cues currently fire. Reflects the user setting and the reduced-motion
|
|
23
|
+
* gate; when `false`, `impact` / `notification` / `selection` are no-ops.
|
|
24
|
+
*/
|
|
25
|
+
isEnabled(): boolean;
|
|
26
|
+
/** Enable or disable all feedback. When disabled, cue methods resolve without firing. */
|
|
27
|
+
setEnabled(enabled: boolean): void;
|
|
29
28
|
}
|
|
30
29
|
/**
|
|
31
|
-
*
|
|
32
|
-
* where there is no haptics backend (web, CI, tests)
|
|
33
|
-
*
|
|
30
|
+
* Immutable no-op provider. Reports disabled and fires nothing — the safe default
|
|
31
|
+
* where there is no haptics backend (web without vibration, CI, tests) and the
|
|
32
|
+
* default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
|
|
33
|
+
* hard "off" default, so wiring it into a context can never accidentally report
|
|
34
|
+
* "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
|
|
35
|
+
* consumer's haptics setting), use {@link createNoopHapticsProvider}.
|
|
34
36
|
*/
|
|
35
37
|
declare const NoopHapticsProvider: HapticsProvider;
|
|
36
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
|
|
40
|
+
* `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
|
|
41
|
+
* without a native backend. Each call is an independent instance; defaults to
|
|
42
|
+
* disabled (matching {@link NoopHapticsProvider}).
|
|
43
|
+
*/
|
|
44
|
+
declare function createNoopHapticsProvider(initialEnabled?: boolean): HapticsProvider;
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/context.d.ts
|
|
37
47
|
/**
|
|
38
48
|
* React context carrying the active `HapticsProvider`. Defaults to
|
|
39
49
|
* `NoopHapticsProvider`.
|
|
40
50
|
*/
|
|
41
|
-
declare const HapticsContext: react.Context<HapticsProvider>;
|
|
51
|
+
declare const HapticsContext: import("react").Context<HapticsProvider>;
|
|
42
52
|
/** Hook returning the current `HapticsProvider`. */
|
|
43
53
|
declare function useHaptics(): HapticsProvider;
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/expo.d.ts
|
|
56
|
+
/**
|
|
57
|
+
* Structural shape of the `expo-haptics` module surface this adapter needs — the
|
|
58
|
+
* `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared
|
|
59
|
+
* here so the package never depends on `expo-haptics`; the app injects the real
|
|
60
|
+
* module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).
|
|
61
|
+
*
|
|
62
|
+
* Style / type params are `string` because expo types them as its own string
|
|
63
|
+
* enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal
|
|
64
|
+
* this package's `HapticImpactStyle` / `HapticNotificationType` literals
|
|
65
|
+
* (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through
|
|
66
|
+
* unchanged and the real `Haptics` module stays structurally assignable.
|
|
67
|
+
*/
|
|
68
|
+
interface ExpoHapticsModule {
|
|
69
|
+
impactAsync(style?: string): Promise<void>;
|
|
70
|
+
notificationAsync(type?: string): Promise<void>;
|
|
71
|
+
selectionAsync(): Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
75
|
+
* Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
|
|
76
|
+
* `notification` → `notificationAsync`, `selection` → `selectionAsync`); an
|
|
77
|
+
* omitted style / type forwards `undefined`, letting expo apply its own default.
|
|
78
|
+
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
79
|
+
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* import * as Haptics from 'expo-haptics';
|
|
83
|
+
* const haptics = createExpoHapticsProvider(Haptics);
|
|
84
|
+
*/
|
|
85
|
+
declare function createExpoHapticsProvider(expo: ExpoHapticsModule): HapticsProvider;
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/web.d.ts
|
|
88
|
+
/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */
|
|
89
|
+
type WebVibrate = (pattern: number | number[]) => boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
|
|
92
|
+
* `navigator.vibrate` bound to `navigator` (e.g.
|
|
93
|
+
* `navigator.vibrate?.bind(navigator)`).
|
|
94
|
+
*
|
|
95
|
+
* When `vibrate` is absent — the API is unsupported on iOS Safari and most
|
|
96
|
+
* desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
|
|
97
|
+
* `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
|
|
98
|
+
* and the provider never claims to be enabled when there is no backend). When
|
|
99
|
+
* present, starts enabled; gate via `setEnabled`.
|
|
100
|
+
*
|
|
101
|
+
* Cue durations: impact scales with intensity, notification uses a distinct pulse
|
|
102
|
+
* pattern per category, selection is a single short tick.
|
|
103
|
+
*/
|
|
104
|
+
declare function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider;
|
|
105
|
+
//#endregion
|
|
106
|
+
export { type ExpoHapticsModule, type HapticImpactStyle, type HapticNotificationType, HapticsContext, type HapticsProvider, NoopHapticsProvider, type WebVibrate, createExpoHapticsProvider, createNoopHapticsProvider, createWebVibrationProvider, useHaptics };
|
|
107
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +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;;;;;;;;;;cA+DA,qBAAqB;;;;;;;iBAgBlB,0BAA0B,2BAAyB;;;;;;;cCnGtD,gCAAc,QAAA;;iBAGX,cAAc;;;;;;;;;;;;;;;UCIb;EACf,YAAY,iBAAiB;EAC7B,kBAAkB,gBAAgB;EAClC,kBAAkB;;;;;;;;;;;;;;iBAeJ,0BAA0B,MAAM,oBAAoB;;;;KCvBxD,cAAc;;;;;;;;;;;;;;;iBAsCV,2BAA2B,UAAU,aAAa"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,45 +1,107 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/provider.d.ts
|
|
3
2
|
/** iOS `UIFeedbackGenerator`-style impact intensities; adapters map to platform APIs. */
|
|
4
3
|
type HapticImpactStyle = 'light' | 'medium' | 'heavy' | 'soft' | 'rigid';
|
|
5
4
|
/** Notification feedback categories. */
|
|
6
5
|
type HapticNotificationType = 'success' | 'warning' | 'error';
|
|
7
6
|
/**
|
|
8
7
|
* Haptics (tactile feedback) provider interface. Consumers supply a concrete
|
|
9
|
-
* adapter (Expo Haptics,
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
8
|
+
* adapter (Expo Haptics, the Web Vibration API, a Core Haptics bridge) at the app
|
|
9
|
+
* boundary. The cue surface is deliberately thin — three semantic calls mirroring
|
|
10
|
+
* the iOS `UIFeedbackGenerator` family so any platform SDK can back it — plus an
|
|
11
|
+
* enabled gate, so the user's haptics setting and the reduced-motion gate
|
|
12
|
+
* (`@flare-engine/a11y`) live in one place instead of at every call site.
|
|
14
13
|
*/
|
|
15
14
|
interface HapticsProvider {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
/** A physical impact cue — weapon fire, projectile hit, bomb detonation. */
|
|
16
|
+
impact(style?: HapticImpactStyle): Promise<void>;
|
|
17
|
+
/** A notification cue — victory, medal earned, error / deny. */
|
|
18
|
+
notification(type?: HapticNotificationType): Promise<void>;
|
|
19
|
+
/** A light selection tick — menu focus, toggle, pickup. */
|
|
20
|
+
selection(): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Whether cues currently fire. Reflects the user setting and the reduced-motion
|
|
23
|
+
* gate; when `false`, `impact` / `notification` / `selection` are no-ops.
|
|
24
|
+
*/
|
|
25
|
+
isEnabled(): boolean;
|
|
26
|
+
/** Enable or disable all feedback. When disabled, cue methods resolve without firing. */
|
|
27
|
+
setEnabled(enabled: boolean): void;
|
|
29
28
|
}
|
|
30
29
|
/**
|
|
31
|
-
*
|
|
32
|
-
* where there is no haptics backend (web, CI, tests)
|
|
33
|
-
*
|
|
30
|
+
* Immutable no-op provider. Reports disabled and fires nothing — the safe default
|
|
31
|
+
* where there is no haptics backend (web without vibration, CI, tests) and the
|
|
32
|
+
* default value of `HapticsContext`. `setEnabled` is inert BY DESIGN: this is the
|
|
33
|
+
* hard "off" default, so wiring it into a context can never accidentally report
|
|
34
|
+
* "enabled". For a no-op whose enabled toggle is observable (e.g. to unit-test a
|
|
35
|
+
* consumer's haptics setting), use {@link createNoopHapticsProvider}.
|
|
34
36
|
*/
|
|
35
37
|
declare const NoopHapticsProvider: HapticsProvider;
|
|
36
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Create a stateful no-op provider: cue methods never fire, but `setEnabled` /
|
|
40
|
+
* `isEnabled` track real state. Use it to unit-test a consumer's haptics toggle
|
|
41
|
+
* without a native backend. Each call is an independent instance; defaults to
|
|
42
|
+
* disabled (matching {@link NoopHapticsProvider}).
|
|
43
|
+
*/
|
|
44
|
+
declare function createNoopHapticsProvider(initialEnabled?: boolean): HapticsProvider;
|
|
45
|
+
//#endregion
|
|
46
|
+
//#region src/context.d.ts
|
|
37
47
|
/**
|
|
38
48
|
* React context carrying the active `HapticsProvider`. Defaults to
|
|
39
49
|
* `NoopHapticsProvider`.
|
|
40
50
|
*/
|
|
41
|
-
declare const HapticsContext: react.Context<HapticsProvider>;
|
|
51
|
+
declare const HapticsContext: import("react").Context<HapticsProvider>;
|
|
42
52
|
/** Hook returning the current `HapticsProvider`. */
|
|
43
53
|
declare function useHaptics(): HapticsProvider;
|
|
44
|
-
|
|
45
|
-
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/expo.d.ts
|
|
56
|
+
/**
|
|
57
|
+
* Structural shape of the `expo-haptics` module surface this adapter needs — the
|
|
58
|
+
* `impactAsync` / `notificationAsync` / `selectionAsync` async calls. Declared
|
|
59
|
+
* here so the package never depends on `expo-haptics`; the app injects the real
|
|
60
|
+
* module (matching the repo's adapter pattern — cf. `@flare-engine/iap-revenuecat`).
|
|
61
|
+
*
|
|
62
|
+
* Style / type params are `string` because expo types them as its own string
|
|
63
|
+
* enums (`ImpactFeedbackStyle` / `NotificationFeedbackType`) whose VALUES equal
|
|
64
|
+
* this package's `HapticImpactStyle` / `HapticNotificationType` literals
|
|
65
|
+
* (`'light'`…`'rigid'`, `'success'`…`'error'`), so the cue kind forwards through
|
|
66
|
+
* unchanged and the real `Haptics` module stays structurally assignable.
|
|
67
|
+
*/
|
|
68
|
+
interface ExpoHapticsModule {
|
|
69
|
+
impactAsync(style?: string): Promise<void>;
|
|
70
|
+
notificationAsync(type?: string): Promise<void>;
|
|
71
|
+
selectionAsync(): Promise<void>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
75
|
+
* Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
|
|
76
|
+
* `notification` → `notificationAsync`, `selection` → `selectionAsync`); an
|
|
77
|
+
* omitted style / type forwards `undefined`, letting expo apply its own default.
|
|
78
|
+
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
79
|
+
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* import * as Haptics from 'expo-haptics';
|
|
83
|
+
* const haptics = createExpoHapticsProvider(Haptics);
|
|
84
|
+
*/
|
|
85
|
+
declare function createExpoHapticsProvider(expo: ExpoHapticsModule): HapticsProvider;
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/web.d.ts
|
|
88
|
+
/** Structural shape of `navigator.vibrate`: buzz for `ms`, or run an on/off pattern; returns whether it fired. */
|
|
89
|
+
type WebVibrate = (pattern: number | number[]) => boolean;
|
|
90
|
+
/**
|
|
91
|
+
* Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
|
|
92
|
+
* `navigator.vibrate` bound to `navigator` (e.g.
|
|
93
|
+
* `navigator.vibrate?.bind(navigator)`).
|
|
94
|
+
*
|
|
95
|
+
* When `vibrate` is absent — the API is unsupported on iOS Safari and most
|
|
96
|
+
* desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
|
|
97
|
+
* `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
|
|
98
|
+
* and the provider never claims to be enabled when there is no backend). When
|
|
99
|
+
* present, starts enabled; gate via `setEnabled`.
|
|
100
|
+
*
|
|
101
|
+
* Cue durations: impact scales with intensity, notification uses a distinct pulse
|
|
102
|
+
* pattern per category, selection is a single short tick.
|
|
103
|
+
*/
|
|
104
|
+
declare function createWebVibrationProvider(vibrate?: WebVibrate): HapticsProvider;
|
|
105
|
+
//#endregion
|
|
106
|
+
export { type ExpoHapticsModule, type HapticImpactStyle, type HapticNotificationType, HapticsContext, type HapticsProvider, NoopHapticsProvider, type WebVibrate, createExpoHapticsProvider, createNoopHapticsProvider, createWebVibrationProvider, useHaptics };
|
|
107
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +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;;;;;;;;;;cA+DA,qBAAqB;;;;;;;iBAgBlB,0BAA0B,2BAAyB;;;;;;;cCnGtD,gCAAc,QAAA;;iBAGX,cAAc;;;;;;;;;;;;;;;UCIb;EACf,YAAY,iBAAiB;EAC7B,kBAAkB,gBAAgB;EAClC,kBAAkB;;;;;;;;;;;;;;iBAeJ,0BAA0B,MAAM,oBAAoB;;;;KCvBxD,cAAc;;;;;;;;;;;;;;;iBAsCV,2BAA2B,UAAU,aAAa"}
|
package/dist/index.js
CHANGED
|
@@ -1,56 +1,164 @@
|
|
|
1
|
-
"
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
let react = require("react");
|
|
3
|
+
//#region 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() {}
|
|
17
36
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
},
|
|
33
|
-
async notification() {
|
|
34
|
-
},
|
|
35
|
-
async selection() {
|
|
36
|
-
},
|
|
37
|
-
isEnabled() {
|
|
38
|
-
return false;
|
|
39
|
-
},
|
|
40
|
-
setEnabled() {
|
|
41
|
-
}
|
|
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() {}
|
|
42
51
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
//#endregion
|
|
62
|
+
//#region src/context.ts
|
|
63
|
+
/**
|
|
64
|
+
* React context carrying the active `HapticsProvider`. Defaults to
|
|
65
|
+
* `NoopHapticsProvider`.
|
|
66
|
+
*/
|
|
67
|
+
const HapticsContext = (0, react.createContext)(NoopHapticsProvider);
|
|
68
|
+
/** Hook returning the current `HapticsProvider`. */
|
|
47
69
|
function useHaptics() {
|
|
48
|
-
|
|
70
|
+
return (0, react.useContext)(HapticsContext);
|
|
49
71
|
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
72
|
+
//#endregion
|
|
73
|
+
//#region src/expo.ts
|
|
74
|
+
/**
|
|
75
|
+
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
76
|
+
* Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
|
|
77
|
+
* `notification` → `notificationAsync`, `selection` → `selectionAsync`); an
|
|
78
|
+
* omitted style / type forwards `undefined`, letting expo apply its own default.
|
|
79
|
+
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
80
|
+
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* import * as Haptics from 'expo-haptics';
|
|
84
|
+
* const haptics = createExpoHapticsProvider(Haptics);
|
|
85
|
+
*/
|
|
86
|
+
function createExpoHapticsProvider(expo) {
|
|
87
|
+
return createGatedProvider({
|
|
88
|
+
impact: (style) => expo.impactAsync(style),
|
|
89
|
+
notification: (type) => expo.notificationAsync(type),
|
|
90
|
+
selection: () => expo.selectionAsync()
|
|
91
|
+
}, true);
|
|
92
|
+
}
|
|
93
|
+
//#endregion
|
|
94
|
+
//#region src/web.ts
|
|
95
|
+
/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
|
|
96
|
+
const IMPACT_MS = {
|
|
97
|
+
light: 10,
|
|
98
|
+
medium: 20,
|
|
99
|
+
heavy: 40,
|
|
100
|
+
soft: 15,
|
|
101
|
+
rigid: 30
|
|
102
|
+
};
|
|
103
|
+
/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */
|
|
104
|
+
const NOTIFICATION_PATTERN = {
|
|
105
|
+
success: [
|
|
106
|
+
10,
|
|
107
|
+
40,
|
|
108
|
+
20
|
|
109
|
+
],
|
|
110
|
+
warning: [
|
|
111
|
+
20,
|
|
112
|
+
60,
|
|
113
|
+
20
|
|
114
|
+
],
|
|
115
|
+
error: [
|
|
116
|
+
30,
|
|
117
|
+
40,
|
|
118
|
+
30,
|
|
119
|
+
40,
|
|
120
|
+
30
|
|
121
|
+
]
|
|
122
|
+
};
|
|
123
|
+
/** Selection tick duration (ms) — a single light buzz. */
|
|
124
|
+
const SELECTION_MS = 5;
|
|
125
|
+
const DEFAULT_IMPACT = "medium";
|
|
126
|
+
const DEFAULT_NOTIFICATION = "success";
|
|
127
|
+
/**
|
|
128
|
+
* Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
|
|
129
|
+
* `navigator.vibrate` bound to `navigator` (e.g.
|
|
130
|
+
* `navigator.vibrate?.bind(navigator)`).
|
|
131
|
+
*
|
|
132
|
+
* When `vibrate` is absent — the API is unsupported on iOS Safari and most
|
|
133
|
+
* desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
|
|
134
|
+
* `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
|
|
135
|
+
* and the provider never claims to be enabled when there is no backend). When
|
|
136
|
+
* present, starts enabled; gate via `setEnabled`.
|
|
137
|
+
*
|
|
138
|
+
* Cue durations: impact scales with intensity, notification uses a distinct pulse
|
|
139
|
+
* pattern per category, selection is a single short tick.
|
|
140
|
+
*/
|
|
141
|
+
function createWebVibrationProvider(vibrate) {
|
|
142
|
+
if (typeof vibrate !== "function") return NoopHapticsProvider;
|
|
143
|
+
const buzz = vibrate;
|
|
144
|
+
return createGatedProvider({
|
|
145
|
+
async impact(style) {
|
|
146
|
+
buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);
|
|
147
|
+
},
|
|
148
|
+
async notification(type) {
|
|
149
|
+
buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);
|
|
150
|
+
},
|
|
151
|
+
async selection() {
|
|
152
|
+
buzz(SELECTION_MS);
|
|
153
|
+
}
|
|
154
|
+
}, true);
|
|
155
|
+
}
|
|
156
|
+
//#endregion
|
|
157
|
+
exports.HapticsContext = HapticsContext;
|
|
158
|
+
exports.NoopHapticsProvider = NoopHapticsProvider;
|
|
159
|
+
exports.createExpoHapticsProvider = createExpoHapticsProvider;
|
|
160
|
+
exports.createNoopHapticsProvider = createNoopHapticsProvider;
|
|
161
|
+
exports.createWebVibrationProvider = createWebVibrationProvider;
|
|
162
|
+
exports.useHaptics = useHaptics;
|
|
163
|
+
|
|
56
164
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/provider.ts","../src/context.ts"],"sourcesContent":["export {\n NoopHapticsProvider,\n type HapticImpactStyle,\n type HapticNotificationType,\n type HapticsProvider,\n} from './provider.js';\nexport { HapticsContext, useHaptics } from './context.js';\n","/** 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, react-native `Vibration`, a Core Haptics bridge) at the\n * app boundary. The cue surface is deliberately thin — three semantic calls\n * mirroring the iOS `UIFeedbackGenerator` family so any platform SDK can back it\n * — plus an enabled gate, so the user's haptics setting and the reduced-motion\n * gate (`@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 * Default no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web, CI, tests). All cues resolve\n * immediately; `setEnabled` is inert because there is nothing to drive.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\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 isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // no-op\n },\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"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACmCO,IAAM,sBAAuC;AAAA,EAClD,MAAM,SAAwB;AAAA,EAE9B;AAAA,EACA,MAAM,eAA8B;AAAA,EAEpC;AAAA,EACA,MAAM,YAA2B;AAAA,EAEjC;AAAA,EACA,YAAqB;AACnB,WAAO;AAAA,EACT;AAAA,EACA,aAAmB;AAAA,EAEnB;AACF;;;ACnDA,mBAA0C;AAOnC,IAAM,qBAAiB,4BAA+B,mBAAmB;AAGzE,SAAS,aAA8B;AAC5C,aAAO,yBAAW,cAAc;AAClC;","names":[]}
|
|
1
|
+
{"version":3,"file":"index.js","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,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,GAAA,MAAA,cAAA,CAAgD,mBAAmB;;AAGhF,SAAgB,aAA8B;CAC5C,QAAA,GAAA,MAAA,WAAA,CAAkB,cAAc;AAClC;;;;;;;;;;;;;;;ACoBA,SAAgB,0BAA0B,MAA0C;CAClF,OAAO,oBACL;EACE,SAAS,UAAU,KAAK,YAAY,KAAK;EACzC,eAAe,SAAS,KAAK,kBAAkB,IAAI;EACnD,iBAAiB,KAAK,eAAe;CACvC,GACA,IACF;AACF;;;;AC7BA,MAAM,YAA+C;CACnD,OAAO;CACP,QAAQ;CACR,OAAO;CACP,MAAM;CACN,OAAO;AACT;;AAGA,MAAM,uBAAiE;CACrE,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,OAAO;EAAC;EAAI;EAAI;EAAI;EAAI;CAAE;AAC5B;;AAGA,MAAM,eAAe;AAErB,MAAM,iBAAoC;AAC1C,MAAM,uBAA+C;;;;;;;;;;;;;;;AAgBrD,SAAgB,2BAA2B,SAAuC;CAChF,IAAI,OAAO,YAAY,YACrB,OAAO;CAET,MAAM,OAAO;CACb,OAAO,oBACL;EACE,MAAM,OAAO,OAAsB;GACjC,KAAK,UAAU,SAAS,eAAe;EACzC;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
|
@@ -1,27 +1,158 @@
|
|
|
1
|
-
// src/provider.ts
|
|
2
|
-
var NoopHapticsProvider = {
|
|
3
|
-
async impact() {
|
|
4
|
-
},
|
|
5
|
-
async notification() {
|
|
6
|
-
},
|
|
7
|
-
async selection() {
|
|
8
|
-
},
|
|
9
|
-
isEnabled() {
|
|
10
|
-
return false;
|
|
11
|
-
},
|
|
12
|
-
setEnabled() {
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// src/context.ts
|
|
17
1
|
import { createContext, useContext } from "react";
|
|
18
|
-
|
|
2
|
+
//#region 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
|
+
//#endregion
|
|
61
|
+
//#region src/context.ts
|
|
62
|
+
/**
|
|
63
|
+
* React context carrying the active `HapticsProvider`. Defaults to
|
|
64
|
+
* `NoopHapticsProvider`.
|
|
65
|
+
*/
|
|
66
|
+
const HapticsContext = createContext(NoopHapticsProvider);
|
|
67
|
+
/** Hook returning the current `HapticsProvider`. */
|
|
19
68
|
function useHaptics() {
|
|
20
|
-
|
|
69
|
+
return useContext(HapticsContext);
|
|
70
|
+
}
|
|
71
|
+
//#endregion
|
|
72
|
+
//#region src/expo.ts
|
|
73
|
+
/**
|
|
74
|
+
* Create a {@link HapticsProvider} backed by an injected `expo-haptics` module.
|
|
75
|
+
* Maps the cue surface onto expo's calls (`impact` → `impactAsync`,
|
|
76
|
+
* `notification` → `notificationAsync`, `selection` → `selectionAsync`); an
|
|
77
|
+
* omitted style / type forwards `undefined`, letting expo apply its own default.
|
|
78
|
+
* Starts enabled — gate it off via `setEnabled(false)` (e.g. from the
|
|
79
|
+
* `@flare-engine/a11y` reduced-motion signal, or a user setting).
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* import * as Haptics from 'expo-haptics';
|
|
83
|
+
* const haptics = createExpoHapticsProvider(Haptics);
|
|
84
|
+
*/
|
|
85
|
+
function createExpoHapticsProvider(expo) {
|
|
86
|
+
return createGatedProvider({
|
|
87
|
+
impact: (style) => expo.impactAsync(style),
|
|
88
|
+
notification: (type) => expo.notificationAsync(type),
|
|
89
|
+
selection: () => expo.selectionAsync()
|
|
90
|
+
}, true);
|
|
21
91
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/web.ts
|
|
94
|
+
/** Impact cue durations (ms), keyed by intensity — heavier styles buzz longer. */
|
|
95
|
+
const IMPACT_MS = {
|
|
96
|
+
light: 10,
|
|
97
|
+
medium: 20,
|
|
98
|
+
heavy: 40,
|
|
99
|
+
soft: 15,
|
|
100
|
+
rigid: 30
|
|
101
|
+
};
|
|
102
|
+
/** Notification cue patterns (ms on/off/on…), one distinct pulse per category. */
|
|
103
|
+
const NOTIFICATION_PATTERN = {
|
|
104
|
+
success: [
|
|
105
|
+
10,
|
|
106
|
+
40,
|
|
107
|
+
20
|
|
108
|
+
],
|
|
109
|
+
warning: [
|
|
110
|
+
20,
|
|
111
|
+
60,
|
|
112
|
+
20
|
|
113
|
+
],
|
|
114
|
+
error: [
|
|
115
|
+
30,
|
|
116
|
+
40,
|
|
117
|
+
30,
|
|
118
|
+
40,
|
|
119
|
+
30
|
|
120
|
+
]
|
|
26
121
|
};
|
|
122
|
+
/** Selection tick duration (ms) — a single light buzz. */
|
|
123
|
+
const SELECTION_MS = 5;
|
|
124
|
+
const DEFAULT_IMPACT = "medium";
|
|
125
|
+
const DEFAULT_NOTIFICATION = "success";
|
|
126
|
+
/**
|
|
127
|
+
* Create a {@link HapticsProvider} backed by the Web Vibration API. Pass
|
|
128
|
+
* `navigator.vibrate` bound to `navigator` (e.g.
|
|
129
|
+
* `navigator.vibrate?.bind(navigator)`).
|
|
130
|
+
*
|
|
131
|
+
* When `vibrate` is absent — the API is unsupported on iOS Safari and most
|
|
132
|
+
* desktop browsers — this returns the inert {@link NoopHapticsProvider}, so
|
|
133
|
+
* `isEnabled()` stays `false` and cues never fire (callers need not feature-detect,
|
|
134
|
+
* and the provider never claims to be enabled when there is no backend). When
|
|
135
|
+
* present, starts enabled; gate via `setEnabled`.
|
|
136
|
+
*
|
|
137
|
+
* Cue durations: impact scales with intensity, notification uses a distinct pulse
|
|
138
|
+
* pattern per category, selection is a single short tick.
|
|
139
|
+
*/
|
|
140
|
+
function createWebVibrationProvider(vibrate) {
|
|
141
|
+
if (typeof vibrate !== "function") return NoopHapticsProvider;
|
|
142
|
+
const buzz = vibrate;
|
|
143
|
+
return createGatedProvider({
|
|
144
|
+
async impact(style) {
|
|
145
|
+
buzz(IMPACT_MS[style ?? DEFAULT_IMPACT]);
|
|
146
|
+
},
|
|
147
|
+
async notification(type) {
|
|
148
|
+
buzz(NOTIFICATION_PATTERN[type ?? DEFAULT_NOTIFICATION]);
|
|
149
|
+
},
|
|
150
|
+
async selection() {
|
|
151
|
+
buzz(SELECTION_MS);
|
|
152
|
+
}
|
|
153
|
+
}, true);
|
|
154
|
+
}
|
|
155
|
+
//#endregion
|
|
156
|
+
export { HapticsContext, NoopHapticsProvider, createExpoHapticsProvider, createNoopHapticsProvider, createWebVibrationProvider, useHaptics };
|
|
157
|
+
|
|
27
158
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/provider.ts","../src/context.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, react-native `Vibration`, a Core Haptics bridge) at the\n * app boundary. The cue surface is deliberately thin — three semantic calls\n * mirroring the iOS `UIFeedbackGenerator` family so any platform SDK can back it\n * — plus an enabled gate, so the user's haptics setting and the reduced-motion\n * gate (`@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 * Default no-op provider. Reports disabled and fires nothing — the safe default\n * where there is no haptics backend (web, CI, tests). All cues resolve\n * immediately; `setEnabled` is inert because there is nothing to drive.\n */\nexport const NoopHapticsProvider: HapticsProvider = {\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 isEnabled(): boolean {\n return false;\n },\n setEnabled(): void {\n // no-op\n },\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"],"mappings":";AAmCO,IAAM,sBAAuC;AAAA,EAClD,MAAM,SAAwB;AAAA,EAE9B;AAAA,EACA,MAAM,eAA8B;AAAA,EAEpC;AAAA,EACA,MAAM,YAA2B;AAAA,EAEjC;AAAA,EACA,YAAqB;AACnB,WAAO;AAAA,EACT;AAAA,EACA,aAAmB;AAAA,EAEnB;AACF;;;ACnDA,SAAS,eAAe,kBAAkB;AAOnC,IAAM,iBAAiB,cAA+B,mBAAmB;AAGzE,SAAS,aAA8B;AAC5C,SAAO,WAAW,cAAc;AAClC;","names":[]}
|
|
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,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;;;;;;;;;;;;;;;ACoBA,SAAgB,0BAA0B,MAA0C;CAClF,OAAO,oBACL;EACE,SAAS,UAAU,KAAK,YAAY,KAAK;EACzC,eAAe,SAAS,KAAK,kBAAkB,IAAI;EACnD,iBAAiB,KAAK,eAAe;CACvC,GACA,IACF;AACF;;;;AC7BA,MAAM,YAA+C;CACnD,OAAO;CACP,QAAQ;CACR,OAAO;CACP,MAAM;CACN,OAAO;AACT;;AAGA,MAAM,uBAAiE;CACrE,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,SAAS;EAAC;EAAI;EAAI;CAAE;CACpB,OAAO;EAAC;EAAI;EAAI;EAAI;EAAI;CAAE;AAC5B;;AAGA,MAAM,eAAe;AAErB,MAAM,iBAAoC;AAC1C,MAAM,uBAA+C;;;;;;;;;;;;;;;AAgBrD,SAAgB,2BAA2B,SAAuC;CAChF,IAAI,OAAO,YAAY,YACrB,OAAO;CAET,MAAM,OAAO;CACb,OAAO,oBACL;EACE,MAAM,OAAO,OAAsB;GACjC,KAAK,UAAU,SAAS,eAAe;EACzC;EACA,MAAM,aAAa,MAAqB;GACtC,KAAK,qBAAqB,QAAQ,qBAAqB;EACzD;EACA,MAAM,YAA2B;GAC/B,KAAK,YAAY;EACnB;CACF,GACA,IACF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flare-engine/haptics",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Haptics (tactile feedback) provider interface (consumer-supplied adapter)",
|
|
5
5
|
"keywords": ["flare-engine", "haptics", "provider", "interface"],
|
|
6
|
-
"license": "
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
7
|
"author": "Grzegorz Otto",
|
|
8
8
|
"homepage": "https://github.com/grzott/flare-engine#readme",
|
|
9
9
|
"bugs": {
|
|
@@ -19,23 +19,31 @@
|
|
|
19
19
|
"types": "dist/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
21
|
".": {
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
22
|
+
"import": {
|
|
23
|
+
"types": "./dist/index.d.mts",
|
|
24
|
+
"default": "./dist/index.mjs"
|
|
25
|
+
},
|
|
26
|
+
"require": {
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"default": "./dist/index.js"
|
|
29
|
+
}
|
|
25
30
|
}
|
|
26
31
|
},
|
|
27
|
-
"files": ["dist", "README.md", "LICENSE"],
|
|
32
|
+
"files": ["dist", "README.md", "LICENSE", "NOTICE"],
|
|
28
33
|
"scripts": {
|
|
29
|
-
"build": "
|
|
34
|
+
"build": "tsdown",
|
|
30
35
|
"test": "bun test",
|
|
31
36
|
"lint": "biome check src/",
|
|
32
37
|
"typecheck": "tsc --noEmit"
|
|
33
38
|
},
|
|
34
39
|
"devDependencies": {
|
|
35
|
-
"@types/react": "^
|
|
40
|
+
"@types/react": "^19.0.0"
|
|
36
41
|
},
|
|
37
42
|
"peerDependencies": {
|
|
38
|
-
"react": ">=
|
|
43
|
+
"react": ">=19.0.0"
|
|
44
|
+
},
|
|
45
|
+
"engines": {
|
|
46
|
+
"node": ">=20"
|
|
39
47
|
},
|
|
40
48
|
"publishConfig": {
|
|
41
49
|
"access": "restricted",
|