UNPKG

react-sticky

Version:
135 lines (105 loc) 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 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 _raf = require("raf"); var _raf2 = _interopRequireDefault(_raf); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 Container = function (_PureComponent) { _inherits(Container, _PureComponent); function Container() { var _ref; var _temp, _this, _ret; _classCallCheck(this, Container); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = Container.__proto__ || Object.getPrototypeOf(Container)).call.apply(_ref, [this].concat(args))), _this), _this.events = ["resize", "scroll", "touchstart", "touchmove", "touchend", "pageshow", "load"], _this.subscribers = [], _this.rafHandle = null, _this.subscribe = function (handler) { _this.subscribers = _this.subscribers.concat(handler); }, _this.unsubscribe = function (handler) { _this.subscribers = _this.subscribers.filter(function (current) { return current !== handler; }); }, _this.notifySubscribers = function (evt) { if (!_this.framePending) { var currentTarget = evt.currentTarget; _this.rafHandle = (0, _raf2.default)(function () { _this.framePending = false; var _this$node$getBoundin = _this.node.getBoundingClientRect(), top = _this$node$getBoundin.top, bottom = _this$node$getBoundin.bottom; _this.subscribers.forEach(function (handler) { return handler({ distanceFromTop: top, distanceFromBottom: bottom, eventSource: currentTarget === window ? document.body : _this.node }); }); }); _this.framePending = true; } }, _this.getParent = function () { return _this.node; }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(Container, [{ key: "getChildContext", value: function getChildContext() { return { subscribe: this.subscribe, unsubscribe: this.unsubscribe, getParent: this.getParent }; } }, { key: "componentDidMount", value: function componentDidMount() { var _this2 = this; this.events.forEach(function (event) { return window.addEventListener(event, _this2.notifySubscribers); }); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { var _this3 = this; if (this.rafHandle) { _raf2.default.cancel(this.rafHandle); this.rafHandle = null; } this.events.forEach(function (event) { return window.removeEventListener(event, _this3.notifySubscribers); }); } }, { key: "render", value: function render() { var _this4 = this; return _react2.default.createElement("div", _extends({}, this.props, { ref: function ref(node) { return _this4.node = node; }, onScroll: this.notifySubscribers, onTouchStart: this.notifySubscribers, onTouchMove: this.notifySubscribers, onTouchEnd: this.notifySubscribers })); } }]); return Container; }(_react.PureComponent); Container.childContextTypes = { subscribe: _propTypes2.default.func, unsubscribe: _propTypes2.default.func, getParent: _propTypes2.default.func }; exports.default = Container;