UNPKG

react-native-scroll-up

Version:

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

120 lines (112 loc) 5.86 kB
// @flow import * as React from 'react'; import { TouchableOpacity, Text, Image, StyleSheet, } from 'react-native'; type Props = { root: React.Ref, refView: string, visible: boolean, type: string, icon: React.Element, right: number, bottom: number, width: number, height: number, radius: boolean, borderRadius: number, backgroundColor: string, text: string, fontSize: number, color: string, customTextStyle: Object, image: Object, customImageStyle: Object, }; const scrollUpImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAMAAABHPGVmAAAC31BMVEVAQEDGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbGxsbExMTFxcXGxsa+vr7AwMDBwcG3t7e4uLi7u7u8vLyxsbGysrKzs7O1tbW2traqqqqrq6utra2urq6vr6+wsLCmpqanp6eoqKipqamenp6fn5+hoaGioqKkpKSZmZmampqcnJydnZ2RkZGWlpaXl5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jmkgWnKbttjwC13TK2oqkagqmLq7cP6F7XJZoelH1HTivwHxrfn/g2Cw1IAAAAASUVORK5CYII='; const styles = StyleSheet.create({ wrapper: { position: 'absolute', flex: 1, justifyContent: 'center', alignItems: 'center', }, }); export default class ScrollUp extends React.Component<Props> { static defaultProps = { visible: true, radius: true, backgroundColor: 'transparent', width: 40, height: 40, color: '#fff', fontSize: 12, icon: null, right: 30, bottom: 30, borderRadius: 50, text: 'Scroll Up', image: { uri: scrollUpImage }, customTextStyle: {}, customImageStyle: {}, }; handleOnPress = () => { if (this.props.refView === 'ListView' || this.props.refView === 'ScrollView') { this.props.root.scrollTo({ x: 0, y: 0, animated: true }); } if (this.props.refView === 'FlatList') { this.props.root.scrollToOffset({ offset: 0, animated: true }); } if (this.props.refView === 'SectionList') { this.props.root.scrollToLocation({ itemIndex: 0, sectionIndex: 0 }); } } render() { if (!this.props.visible) return null; return ( <TouchableOpacity onPress={() => this.handleOnPress()} style={[ styles.wrapper, { width: this.props.width, height: this.props.height, right: this.props.right, bottom: this.props.bottom, backgroundColor: this.props.backgroundColor, borderRadius: this.props.radius ? this.props.borderRadius : 0, }, ]} > {this.props.type === 'icon' && { ...this.props.icon }} {this.props.type === 'text' && <Text style={[ { textAlign: 'center', color: this.props.color, fontSize: this.props.fontSize, }, this.props.customTextStyle, ]} > {this.props.text} </Text>} {this.props.type === 'image' && <Image style={[ { width: this.props.width, height: this.props.height, }, this.props.customImageStyle, ]} source={this.props.image} />} </TouchableOpacity> ); } }