UNPKG

react-simple-scroll-container

Version:
141 lines (114 loc) 6.17 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 _class, _temp2; var _react = require('react'); var _react2 = _interopRequireDefault(_react); var _propTypes = require('prop-types'); var _propTypes2 = _interopRequireDefault(_propTypes); var _reactDom = require('react-dom'); 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 ScrollContainer = (_temp2 = _class = function (_React$Component) { _inherits(ScrollContainer, _React$Component); function ScrollContainer() { var _ref; var _temp, _this, _ret; _classCallCheck(this, ScrollContainer); for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } return _ret = (_temp = (_this = _possibleConstructorReturn(this, (_ref = ScrollContainer.__proto__ || Object.getPrototypeOf(ScrollContainer)).call.apply(_ref, [this].concat(args))), _this), _this.state = { activeElement: '' }, _this.elements = {}, _this.getActiveElement = function () { var result = _this.state.activeElement; var containerScrollTop = _this.container.scrollTop; Object.keys(_this.elements).forEach(function (key) { var element = (0, _reactDom.findDOMNode)(_this.elements[key]); // if offset height === 0, the element is empty if (element.offsetHeight !== 0) { var isActive = containerScrollTop >= element.offsetTop - _this.props.topScrollOffset && containerScrollTop <= element.offsetHeight + element.offsetTop; if (isActive) { result = key; } } }); return result; }, _this.handleScroll = function (e) { var activeElement = _this.getActiveElement(); if (_this.state.activeElement !== activeElement) { console.log('active!'); // Do the call back with the active id _this.setState({ activeElement: activeElement }); _this.props.topScrollCallback(activeElement); } }, _this.getNode = function (name) { return (0, _reactDom.findDOMNode)(_this.elements[name]); }, _this.register = function (name, ref) { _this.elements[name] = ref; }, _this.unregister = function (name) { delete _this.elements[name]; }, _temp), _possibleConstructorReturn(_this, _ret); } _createClass(ScrollContainer, [{ key: 'getChildContext', value: function getChildContext() { return { scroll: { register: this.register, unregister: this.unregister } }; } }, { key: 'render', value: function render() { var _this2 = this; return _react2.default.createElement( 'div', { style: _extends({ overflowY: 'scroll', position: 'relative' }, this.props.extraStyle), id: this.props.containerId, className: this.props.extraClassName, onScroll: this.props.topScroll ? this.handleScroll : function () { return null; }, ref: function ref(_ref2) { return _this2.container = _ref2; } }, this.props.children ); } }]); return ScrollContainer; }(_react2.default.Component), _class.childContextTypes = { scroll: _propTypes2.default.object }, _temp2); ScrollContainer.defaultProps = { containerId: '', topScroll: false, topScrollCallback: function topScrollCallback() { return null; }, topScrollOffset: 10, extraStyle: {}, extraClassName: '' }; ScrollContainer.propTypes = { containerId: _propTypes2.default.string, topScroll: _propTypes2.default.bool, topScrollCallback: _propTypes2.default.func, topScrollOffset: _propTypes2.default.number, extraStyle: _propTypes2.default.object, extraClassName: _propTypes2.default.string }; exports.default = ScrollContainer; module.exports = exports['default'];