avatarChelaru Adrian

Summary

The website provides a guide on creating a custom scroll bar indicator for React Native applications using Reanimated and Gesture Handler libraries, with a focus on cross-platform compatibility and a versatile component design.

Abstract

The article discusses the limitations of cross-platform scroll bar customization in React Native and offers a solution through a custom wrapper component around the ScrollView. This solution utilizes React Native Reanimated and Gesture Handler libraries to enable animated indicator positioning, auto-hiding, and responsive sizing based on content height. The custom scroll bar is demonstrated to work seamlessly across Android, iOS, and Web platforms, and is showcased in a web version and a mobile app settings drop-down. The component is designed to be versatile, allowing developers to set specific heights and styles, and includes features such as gesture-based scrolling and dynamic adjustment to content changes. The author invites feedback and suggestions for future feature inclusions and provides a Snack demo and GitHub Gist for developers to access the code and examples.

Opinions

  • The author believes that the native React Native ScrollView component does not provide sufficient customization options for a stylized scroll bar indicator across platforms.
  • The custom ScrollView component is praised for its versatility and control over the appearance and behavior of the scroll bar indicator.
  • The author values user feedback and is open to community input for enhancing the component's features in future releases.
  • The use of Reanimated and Gesture Handler is endorsed for achieving smooth animations and gesture interactions in the custom scroll bar implementation.

Creating a custom scroll bar indicator — React Native, Reanimated, Gesture

If you land here, you are looking on how to customize the scroll bar indicator, but sadly you can’t do a cross-platform stylized indicator by simply setting parameters on the React Native <ScrollView> component.

This works cross-platform on the Android, iOS, and Web.

You can find the code and the Snack demo at the bottom of the article.

This is using React Native Reanimated, and Gesture Handler in order to move the indicator and handle the classic click or touch to scroll gesture.

Demonstrating the features of the custom wrapper component around React Native ScrollView

The demonstration above is on the web version, but it works and look the same on the mobile, through Expo. I also use it to create a drop-down menu for the app settings which need scrolling.

Custom scroll indicator in a React Native app settings drop-down

The component is versatile, as you can directly set the height of it, so it gives you control over how the overall scroll component should look like.

Features

There are multiple features integrated into this custom ScrollView component:

  • Animated indicator position based on the scrolling percentage
  • Automatically hide after timeout
  • Don’t show scroll bar when the view height is bigger than content height
  • Pan gesture for the indicator
  • Automatically modify the position and the height of the indicator when the content height changes

What do you think should be included into the next release features? Let me know in the replay section 😄.

How to use

Wrap your components with:

<CustomScrollView height={SET_HEIGHT_NUMBER}>

Don’t forget to set the height. You might want to adjust the style of the indicator inside the component. Check the indicatorStaticStyle.

Where I can find the code

Don’t forget to wrap your App.js with <GestureHandlerRootView> or the drag gesture will not work on mobile.

You can find the component and code example on this Snack above or on this GitHub Gist.

React Native
Expo
Web Development
Development
Mobile Development
Recommended from ReadMedium