Skip to content

Commit

Permalink
Merge pull request #525 from oliverloops/main
Browse files Browse the repository at this point in the history
New base sample added and Readme update
  • Loading branch information
oliverloops authored Jan 8, 2024
2 parents 2966e4c + 3ac1c5c commit 2870b76
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Check out [the documentation website](https://reanimated-carousel.dev).
| <a href="./example/app/src/pages/parallax/index.tsx">parallax-vertical</a> | <a href="./example/app/src/pages/stack/index.tsx">stack-horizontal-left</a> | <a href="./example/app/src/pages/stack/index.tsx">stack-horizontal-right</a> |
| <img src="assets/stack-vertical-left.gif"/> | <img src="assets/stack-vertical-right.gif"/> | <img src="assets/stack-horizontal-right.gif"/> |
| <a href="./example/app/src/pages/stack/index.tsx">stack-vertical-left</a> | <a href="./example/app/src/pages/stack/index.tsx">stack-vertical-right</a> | <a href="./example/app/src/pages/stack/index.tsx">stack-horizontal-right</a> |
| <img src="assets/left-align.gif"/> | <img src="assets/right-align.gif" > |
| <a href="./example/app/src/pages/left-align/index.tsx">left-align</a> | <a href="./example/app/src/pages/right-align/index.tsx">right-align</a> |
| <img src="assets/left-align.gif"/> | | <img src="assets/right-align.gif" > |
| <a href="./example/app/src/pages/left-align/index.tsx">left-align</a> | | <a href="./example/app/src/pages/right-align/index.tsx">right-align</a> |

> You can make cool animations with custom animation API [[Details]](https://reanimated-carousel.dev/custom-animations)
Expand Down
5 changes: 5 additions & 0 deletions example/app/src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import StackComponent from "./pages/stack";
import StackCards from "./pages/stack-cards";
import Tear from "./pages/tear";
import QuickSwipe from "./pages/quick-swipe";
import Material3 from "./pages/material-3";
import { useWebContext } from "./store/WebProvider";
import { convertName } from "./utils/helpers";
import { useColor } from "./hooks/useColor";
Expand Down Expand Up @@ -79,6 +80,10 @@ export const CustomAnimations = [
name: 'blur-rotate',
page: BlurRotate,
},
{
name: 'material-3',
page: Material3
},
{
name: "curve",
page: Curve,
Expand Down
142 changes: 142 additions & 0 deletions example/app/src/pages/material-3/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import * as React from "react";
import type { ICarouselInstance } from "react-native-reanimated-carousel";
import Carousel from "react-native-reanimated-carousel";
import { SafeAreaView } from "react-native-safe-area-context";
import { getImages } from "../../utils/get-images";

import SButton from "../../components/SButton";
import { ElementsText, window } from "../../constants";
import { useWindowDimensions, View, FlatList, StyleSheet } from "react-native";
import Animated, { Extrapolation, interpolate, useSharedValue, useAnimatedStyle } from "react-native-reanimated";

const PAGE_WIDTH = window.width;
const LARGE_IMAGE_WIDTH = PAGE_WIDTH * 0.5;
const MEDIUM_IMAGE_WIDTH = LARGE_IMAGE_WIDTH * 0.5;
const SMALL_IMAGE_WIDTH = MEDIUM_IMAGE_WIDTH * 0.5;
const COUNT = 3;

const data = getImages();

function Index() {
const windowWidth = useWindowDimensions().width;
const [isAutoPlay, setIsAutoPlay] = React.useState(false);
const ref = React.useRef<ICarouselInstance>(null);

const baseOptions = {
vertical: false,
width: windowWidth * 0.45,
height: PAGE_WIDTH / 1.5,
style: {
width: PAGE_WIDTH,
}
} as const;

//* Custom animation
const scrollX = useSharedValue<number>(0);

const onScroll = (offsetProgress: number) => {
scrollX.value = offsetProgress * -1;
};

return (
<SafeAreaView edges={["bottom"]} style={{ flex: 1 }}>
{/* <FlatList
horizontal
style={{ width: "100%" }}
data={data}
onScroll={onScroll}
scrollEventThrottle={16}
showHorizontalScrollIndicator={false}
renderItem={({ index, item }: any) => (
<Item
id={index}
img={item}
scrollX={scrollX}
/>
)}
/> */}
<Carousel
{...baseOptions}
loop
enabled // Default is true, just for demo
ref={ref}
autoPlay={isAutoPlay}
data={data}
onProgressChange={onScroll}
renderItem={({ index, item }: any) => (
<Item
id={index}
img={item}
scrollX={scrollX}
/>
)}
/>
<View style={{ flex: 1 }}>
<SButton
onPress={() => {
setIsAutoPlay(!isAutoPlay);
}}
>
{ElementsText.AUTOPLAY}:{`${isAutoPlay}`}
</SButton>
<SButton
onPress={() => {
ref.current?.scrollTo({ count: -1, animated: true });
}}
>
prev
</SButton>
<SButton
onPress={() => {
ref.current?.scrollTo({ count: 1, animated: true });
}}
>
next
</SButton>
</View>
</SafeAreaView>
);
}

const Item = ({ id, img, scrollX }: { id: number, img: ImageSourcePropType, scrollX: any }) => {
const inputRange = [
(id - 2) * SMALL_IMAGE_WIDTH,
(id - 1) * SMALL_IMAGE_WIDTH,
id * SMALL_IMAGE_WIDTH,
(id + 1) * SMALL_IMAGE_WIDTH
];

const outputRange = [
SMALL_IMAGE_WIDTH,
MEDIUM_IMAGE_WIDTH,
LARGE_IMAGE_WIDTH,
SMALL_IMAGE_WIDTH
];

const animatedStyle = useAnimatedStyle(() => ({
width: interpolate(scrollX.value, inputRange, outputRange, Extrapolation.CLAMP)
}));

return (
<View style={styles.container}>
<Animated.Image
source={img}
style={[styles.image, animatedStyle]}
/>
</View>
);
};

const styles = StyleSheet.create({
container: {
marginVertical: 10,
},
image: {
width: PAGE_WIDTH,
height: 250,
borderRadius: 20,
marginRight: 10
}
});

export default Index;
2 changes: 1 addition & 1 deletion example/website/theme.config.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const config: DocsThemeConfig = {
},
docsRepositoryBase: "https://github.com/shuding/nextra-docs-template",
footer: {
text: "Copyright © 2023 Caspian. Built with Nextra.",
text: "Copyright © 2024 Caspian. Built with Nextra.",
},
sidebar: {
defaultMenuCollapseLevel: 1,
Expand Down

0 comments on commit 2870b76

Please sign in to comment.