2.3.0 • Published 2 months ago

react-native-page-indicator v2.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

React Native Page Indicator

React Native component designed to display the current page of a swiper, slideshow, carousel, and more. You can choose from three pre-defined design variants and further customize the look and feel according to your specific requirements. All design variants support both horizontal and vertical orientations. This package has no dependencies and utilizes React Native's Animated API with the native driver to achieve seamless and fluid animations.

HorizontalVertical

Design Variants

MorseBeadsTrain

Installation

yarn

yarn add react-native-page-indicator

npm

npm install react-native-page-indicator

Basic example

Pass the total number of pages as the count prop and the current page index as the current prop.

import React from 'react';
import { View, StyleSheet } from 'react-native';
import { PageIndicator } from 'react-native-page-indicator';

const MyComponent = ({ pages, current }) => (
  <View style={styles.wrapper}>
    <PageIndicator count={pages} current={current} />
  </View>
);

const styles = StyleSheet.create({
  wrapper: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default MyComponent;

Advanced example

To create a more engaging experience where the indicator dynamically responds to the scroll position, you can pass the current page index as an Animated.Value rather than a regular number. The easiest approach to obtain the animated value is by dividing the corresponding scroll position by the page width (for horizontal scrolling) or page height (for vertical scrolling).

import React, { useRef } from 'react';
import { Animated, StyleSheet, Text, View, useWindowDimensions } from 'react-native';
import { PageIndicator } from 'react-native-page-indicator';

const pages = ['Page 1', 'Page 2', 'Page 3'];

const App = () => {
  const { width, height } = useWindowDimensions();
  const scrollX = useRef(new Animated.Value(0)).current;
  const animatedCurrent = useRef(Animated.divide(scrollX, width)).current;

  return (
    <View style={styles.root}>
      <Animated.ScrollView
        horizontal={true}
        pagingEnabled={true}
        showsHorizontalScrollIndicator={false}
        onScroll={Animated.event([{ nativeEvent: { contentOffset: { x: scrollX } } }], {
          useNativeDriver: true,
        })}
      >
        {pages.map((page, index) => (
          <View key={index} style={[styles.page, { width, height }]}>
            <Text>{page}</Text>
          </View>
        ))}
      </Animated.ScrollView>
      <View style={styles.pageIndicator}>
        <PageIndicator count={pages.length} current={animatedCurrent} />
      </View>
    </View>
  );
};

const styles = StyleSheet.create({
  root: {
    flex: 1,
  },
  page: {
    alignItems: 'center',
    justifyContent: 'center',
  },
  pageIndicator: {
    left: 20,
    right: 20,
    bottom: 50,
    position: 'absolute',
    alignItems: 'center',
    justifyContent: 'center',
  },
});

export default App;

Common Props

PropTypeDefaultDescription
countnumberThe total number of pages (required)
currentnumber | Animated.Value0The current page index can be either a number or an animated value obtained from the scroll position
variant'morse' | 'beads' | 'train'morsePre-defined design variant
verticalbooleanfalseWhen true the indicators will be stacked vertically
colorstringblackColor of the indicators
activeColorstringOptional color of the active indicator
gapnumber6Distance between the indicators
opacitynumber0.5Opacity of the inactive indicators
borderRadiusnumbersize / 2Border radius of the indicators
durationnumber500Animation duration (has no effect when Animated.Value is provided for the current prop)
easingEasingFunctionEasing.out()Animation easing function (has no effect when Animated.Value is provided for the current prop)
styleViewStyleStyle object applied to the wrapping view

Morse Variant Props

PropTypeDefaultDescription
sizenumber6Size of the inactive indicators and the thickness of the active indicator
dashSizenumbersize * 4Length of the active indicator, cannot be smaller than size * 2

Train Variant Props

PropTypeDefaultDescription
sizenumber6Thickness of the indicators
dashSizenumbersize * 4Length of the indicators, setting it to 0 will stretch the indicators to the available width

Beads Variant Props

PropTypeDefaultDescription
sizenumber6Size of the indicators
scalenumber1.5Scaling factor of the active indicator

Feedback

I appreciate your feedback, so please star the repository if you like it. This is the best motivation for me to maintain the package and add new features. If you have any feature requests, found a bug, or have ideas for improvement, feel free to open an issue.

License

Licensed under the MIT license.

2.3.0

2 months ago

2.2.1

4 months ago

2.2.0

8 months ago

1.1.0

11 months ago

2.1.0

8 months ago

2.0.1

10 months ago

2.0.0

11 months ago

1.0.0

1 year ago