Animate a toggle with Reanimated CSS transitions
This tutorial builds a settings screen with three toggles in an Expo app. When a toggle changes, its track changes color and its thumb moves 20 px, and both changes animate with the CSS transitions of react-native-reanimated 4.7.1, which runs on react-native-worklets 0.13.0. The tests check the toggles' state and the thumb's target, and the screenshot script checks on web that the thumb moves.

The example is an Expo project. Its own files are App.tsx, src/Toggle.tsx, src/Settings.tsx and __tests__/Settings.test.tsx.
The toggle
import { Pressable, StyleSheet, Text } from 'react-native';
import Animated from 'react-native-reanimated';
// The thumb is a circle that moves TRAVEL px to the right when the toggle
// is on. The track is just wide enough for it, with PADDING on each side.
const THUMB_SIZE = 24;
const PADDING = 2;
const TRAVEL = 20;
const TRACK_WIDTH = THUMB_SIZE + TRAVEL + 2 * PADDING;
const OFF_COLOR = '#c2c6cc';
const ON_COLOR = '#2563eb';
// How long each change animates, in milliseconds.
const DURATION = 300;
type Props = {
label: string;
value: boolean;
onValueChange: (value: boolean) => void;
};
export function Toggle({ label, value, onValueChange }: Props) {
return (
<Pressable
style={styles.row}
onPress={() => onValueChange(!value)}
role="switch"
aria-checked={value}
aria-label={label}
>
<Text style={styles.label}>{label}</Text>
<Animated.View
style={[
styles.track,
{
backgroundColor: value ? ON_COLOR : OFF_COLOR,
transitionProperty: 'backgroundColor',
transitionDuration: DURATION,
},
]}
>
<Animated.View
testID={`${label}-thumb`}
style={[
styles.thumb,
{
transform: [{ translateX: value ? TRAVEL : 0 }],
transitionProperty: 'transform',
transitionDuration: DURATION,
},
]}
/>
</Animated.View>
</Pressable>
);
}
const styles = StyleSheet.create({
row: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingVertical: 12,
},
label: { fontSize: 17 },
track: {
width: TRACK_WIDTH,
padding: PADDING,
borderRadius: THUMB_SIZE / 2 + PADDING,
},
thumb: {
width: THUMB_SIZE,
height: THUMB_SIZE,
borderRadius: THUMB_SIZE / 2,
backgroundColor: '#fff',
},
});Toggle is a controlled switch: it shows value, and a press calls onValueChange with the opposite value.
The track and the thumb are Animated.View components from react-native-reanimated, and their styles say where the toggle ends up for the current value:
- the track's
backgroundColorisOFF_COLOR, a gray, orON_COLOR, a blue; - the thumb's
transformis[{ translateX: value ? TRAVEL : 0 }].TRAVELis 20, and the track isTHUMB_SIZE + TRAVEL + 2 * PADDINGwide, so the thumb crosses the track.
Two more style properties turn a change of those values into an animation. transitionProperty names the property to animate, 'backgroundColor' on the track and 'transform' on the thumb, and transitionDuration sets how long the change takes: DURATION, 300 ms. When value changes, the toggle renders with the new target style, and Reanimated animates from the old value to the new one.
The row's role, aria-label and aria-checked describe it as a switch, with its name and whether it is on. The tests find each switch by its role and name and check that state, and they find the thumb by its testID.
Why there are no shared values
The toggle never starts an animation: each state change renders it with a new target style. The post Reanimated 4 is stable with CSS animations and transitions says that the CSS-style API targets animations triggered and controlled by state updates, and that before it, even a simple animation of this kind needed a shared value plus an animated style worklet. For state-driven animations, the Reanimated team recommends the CSS API. Worklets and shared values remain the recommended tools for cases such as gesture- or scroll-driven animations.
The settings screen
import { useState } from 'react';
import { StyleSheet, View } from 'react-native';
import { Toggle } from './Toggle';
type Values = {
notifications: boolean;
darkMode: boolean;
autoplayVideos: boolean;
};
export function Settings() {
// The three values live in one state object. Every toggle starts off.
const [values, setValues] = useState<Values>({
notifications: false,
darkMode: false,
autoplayVideos: false,
});
// Returns the handler for one key: it replaces that value and keeps the
// other two.
const set = (key: keyof Values) => (value: boolean) =>
setValues((current) => ({ ...current, [key]: value }));
return (
<View style={styles.list}>
<Toggle label="Notifications" value={values.notifications} onValueChange={set('notifications')} />
<Toggle label="Dark mode" value={values.darkMode} onValueChange={set('darkMode')} />
<Toggle label="Autoplay videos" value={values.autoplayVideos} onValueChange={set('autoplayVideos')} />
</View>
);
}
const styles = StyleSheet.create({
list: { paddingHorizontal: 16, paddingTop: 8 },
});Settings keeps the three values in one useState object, all false at first. set(key) returns the handler for one toggle: it replaces that key's value and keeps the other two.
import { StatusBar } from 'expo-status-bar';
import { StyleSheet } from 'react-native';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import { Settings } from './src/Settings';
export default function App() {
return (
<SafeAreaProvider>
<SafeAreaView style={styles.container}>
<Settings />
<StatusBar style="auto" />
</SafeAreaView>
</SafeAreaProvider>
);
}
const styles = StyleSheet.create({
container: { flex: 1, backgroundColor: '#fff' },
});App wraps the screen in SafeAreaProvider and SafeAreaView, from react-native-safe-area-context.

