Skip to content

Commit

Permalink
feat: scroll behavior with pan gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
enzomanuelmangano committed Jun 4, 2021
1 parent 8dd34fa commit e728188
Show file tree
Hide file tree
Showing 14 changed files with 6,491 additions and 0 deletions.
4 changes: 4 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/.expo-shared/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"12bb71342c6255bbf50437ec8f4441c083f47cdb74bd89160c15e4f43e52a1cb": true,
"40b842e832070c58deac6aa9e08fa459302ee3f9da492c7e77d93d2fbf4a56fd": true
}
13 changes: 13 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
node_modules/
.expo/
npm-debug.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision
*.orig.*
web-build/

# macOS
.DS_Store
73 changes: 73 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { StatusBar } from 'expo-status-bar';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {
PanGestureHandler,
PanGestureHandlerGestureEvent,
} from 'react-native-gesture-handler';
import Animated, {
cancelAnimation,
useAnimatedGestureHandler,
useDerivedValue,
useSharedValue,
withDecay,
} from 'react-native-reanimated';
import Page, { PAGE_WIDTH } from './components/Page';

const titles = ["What's", 'up', 'mobile', 'devs?'];

type ContextType = {
x: number;
};

const MAX_TRANSLATE_X = -PAGE_WIDTH * (titles.length - 1);

export default function App() {
const translateX = useSharedValue(0);

const clampedTranslateX = useDerivedValue(() => {
return Math.max(Math.min(translateX.value, 0), MAX_TRANSLATE_X);
});

const panGestureEvent = useAnimatedGestureHandler<
PanGestureHandlerGestureEvent,
ContextType
>({
onStart: (_, context) => {
context.x = clampedTranslateX.value;
cancelAnimation(translateX);
},
onActive: (event, context) => {
translateX.value = event.translationX + context.x;
},
onEnd: (event) => {
translateX.value = withDecay({ velocity: event.velocityX });
},
});

return (
<View style={styles.container}>
<PanGestureHandler onGestureEvent={panGestureEvent}>
<Animated.View style={{ flex: 1, flexDirection: 'row' }}>
{titles.map((title, index) => {
return (
<Page
key={index.toString()}
translateX={clampedTranslateX}
index={index}
title={title}
/>
);
})}
</Animated.View>
</PanGestureHandler>
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
});
32 changes: 32 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"expo": {
"name": "06-scroll-behavior-with-pan-gesture",
"slug": "06-scroll-behavior-with-pan-gesture",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = function(api) {
api.cache(true);
return {
presets: ['babel-preset-expo'],
plugins: ['react-native-reanimated/plugin']
};
};
51 changes: 51 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/components/Page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import React from 'react';
import { Dimensions, StyleSheet, Text, View } from 'react-native';
import Animated, { useAnimatedStyle } from 'react-native-reanimated';

interface PageProps {
index: number;
title: string;
translateX: Animated.SharedValue<number>;
}

const { width: PAGE_WIDTH } = Dimensions.get('window');

const Page: React.FC<PageProps> = ({ index, title, translateX }) => {
const pageOffset = PAGE_WIDTH * index;

const rStyle = useAnimatedStyle(() => {
return {
transform: [{ translateX: translateX.value + pageOffset }],
};
});

return (
<Animated.View
style={[
{
...StyleSheet.absoluteFillObject,
backgroundColor: `rgba(0,0,256,0.${index + 2})`,
alignItems: 'center',
justifyContent: 'center',
},
rStyle,
]}
>
<Text
style={{
fontSize: 70,
fontWeight: '700',
letterSpacing: 1.5,
textTransform: 'uppercase',
}}
>
{title}
</Text>
</Animated.View>
);
};

export { PAGE_WIDTH };
export default Page;

const styles = StyleSheet.create({});
27 changes: 27 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"expo": "~41.0.1",
"expo-status-bar": "~1.0.4",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-41.0.0.tar.gz",
"react-native-gesture-handler": "^1.10.3",
"react-native-reanimated": "^2.2.0",
"react-native-web": "~0.13.12"
},
"devDependencies": {
"@babel/core": "^7.9.0",
"@types/react": "~16.9.35",
"@types/react-native": "~0.63.2",
"typescript": "~4.0.0"
},
"private": true
}
6 changes: 6 additions & 0 deletions 06-scroll-behavior-with-pan-gesture/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "expo/tsconfig.base",
"compilerOptions": {
"strict": true
}
}
Loading

0 comments on commit e728188

Please sign in to comment.