API
Every component, prop, helper, and public type.
Components
Haptic and HapticTarget are aliases of the same component. Both wrap one React element. A custom component must render one host target; forwarding its ref remains the custom component's responsibility.
| Prop | Type | Default |
|---|---|---|
children | One target React element | Required |
iosPhysicalTap | boolean; opt into the iOS overlay | false |
enabled | boolean | true |
disabled | boolean | false |
feedback | HapticInput | 'selection' |
triggerOptions | TriggerOptions | Unspecified |
webHapticsOptions | WebHapticsOptions | Unspecified |
className | string | Unspecified |
style | React CSSProperties | Wrapper uses inline-grid |
triggerOptions.intensity supplies the default intensity from 0 to 1 for events that do not specify their own. Named presets specify their own intensities, so this option affects custom patterns instead. It does not change the iOS system tick. webHapticsOptions.debug enables desktop preview audio; showSwitch controls the visible preview checkbox.
import { Haptic } from '@bloxwap/haptic';
<Haptic
feedback="light"
triggerOptions={{ intensity: 0.7 }}
webHapticsOptions={{ debug: false, showSwitch: false }}
style={{ display: 'grid', width: '100%' }}
>
<button>Continue</button>
</Haptic>Imperative feedback
canTriggerHaptic(): HapticCapability checks current imperative availability without creating DOM or requesting feedback. available describes a request route, and observed is always false: JavaScript cannot certify tactile output. The probe accounts for page visibility and sticky activation when the browser exposes it.
triggerHaptic(cue?: HapticInput, options?: TriggerOptions): Promise<HapticTriggerResult> submits the request before returning its promise. The default cue is selection. Vibration presets, custom forms, the per-event 1000ms duration cap, and intensity modulation match HapticTarget. No engine, auxiliary controls, tab stops, or accessibility nodes are created.
import { triggerHaptic } from '@bloxwap/haptic';
function SaveAction({ save }: { save: () => void }) {
return <button onClick={() => {
save();
void triggerHaptic('success').then(result => {
if (!result.delivered) console.log(result.reason);
});
}}>Save</button>;
}The result contains delivered, mechanism, and an optional reason. delivered: true means navigator.vibrate accepted the request; it does not prove a physical response. A rejection, exception, unavailable environment, invalid input, or silent pattern resolves delivered: false with mechanism none. Feedback failures never reject the returned promise.
On iOS WebKit without vibration, imperative feedback resolves unavailable even inside a synchronous click handler. Transient activation alone does not make a script-created click trusted. WebKit's trusted-event change explicitly rejects programmatic label activation. Use <HapticTarget iosPhysicalTap> for eligible physical taps after reviewing the overlay limitations; its iOS feedback is a system tick, without success/warning/error patterns. Timers and calls after an await do not gain an iOS delivery promise. Physical certification remains tracked in #9 and the remaining native API request in #27.
| Reason | Meaning |
|---|---|
browser-unavailable | Server or missing browser globals |
document-hidden | Page is hidden |
user-activation-required | Browser requires prior interaction, or iOS has no active interaction |
trusted-physical-tap-required | iOS is active, but this imperative API cannot create a trusted tap |
vibration-unavailable | No imperative platform route |
invalid-input | Unknown preset or invalid timing/intensity |
empty-pattern | No positive vibration duration |
vibration-rejected | Browser returned false |
vibration-error | Feedback request failed |
Platform helper
isIOSWebKitWithoutVibration(navigatorLike?: Navigator): boolean recognizes iOS WebKit identities without the Vibration API. The default argument uses the browser navigator when available. In a server environment without it, the helper returns false.
It recognizes iPadOS desktop-mode identities, but routing recognition does not establish vibration hardware. A supplied navigator.vibrate takes priority over the native-switch path.
Hook
useIOSNativeHaptics(): boolean returns the detected routing value on the first client-only render. It returns false on the server and during the first hydration render, then resolves client routing after hydration. Use it only inside a React component or hook. It does not detect whether the user can physically feel the feedback.
Public types
import type {
HapticTargetProps, HapticInput, HapticPattern, HapticPreset,
TriggerOptions, Vibration, WebHapticsOptions,
HapticCapability, HapticTriggerResult,
} from '@bloxwap/haptic';HapticInput: a duration number, preset string,HapticPattern, orHapticPreset.HapticPattern: alternating on/offnumber[]durations, orVibration[].Vibration:duration: number,delay?: number,intensity?: number.HapticPreset:{ pattern: Vibration[] }.TriggerOptions:{ intensity?: number }.WebHapticsOptions:{ debug?: boolean; showSwitch?: boolean }.
Feedback types come from the retained web-haptics dependency. All timings use milliseconds, rounded down to whole milliseconds. Both the component and imperative API accept at most 64 events (128 alternating numeric entries), 1,024 encoded segments and 10 seconds including delays. Oversized input is rejected as invalid-input; each vibration duration is capped at 1,000 ms. Browser-specific truncation may impose tighter limits.
Lifecycle and events
By default, Haptic preserves the original pointer and click events. On iOS WebKit without vibration it stays silent. Set iosPhysicalTap to opt into the native switch overlay on a button or link that has no nested controls. That overlay intercepts pointer handlers, forwards an untrusted click without the original modifiers, coordinates or click count, and exposes auxiliary clicks to ancestor capture handlers. Keep it off for gesture controls, modified links, or capture-based actions. Unsupported hosts keep their original interaction without an overlay.
The original target element is rendered intact. Feedback runs before its React click handler, including handlers that stop propagation or prevent default. Original handlers still control bubbling and browser defaults.
Engine resources are created when first used and released on unmount or construction-option changes. Ordinary renders and feedback/intensity changes do not recreate the engine. Unmounting invalidates pending preview work and cancels only vibration still owned by that component. Direct actuator calls outside this package cannot participate in ownership tracking. Feedback errors leave the original action available.
The positioning wrapper is a span marked with data-haptic-target. Non-element children (null, text, arrays) are rejected with an error.
The child must render exactly one DOM element. A fragment, or a component that renders several elements or text beside its element, keeps its own actions but gets no feedback: no iOS overlay is added and no vibration is requested, and development builds log a console.error. A child that renders nothing is silently inactive.
Nested targets defer to the innermost one. A tap inside an inner target runs its action once and requests feedback once, using the inner target's props. On iOS an outer target whose element contains another target adds no overlay, so taps on the rest of the outer element get no tick; on vibration browsers they still use the outer target's feedback. Development builds log a console.error for nesting.