Giter VIP home page Giter VIP logo

react-style-transition-group's Introduction

React - Style Transition Group

BETA v0.0.0

The StyleTransitionGroup is very similar to React's CSSTransitionGroup, but it uses style updates instead of class updates. If your're unfamilar with React's CSSTransitionGroup, Facebook has written a great set of documentation that explains it's use and more about animation in general as it pertains to React.

The CSSTransitionGroup alters classes during the entry and exit parts of a component's lifecycle by watching browser transition events in order to apply classes in a specific order. This technique however requires the CSS attributes for those classes be written ahead of time.

Sometimes however, those attributes need to be dynamically generated at run time and this is where StyleTransitionGroup comes in. Instead of passing in a transitionName to the <ReactCSSTransitionGroup>, the <ReactStyleTransitionGroup> requires a transitionStyles object to be passed to the child components at render. These styles can be calculated and therefore can by dynamic based on a variety of information.

Example

The example below is a theoretical menu of vertical items that have transition timing & transform values relevant to the number of items being drawn.

var ReactStyleTransitionGroup = require( 'react-style-transition-group' );

var menuClass = React.createClass( {

  // ...

  render: function() {
    var navItems = this.props.navItems.map( function( navItem, index ) {
      var transitionTiming = ( ( index + 1 ) * 200 ) + 'ms';
      var transitionStyles = {
        enter: {
          transition: 'transform ' + transitionTiming + ' ease, opacity ' + transitionTiming + ' ease',
          transform: 'translateY(-' + ( ( index + 1 ) * 100 ) + '%)',
          opacity: '0'
        },
        enterActive: {
          transform: 'translateY(0)',
          opacity: '1'
        },
        leave: {
          transition: 'opacity 0.2s ease',
          opacity: '1'
        },
        leaveActive: {
          opacity: '0'
        }
      };

      return (
        <MenuItem key={ 'menuItem-' + index } transitionStyles={ transitionStyles }>{ navItem }</MenuItem>
      );
    }, this );
    
    return (
      <ReactStyleTransitionGroup component="ul">
        { ( this.props.open ) ?
          navItems
        : null }
      </ReactStyleTransitionGroup>
    );
  }

} );

The transitionStyles prop takes a particular shape of object that represents the styles that will be applied during specific times at the component's lifecycle.

var transitionStyles = {
  enter: {
    // ... component is about to enter, timing is key
  },
  enterActive: {
    // ... component has been painted, how will animate?
  },
  leave: {
    // ... component is about to leave, timing is key
  },
  leaveActive: {
    // ... component styles to transition to before being unmounted
  }
}

The style attributes themselves follow the inline style format as described in React's documentation.

Install

With npm do:

npm install git://github.com/adambbecker/react-style-transition-group.git

And require it just like other components:

var ReactStyleTransitionGroup = require( 'react-style-transition-group' );

License

MIT

react-style-transition-group's People

Contributors

adambbecker 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.