A bottom sheet that slides up rarely does only that: the backdrop darkens, a chevron turns and the header changes color at the same time. Running a separate animation for each style means keeping four timings in step by hand. With interpolate() you animate one value and derive every style from it, so a single Animated.timing call moves all of them together.

Key terms

  • interpolate(): a method on Animated.Value that takes a config object and returns a new animated node computed from that value.
  • AnimatedInterpolation: the node interpolate() returns, which goes into a style like an animated value and has its own interpolate() method for chaining.
  • inputRange and outputRange: the two arrays of an interpolation config, pairing each input number with the output it maps to.
  • Extrapolation: what an interpolation does with inputs outside inputRange, set with extrapolate, extrapolateLeft or extrapolateRight.

Two ranges and a straight line

An interpolation config needs two arrays of the same length: inputRange, the numbers the source value passes through, and outputRange, what each of those numbers maps to. Between two neighboring entries the output follows a straight line, because the default easing is Easing.linear. For an input inside a segment, the output is:

outputMin + (input - inputMin) * (outputMax - outputMin) / (inputMax - inputMin)

With inputRange: [0, 1] and outputRange: [240, 0], a progress of 0.25 gives a translateY of 180, a quarter of the way from 240 down to 0. Add an easing function to the config and the normalized ratio goes through it before being mapped back onto the output segment.

inputRange needs at least two entries and must never decrease. In development, breaking either rule, or giving the two arrays different lengths, throws an invariant error as soon as interpolate() is called.

Several segments in one range

Add more entries and the range splits into segments, each with its own line. For an input, the interpolation picks the segment whose upper bound is the first inputRange entry at or above it. An input below the first entry uses the first segment, and one above the last entry uses the last segment.

A segment whose two outputs are equal returns that output for every input that lands in it, which gives you a flat stretch, often called a dead zone. The sheet's backdrop uses one:

inputRange: [0, 0.6, 1], outputRange: [0, 0.5, 0.5]

The backdrop reaches half opacity over the first 60% of the slide, then holds at 0.5 while the sheet finishes moving.

Inputs outside the range

Source values often leave the range you planned for. A scroll offset keeps growing after the header you mapped it to has collapsed. With no option set, both sides default to 'extend', and the outermost segment's line keeps going. Map a scroll offset with inputRange: [0, 100] and outputRange: [1, 0], and an offset of 150 produces -0.5, a negative opacity.

The three modes:

  • 'extend' keeps the slope of the outermost segment, so the output keeps changing at the same rate.
  • 'clamp' replaces the input with the range's edge, so the output stops at the first or last outputRange value. The scroll example stays at 0.
  • 'identity' ignores outputRange and returns the raw input. The scroll example gives 150.

extrapolate sets both sides. extrapolateLeft and extrapolateRight set one side each and win over extrapolate, which only fills a side that has no option of its own. {extrapolate: 'clamp', extrapolateLeft: 'extend'} clamps above the range and extends below it.

Degrees, colors and other strings

outputRange can hold strings as well as numbers. The interpolation extracts the numbers from each output string, interpolates them, and rebuilds a string of the same shape, so ['0deg', '180deg'] gives '90deg' halfway through.

Colors take a separate path. Each output string is first checked as a color, and recognized colors are interpolated channel by channel. The output is always an rgba() string with red, green and blue rounded to integers and alpha rounded to three decimals, whatever color format you wrote.

In development, invariants hold string ranges to a common shape:

  • Every output is a color, or none is.
  • Every output string has the same number of numeric parts and the same text around them, so '0deg' and '3.14rad' can't share a range.
  • The input side is always numeric. Interpolating an input that is not a number fails.

One value behind four styles

The FilterSheet component below the lesson keeps a single progress value from useAnimatedValue(0) and derives four styles from it. The sheet slides with outputRange: [240, 0], the backdrop uses the dead zone from earlier, and the chevron rotates with outputRange: ['0deg', '180deg']. The header color runs between two rgba() strings:

outputRange: ['rgba(240, 240, 240, 1)', 'rgba(30, 58, 138, 1)']

Tapping the header starts one Animated.timing from 0 to 1 or back, and all four styles follow it. Each of these styles can run on the native driver, which is why the timing sets useNativeDriver: true.

Code Examples

tsx
import {useState} from 'react';
import {Animated, Pressable, StyleSheet, Text, View, useAnimatedValue} from 'react-native';

export function FilterSheet() {
  const progress = useAnimatedValue(0);
  const [open, setOpen] = useState(false);

  function toggle() {
    Animated.timing(progress, {
      toValue: open ? 0 : 1,
      duration: 300,
      useNativeDriver: true,
    }).start();
    setOpen(!open);
  }

  const sheetY = progress.interpolate({
    inputRange: [0, 1],
    outputRange: [240, 0],
  });
  const backdropOpacity = progress.interpolate({
    inputRange: [0, 0.6, 1],
    outputRange: [0, 0.5, 0.5],
  });
  const chevronRotate = progress.interpolate({
    inputRange: [0, 1],
    outputRange: ['0deg', '180deg'],
  });
  const headerColor = progress.interpolate({
    inputRange: [0, 1],
    outputRange: ['rgba(240, 240, 240, 1)', 'rgba(30, 58, 138, 1)'],
  });

  return (
    <View style={styles.screen}>
      <Animated.View style={[styles.backdrop, {opacity: backdropOpacity}]} />
      <Animated.View style={[styles.sheet, {transform: [{translateY: sheetY}]}]}>
        <Pressable onPress={toggle}>
          <Animated.View style={[styles.header, {backgroundColor: headerColor}]}>
            <Text style={styles.title}>Filters</Text>
            <Animated.Text style={{transform: [{rotate: chevronRotate}]}}>
              ^
            </Animated.Text>
          </Animated.View>
        </Pressable>
        <Text style={styles.option}>Price: low to high</Text>
        <Text style={styles.option}>In stock only</Text>
      </Animated.View>
    </View>
  );
}

const styles = StyleSheet.create({
  screen: {flex: 1, justifyContent: 'flex-end'},
  backdrop: {position: 'absolute', top: 0, right: 0, bottom: 0, left: 0, backgroundColor: 'black'},
  sheet: {height: 300, backgroundColor: 'white'},
  header: {flexDirection: 'row', justifyContent: 'space-between', padding: 16},
  title: {fontSize: 18, fontWeight: '600'},
  option: {padding: 16},
});
✓ Completed