A lightweight and customizable React Native component for creating before and after image sliders using react-native-reanimated and react-native-gesture-handler.
- Smooth and performant animations using Reanimated 2
- Highly customizable with styled-components
- Easy to integrate into any React Native project
npm install react-native-reanimated react-native-gesture-handler styled-componentsnpm install react-native-before-after-slideryarn add react-native-reanimated react-native-gesture-handler styled-componentsyarn add react-native-before-after-sliderMake sure to follow the setup instructions for react-native-reanimated and react-native-gesture-handler if you haven't already.
- Add Reanimated's Babel plugin to your
babel.config.js:
module.exports = {
presets: ["module:metro-react-native-babel-preset"],
plugins: ["react-native-reanimated/plugin"],
};- Wrap your app with Reanimated's GestureHandlerRootView:
import "react-native-gesture-handler";
import React from "react";
import { GestureHandlerRootView } from "react-native-gesture-handler";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { enableScreens } from "react-native-screens";
import MainScreen from "./screens/MainScreen";
enableScreens();
const Stack = createStackNavigator();
export default function App() {
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Main" component={MainScreen} />
</Stack.Navigator>
</NavigationContainer>
</GestureHandlerRootView>
);
}Here is a basic example of how to use the BeforeAfterSlider component:
import React from "react";
import { View } from "react-native";
import { BeforeAfterSlider } from "react-native-before-after-slider";
const beforeImage = require("./path-to-before-image.jpg");
const afterImage = require("./path-to-after-image.jpg");
const delimiterIcon = require("./path-to-icon.png");
const App = () => {
return (
<View style={{ flex: 1 }}>
<BeforeAfterSlider
beforeImage={beforeImage}
afterImage={afterImage}
handleWidth={30}
handleColor="blue"
delimiterIcon={delimiterIcon}
delimiterIconStyles={{ width: 30, height: 30, tintColor: "red" }} // Customize the styles here
/>
</View>
);
};
export default App;| Prop | Type | Default | Description |
|---|---|---|---|
beforeImage |
ImageSourcePropType |
Required | The source for the before image. |
afterImage |
ImageSourcePropType |
Required | The source for the after image. |
handleWidth |
number |
20 | The width of the slider handle. |
handleColor |
string |
'white' | The color of the slider handle. |
delimiterIcon |
ImageSourcePropType |
Optional | The source for the delimiter icon inside the handle. |
delimiterIconStyles |
object |
{} | Styles for the delimiter icon. |
Contributions are welcome! Please open an issue or submit a pull request to discuss any changes.
This project is licensed under the MIT License.