$ git show toggle-switch

Toggle switch

Intermediate/anim toggle-switch

Custom switch: the thumb springs across and the track changes color.

Main · New Architecture only · worklets live in react-native-worklets
transitionPropertycubicBezierCSS transitions
toggle-switch.tsx · reanimated 4
import Animated, { cubicBezier } from 'react-native-reanimated';

<Pressable onPress={() => setOn(v => !v)} accessibilityRole="switch" accessibilityState={{ checked: on }}>
  <Animated.View
    style={[styles.track, {
      backgroundColor: on ? '#0B7A93' : '#D8E0E4',
      transitionProperty: 'backgroundColor',
      transitionDuration: '200ms',
    }]}>
    <Animated.View
      style={[styles.thumb, {
        transform: [{ translateX: on ? 26 : 0 }],
        transitionProperty: 'transform',
        transitionDuration: '250ms',
        transitionTimingFunction: cubicBezier(0.3, 1.4, 0.5, 1),
      }]}
    />
  </Animated.View>
</Pressable>