react-spark-scroll
Version:
Scroll-based animation and actions for React
151 lines (121 loc) • 6.31 kB
JavaScript
;
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 _get = function get(_x, _x2, _x3) { var _again = true; _function: while (_again) { var object = _x, property = _x2, receiver = _x3; _again = false; if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { _x = parent; _x2 = property; _x3 = receiver; _again = true; desc = parent = undefined; continue _function; } } else if ('value' in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } } };
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 _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 React = require('react');
var ReactDOM = require('react-dom');
var sparkFactory = require('./spark');
var _isString = function _isString(x) {
return typeof x === 'string';
};
function factory(options) {
var proxyElements = {};
var spark = sparkFactory(options);
var invalidate = spark.invalidate;
var enableInvalidationInterval = spark.enableInvalidationInterval;
var disableInvalidationInterval = spark.disableInvalidationInterval;
var sparkScrollFactory = function sparkScrollFactory(defaultComponent) {
return (function (_React$Component) {
_inherits(SparkScroll, _React$Component);
function SparkScroll() {
_classCallCheck(this, SparkScroll);
_get(Object.getPrototypeOf(SparkScroll.prototype), 'constructor', this).apply(this, arguments);
}
_createClass(SparkScroll, [{
key: 'render',
value: function render() {
var Component = this.props.component || defaultComponent;
var _props = this.props;
var callback = _props.callback;
var component = _props.component;
var proxy = _props.proxy;
var timeline = _props.timeline;
var children = _props.children;
var rest = _objectWithoutProperties(_props, ['callback', 'component', 'proxy', 'timeline', 'children']);
return React.createElement(
Component,
rest,
this.props.children
);
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
var _this = this;
var element = ReactDOM.findDOMNode(this);
if (this.props.proxy) {
spark(element, function () {
return proxyElements[_this.props.proxy] || element;
}, this.props.timeline, this.props);
} else {
spark(element, function () {
return element;
}, this.props.timeline, this.props);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
spark.cleanup();
}
}]);
return SparkScroll;
})(React.Component);
};
var SparkScroll = sparkScrollFactory('div');
SparkScroll.div = SparkScroll;
var sparkProxyFactory = function sparkProxyFactory(defaultComponent) {
return (function (_React$Component2) {
_inherits(SparkProxy, _React$Component2);
function SparkProxy() {
_classCallCheck(this, SparkProxy);
_get(Object.getPrototypeOf(SparkProxy.prototype), 'constructor', this).apply(this, arguments);
}
_createClass(SparkProxy, [{
key: 'render',
value: function render() {
var Component = this.props.component || defaultComponent;
var _props2 = this.props;
var component = _props2.component;
var proxyId = _props2.proxyId;
var children = _props2.children;
var rest = _objectWithoutProperties(_props2, ['component', 'proxyId', 'children']);
return React.createElement(
Component,
rest,
this.props.children
);
}
}, {
key: 'componentDidMount',
value: function componentDidMount() {
proxyElements[this.props.proxyId] = ReactDOM.findDOMNode(this);
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
delete proxyElements[this.props.proxyId];
}
}]);
return SparkProxy;
})(React.Component);
};
var SparkProxy = sparkProxyFactory('div');
SparkProxy.div = SparkProxy;
['span', 'h1', 'h2', 'h3', 'h4', 'h5', 'li', 'ul', 'ol', 'header', 'section'].forEach(function (tag) {
SparkScroll[tag] = sparkScrollFactory(tag);
SparkProxy[tag] = sparkProxyFactory(tag);
});
return {
sparkScrollFactory: sparkScrollFactory,
sparkProxyFactory: sparkProxyFactory,
SparkScroll: SparkScroll,
SparkProxy: SparkProxy,
invalidate: invalidate,
enableInvalidationInterval: enableInvalidationInterval,
disableInvalidationInterval: disableInvalidationInterval
};
}
module.exports = factory;