halogen
Version:
A collection of loading spinners with React.js
120 lines (105 loc) • 2.77 kB
JavaScript
'use strict';
var React = require('react');
var assign = require('domkit/appendVendorPrefix');
var insertKeyframesRule = require('domkit/insertKeyframesRule');
/**
* @type {Object}
*/
var keyframes = {
'0%, 100%': {
transform: 'scale(0)'
},
'50%': {
transform: 'scale(1.0)'
}
};
/**
* @type {String}
*/
var animationName = insertKeyframesRule(keyframes);
var Loader = React.createClass({
displayName: 'Loader',
/**
* @type {Object}
*/
propTypes: {
loading: React.PropTypes.bool,
color: React.PropTypes.string,
size: React.PropTypes.string
},
/**
* @return {Object}
*/
getDefaultProps: function getDefaultProps() {
return {
loading: true,
color: '#ffffff',
size: '60px'
};
},
/**
* @return {Object}
*/
getBallStyle: function getBallStyle() {
return {
backgroundColor: this.props.color,
width: this.props.size,
height: this.props.size,
borderRadius: '100%',
opacity: 0.6,
position: 'absolute',
top: 0,
left: 0,
verticalAlign: this.props.verticalAlign
};
},
/**
* @param {Number} i
* @return {Object}
*/
getAnimationStyle: function getAnimationStyle(i) {
var animation = [animationName, '2s', i == 1 ? '1s' : '0s', 'infinite', 'ease-in-out'].join(' ');
var animationFillMode = 'both';
return {
animation: animation,
animationFillMode: animationFillMode
};
},
/**
* @param {Number} i
* @return {Object}
*/
getStyle: function getStyle(i) {
if (i) {
return assign(this.getBallStyle(i), this.getAnimationStyle(i));
}
return assign({
width: this.props.size,
height: this.props.size,
position: 'relative'
});
},
/**
* @param {Boolean} loading
* @return {ReactComponent || null}
*/
renderLoader: function renderLoader(loading) {
if (loading) {
return React.createElement(
'div',
{ id: this.props.id, className: this.props.className },
React.createElement(
'div',
{ style: this.getStyle() },
React.createElement('div', { style: this.getStyle(1) }),
React.createElement('div', { style: this.getStyle(2) })
)
);
}
return null;
},
render: function render() {
return this.renderLoader(this.props.loading);
}
});
module.exports = Loader;