React Native course · Module 10: Animations

expo-haptics - Vibrations

2 min read ·

Add haptic feedback to interactions!

npx expo install expo-haptics
import * as Haptics from 'expo-haptics';

// Different vibration types
const lightVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
const mediumVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
const heavyVibration = () => Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Heavy);

// Notifications
const success = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Success);
const warning = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Warning);
const error = () => Haptics.notificationAsync(Haptics.NotificationFeedbackType.Error);

// Selection
const selection = () => Haptics.selectionAsync();

// Usage with gesture
const panGesture = Gesture.Pan()
  .onStart(() => {
    runOnJS(Haptics.selectionAsync)();
  });

Project: Animated card

import Animated, { useSharedValue, useAnimatedStyle, withSpring, withTiming, interpolate } from 'react-native-reanimated';
import { Gesture, GestureDetector } from 'react-native-gesture-handler';
import * as Haptics from 'expo-haptics';

function FlipCard({ front, back }: { front: ReactNode; back: ReactNode }) {
  const rotation = useSharedValue(0);
  const isFlipped = useSharedValue(false);

  const flip = () => {
    Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
    rotation.value = withSpring(isFlipped.value ? 0 : 180);
    isFlipped.value = !isFlipped.value;
  };

  const frontStyle = useAnimatedStyle(() => ({
    transform: [{ rotateY: `${rotation.value}deg` }],
    opacity: interpolate(rotation.value, [0, 90, 180], [1, 0, 0]),
  }));

  const backStyle = useAnimatedStyle(() => ({
    transform: [{ rotateY: `${rotation.value + 180}deg` }],
    opacity: interpolate(rotation.value, [0, 90, 180], [0, 0, 1]),
  }));

  return (
    <Pressable onPress={flip}>
      <Animated.View style={[styles.card, frontStyle]}>{front}</Animated.View>
      <Animated.View style={[styles.card, styles.cardBack, backStyle]}>{back}</Animated.View>
    </Pressable>
  );
}
Code for this lesson: App.tsx
import React from 'react';
import { View, Text, Pressable, StyleSheet } from 'react-native';
import Animated, {
  useSharedValue,
  useAnimatedStyle,
  withSpring,
  interpolate,
} from 'react-native-reanimated';
import * as Haptics from 'expo-haptics';

export default function App() {
  const rotation = useSharedValue(0);
  const isFlipped = useSharedValue(false);

  const flip = () => {
    // TODO: trigger a vibration: Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium);
    // TODO: rotate the card: rotation.value = withSpring(isFlipped.value ? 0 : 180);
    // TODO: toggle the flag: isFlipped.value = !isFlipped.value;

  };

  const frontStyle = useAnimatedStyle(() => ({
    // TODO: rotate the front of the card around the Y axis by rotation.value degrees:
    //       transform: [{ rotateY: `${rotation.value}deg` }],
    // TODO: hide the front during the flip using interpolate:
    //       opacity: interpolate(rotation.value, [0, 90, 180], [1, 0, 0]),
  }));

  const backStyle = useAnimatedStyle(() => ({
    // TODO: the back is rotated by an extra 180 degrees:
    //       transform: [{ rotateY: `${rotation.value + 180}deg` }],
    // TODO: show the back only after the halfway point:
    //       opacity: interpolate(rotation.value, [0, 90, 180], [0, 0, 1]),
  }));

  return (
    <View style={styles.container}>
      <Pressable onPress={flip}>
        <Animated.View style={[styles.card, frontStyle]}>
          <Text style={styles.cardText}>Front</Text>
        </Animated.View>
        <Animated.View style={[styles.card, styles.cardBack, backStyle]}>
          <Text style={styles.cardText}>Back</Text>
        </Animated.View>
      </Pressable>
      <Text style={styles.hint}>Tap to flip</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#1a5276',
  },
  card: {
    width: 200,
    height: 280,
    borderRadius: 16,
    backgroundColor: '#00d4ff',
    justifyContent: 'center',
    alignItems: 'center',
    backfaceVisibility: 'hidden',
  },
  cardBack: {
    position: 'absolute',
    backgroundColor: '#16a085',
  },
  cardText: {
    color: '#0a2540',
    fontSize: 22,
    fontWeight: '700',
  },
  hint: {
    marginTop: 24,
    color: '#aed6f1',
    fontSize: 14,
  },
});

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. How do you install expo-haptics?

  2. 2. Which impact styles are available in Haptics?

These are 2 of 4 questions for this lesson. Solve the rest in the game.

Hands-on tasks in the game

  • Code editor

    Complete the FlipCard code.

  1. QuestionInstalling Haptics
  2. QuestionImpactFeedbackStyle
  3. QuestionnotificationAsync
  4. QuestionselectionAsync
  5. Coding taskFlip Card
  6. Project to submitProject: Animations
  7. VideoModule summary

Useful articles