All Projects → Gapur → react-native-scrollable-animated-header

Gapur / react-native-scrollable-animated-header

Licence: other
🤯 React Native Animated Header with ScrollView

Programming Languages

java
68154 projects - #9 most used programming language
ruby
36898 projects - #4 most used programming language
javascript
184084 projects - #8 most used programming language
objective c
16641 projects - #2 most used programming language
Starlark
911 projects

Projects that are alternatives of or similar to react-native-scrollable-animated-header

Alive Progress
A new kind of Progress Bar, with real-time throughput, ETA, and very cool animations!
Stars: ✭ 2,940 (+2525%)
Mutual labels:  animated
use-axios-react
React axios hooks for CRUD
Stars: ✭ 31 (-72.32%)
Mutual labels:  react-hooks
UnityDynamicScrollView
Dynamic scrollView based on UGUI
Stars: ✭ 161 (+43.75%)
Mutual labels:  scrollview
Svg Gauge
Minimalistic, animated SVG gauge. Zero dependencies
Stars: ✭ 188 (+67.86%)
Mutual labels:  animated
react-hooks-lifecycle
⚛️ 🪝 ⏳ React hooks lifecycle diagram: Functional components lifecycle explained
Stars: ✭ 194 (+73.21%)
Mutual labels:  react-hooks
use-route-as-state
Use React Router route and query string as component state
Stars: ✭ 37 (-66.96%)
Mutual labels:  react-hooks
Animatedgraph
Animated Graph which you can include in your application to show information in more attractive way
Stars: ✭ 162 (+44.64%)
Mutual labels:  animated
react-native-rough
⚛️✍️Rough.js bindings for that hand-drawn feel.
Stars: ✭ 106 (-5.36%)
Mutual labels:  animated
react-7h-hooks
(持续增加中)提供一些偏业务的实用 react hooks, 让你每天只工作 7 小时 !
Stars: ✭ 15 (-86.61%)
Mutual labels:  react-hooks
react-facade
Experimental dependency injection for React hooks
Stars: ✭ 95 (-15.18%)
Mutual labels:  react-hooks
React Svg Buttons
React configurable animated svg buttons
Stars: ✭ 209 (+86.61%)
Mutual labels:  animated
Cv
🎓 Best in Class modern CV, Resume and Portfolio website template. All-in-One-Page site with simply customizable builder.
Stars: ✭ 232 (+107.14%)
Mutual labels:  animated
react-immer-hooks
Easy immutability in React Hooks with Immer.
Stars: ✭ 45 (-59.82%)
Mutual labels:  react-hooks
React Native Confetti Cannon
React Native confetti explosion and fall like iOS does.
Stars: ✭ 149 (+33.04%)
Mutual labels:  animated
use-double-click
React hook for combining double-click function into click event, as well as repeatable double-click
Stars: ✭ 17 (-84.82%)
Mutual labels:  react-hooks
Komorebi
A beautiful and customizable wallpapers manager for Linux
Stars: ✭ 2,472 (+2107.14%)
Mutual labels:  animated
use-google-sheets
📝 A React Hook for getting data from Google Sheets API v4
Stars: ✭ 75 (-33.04%)
Mutual labels:  react-hooks
eslint-config-ns
ESLint config ready to be used in multiple projects. Based on Airbnb's code style with prettier, jest and react support.
Stars: ✭ 27 (-75.89%)
Mutual labels:  react-hooks
fetchye
✨ If you know how to use Fetch, you know how to use Fetchye [fetch-yae]. Simple React Hooks, Centralized Cache, Infinitely Extensible.
Stars: ✭ 36 (-67.86%)
Mutual labels:  react-hooks
react-use-countdown
React hook for countdown state.
Stars: ✭ 19 (-83.04%)
Mutual labels:  react-hooks

React Native ScrollView Animated Header

React Native Animated Header App with ScrollView

Animated header is the most common design pattern in today’s apps. Animations are an important part of mobile applications.

Getting Started

  1. Clone this repository
git clone https://github.com/Gapur/react-native-scrollview-animated-header.git
  1. Install dependencies
yarn
  1. Launch app
npm run ios # for npm

Making Magic Code

We need to define some constants for the animated header which will be used to interpolate the scroll position value.

const HEADER_MAX_HEIGHT = 240;
const HEADER_MIN_HEIGHT = 84;
const HEADER_SCROLL_DISTANCE = HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT;

I will display user data with ScrollView component. So We should to change App.js file like this:

const HEADER_MAX_HEIGHT = 240; // max header height
const HEADER_MIN_HEIGHT = 84; // min header height
const HEADER_SCROLL_DISTANCE = HEADER_MAX_HEIGHT - HEADER_MIN_HEIGHT; // header scrolling value

