Giter VIP home page Giter VIP logo

react-native-animation-hooks's Introduction

react-native-animation-hooks

NPM Build Status

yarn add react-native-animation-hooks

The most convenient way to add little animations to your ReactNative application.

const Comp = () => {
  const [bool, setBool] = useState(false)
  const toggle = () => setBool(!bool);

  const animatedOpacity = useAnimation({
    toValue: bool ? 1 : 0.5,
  })

  return (
    <Animated.View style={{ opacity: animatedOpacity }}>
      <Text onPress={toggle}>Hello</Text>
    </Animated.View>
  )
}
  • Tiny interface using hooks and Animated framework
  • Compatible with Expo
  • Reduce Animated.Value management boilerplate
  • Full support for spring and timing animations
  • Typescript definitions

Advanced example:

This repo is an Expo app that you can run with yarn start.

import React, { useState } from 'react'
import { Animated, Switch, View, StyleProp, ViewStyle } from 'react-native'

import { useAnimation } from 'react-native-animation-hooks'

const AnimatedShape = ({ style }: { style?: StyleProp<ViewStyle> }) => (
  <Animated.View
    style={[{ width: 50, height: 50, backgroundColor: 'red' }, style]}
  />
)

const ExampleScreen = () => {
  const [bool, setBool] = useState(false)

  const animatedValue = useAnimation({
    type: 'timing',
    initialValue: 1,
    toValue: bool ? 1 : 0,
    duration: bool ? 1000 : 300,
    useNativeDriver: true,
  })

  return (
    <View style={{ flex: 1 }}>
      <View style={{ padding: 10 }}>
        <Switch value={bool} onValueChange={setBool} />
      </View>
      <View
        style={{
          flex: 1,
          padding: 10,
          justifyContent: 'space-around',
          alignItems: 'center',
        }}
      >
        <AnimatedShape
          style={{
            opacity: animatedValue.interpolate({
              inputRange: [0, 1],
              outputRange: [0.3, 1],
            }) as any,
          }}
        />
        <AnimatedShape
          style={{
            transform: [
              {
                scale: animatedValue.interpolate({
                  inputRange: [0, 1],
                  outputRange: [0.5, 1],
                }) as any,
              },
            ],
          }}
        />
        <AnimatedShape
          style={{
            transform: [
              {
                translateX: animatedValue.interpolate({
                  inputRange: [0, 1],
                  outputRange: [-100, 100],
                }) as any,
              },
            ],
          }}
        />
      </View>
    </View>
  )
}

export default ExampleScreen

License

MIT

Hire a freelance expert

Looking for a React/ReactNative freelance expert with more than 5 years production experience? Contact me from my website or with Twitter.

react-native-animation-hooks's People

Contributors

pistou avatar slorber avatar xcarpentier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

react-native-animation-hooks's Issues

Not working with text

Hello, the library is not working with the following snippet,

import React from 'react';
import { Text, View, StyleSheet, Animated } from 'react-native';
import { useAnimation } from 'react-native-animation-hooks'

const s = StyleSheet.create({
  container: {
    alignItems: "center"
  },
  titleText: {
    color: "#fff",
    fontWeight: "bold",
    fontSize: 20
  },
  fadedText: {
    color: "#898989",
    fontSize: 11
  }
});


const StatText = (props) => {
    const animatedValue = useAnimation({
      type: 'timing',
      initialValue: 0,
      toValue: props.value,
      duration: 1000,
      useNativeDriver: true,
    })

  return (<View style={s.container}>
    <Animated.Text style={s.titleText}>{animatedValue}</Animated.Text>
    <Text style={s.fadedText}>{props.name}</Text>
  </View>)
}

export default StatText

Missing option to loop an animation indefinitely

The useAnimation hook is missing an option to loop the animation using Animated.loop(). Is this something you will be willing to include? I went through the implementation, and it seems to be simple enough to implement. Adding a loop config option and some conditionals in the hook.

Invalid prop `top` supplied to `View`

Hi,

I just tried your library with a simple example and I receive the error: Failed prop type: Invalid prop top supplied to View.

<AutoAnimate 
        initialValue={400}
        value={isOpen ? 40 : 400 }
        duration={200}
      >
        {top => 
          <View style={[styles.wrapper, { top: top }]} />
        }
</AutoAnimate>

I then took a look at your code and saw you were using Animated value and I then try top._value and it worked. I remember that with Animated Value you need to use ._value to get the int value.

Maybe I did something wrong but if not, maybe it should be specified in the DOC.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.