Skip to main content

Haptics API

A low-level API for triggering haptic feedback on native platforms without React hooks.

Overview

The Haptics API provides direct access to haptic feedback functionality. Use this API when you need to trigger haptics outside of React components, such as in utility functions, callbacks, or non-React code paths.

For React components, consider using the useHaptics hook instead, which provides the same functionality with React-friendly memoization.

Import

import { trigger, setHapticsEnabled } from "@ppb/the-wall-native/api/haptics";

Usage

Basic Example

import { trigger, setHapticsEnabled } from "@ppb/the-wall-native/api/haptics";

// Enable haptics globally (typically in app initialization)
setHapticsEnabled(true);

// Trigger haptic feedback directly
trigger("light");

In Utility Functions

import { trigger } from "@ppb/the-wall-native/api/haptics";

export const handlePaymentSuccess = (transaction: Transaction) => {
trigger("success");
// Process successful payment
};

export const handleValidationError = (errors: ValidationError[]) => {
trigger("error");
// Handle validation errors
};

API Reference

trigger(type: HapticFeedbackType)

Triggers haptic feedback with the specified type. Only triggers feedback if haptics are globally enabled via setHapticsEnabled(true).

Parameters:

ParameterTypeDescription
typeHapticFeedbackTypeThe type of haptic feedback

Returns: void

setHapticsEnabled(enabled: boolean)

Globally enables or disables haptic feedback for the entire application.

Parameters:

ParameterTypeDescription
enabledbooleantrue to enable haptics, false to disable

Returns: void

HapticFeedbackType

A TypeScript type representing valid haptic feedback types:

type HapticFeedbackType =
| "light"
| "medium"
| "heavy"
| "soft"
| "rigid"
| "selection"
| "success"
| "warning"
| "error";

Haptic Types

TypeDescriptionUse Case
"light"Light impact feedbackButton taps, simple interactions
"medium"Medium impact feedbackMore significant interactions
"heavy"Heavy impact feedbackImportant or impactful actions
"soft"Soft impact feedbackGentle, subtle interactions
"rigid"Rigid impact feedbackFirm, precise interactions
"selection"Selection feedbackToggle switches, picker selections
"success"Success notificationSuccessful actions or confirmations
"warning"Warning notificationWarning states or cautionary actions
"error"Error notificationError states or failed actions

When to Use

Use the Haptics API directly when:

  • You need to trigger haptics outside of React components
  • You're working in utility functions or service layers
  • You need to trigger haptics from animation callbacks
  • You want to avoid React hook overhead in performance-critical code

Use the useHaptics hook when:

  • You're working within a React component
  • You want memoized trigger functions for stable references
  • You need the trigger function in dependency arrays

Platform Support

  • ✅ iOS
  • ✅ Android

Haptic feedback only works on devices that support it. The API gracefully handles unsupported devices by not triggering any feedback.

  • useHaptics - React hook wrapper for haptic feedback