@fanchenbao/react-native-scroll-indicator

中文文档
0.4.0发布于 2024/08/23MITTypeScriptESM + CJS零依赖近 30 天下载 8763

A React Native component that offers a customizable scroll indicator for ScrollView and FlatList

原始使用文档
文档目录12 节
  1. Disclaimer
  2. Why do we need another customizable scroll indicator?
  3. Installation
  4. Usage
  5. ScrollViewIndicator
  6. FlatListIndicator
  7. Comprehensive Examples
  8. Run Comprehensive Examples
  9. Props
  10. Limitations
  11. Contributing
  12. License

@fanchenbao/react-native-scroll-indicator

A react-native component that offers a customizable scroll indicator for ScrollView and FlatList

Disclaimer

The idea of animating a scroll indicator in response to the scrolling on a scrollable component is borrowed from Lord Pooria's SO answer.

Why do we need another customizable scroll indicator?

If you search for react native scroll indicator on npm, there are more than 10 packages already there. Compared to those, this package has the following advantages:

  • Supports:
    • Both ScrollView and FlatList
    • Both vertical and horizontal scrolling
    • Indicator shrinking in iOS when user scrolls beyond the edge
    • inverted={true} in FlatList
    • Indicator customization just like a regular View (e.g. color, width, position, etc.)
    • Draggable indicator (added in v0.3.0)
  • Detailed documentation with comprehensive examples
  • The animation logic of the indicator is well documented in the source code.

Installation

npm install @fanchenbao/react-native-scroll-indicator

Usage

ScrollViewIndicator

import * as React from 'react';
import {View, Text} from 'react-native';
import {ScrollViewIndicator} from '@fanchenbao/react-native-scroll-indicator';

const App = () => {
  return (
    <View
      style={{
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
      }}
    >
      <View style={{height: '30%', width: '80%'}}>
        <ScrollViewIndicator
          indStyle={{backgroundColor: 'green'}}
          containerStyle={{
            borderWidth: 1,
            borderColor: 'black',
          }}
        >
          <View>
            <Text>
              Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam
              rhoncus nisl egestas, auctor mi pulvinar, aliquam metus. Nunc
              porttitor quam molestie tempor laoreet. Sed vitae ex vitae turpis
              convallis accumsan. Fusce consequat quis nisi sed lacinia. Nullam
              quis condimentum tellus. Donec ornare quam sit amet tempor
              imperdiet. Proin condimentum leo quis ipsum pretium ornare
              facilisis eget nunc. Pellentesque orci purus, pellentesque et nisl
              at, varius molestie nisi. Vestibulum ante ipsum primis in faucibus
              orci luctus et ultrices posuere cubilia curae; Nam ante nulla,
              sagittis vitae ante id, dignissim vestibulum ligula. Mauris
              iaculis nisi sed sapien finibus, sed pharetra risus rutrum. Cras
              laoreet mattis egestas. Pellentesque feugiat accumsan ultricies.
              Nullam viverra sapien nec tellus commodo aliquet. Integer faucibus
              quam sed nibh congue, at cursus risus vulputate. Morbi commodo
              mollis tempus.
            </Text>
          </View>
        </ScrollViewIndicator>
      </View>
    </View>
  );
};

export default App;

Minimal ScrollView Demo

FlatListIndicator

import * as React from 'react';
import {View, Text} from 'react-native';
import {FlatListIndicator} from '@fanchenbao/react-native-scroll-indicator';

const App = () => {
  return (
    <View
      style={{
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
      }}
    >
      <View
        style={{
          height: '10%',
          width: '80%',
        }}
      >
        <FlatListIndicator
          flatListProps={{
            ItemSeparatorComponent: () => (
              <View style={{width: 1, backgroundColor: 'blue'}} />
            ),
            data: [
              'Druid',
              'Sorceress',
              'Paladin',
              'Barbarian',
              'Necromancer',
              'Assassin',
              'Amazon',
            ],
            renderItem: ({item}) => (
              <View style={{justifyContent: 'center', padding: 10}}>
                <Text>{item}</Text>
              </View>
            ),
          }}
          horizontal={true}
          position="bottom"
          indStyle={{width: 30}}
          containerStyle={{borderWidth: 1, borderColor: 'black'}}
        />
      </View>
    </View>
  );
};

export default App;

Minimal FlatList Demo

Comprehensive Examples

Try it on Snack Expo

The comprehensive example takes advantage of the scroll indicator's customizability. A brief summary of the corresponding props for each option is given below. For details, refer to the Props table.

  • Horizontal: horizontal={true}
  • Vertical: horizontal={false}
  • left: position="left"
  • right: position="right"
  • top: position="top"
  • bottom: position="bottom"
  • 20: position={20}
  • 50: position={50}
  • 80: position={80}
  • Normal: indStyle={backgroundColor: 'grey', width: 5}
  • Crazy: indStyle={backgroundColor: 'red', width: 40}

