UNPKG

react-countup

Version:

A React component wrapper around CountUp.js

352 lines (294 loc) 10.6 kB
'use strict'; function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var Count = _interopDefault(require('countup.js')); var PropTypes = _interopDefault(require('prop-types')); var React = require('react'); var React__default = _interopDefault(React); var warning = _interopDefault(require('warning')); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a 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); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _possibleConstructorReturn(self, call) { if (call && (typeof call === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } var CountUp = /*#__PURE__*/ function (_Component) { _inherits(CountUp, _Component); function CountUp() { var _getPrototypeOf2; var _this; _classCallCheck(this, CountUp); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(CountUp)).call.apply(_getPrototypeOf2, [this].concat(args))); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "createInstance", function () { if (typeof _this.props.children === 'function') { // Warn when user didn't use containerRef at all warning(_this.containerRef.current && _this.containerRef.current instanceof HTMLElement, "Couldn't find attached element to hook the CountUp instance into! Try to attach \"containerRef\" from the render prop to a an HTMLElement, eg. <span ref={containerRef} />."); } var _this$props = _this.props, decimal = _this$props.decimal, decimals = _this$props.decimals, duration = _this$props.duration, easingFn = _this$props.easingFn, end = _this$props.end, formattingFn = _this$props.formattingFn, prefix = _this$props.prefix, separator = _this$props.separator, start = _this$props.start, suffix = _this$props.suffix, useEasing = _this$props.useEasing; return new Count(_this.containerRef.current, start, end, decimals, duration, { decimal: decimal, easingFn: easingFn, formattingFn: formattingFn, separator: separator, prefix: prefix, suffix: suffix, useEasing: useEasing, useGrouping: !!separator }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "pauseResume", function () { var _assertThisInitialize = _assertThisInitialized(_assertThisInitialized(_this)), reset = _assertThisInitialize.reset, start = _assertThisInitialize.restart, update = _assertThisInitialize.update; var onPauseResume = _this.props.onPauseResume; _this.instance.pauseResume(); onPauseResume({ reset: reset, start: start, update: update }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "reset", function () { var _assertThisInitialize2 = _assertThisInitialized(_assertThisInitialized(_this)), pauseResume = _assertThisInitialize2.pauseResume, start = _assertThisInitialize2.restart, update = _assertThisInitialize2.update; var onReset = _this.props.onReset; _this.instance.reset(); onReset({ pauseResume: pauseResume, start: start, update: update }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "restart", function () { _this.reset(); _this.start(); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "start", function () { var _assertThisInitialize3 = _assertThisInitialized(_assertThisInitialized(_this)), pauseResume = _assertThisInitialize3.pauseResume, reset = _assertThisInitialize3.reset, start = _assertThisInitialize3.restart, update = _assertThisInitialize3.update; var _this$props2 = _this.props, delay = _this$props2.delay, onEnd = _this$props2.onEnd, onStart = _this$props2.onStart; var run = function run() { return _this.instance.start(function () { return onEnd({ pauseResume: pauseResume, reset: reset, start: start, update: update }); }); }; // Delay start if delay prop is properly set if (delay > 0) { setTimeout(run, delay * 1000); } else { run(); } onStart({ pauseResume: pauseResume, reset: reset, update: update }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "update", function (newEnd) { var _assertThisInitialize4 = _assertThisInitialized(_assertThisInitialized(_this)), pauseResume = _assertThisInitialize4.pauseResume, reset = _assertThisInitialize4.reset, start = _assertThisInitialize4.restart; var onUpdate = _this.props.onUpdate; _this.instance.update(newEnd); onUpdate({ pauseResume: pauseResume, reset: reset, start: start }); }); _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "containerRef", React__default.createRef()); return _this; } _createClass(CountUp, [{ key: "componentDidMount", value: function componentDidMount() { var _this$props3 = this.props, children = _this$props3.children, delay = _this$props3.delay; this.instance = this.createInstance(); // Don't invoke start if component is used as a render prop if (typeof children === 'function' && delay !== 0) return; // Otherwise just start immediately this.start(); } }, { key: "shouldComponentUpdate", value: function shouldComponentUpdate(nextProps) { var hasCertainPropsChanged = this.props.duration !== nextProps.duration || this.props.end !== nextProps.end || this.props.start !== nextProps.start; return hasCertainPropsChanged || this.props.redraw; } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { // If duration or start has changed, there's no way to update the duration // or start value. So we need to re-create the CountUp instance in order to // restart it. if (this.props.duration !== prevProps.duration || this.props.start !== prevProps.start) { this.instance = this.createInstance(); this.start(); } // Only end value has changed, so reset and and re-animate with the updated // end value. if (this.props.end !== prevProps.end) { this.instance.reset(); this.instance.update(this.props.end); } } }, { key: "render", value: function render() { var _this$props4 = this.props, children = _this$props4.children, className = _this$props4.className, style = _this$props4.style; var containerRef = this.containerRef, pauseResume = this.pauseResume, reset = this.reset, restart = this.restart, update = this.update; if (typeof children === 'function') { return children({ countUpRef: containerRef, pauseResume: pauseResume, reset: reset, start: restart, update: update }); } return React__default.createElement("span", { className: className, ref: containerRef, style: style }); } }]); return CountUp; }(React.Component); _defineProperty(CountUp, "propTypes", { decimal: PropTypes.string, decimals: PropTypes.number, delay: PropTypes.number, easingFn: PropTypes.func, end: PropTypes.number.isRequired, formattingFn: PropTypes.func, onEnd: PropTypes.func, onStart: PropTypes.func, prefix: PropTypes.string, redraw: PropTypes.bool, separator: PropTypes.string, start: PropTypes.number, suffix: PropTypes.string, style: PropTypes.object, useEasing: PropTypes.bool }); _defineProperty(CountUp, "defaultProps", { decimal: '.', decimals: 0, delay: null, duration: null, easingFn: null, formattingFn: null, onEnd: function onEnd() {}, onPauseResume: function onPauseResume() {}, onReset: function onReset() {}, onStart: function onStart() {}, onUpdate: function onUpdate() {}, prefix: '', redraw: false, separator: '', start: 0, suffix: '', style: undefined, useEasing: true }); module.exports = CountUp;