Haptics
Haptics fires haptic feedback: a collision, a success or failure, a selection change.
import { Haptics } from '@ng-native/expo';
inject(Haptics)It talks to Expo's native module directly rather than through expo-haptics's own JavaScript,
which adds exactly one thing on top: a throw when the module is missing. Asking for it optionally
answers the same question without the throw, and sidesteps the enum its functions take. The
expo-haptics package still has to be installed - that is what links the native side.
Install
npx expo install expo-hapticsimport { Haptics } from '@ng-native/expo/haptics';The smallest useful example
import { Component, inject } from '@angular/core';
import { Haptics } from '@ng-native/expo/haptics';
@Component({
selector: 'app-save-button',
template: `<pressable (press)="save()"><text>Save</text></pressable>`,
})
export class SaveButton {
private readonly haptics = inject(Haptics);
protected save(): void {
this.haptics.notify('success');
}
}What it does
impact(style)- a collision between interface elements.styleis'light','medium'(the default),'heavy','rigid'or'soft'.'rigid'and'soft'need iOS 13+.notify(type)- a task succeeded or failed:'success','warning'or'error'. iOS plays a distinct pattern for each.select()- a selection changed, the lightest of the three.available- whether the native module is installed at all.
Every method returns nothing and swallows failures. Expo's own equivalents are all promises, and a
caller who does not await one gets an unhandled rejection on a device with no Taptic Engine, in a
simulator, or on an Android build with the vibration permission off - none of which is a reason for
anything to go wrong on screen. Nobody awaits a vibration, so nothing here surfaces one going
nowhere; if that ever needs debugging, add logging under __DEV__ rather than making call sites
handle a rejection.
Without the module installed
Every method does nothing. available is false.
Reference
Hapticsservice@ng-native/expoimport { Haptics } from '@ng-native/expo';Every method returns nothing and swallows failures.
Methods
impact(style: ImpactStyle = 'medium'): voidA collision between interface elements.
notify(type: NotificationType): voidA task succeeded or failed.
select(): voidA selection changed: the lightest of the three.