Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-native-page-indicator

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-native-page-indicator

React Native component to display the current page of a swiper, slideshow, carousel etc.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

React Native Page Indicator

React Native component to display the current page of a swiper, slideshow, carousel etc. The package has zero dependencies and uses React Native's Animated API with native driver to produce silky-smooth animations.

HorizontalVertical

Installation

yarn add react-native-page-indicator
# or
npm install react-native-page-indicator

Basic example

Pass the total number of pages in the count prop and the current page index in 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

For a more appealing experience when the indicator directly responds to scroll position, you should pass the current page index of Animated.Value type to animatedCurrent prop. The simplest way to obtain the value is to divide 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;

  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>
      <PageIndicator
        style={styles.pageIndicator}
        count={pages.length}
        animatedCurrent={Animated.divide(scrollX, width)}
      />
    </View>
  );
};

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

export default App;

Props

PropTypeDefaultDescription
countnumberThe total number of pages (required)
currentnumber0The current page index
colorstringblackColor of the indicators
sizenumber6Size of the indicators
gapnumber6Distance between the indicators
opacitynumber0.6Opacity of inactive indicators
strokenumbersize * 4Length of the active indicator stroke
borderRadiusnumbersize / 2Border radius of the indicators
verticalbooleanfalseWhen true the indicators will be stacked vertically
durationnumber500Duration of the animation (has no effect when animatedCurrent provided)
easingEasingFunctionEasing.out(Easing.cubic)Easing function, see available options
animatedCurrentAnimated.ValueAnimated current page index
styleViewStyleStyle object applied to the wrapping View

License

MIT

Keywords

FAQs

Package last updated on 21 Apr 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc