react-typing
Version:
Typing animation with react
142 lines (112 loc) • 5.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _utils = require('./utils');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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 Typing = function (_Component) {
_inherits(Typing, _Component);
function Typing(props) {
_classCallCheck(this, Typing);
var _this = _possibleConstructorReturn(this, (Typing.__proto__ || Object.getPrototypeOf(Typing)).call(this, props));
_this.typeCharacter = _this.typeCharacter.bind(_this);
_this.state = {
charactersToType: '',
charactersTyped: [],
characterIndex: 0,
keyDelay: props.keyDelay || 100
};
return _this;
}
_createClass(Typing, [{
key: 'componentDidMount',
value: function componentDidMount() {
var children = this.props.children;
var keyDelay = this.state.keyDelay;
var charactersToType = (0, _utils.extractText)(children);
this.setState(function () {
return { charactersToType: charactersToType };
}); // eslint-disable-line
this.startTyping(keyDelay);
}
}, {
key: 'componentWillReceiveProps',
value: function componentWillReceiveProps(nextProps) {
if (nextProps.keyDelay !== this.state.keyDelay) {
this.setState(function () {
return { keyDelay: nextProps.keyDelay };
});
this.startTyping(nextProps.keyDelay);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
clearInterval(this.typeCharacterInterval);
}
}, {
key: 'startTyping',
value: function startTyping(keyDelay) {
clearInterval(this.typeCharacterInterval);
this.typeCharacterInterval = setInterval(this.typeCharacter, keyDelay);
}
}, {
key: 'typeCharacter',
value: function typeCharacter() {
var _props = this.props,
children = _props.children,
onDone = _props.onDone;
var _state = this.state,
charactersToType = _state.charactersToType,
characterIndex = _state.characterIndex;
var composeTreeText = (0, _utils.composeTree)(children, charactersToType, characterIndex);
this.setState(function () {
return {
charactersTyped: composeTreeText,
characterIndex: characterIndex + 1
};
});
if (charactersToType && charactersToType.length - 1 < characterIndex || !charactersToType) {
clearInterval(this.typeCharacterInterval);
if (onDone) {
onDone();
}
}
}
}, {
key: 'render',
value: function render() {
var restProps = _objectWithoutProperties(this.props, []);
delete restProps.children;
delete restProps.keyDelay;
delete restProps.onDone;
var charactersTyped = this.state.charactersTyped;
return _react2.default.createElement(
'span',
restProps,
charactersTyped
);
}
}]);
return Typing;
}(_react.Component);
Typing.defaultProps = {
children: '',
keyDelay: 100,
onDone: null
};
Typing.propTypes = {
children: _propTypes2.default.node,
keyDelay: _propTypes2.default.number,
onDone: _propTypes2.default.func
};
exports.default = Typing;