React Native scrollable bottom sheet native animations gestures 60FPS

栏目: IT技术 · 发布时间: 5年前

内容简介:Cross platform scrollable bottom sheet with virtualisation support and fully native animations, that integrates with all core scrollable components from React Native:If you don't use Expo, you also need to install

Scroll Bottom Sheet

Cross platform scrollable bottom sheet with virtualisation support and fully native animations, that integrates with all core scrollable components from React Native: FlatList , ScrollView and SectionList . Also, it's 100% compatible with Expo.

React Native scrollable bottom sheet native animations gestures 60FPS React Native scrollable bottom sheet native animations gestures 60FPS

Features

  • React Native scrollable bottom sheet native animations gestures 60FPS Virtualisation support : FlatList and SectionList components are 1st class citizens, as well as ScrollView
  • :fire: Peformant : runs at 60 FPS even on low grade Android devices
  • :white_check_mark: Horizontal mode : allows for nice implementation of Google or Apple Maps bottom sheets types, where you have several horizontal lists embedded
  • ⚙️ Minimalistic : exposes a set of fundamental props to easily control its behaviour
  • :point_down: Support for interruptions : animations can be interrupted anytime smoothly without sudden jumps
  • :sunglasses: Imperative snapping : for cases where you need to close the bottom sheet by pressing an external touchable
  • :rocket: Animate all the things : you can animate other elements on the screen based on the bottom sheet position
  • :muscle: No native dependencies : fully implemented in JS land, thanks to the powerful Gesture Handler and Reanimated libraries
  • :iphone: Expo compatible : no need to eject to enjoy this component!
  • TS definitions : For those of you like me who can't look back to start a project in plain JS

Installation

npm

npm i react-native-scroll-bottom-sheet

yarn

yarn add react-native-scroll-bottom-sheet

If you don't use Expo, you also need to install react-native-gesture-handler and react-native-reanimated libraries along with this one.

Compatibility table

You may add some touchable components inside the bottom sheet or several FlatList widgets for horizontal mode. Unfortunately, not all interactable React Native components are compatible with this library. This is due to some limitations on react-native-gesture-handler , which this library uses behind the scenes. For that, please follow this compatibility table:

Import Touchable Flatlist
react-native iOS :no_entry_sign:
react-native-gesture-handler Android Android, iOS

Touchables

As you can see on the table, for any touchable component ( TouchableOpacity , TouchableHighlight , ...) you need to have different imports depending on the platform. The below is a snippet you may find useful to abstract that into a component.

import React from "react";
import { Platform, TouchableOpacity } from "react-native";
import { TouchableOpacity as RNGHTouchableOpacity } from "react-native-gesture-handler";

const BottomSheetTouchable = (props) => {
  if (Platform.OS === "android") {
    return (
      <RNGHTouchableOpacity {...props} />
    );
  }

  return <TouchableOpacity {...props} />
};

export default BottomSheetTouchable;

Horizontal Mode

For this mode to work properly, you have to import FlatList from react-native-gesture-handler instead of react-native.

import { FlatList } from 'react-native-gesture-handler';

...

Usage

The below is an example using the core FlatList from React Native as the scrollable component.

import React from 'react';
import { Dimensions, StyleSheet, Text, View } from 'react-native';
import ScrollBottomSheet from 'react-native-scroll-bottom-sheet';

const windowHeight = Dimensions.get('window').height;

