UNPKG

react-native-haptic-feedback

Version:
132 lines (131 loc) 4.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _NativeHapticFeedback = _interopRequireDefault(require("./codegenSpec/NativeHapticFeedback")); var _types = require("./types"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } // Characteristic sharpness per impact type for use with impact() const IMPACT_SHARPNESS = { [_types.HapticFeedbackTypes.impactLight]: 0.3, [_types.HapticFeedbackTypes.impactMedium]: 0.6, [_types.HapticFeedbackTypes.impactHeavy]: 0.8, [_types.HapticFeedbackTypes.rigid]: 1.0, [_types.HapticFeedbackTypes.soft]: 0.1, [_types.HapticFeedbackTypes.selection]: 0.3 }; const defaultOptions = { enableVibrateFallback: false, ignoreAndroidSystemSettings: false }; let _enabled = true; const RNHapticFeedback = { /** * Enable or disable all haptic feedback library-wide. * Useful for respecting a user's in-app haptics preference. * The setting is in-memory only — persist it yourself if needed across sessions. */ setEnabled(value) { _enabled = value; }, /** Returns whether haptic feedback is currently enabled. */ isEnabled() { return _enabled; }, trigger(type = _types.HapticFeedbackTypes.selection, options = {}) { if (!_enabled) return; try { _NativeHapticFeedback.default.trigger(type, { ...defaultOptions, ...options }); } catch (e) { console.warn("RNReactNativeHapticFeedback: trigger failed –", e); } }, stop() { if (!_enabled) return; try { _NativeHapticFeedback.default.stop(); } catch (e) { console.warn("RNReactNativeHapticFeedback: stop failed –", e); } }, isSupported() { try { return _NativeHapticFeedback.default.isSupported(); } catch { return false; } }, triggerPattern(events, options = {}) { if (!_enabled) return; try { _NativeHapticFeedback.default.triggerPattern(events, { ...defaultOptions, ...options }); } catch (e) { console.warn("RNReactNativeHapticFeedback: triggerPattern failed –", e); } }, /** * Play an Apple Haptic and Audio Pattern (AHAP) file by name. * * Place `.ahap` files in `<bundle>/haptics/` or the bundle root. * Resolves immediately on Android (AHAP is an Apple-only format). * * For cross-platform usage, prefer `playHaptic(ahapFile, fallback)`. * * @platform ios */ playAHAP(fileName) { if (!_enabled) return Promise.resolve(); try { return _NativeHapticFeedback.default.playAHAP(fileName); } catch { return Promise.resolve(); } }, /** * Play a haptic with a custom intensity (0.0–1.0). * * On iOS (Core Haptics), the intensity is applied precisely via CHHapticEngine. * On Android it maps to `VibrationEffect` amplitude. * On devices without haptic hardware this is a no-op. * * @param type - haptic type that determines the base sharpness/character (default `impactMedium`) * @param intensity - 0.0 (silent) to 1.0 (maximum), default 0.7 * @param options - same options as `trigger()` */ impact(type = _types.HapticFeedbackTypes.impactMedium, intensity = 0.7, options = {}) { if (!_enabled) return; const sharpness = IMPACT_SHARPNESS[type] ?? 0.5; const clampedIntensity = Math.max(0, Math.min(1, intensity)); try { _NativeHapticFeedback.default.triggerPattern([{ time: 0, intensity: clampedIntensity, sharpness }], { ...defaultOptions, ...options }); } catch (e) { console.warn("RNReactNativeHapticFeedback: impact failed –", e); } }, async getSystemHapticStatus() { try { return await _NativeHapticFeedback.default.getSystemHapticStatus(); } catch { return { vibrationEnabled: false, ringerMode: null }; } } }; var _default = exports.default = RNHapticFeedback; //# sourceMappingURL=hapticFeedback.js.map