Comprehensive Demo

Run Comprehensive Examples

Download this repo

git clone https://github.com/FanchenBao/react-native-scroll-indicator.git

Go to the example folder and sets up the example app

npm install

While inside the example folder, run the app for iOS

npm run ios

or Android

npm run android

The example app is running from ./example/App.tsx

Props

PropsTypeDefaultNote
horizontalbooleanfalseIf false, vertical scrolling. If true, horizontal scrolling.
positionstring | number''Desired position of the indicator. One can supply "left" or "right" for vertical scrolling, in which case the indicator will be placed at the left or right edge of the view. The same goes for "top" or "bottom" for horizontal scrolling. In addition, one can supply a number, which indicates where in the view the indicator's center line (along the scrolling direction) will be located. For example, position={20} horizontal={false} would place the center line of the indicator at 20% of the view's width from its left edge. position={80} horizontal={true} would place the center line of the indicator at 80% of the view's height from its top edge. If the indicator's girth is too big such that if its center line is placed at the desired location, the indicator would overflow outside the edge, the position will be automatically clamped to ensure no overflow takes place. This is demonstrated in the comprehensive example when the "Crazy" option is selected. If supplied an empty string, the position is automatically assigned to "right" if vertical scrolling, or "bottom" if horizontal scrolling.
persistentScrollbarbooleanfalseIf false, scroll indicator does not show if the content can fit the view. If true, scroll indicator shows under all circumstances.
indStyleViewStyle{backgroundColor: 'grey', width: 5}Styling of the scroll indicator. The scroll indicator is just an Animated.View. Thus, any props that modifies a View can potentially modify the appearance of the scroll indicator. Note that width refers to the girth of the indicator, which is width in vertical scrolling but height in horizontal scrolling. borderRadius value will be derived from the width prop to make it round (i.e., half the value of width) if not overridden. Please do not assign the following props, as they will be overwritten and won't have any effect: position, height, transform.
scrollViewPropsScrollViewProps{}ScrollViewIndicator only. Props to pass to the underlying ScrollView.

Please do not pass the following props, as they will be overwritten and won't have any effect: horizontal, showsVerticalScrollIndicator, showsHorizontalScrollIndicator, onContentSizeChange, scrollEventThrottle.

Note that onScroll and onLayout are allowed (updated in v0.4.0).
flatListPropsScrollViewProps & FlatListPropsrequiredFlatListIndicator only. Props to pass to the underlying FlatList. This is a required prop, as one must supply data and renderItem to FlatList.

Please do not pass the following props, as they will be overwritten and won't have any effect: horizontal, showsVerticalScrollIndicator, showsHorizontalScrollIndicator, onContentSizeChange, scrollEventThrottle.

Note that onScroll and onLayout are allowed (updated in v0.4.0).
containerStyleViewStyle{}Styling of the parent container that holds both the scroll indicator and the scrollable component (updated in v0.4.0).

Limitations

The package is designed to be a substitute of ScrollView and FlatList under the most basic usage. Thus, despite allowing any props from ScrollView and FlatList to be passed to ScrollViewIndicator and FlatListIndicator, the package has NOT been fully tested on the combination of all the props. It is very likely that some prop combinations would break the custom scroll indicator. If that happens, please raise an issue or suggest a feature request on GitHub.

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT


Made with create-react-native-library


版本与发布记录

@fanchenbao/react-native-scroll-indicator 共发布 7 个版本,首次发布于 2023/02/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.4.0latest2024/08/232 年前
0.3.22024/08/222 年前
0.3.12024/08/222 年前
0.3.02024/08/222 年前
0.2.22023/10/103 年前
0.2.12023/02/143 年前

关于 @fanchenbao/react-native-scroll-indicator 的常见问题

@fanchenbao/react-native-scroll-indicator 是什么?

A React Native component that offers a customizable scroll indicator for ScrollView and FlatList

如何安装 @fanchenbao/react-native-scroll-indicator?

在项目目录运行 npm install @fanchenbao/react-native-scroll-indicator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(*)、react-native(*)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fanchenbao/react-native-scroll-indicator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fanchenbao/react-native-scroll-indicator 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fanchenbao/react-native-scroll-indicator 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 16.0.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@fanchenbao/react-native-scroll-indicator 可以商用吗?

@fanchenbao/react-native-scroll-indicator 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@fanchenbao/react-native-scroll-indicator 的下载量有多少?

根据 npm 官方下载统计,@fanchenbao/react-native-scroll-indicator 在 2026/10/14 – 2026/11/12 这 30 天内被下载 8,763 次,日均约 292 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fanchenbao/react-native-scroll-indicator 还在维护吗?

最新版本 0.4.0 发布于 2024/08/23(2 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。