UNPKG

halogen

Version:

A collection of loading spinners with React.js

141 lines (124 loc) 3.66 kB
'use strict'; var React = require('react'); var assign = require('domkit/appendVendorPrefix'); var insertKeyframesRule = require('domkit/insertKeyframesRule'); /** * @type {Object} */ var keyframes = { '0%': { transform: 'scale(1)' }, '50%': { transform: 'scale(0.5)', opacity: 0.7 }, '100%': { transform: 'scale(1)', opacity: 1 } }; /** * @type {String} */ var animationName = insertKeyframesRule(keyframes); /** * @param {Number} top * @return {Number} */ function random(top) { return Math.random() * top; } var Loader = React.createClass({ displayName: 'Loader', /** * @type {Object} */ propTypes: { loading: React.PropTypes.bool, color: React.PropTypes.string, size: React.PropTypes.string, margin: React.PropTypes.string }, /** * @return {Object} */ getDefaultProps: function getDefaultProps() { return { loading: true, color: '#ffffff', size: '15px', margin: '2px' }; }, /** * @return {Object} */ getBallStyle: function getBallStyle() { return { backgroundColor: this.props.color, width: this.props.size, height: this.props.size, margin: this.props.margin, borderRadius: '100%', verticalAlign: this.props.verticalAlign }; }, /** * @param {Number} i * @return {Object} */ getAnimationStyle: function getAnimationStyle(i) { var animationDuration = random(100) / 100 + 0.6 + 's'; var animationDelay = random(100) / 100 - 0.2 + 's'; var animation = [animationName, animationDuration, animationDelay, 'infinite', 'ease'].join(' '); var animationFillMode = 'both'; return { animation: animation, animationFillMode: animationFillMode }; }, /** * @param {Number} i * @return {Object} */ getStyle: function getStyle(i) { return assign(this.getBallStyle(i), this.getAnimationStyle(i), { display: 'inline-block' }); }, /** * @param {Boolean} loading * @return {ReactComponent || null} */ renderLoader: function renderLoader(loading) { if (loading) { var style = { width: parseFloat(this.props.size) * 3 + parseFloat(this.props.margin) * 6, fontSize: 0 }; return React.createElement( 'div', { id: this.props.id, className: this.props.className }, React.createElement( 'div', { style: style }, React.createElement('div', { style: this.getStyle(1) }), React.createElement('div', { style: this.getStyle(2) }), React.createElement('div', { style: this.getStyle(3) }), React.createElement('div', { style: this.getStyle(4) }), React.createElement('div', { style: this.getStyle(5) }), React.createElement('div', { style: this.getStyle(6) }), React.createElement('div', { style: this.getStyle(7) }), React.createElement('div', { style: this.getStyle(8) }), React.createElement('div', { style: this.getStyle(9) }) ) ); } return null; }, render: function render() { return this.renderLoader(this.props.loading); } }); module.exports = Loader;