Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add animation event #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions src/NotificationItem.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,33 @@ var Helpers = require('./helpers');
var merge = require('object-assign');

/* From Modernizr */
var whichTransitionEvent = function() {
var whichEvent = function (props) {
var t;
var el = document.createElement('fakeelement');

for (p in props) {
if (el.style[p] !== undefined) {
return props[p];
}
}
}
var whichTransitionEvent = function() {
var transitions = {
'transition': 'transitionend',
'OTransition': 'oTransitionEnd',
'MozTransition': 'transitionend',
'WebkitTransition': 'webkitTransitionEnd'
};

return whichEvent(transitions)
};
var whichAnimationEvent = function() {
var animations = {
'animation': 'animationend',
'webkitAnimation': 'webkitAnimationEnd'
};

for (t in transitions) {
if (el.style[t] !== undefined) {
return transitions[t];
}
}
return whichEvent(animations)
};

var NotificationItem = React.createClass({
Expand Down Expand Up @@ -185,6 +197,7 @@ var NotificationItem = React.createClass({
componentDidMount: function() {
var self = this;
var transitionEvent = whichTransitionEvent();
var animationEvent = whichAnimationEvent();
var notification = this.props.notification;
var element = ReactDOM.findDOMNode(this);

Expand All @@ -195,13 +208,17 @@ var NotificationItem = React.createClass({
// Watch for transition end
if (!this._noAnimation) {
if (transitionEvent) {
element.addEventListener(transitionEvent, this._onTransitionEnd);
} else {
transitionEvent && element.addEventListener(transitionEvent, this._onTransitionEnd);
}
if (animationEvent) {
animationEvent && element.addEventListener(animationEvent, this._onTransitionEnd);
}

if (!(transitionEvent || animationEvent)) {
this._noAnimation = true;
}
}


if (notification.autoDismiss) {
this._notificationTimer = new Helpers.Timer(function() {
self._hideNotification();
Expand All @@ -228,7 +245,15 @@ var NotificationItem = React.createClass({
componentWillUnmount: function() {
var element = ReactDOM.findDOMNode(this);
var transitionEvent = whichTransitionEvent();
element.removeEventListener(transitionEvent, this._onTransitionEnd);
var animationEvent = whichAnimationEvent();

if (transitionEvent) {
element.removeEventListener(transitionEvent, this._onTransitionEnd);
}
if (animationEvent) {
element.removeEventListener(animationEvent, this._onTransitionEnd);
}

this._isMounted = false;
},

Expand Down