SlideIn
Smooth slide animation from any direction with configurable duration.
9:41
signal_cellular_altwifibattery_full
SlideIn
Preview Controls
Animation
Slides in from left with spring
This is a CSS-based preview of the animation. The actual React Native component uses physics-based springs for a more natural feel.
Installation
$
Usage
Example.tsx
import { SlideIn } from './components/SlideIn';export default function App() {return (<SlideIn direction="right" delay={200}><Card title="Animated Card" /></SlideIn>);}
Full Component Code
SlideIn.tsx
1import React, { useEffect } from 'react';2import { ViewStyle, StyleProp, Dimensions } from 'react-native';3import Animated, {4 useSharedValue,5 useAnimatedStyle,6 withTiming,7 withDelay,8 Easing,9} from 'react-native-reanimated';1011type Direction = 'left' | 'right' | 'top' | 'bottom';1213interface SlideInProps {14 children: React.ReactNode;15 direction?: Direction;16 distance?: number;17 delay?: number;18 duration?: number;19 style?: StyleProp<ViewStyle>;20}2122const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get('window');2324export const SlideIn: React.FC<SlideInProps> = ({25 children,26 direction = 'left',27 distance,28 delay = 0,29 duration = 500,30 style,31}) => {32 const getInitialOffset = () => {33 const defaultDistance = direction === 'left' || direction === 'right'34 ? SCREEN_WIDTH35 : SCREEN_HEIGHT;36 const dist = distance ?? defaultDistance * 0.3;3738 switch (direction) {39 case 'left': return { x: -dist, y: 0 };40 case 'right': return { x: dist, y: 0 };41 case 'top': return { x: 0, y: -dist };42 case 'bottom': return { x: 0, y: dist };43 }44 };4546 const initialOffset = getInitialOffset();47 const translateX = useSharedValue(initialOffset.x);48 const translateY = useSharedValue(initialOffset.y);49 const opacity = useSharedValue(0);5051 useEffect(() => {52 const timingConfig = {53 duration,54 easing: Easing.out(Easing.ease),55 };5657 translateX.value = withDelay(delay, withTiming(0, timingConfig));58 translateY.value = withDelay(delay, withTiming(0, timingConfig));59 opacity.value = withDelay(delay, withTiming(1, { duration }));60 }, []);6162 const animatedStyle = useAnimatedStyle(() => ({63 transform: [64 { translateX: translateX.value },65 { translateY: translateY.value },66 ],67 opacity: opacity.value,68 }));6970 return (71 <Animated.View style={[style, animatedStyle]}>72 {children}73 </Animated.View>74 );75};7677export default SlideIn;
Props
| Name | Type | Default | Description |
|---|---|---|---|
| children | React.ReactNode | - | Content to animate |
| direction | 'left' | 'right' | 'top' | 'bottom' | 'left' | Direction to slide from |
| distance | number | 30% of screen | Distance to slide |
| delay | number | 0 | Delay before animation |
| duration | number | 500 | Animation duration in ms |
