npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

carousel-with-pagination-rn

v1.1.8

Published

Custom carousel with pagination for React-Native

Downloads

327

Readme

version npmtrends

carousel giph

Performant and accessible Carousel component for React-Native that can be used with images or nested components. Built on top of the pure FlatList component with additional styling and added animations. The pagination indicators allow user to navigate to a specific element of the carousel with animated indicator transition. All elements within the carousel are accessible for the user using the VoiceOverText feature of their device (iOS and Android)

Nothing special, but might be useful 😶‍🌫️

🤳 Showcase:

basic showcase

🚀 Installation:

Install latest carousel-with-pagination-rn :

yarn:

yarn add carousel-with-pagination-rn

npm:

npm install carousel-with-pagination-rn

🎮 Usage:

Import the default component:

import CustomCarousel from 'carousel-with-pagination-rn';

Pass the required props data and renderItem:

const YourScreen = () => {
    return(
        <CustomCarousel
          data={dummyCarousel}
          renderItem={({item}) => <YourItem item={item} />}
        />
    )
}

Plug & Play example:

import CustomCarousel from 'carousel-with-pagination-rn';

const dummyData = [
  {
    id: 1,
    img: 'https://picsum.photos/400/600?random=1',
    title: 'Element 1',
    description: 'Pressable and animated pagination',
    price: 'Fast',
  },
  {
    id: 2,
    img: 'https://picsum.photos/400/600?random=2',
    title: 'Element 2',
    description: 'Full customization for carousel',
    price: 'Simple',
  },
  {
    id: 3,
    img: 'https://picsum.photos/400/600?random=3',
    title: 'Element 3',
    description: 'Accessible for VoiceOver',
    price: 'Efficient',
  },
];

const {width} = Dimensions.get('screen');

const YourScreen = () => {
  return (
    <SafeAreaView style={{flex: 1}}>
      <View
        style={{
          justifyContent: 'center',
          alignItems: 'center',
        }}
      >
        <CustomCarousel
          data={dummyData}
          renderItem={({item}) => {
            return (
              <View style={styles.container}>
                <Image
                  source={{uri: item.img}}
                  style={styles.image}
                  resizeMode='contain'
                />

                <View style={styles.content}>
                  <Text style={styles.title}>{item.title}</Text>
                  <Text style={styles.description}>{item.description}</Text>
                  <Text style={styles.extraDesc}>{item.price}</Text>
                </View>
              </View>
            );
          }}
        />
      </View>
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    width,
    height: 700,
    alignItems: 'center',
  },
  image: {
    flex: 0.8,
    width: '100%',
  },
  content: {
    flex: 0.4,
    alignItems: 'center',
  },
  title: {
    fontSize: 24,
    fontWeight: 'bold',
    color: '#333',
  },
  description: {
    fontSize: 18,
    marginVertical: 12,
    color: '#333',
  },
  extraDesc: {
    fontSize: 32,
    fontWeight: 'bold',
  },
});

export default YourScreen;

🥷 Properties:

| Prop | Description | Default | Type | | -------------- | ----------- | ------- | ---- | | data ❗Required | Used to pass in array of elements that should be rendere. The same as data prop in FlatList component | N/A | Array<any> | | renderItem ❗Required | Takes an item from data props and renders it as required. The same as renderItem prop in FlatList component | N/A | ListRenderItem<any> | | mainContainerStyle | Used to apply styling to the main container that wraps both: carousel and pagination | N/A | StyleProp<ViewStyle> | | carouselContainerStyle | Equivalent to style prop in FlatList and has the same affect to the Carousel | N/A | StyleProp<ViewStyle> | | carouselContentContainerStyle | Equivalent to contentContainerStyle prop in FlatList and has the same affect to the Carousel | N/A | StyleProp<ViewStyle> | | paginationContainerStyle | Used to apply styling to the container that wraps the pagination | N/A | StyleProp<ViewStyle> | | widthBoundaryForPagination | Is used to adjust inputRange of interpolated animations of pagination. Has to match to the width of renderItem. If not set, the device screen width will be used.| Dimensions.get('window').width | number | | indicatorWidth | Is used to adjust dynamic width of each pagination indicator. Has to be an array of 3 numbers where the second element of array will be assigned to the height of the active indicator. Example: indicatorWidth={[15,30,15]}. If static width is required, pass an array of three equal numbers as: [10,10,10] | [20,40,20] | Array<number> | | indicatorHeight | Is used to adjust dynamic height of each pagination indicator. Has to be an array of 3 numbers where the second element of array will be assigned to the width of the active indicator. Example: indicatorHeight={[15,30,15]}. If static width is required, pass an array of three equal numbers as: [10,10,10] | [15,15,15] | Array<number> | | indicatorColor | Is used to adjust dynamic color of each pagination indicator. Has to be an array of 3 strings/colors where the second element of array will be assigned to color of the active indicator. Example: indicatorColor={['grey', 'black', 'grey']}. If static width is required, pass an array of three equal numbers as: ['grey', 'grey', 'grey'] | ['grey', 'black', 'grey'] | Array<string> | | inidicatorBorderRadius | Is used to adjust borderRadius of each pagination indicator | 5 | number | | indicatorHorizontalPadding | Is used to adjust horizontalPadding of each pagination indicator | 10 | number | | paginataionBackgroundColor | Is used to change backgroundColor of each pagination container | transparent | string | | isEndReached | Will be called once the last element of the Carousel is displayed | (endReached: boolean) => void |

❗❗ It is important to pass a value to prop widthBoundaryForPagination if parent container within renderItem is not taking the full width of the screen. These values have to be the same.

⚙️ Methods attached to the Ref:

| Method | Description | Type | | -------------- | ----------- | ---- | | ref | Can be used to access two methods: showNextItem() & showPreviousItem(). These methods then can be used to access behavior of the carousel and define it manually i.e. move to the next item in the carousel or move to the previous one. The Example below should provide a bit more information on the use and the implementation of these methods. |RefProps |

Explanation of use: This feature was added in v1.1.5 following a feature request on the GitHub repository. Now you can access the ref for CustomCarousel component, assign it as custom ref within your component using useRef and trigger two methods: showNextItem() AND showPreviousItem() to scroll trhough the Carousel using your custom component ( arrows as an example ).

Showcase:

Example:

const CarouselScreen = () => {
  let carouselRef = useRef<RefProps>(null); //RefProps interface can be imported from the library

  const handleNextClick = () => {
    carouselRef.current?.showNextItem(); //will scroll to the next item in carousel
  };
  const handlePreviousClick = () => {
    carouselRef.current?.showPreviousItem(); //will scroll to the previous item in carousel
  };
  return (
    <SafeAreaView style={{flex: 1}}>
      <View
        style={{
          justifyContent: 'center',
          alignItems: 'center',
        }}
      >
        <CustomCarousel
          ref={carouselRef} //access and assign the reference for the further use and manipulation
          data={dummyData}
          renderItem={renderItem} //Copy the renderItem from the 'Plug&Play' example or use your own renderItem.
          disablePagination={true} //used to hide the pagination of the carousel
        />
        <View
          style={{
            flexDirection: 'row',
            width: 100,
            justifyContent: 'space-between',
          }}
        >
          <TouchableOpacity onPress={handlePreviousClick}>
            <Text>Previous</Text>
          </TouchableOpacity>
          <TouchableOpacity onPress={handleNextClick}>
            <Text>Next</Text>
          </TouchableOpacity>
        </View>
      </View>
    </SafeAreaView>
  );
};

🪪 License & Sources: