Giter Site home page Giter Site logo

coteviande / react-native-awesome-button Goto Github PK

View Code? Open in Web Editor NEW

This project forked from larsvinter/react-native-awesome-button

0.0 2.0 0.0 867 KB

A button React Native component supporting showing different states with animations

License: MIT License

JavaScript 100.00%

react-native-awesome-button's Introduction

React Native Awesome Button

An <AwesomeButton /> component that creates a button depicting different states in terms of e.g. color and label text. When a new buttonState is passed to the component the button will animate transition to the new appearance specified upon initiation. You can also pass in different functions to be called on touch depending of the current state of the button.

Content

Installation

  1. npm install react-native-awesome-button --save
  2. Whenever you want to use it within React Native code now you can: const AwesomeButton = require('react-native-awesome-button')

Usage Example

Simple Example

To use <AwesomeButton>, you need to require react-native-awesome-button to your module and insert <AwesomeButton> tag inside render function with the required properties as it's done below:

import React, { Component } from 'react';

import {
  AppRegistry,
  View,
  StyleSheet
} from 'react-native';

import AwesomeButton from 'react-native-awesome-button';


const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    margin: 20,
    alignItems: 'center',
    justifyContent: 'center'
  }
})


class Simple extends Component {
  
  handleButtonPress() {
    console.log('I was pressed')
  }

  render() {
    return (
      <View style={styles.container}>
        <AwesomeButton states={{
                        default: {
                          text: 'Press me',
                          onPress: this.handleButtonPress,
                          backgroundColor: '#1155DD'
                        }
                       }} />
      </View>
    )
  }

}


AppRegistry.registerComponent('Simple', () => Simple)

In this example, a simple button with only one appearance and onPress-function will be rendered. This would produce something like this:

Example code result

To run this example yourself, you need to do the following:

  1. Clone the full source code from github: git clone [email protected]:larsvinter/react-native-awesome-button.git
  2. Change to the proper directory of the simple example: cd react-native-awesome-button/examples/Simple
  3. Install dependencies: npm install
  4. Open the project in Xcode and hit 'run' (the project is inside 'ios' folder) (remember to restart the packager)


Advanced Example

This shows how you might implement an animated button that transitions to a new state upon e.g. a user press. Notice that you create an object that specifies the different states and then pass the prop buttonState to the button, when you want it to transition to a new appearance. You can have an unlimited number of possible states for a button.

const React = require('react-native')
const AwesomeButton = require('react-native-awesome-button')


const {
  AppRegistry,
  Component,
  View,
  StyleSheet
} = React


const styles = StyleSheet.create({
  container: {
    flex: 1,
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    margin: 20
  },
  loginButtonBackground: {
    flex: 1,
    height: 40,
    borderRadius: 5
  },
  loginButtonLabel: {
    color: 'white'
  }
})


class Advanced extends Component {

  constructor(props) {
    super(props)
    this.state = {
        buttonState: 'idle'
    }
    this.logIn = this.logIn.bind(this)
  }

  logIn() {
    this.setState({ buttonState: 'busy' })
    setTimeout(() => {
      this.setState({ buttonState: 'success' })
    }, 2000);
  }

  render() {
    return (
      <View style={styles.container}>
        <AwesomeButton  backgroundStyle={styles.loginButtonBackground}
                        labelStyle={styles.loginButtonLabel}
                        transitionDuration={200}
                        states={{
                          idle: {
                            text: 'Log In',
                            onPress: this.logIn,
                            backgroundColor: '#1155DD',
                          },
                          busy: {
                            text: 'Logging In',
                            backgroundColor: '#002299',
                            spinner: true,
                          },
                          success: {
                            text: 'Logged In',
                            backgroundColor: '#339944'
                          }
                        }}
                        buttonState={this.state.buttonState}
                        />
      </View>
    )
  }
}


AppRegistry.registerComponent('Advanced', () => Advanced)

In this example, a login-button with three different appearances will be rendered. This would produce something like this (in 3 steps): Example code result

To run this example yourself, you need to do the following:

  1. Clone the full source code from github: git clone [email protected]:larsvinter/react-native-awesome-button.git
  2. Change to the proper directory of the advanced example: cd react-native-awesome-button/examples/Advanced
  3. Install dependencies: npm install
  4. Open the project in Xcode and hit 'run' (the project is inside 'ios' folder) (remember to restart the packager)


### Component properties
Prop Type Description
states object Includes a nested object for each possible state and the parameters for this state. The key used for the nested object will be the name for that specific state
buttonState string The key used to tell the button what apparance and functionality to take on. Changing this will trigger an animation to the new state given
backgroundStyle stylesheet object A standard style object for the button (with same style properties as for a <View> component. Possible to reference a StyleSheet object
labelStyle stylesheet object A standard style object for the button label (with same style properties as for a <Text> component. Possible to reference a StyleSheet object

Keys in the states objects

Each state in the states object should have a unique key. For each of these keys you need to specify the parameters for the button when it is in that specific state. A buttonstate can have one or more of the following parameters:

Key Type Description
onPress function The function fired when the button is pressed. If not defined the button has no press functionality and simply renders as a view
spinner bool If true the button will show a spinner on the left hand side of the label when in this state
text string The label of the button when in this state
backgroundColor string A color for the background of the button when in this state

Roadmap

  • Improve transition animation (i.e. cross-fade the text label)

Contributing

If you would like to refactor or help with new features, please create a separate branch, make your changes, commit, push and submit a pull-request.

react-native-awesome-button's People

Contributors

king6cong avatar larsvinter avatar nomuppets avatar sh3rawi avatar

Watchers

 avatar  avatar

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.