0.1.6 • Published 6 years ago

react-native-scroll-up v0.1.6

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

react-native-scroll-up

A React Native component to add custom button for scrolling back to top

Install

npm install react-native-scroll-up

or

yarn add react-native-scroll-up

Preview

preview

Demo

https://snack.expo.io/@rizalibnu/react-native-scroll-up-example

Example

Basic Example

import React from 'react';
import { ScrollView, View, Text } from 'react-native';
import ScrollUp from 'react-native-scroll-up';

const data = Array.from(Array(20));

class App extends React.Component {
  state = {
    visible: false,
  };

  handleOnScroll = event => {
    const offsetY = event.nativeEvent.contentOffset.y;

    if (offsetY > 50) {
      this.setState({
        visible: true,
      });
    } else {
      this.setState({
        visible: false,
      });
    }
  };

  render() {
    return (
      <React.Fragment>
        <ScrollView
          ref={scrollview => {
            this.scrollview = scrollview;
          }}
          onScroll={event => this.handleOnScroll(event)}>
          {data.map((item, index) => (
            <View
              style={{
                height: 200,
                backgroundColor: 'purple',
                justifyContent: 'center',
              }}>
              <Text
                style={{
                  marginTop: 100,
                  textAlign: 'center',
                  fontWeight: 'bold',
                  fontSize: 20,
                  color: 'white',
                }}>
                scrollview component {index}
              </Text>
            </View>
          ))}
        </ScrollView>
        <ScrollUp
          refView="ScrollView"
          root={this.scrollview}
          visible={this.state.visible}
          type="image"
        />
      </React.Fragment>
    );
  }
}

export default App;

Image Example

import arrowUpImage from '../../assets/image.png';

<ScrollUp
  refView="ScrollView"
  root={this.scrollview}
  type="image"
  image={arrowUpImage}
/>

Text Example

<ScrollUp
  refView="ScrollView"
  root={this.scrollview}
  type="text"
  text="scroll to top"
  fontSize={14}
  color="#fff"
  backgroundColor="#dd0000"
/>

Custom Icon Example

import Icon from 'react-native-vector-icons/Feather';

<ScrollUp
  refView="ScrollView"
  root={this.scrollview}
  type="icon"
  icon={<Icon name="arrow-up" size={24} style={{ color: '#fff' }} />}
  backgroundColor="#dd0000"
/>

Props

PropTypeOptionalDefaultDescription
rootReact.Refrequiredpass a ref of the root view
refViewstringrequiredtype of your scrollable view. choose one of ListView, ScrollView, FlatList, SectionList
typestringrequiredtype of your button component. choose one of image, text, icon
visiblebooleanYestrueis button visible. you can use this for example button will visible when scroll offset Y more than 50, else button will not visible
iconReact.ElementYesnullcustom icon button, if type is icon. you can use this to put custom component
rightnumberYes30distance from the right
bottomnumberYes30distance from the bottom
widthnumberYes40width of the button and the image if you have
heightnumberYes40height of the button and the image if you have
radiusbooleanYestrueis a round view
borderRadiusnumberYes50border radius of the button
backgroundColorstringYes'transparent'background color of the button,
textstringYesScroll Upcustom text button, if type is text
fontSizenumberYes12font size of the text button, if type is text
colorstringYes#fffcolor of the text button, if type is text
customTextStyleObjectYes{}custom stylesheet of the text button, if type is text
imageObjectYes{ uri : a base64 image }source of your image button, if type is image
customImageStylestringYes{}custom stylesheet of your image button, if type is image