Both screenshots show the web export. The second was taken after clicking "Dark mode", then "Notifications", once nothing moved.
The tests
import { render, screen, userEvent } from '@testing-library/react-native';
import { Settings } from '../src/Settings';
test('the three switches render, all unchecked', async () => {
await render(<Settings />);
const switches = screen.getAllByRole('switch');
expect(switches).toHaveLength(3);
for (const toggle of switches) {
expect(toggle).not.toBeChecked();
}
});
test('pressing a switch checks it and leaves the others unchecked', async () => {
const user = userEvent.setup();
await render(<Settings />);
await user.press(screen.getByRole('switch', { name: 'Notifications' }));
expect(screen.getByRole('switch', { name: 'Notifications' })).toBeChecked();
expect(screen.getByRole('switch', { name: 'Dark mode' })).not.toBeChecked();
expect(screen.getByRole('switch', { name: 'Autoplay videos' })).not.toBeChecked();
});
test('pressing a switch again unchecks it', async () => {
const user = userEvent.setup();
await render(<Settings />);
const notifications = screen.getByRole('switch', { name: 'Notifications' });
await user.press(notifications);
await user.press(notifications);
expect(notifications).not.toBeChecked();
});
test("the thumb's style targets translateX 0, then 20 after a press", async () => {
const user = userEvent.setup();
await render(<Settings />);
const thumb = screen.getByTestId('Dark mode-thumb');
// These check the target of the thumb's transition, the translateX in its
// style, and not the motion between the two positions.
expect(thumb).toHaveStyle({ transform: [{ translateX: 0 }] });
await user.press(screen.getByRole('switch', { name: 'Dark mode' }));
expect(thumb).toHaveStyle({ transform: [{ translateX: 20 }] });
});The tests render Settings with React Native Testing Library and press the switches with its userEvent. Besides the jest-expo preset, the example's package.json sets one Jest option: resolver: "react-native-reanimated/jest/resolver", which Reanimated's Jest guide documents for loading the web implementation of Reanimated and Worklets.
The four tests cover these behaviors:
- the three switches render, all unchecked;
- pressing "Notifications" checks it, and only it;
- pressing it again unchecks it;
- the "Dark mode" thumb's style has
translateX: 0before the press andtranslateX: 20after it.
The last test reads the style the thumb is given, which is the transition's target: it shows that a press sets the new target, not that the thumb moves there.
The motion check
The screenshot script, not the tests, checks the motion, in the example's web export in Chromium, through Playwright. Before the second screenshot, it clicks "Dark mode" and, from the click, records the thumb's left edge on every animation frame for 600 ms. It fails unless:
- a recorded position lies more than 1 px from both the first and the last, so the thumb passed between them instead of jumping;
- the last position is 20 px, within 1 px, right of the first;
- the track's background color at the end differs from its color before the click.
Before each screenshot, the script also checks that each row's aria-checked attribute in the web page matches its thumb.
Verified with Expo SDK 58 (React Native 0.88.1): type-checked, tested with Jest and React Native Testing Library, and exported for web.
Full example: reactnative/examples/animated-toggle