UNPKG

beauty-scroll-container

Version:

A react component that provider a beauty scroll container

312 lines (262 loc) 8.92 kB
/** * Bundle of beauty-scroll-container * Generated: 2022-05-09 * Version: 1.1.2 * License: MIT * Author: 2631541504@qq.com */ import React, { PureComponent } from 'react'; import classNames from 'classnames'; 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 ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; } function _objectSpread2(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } 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 _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } 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 _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } } function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _createSuper(Derived) { return function () { var Super = _getPrototypeOf(Derived), result; if (_isNativeReflectConstruct()) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } var initState = function initState(props) { return { preProps: props, hScrollable: false, vScrollable: false, reachLeft: true, reachRight: true, reachTop: true, reachBottom: true }; }; var BeautyScrollContainer = /*#__PURE__*/function (_PureComponent) { _inherits(BeautyScrollContainer, _PureComponent); var _super = _createSuper(BeautyScrollContainer); function BeautyScrollContainer() { var _this; _classCallCheck(this, BeautyScrollContainer); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "ref", React.createRef()); _defineProperty(_assertThisInitialized(_this), "state", initState(_this.props)); _defineProperty(_assertThisInitialized(_this), "removeListener", function () { return null; }); _defineProperty(_assertThisInitialized(_this), "updateState", function () { var el = _this.ref.current; var clientWidth = el.clientWidth, scrollWidth = el.scrollWidth, clientHeight = el.clientHeight, scrollHeight = el.scrollHeight, scrollTop = el.scrollTop, scrollLeft = el.scrollLeft; _this.setState(function () { return { hScrollable: scrollWidth > clientWidth, vScrollable: scrollHeight > clientHeight, reachLeft: scrollLeft <= 1, reachRight: scrollLeft >= scrollWidth - clientWidth, reachTop: scrollTop <= 1, reachBottom: scrollTop >= scrollHeight - clientHeight }; }); }); return _this; } _createClass(BeautyScrollContainer, [{ key: "componentDidMount", value: function componentDidMount() { var _this2 = this; this.updateState(); var el = this.ref.current; el.addEventListener('scroll', this.updateState); window.addEventListener('resize', this.updateState); if (typeof MutationObserver !== 'undefined') { var observer = new MutationObserver(this.updateState); observer.observe(el, { attributes: true, childList: true, subtree: true }); this.removeListener = function () { observer.disconnect(); el.removeEventListener('scroll', _this2.updateState); window.removeEventListener('resize', _this2.updateState); }; } else { this.removeListener = function () { el.removeEventListener('scroll', _this2.updateState); window.removeEventListener('resize', _this2.updateState); }; } } }, { key: "componentWillUnmount", value: function componentWillUnmount() { this.removeListener(); } }, { key: "render", value: function render() { var _this3 = this; return /*#__PURE__*/React.createElement("div", { className: classNames('beauty-scroll-container', { 'beauty-scroll-horizontal-scrollable': this.state.hScrollable, 'beauty-scroll-vertical-scrollable': this.state.vScrollable, 'beauty-scroll-reach-left': this.state.reachLeft, 'beauty-scroll-reach-right': this.state.reachRight, 'beauty-scroll-reach-top': this.state.reachTop, 'beauty-scroll-reach-bottom': this.state.reachBottom }, this.props.className), style: this.props.style }, /*#__PURE__*/React.createElement("div", { className: classNames('beauty-scroll-content', this.props.contentClassName), style: this.props.contentStyle, ref: this.ref }, this.props.children), ['left', 'right', 'top', 'bottom'].map(function (pos) { return /*#__PURE__*/React.createElement("div", { key: pos, className: "beauty-scroll-indicator beauty-scroll-indicator-".concat(pos), style: { color: _this3.props.indicatorColor || '#0002' } }); })); } }], [{ key: "getDerivedStateFromProps", value: function getDerivedStateFromProps(props, state) { return _objectSpread2(_objectSpread2({}, state), {}, { preProps: props }); } }]); return BeautyScrollContainer; }(PureComponent); export default BeautyScrollContainer;