react / react-0.13.3 / examples / basic-commonjs / node_modules / react / lib / ReactCSSTransitionGroupChild.js
81143 views/**1* Copyright 2013-2015, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @typechecks9* @providesModule ReactCSSTransitionGroupChild10*/1112'use strict';1314var React = require("./React");1516var CSSCore = require("./CSSCore");17var ReactTransitionEvents = require("./ReactTransitionEvents");1819var onlyChild = require("./onlyChild");20var warning = require("./warning");2122// We don't remove the element from the DOM until we receive an animationend or23// transitionend event. If the user screws up and forgets to add an animation24// their node will be stuck in the DOM forever, so we detect if an animation25// does not start and if it doesn't, we just call the end listener immediately.26var TICK = 17;27var NO_EVENT_TIMEOUT = 5000;2829var noEventListener = null;303132if ("production" !== process.env.NODE_ENV) {33noEventListener = function() {34("production" !== process.env.NODE_ENV ? warning(35false,36'transition(): tried to perform an animation without ' +37'an animationend or transitionend event after timeout (' +38'%sms). You should either disable this ' +39'transition in JS or add a CSS animation/transition.',40NO_EVENT_TIMEOUT41) : null);42};43}4445var ReactCSSTransitionGroupChild = React.createClass({46displayName: 'ReactCSSTransitionGroupChild',4748transition: function(animationType, finishCallback) {49var node = this.getDOMNode();50var className = this.props.name + '-' + animationType;51var activeClassName = className + '-active';52var noEventTimeout = null;5354var endListener = function(e) {55if (e && e.target !== node) {56return;57}58if ("production" !== process.env.NODE_ENV) {59clearTimeout(noEventTimeout);60}6162CSSCore.removeClass(node, className);63CSSCore.removeClass(node, activeClassName);6465ReactTransitionEvents.removeEndEventListener(node, endListener);6667// Usually this optional callback is used for informing an owner of68// a leave animation and telling it to remove the child.69if (finishCallback) {70finishCallback();71}72};7374ReactTransitionEvents.addEndEventListener(node, endListener);7576CSSCore.addClass(node, className);7778// Need to do this to actually trigger a transition.79this.queueClass(activeClassName);8081if ("production" !== process.env.NODE_ENV) {82noEventTimeout = setTimeout(noEventListener, NO_EVENT_TIMEOUT);83}84},8586queueClass: function(className) {87this.classNameQueue.push(className);8889if (!this.timeout) {90this.timeout = setTimeout(this.flushClassNameQueue, TICK);91}92},9394flushClassNameQueue: function() {95if (this.isMounted()) {96this.classNameQueue.forEach(97CSSCore.addClass.bind(CSSCore, this.getDOMNode())98);99}100this.classNameQueue.length = 0;101this.timeout = null;102},103104componentWillMount: function() {105this.classNameQueue = [];106},107108componentWillUnmount: function() {109if (this.timeout) {110clearTimeout(this.timeout);111}112},113114componentWillAppear: function(done) {115if (this.props.appear) {116this.transition('appear', done);117} else {118done();119}120},121122componentWillEnter: function(done) {123if (this.props.enter) {124this.transition('enter', done);125} else {126done();127}128},129130componentWillLeave: function(done) {131if (this.props.leave) {132this.transition('leave', done);133} else {134done();135}136},137138render: function() {139return onlyChild(this.props.children);140}141});142143module.exports = ReactCSSTransitionGroupChild;144145146