function Example() {
  return (
    <View style={styles.container}>
      <ScrollBottomSheet<string> // If you are using TS, that'll infer the renderItem `item` type
        componentType="FlatList"
        snapPoints={[128, '50%', windowHeight - 200]}
        initialSnapIndex={2}
        renderHandle={() => (
          <View style={styles.header}>
            <View style={styles.panelHandle} />
          </View>
        )}
        data={Array.from({ length: 200 }).map((_, i) => String(i))}
        keyExtractor={i => i}
        renderItem={({ item }) => (
          <View style={styles.item}>
            <Text>{`Item ${item}`}</Text>
          </View>
        )}
        contentContainerStyle={styles.contentContainerStyle}
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  contentContainerStyle: {
    padding: 16,
    backgroundColor: '#F3F4F9',
  },
  header: {
    alignItems: 'center',
    backgroundColor: 'white',
    paddingVertical: 20,
    borderTopLeftRadius: 20,
    borderTopRightRadius: 20
  },
  panelHandle: {
    width: 40,
    height: 2,
    backgroundColor: 'rgba(0,0,0,0.3)',
    borderRadius: 4
  },
  item: {
    padding: 20,
    justifyContent: 'center',
    backgroundColor: 'white',
    alignItems: 'center',
    marginVertical: 10,
  },
});

Props

There are 2 types of props this component receives: explicit and inherited.

Explicit

This is the list of exclusive props that are meant to be used to customise the bottom sheet behaviour.

Name Required Type Description
componentType yes string 'FlatList', 'ScrollView', or 'SectionList'
snapPoints yes Array<string | number> Array of numbers and/or percentages that indicate the different resting positions of the bottom sheet (in dp or %), starting from the top . If a percentage is used, that would translate to the relative amount of the total window height. If you want that percentage to be calculated based on the parent available space instead, for example to account for safe areas or navigation bars, use it in combination with topInset prop
initialSnapIndex yes number Index that references the initial resting position of the drawer, starting from the top
renderHandle yes () => React.ReactNode Render prop for the handle, should return a React Element
onSettle no (index: number) => void Callback that is executed right after the bottom sheet settles in one of the snapping points. The new index is provided on the callback
animatedPosition no Animated.Value<number> Animated value that tracks the position of the drawer, being: 0 => closed, 1 => fully opened
animationConfig no { duration: number, easing: Animated.EasingFunction } Timing configuration for the animation, by default it uses a duration of 250ms and easing fn Easing.inOut(Easing.linear)
topInset no number This value is useful to provide an offset (in dp) when applying percentages for snapping points
innerRef no RefObject Ref to the inner scrollable component (ScrollView, FlatList or SectionList), so that you can call its imperative methods. For instance, calling scrollTo on a ScrollView. In order to so, you have to use getNode as well, since it's wrapped into an animated component: ref.current.getNode().scrollTo({y: 0, animated: true})

Inherited

Depending on the value of componentType chosen, the bottom sheet component will inherit its underlying props, being one of FlatListProps , ScrollViewProps or SectionListProps , so that you can tailor the scrollable content behaviour as per your needs.

Methods

snapTo(index)

Imperative method to snap to a specific index, i.e.

bottomSheetRef.current.snapTo(0)

bottomSheetRef refers to the ref passed to the ScrollBottomSheet component.

Example

There is an Expo example application that you can play with to get a good grasp on the different customisation options. In case of Android, you can directly open the project here . For iOS, head to the example folder and run the project locally:

$ npm install

$ npm start

Typescript

The library has been written in Typescript, so you'll get type checking and autocompletion if you use it as well.

License

MIT


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算机程序设计艺术(第1卷)

计算机程序设计艺术(第1卷)

[美] 唐纳德·E. 克努特 / 苏运霖 / 国防工业出版社 / 2002-9 / 98.00元

7卷本《计算机程序设计艺术》的第1卷以基本的程序设计概念和技术开始,然后专注于信息结构——计算机内部信息的表示、数据元素之间的结构关系以及如何有效地处理它们,给出了对于模拟、数值方法、符号计算、软件和系统设计的初等应用。书中附有大量习题和答案,标明了难易程序及数学概念的使用。 此新版本增加了几十项简单且重要的算法和技术,并对有关数学预备知识作了大量修改以适应现时研究的趋势。一起来看看 《计算机程序设计艺术(第1卷)》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

SHA 加密
SHA 加密

SHA 加密工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试