<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>Timer with color stops</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<dic id="root">
</div>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.js'></script>
<script src="js/index.js"></script>
</body>
</html>
/*Downloaded from https://www.codeseek.co/025pro/timer-with-color-stops-pPJzzo */
.timer-progress {
height: 10px;
width: 250px;
border-radius: 25px;
position: relative;
background: #ccc;
}
.timer-progress .after {
display: block;
position: absolute;
top: 0;
left: 0;
height: 100%;
border-radius: 25px;
transition: width linear 1s, background-color linear .3s;
}
/*Downloaded from https://www.codeseek.co/025pro/timer-with-color-stops-pPJzzo */
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var TimerProgress = function (_React$Component) {
_inherits(TimerProgress, _React$Component);
function TimerProgress(props) {
_classCallCheck(this, TimerProgress);
var _this = _possibleConstructorReturn(this, (TimerProgress.__proto__ || Object.getPrototypeOf(TimerProgress)).call(this, props));
_this.timer = props.timer;
_this.max = props.max;
_this.width = props.width;
if (props.current > _this.max) {
throw "Current progress cannot be higher than max value";
}
_this.state = {
current: props.current
};
_this.stops = props.stops;
return _this;
}
_createClass(TimerProgress, [{
key: "componentDidMount",
value: function componentDidMount() {
var self = this;
this.tick = function () {
self.setState({
current: self.state.current - 1
});
};
this.timer.addTick(this.tick);
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.timer.removeTick(this.tick);
}
}, {
key: "componentWillReceiveProps",
value: function componentWillReceiveProps(nextProps) {
if (nextProps.current > this.max) {
throw "Current progress cannot be higher than max value";
}
this.setState({
current: nextProps.current
});
}
}, {
key: "getProgressWidth",
value: function getProgressWidth() {
return this.state.current / this.max * this.width + "px";
}
}, {
key: "getBackColor",
value: function getBackColor() {
var curPercent = this.state.current / this.max * 100;
var backs = this.stops.sort(function (a, b) {
return a.percent - b.bercent;
});
var first = backs.filter(function (a) {
return a.percent <= curPercent;
})[0];
return first.color;
}
}, {
key: "render",
value: function render() {
return React.createElement(
"div",
{ className: "timer-progress", style: { width: this.width } },
React.createElement("div", { className: "after", style: { width: this.getProgressWidth(), background: this.getBackColor() } })
);
}
}]);
return TimerProgress;
}(React.Component);
/* ==== DEMO ==== */
var current = 15;
var step = 1;
var max = 15;
var colorStops = [{
percent: 40,
color: "green"
}, {
percent: 20,
color: "orange"
}, {
percent: 0,
color: "red"
}];
var GlobalTimer = function GlobalTimer() {
var o = this;
var ticks = [];
o.addTick = function (tick) {
ticks.push(tick);
};
o.removeTick = function (tick) {
var index = ticks.indexOf(tick);
ticks.splice(index, 1);
};
setInterval(function () {
ticks.forEach(function (t) {
t();
});
}, 1000);
return o;
};
var globalTimer = new GlobalTimer();
ReactDOM.render(React.createElement(TimerProgress, { max: max, current: current, width: 200, timer: globalTimer, stops: colorStops }), document.getElementById('root'));