// create array by 10 fake user data
const DATA = Array(10)
  .fill(null)
  .map((_, idx) => ({
    id: idx,
    avatar: faker.image.avatar(),
    fullName: `${faker.name.firstName()} ${faker.name.lastName()}`,
  }));

function App() {
  const renderListItem = (item) => (
    <View key={item.id} style={styles.card}>
      <Image style={styles.avatar} source={{uri: item.avatar}} />
      <Text style={styles.fullNameText}>{item.fullName}</Text>
    </View>
  );

  return (
    <SafeAreaView style={styles.saveArea}>
      <ScrollView contentContainerStyle={{ paddingTop: HEADER_MAX_HEIGHT - 32 }}> // it should be under the header 
        {DATA.map(renderListItem)}
      </ScrollView>
    </SafeAreaView>
  );
}

We need to create the header under the ScrollView. We will use Animated.View

<Animated.View
  style={[
    styles.topBar,
    {
      transform: [{scale: titleScale}, {translateY: titleTranslateY}],
    },
  ]}>
  <Text style={styles.title}>Management</Text>
</Animated.View>

Magic Animation

React Native provides Animated API for animations. Animated API focuses on declarative relationships between inputs and outputs, with configurable transforms in between, and start/stop methods to control time-based animation execution.

We are going to use Animated.ScrollView to make the scroll view, and attach a callback to listen to the onScroll event when it is changed. Then, using interpolation to map value between the y-axis and opacity. The interpolation maps input ranges to output ranges, typically using a linear interpolation but also supports easing functions.

Let’s update our App.js file with the following lines of code:

const scrollY = useRef(new Animated.Value(0)).current; // our animated value

// our header y-axis animated from 0 to HEADER_SCROLL_DISTANCE,
// we move our element for -HEADER_SCROLL_DISTANCE at the same time.
const headerTranslateY = scrollY.interpolate({
  inputRange: [0, HEADER_SCROLL_DISTANCE],
  outputRange: [0, -HEADER_SCROLL_DISTANCE],
  extrapolate: 'clamp',
});

// our opacity animated from 0 to 1 and our opacity will be 0
const imageOpacity = scrollY.interpolate({
  inputRange: [0, HEADER_SCROLL_DISTANCE / 2, HEADER_SCROLL_DISTANCE],
  outputRange: [1, 1, 0],
  extrapolate: 'clamp',
});
const imageTranslateY = scrollY.interpolate({
  inputRange: [0, HEADER_SCROLL_DISTANCE],
  outputRange: [0, 100],
  extrapolate: 'clamp',
});

// change header title size from 1 to 0.9
const titleScale = scrollY.interpolate({
  inputRange: [0, HEADER_SCROLL_DISTANCE / 2, HEADER_SCROLL_DISTANCE],
  outputRange: [1, 1, 0.9],
  extrapolate: 'clamp',
});
// change header title y-axis
const titleTranslateY = scrollY.interpolate({
  inputRange: [0, HEADER_SCROLL_DISTANCE / 2, HEADER_SCROLL_DISTANCE],
  outputRange: [0, 0, -8],
  extrapolate: 'clamp',
});

Above, we use useRef to persist Animated value. useRef returns a mutable ref object whose .current property is initialized to the passed argument.

Next, We need to update Animated value when we are scrolling ScrollView. We will catch event by Animated.event. It maps directly to animated value scrollY and update it when we are panning or scrolling.

We can use the native driver by specifying useNativeDriver: true in our animation configuration.

Let’s animate our components by the following codes:

<SafeAreaView style={styles.saveArea}>
  <Animated.ScrollView
    contentContainerStyle={{ paddingTop: HEADER_MAX_HEIGHT - 32 }}
    scrollEventThrottle={16} // 
    onScroll={Animated.event(
      [{ nativeEvent: { contentOffset: { y: scrollY } } }], // event.nativeEvent.contentOffset.x to scrollX
      { useNativeDriver: true }, // use native driver for animation
    )}>
    {DATA.map(renderListItem)}
  </Animated.ScrollView>
  <Animated.View
    style={[styles.header, { transform: [{ translateY: headerTranslateY }] }]}>
    <Animated.Image
      style={[
        styles.headerBackground,
        {
          opacity: imageOpacity,
          transform: [{ translateY: imageTranslateY }],
        },
      ]}
      source={require('./assets/management.jpg')}
    />
  </Animated.View>
  <Animated.View
    style={[
      styles.topBar,
      {
        transform: [{ scale: titleScale }, { translateY: titleTranslateY }],
      },
    ]}>
    <Text style={styles.title}>Management</Text>
  </Animated.View>
</SafeAreaView>

Article on Medium

React Native Scrollable Animated Header

How to contribute?

  1. Fork this repo
  2. Clone your fork
  3. Code 🤓
  4. Test your changes
  5. Submit a PR!
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].