0.2.1 • Published 2 years ago

@amalgamaco/react-native-animated-toggle-button v0.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

ReactNative - Animated Toggle Button

A two states button that can be animated when trasitioning between them.

Example

Installation

Requirements

This library depends on lottie-react-native in order to work. You must add this library as a dependency before installing this package.

yarn add lottie-react-native
yarn add lottie-ios@3.2.3

The version to install depends on the react native version being used in the project, you can see the corresponding version of the library for each react native version next:

App built in React Native versionRequires lottie-react-native versionRequires lottie-ios version
>= 0.593.0.23.0.3
>= 0.604.0.23.2.3
>= 0.634.0.33.2.3
>= 0.644.1.33.2.3
>= 0.66latest3.2.3

With yarn

yarn add @amalgamaco/react-native-animated-toggle-button

With npm

npm install @amalgamaco/react-native-animated-toggle-button

Usage

The AnimatedToggleButton component can be used in a declarative way:

import React, { useState } from 'react';
import { AnimatedToggleButton } from '@amalgamaco/react-native-animated-toggle-button';

import animation from './animation.json';

const BasicExample = () => {
   const [ isActive, setIsActive ] = useStsate( false );

   return (
      <AnimatedToggleButton
         source={animation}
         isActive={isActive}
         onPress={() => setIsActive( !isActive )}
         activeFrame={20}
         inactiveFrame={0}
         toActiveFrameRange={[ 10, 20 ]}
         toInactiveFrameRange={[ 20, 30 ]}
      />
   );
}

There is also a non-button animated component that you can use if you don't need a button and want to show only an animated view between two states:

import React from 'react';
import { AnimatedIcon } from '@amalgamaco/react-native-animated-toggle-button';

import animation from './animation.json';

const BasicExample = () => (
   <AnimatedIcon
      source={animation}
      isActive={true}
      activeFrame={20}
      inactiveFrame={0}
      toActiveFrameRange={[ 10, 20 ]}
      toInactiveFrameRange={[ 20, 30 ]}
   />
);

API

The AnimatedToggleButton accepts several props. They are describe in the following table:

PropDescriptionDefault
sourceMandatory - The source of animation. Can be referenced as a local asset by a string, or remotely with an object with a uri property, or it can be an actual JS object of an animation, obtained (for example) with something like require('../path/to/animation.json').None
inactiveFrameMandatory - A number indicating the still frame to show when the animation is in inactive state.None
activeFrameMandatory - A number indicating the still frame to show when the animation is in active state.None
toActiveFrameRangeMandatory - A range of two numbers indicating an start and end frame for the animation to show when the button passes from inactive to active state. The end frame must be always greater that the start frame. Example: [ 10, 20 ]None
toInactiveFrameRangeMandatory - A range of two numbers indicating an start and end frame for the animation to show when the button passes from active to inactive state. The end frame must be always greater that the start frame. Example: [ 20, 30 ]None
isActiveA boolean flag indicating whether or not the button is in an active state or not. If the button is active it will show the animation frame set in the activeFrame prop, otherwise it will show the animation frame set in the inactiveFrame prop.false
onPressA callback to call when the button is pressed.undefined
speedThe speed the animation will progress. Sending a negative value will reverse the animation1
iconSizeThe size of the animated icon. This prop will set the width and height of the animation view.50
styleStyle attributes for the view, as expected in a standard View.undefined
containerStyleStyle attributes for the container view, as expected in a standard View.undefined

Contributing

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

License

MIT

0.2.1

2 years ago