Dashboard sipadu mbip
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AutoAffix.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
  2. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  3. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  4. function _extends() { _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; }; return _extends.apply(this, arguments); }
  5. import getOffset from 'dom-helpers/query/offset';
  6. import listen from 'dom-helpers/events/listen';
  7. import requestAnimationFrame from 'dom-helpers/util/requestAnimationFrame';
  8. import PropTypes from 'prop-types';
  9. import componentOrElement from 'prop-types-extra/lib/componentOrElement';
  10. import React from 'react';
  11. import Affix from './Affix';
  12. import getContainer from './utils/getContainer';
  13. import getDocumentHeight from './utils/getDocumentHeight';
  14. import ownerDocument from './utils/ownerDocument';
  15. import ownerWindow from './utils/ownerWindow';
  16. var displayName = 'AutoAffix';
  17. var propTypes = _extends({}, Affix.propTypes, {
  18. /**
  19. * The logical container node or component for determining offset from bottom
  20. * of viewport, or a function that returns it
  21. */
  22. container: PropTypes.oneOfType([componentOrElement, PropTypes.func]),
  23. /**
  24. * Automatically set width when affixed
  25. */
  26. autoWidth: PropTypes.bool // This intentionally doesn't inherit default props from `<Affix>`, so that the
  27. // auto-calculated offsets can apply.
  28. });
  29. var defaultProps = {
  30. viewportOffsetTop: 0,
  31. autoWidth: true
  32. /**
  33. * The `<AutoAffix/>` component wraps `<Affix/>` to automatically calculate
  34. * offsets in many common cases.
  35. */
  36. };
  37. var AutoAffix =
  38. /*#__PURE__*/
  39. function (_React$Component) {
  40. _inheritsLoose(AutoAffix, _React$Component);
  41. function AutoAffix(props, context) {
  42. var _this;
  43. _this = _React$Component.call(this, props, context) || this;
  44. _this.onWindowScroll = function () {
  45. _this.onUpdate();
  46. };
  47. _this.onWindowResize = function () {
  48. if (_this.props.autoWidth) {
  49. requestAnimationFrame(function () {
  50. return _this.onUpdate();
  51. });
  52. }
  53. };
  54. _this.onDocumentClick = function () {
  55. requestAnimationFrame(function () {
  56. return _this.onUpdate();
  57. });
  58. };
  59. _this.onUpdate = function () {
  60. if (!_this._isMounted) {
  61. return;
  62. }
  63. var _getOffset = getOffset(_this.positioner),
  64. offsetTop = _getOffset.top,
  65. width = _getOffset.width;
  66. var container = getContainer(_this.props.container);
  67. var offsetBottom;
  68. if (container) {
  69. var documentHeight = getDocumentHeight(ownerDocument(_assertThisInitialized(_assertThisInitialized(_this))));
  70. var _getOffset2 = getOffset(container),
  71. top = _getOffset2.top,
  72. height = _getOffset2.height;
  73. offsetBottom = documentHeight - top - height;
  74. } else {
  75. offsetBottom = null;
  76. }
  77. _this.updateState(offsetTop, offsetBottom, width);
  78. };
  79. _this.updateState = function (offsetTop, offsetBottom, width) {
  80. if (offsetTop === _this.state.offsetTop && offsetBottom === _this.state.offsetBottom && width === _this.state.width) {
  81. return;
  82. }
  83. _this.setState({
  84. offsetTop: offsetTop,
  85. offsetBottom: offsetBottom,
  86. width: width
  87. });
  88. };
  89. _this.state = {
  90. offsetTop: null,
  91. offsetBottom: null,
  92. width: null
  93. };
  94. return _this;
  95. }
  96. var _proto = AutoAffix.prototype;
  97. _proto.componentDidMount = function componentDidMount() {
  98. var _this2 = this;
  99. this._isMounted = true;
  100. this.removeScrollListener = listen(ownerWindow(this), 'scroll', function () {
  101. return _this2.onWindowScroll();
  102. });
  103. this.removeResizeListener = listen(ownerWindow(this), 'resize', function () {
  104. return _this2.onWindowResize();
  105. });
  106. this.removeClickListener = listen(ownerDocument(this), 'click', function () {
  107. return _this2.onDocumentClick();
  108. });
  109. this.onUpdate();
  110. };
  111. _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
  112. if (prevProps !== this.props) {
  113. this.onUpdate();
  114. }
  115. };
  116. _proto.componentWillUnmount = function componentWillUnmount() {
  117. this._isMounted = false;
  118. if (this.removeScrollListener) this.removeScrollListener();
  119. if (this.removeClickListener) this.removeClickListener();
  120. if (this.removeResizeListener) this.removeResizeListener();
  121. };
  122. _proto.render = function render() {
  123. var _this3 = this;
  124. var _this$props = this.props,
  125. autoWidth = _this$props.autoWidth,
  126. viewportOffsetTop = _this$props.viewportOffsetTop,
  127. children = _this$props.children,
  128. props = _objectWithoutPropertiesLoose(_this$props, ["autoWidth", "viewportOffsetTop", "children"]);
  129. var _this$state = this.state,
  130. offsetTop = _this$state.offsetTop,
  131. offsetBottom = _this$state.offsetBottom,
  132. width = _this$state.width;
  133. delete props.container;
  134. var effectiveOffsetTop = Math.max(offsetTop, viewportOffsetTop || 0);
  135. var _this$props2 = this.props,
  136. affixStyle = _this$props2.affixStyle,
  137. bottomStyle = _this$props2.bottomStyle;
  138. if (autoWidth) {
  139. affixStyle = _extends({
  140. width: width
  141. }, affixStyle);
  142. bottomStyle = _extends({
  143. width: width
  144. }, bottomStyle);
  145. }
  146. return React.createElement("div", null, React.createElement("div", {
  147. ref: function ref(c) {
  148. _this3.positioner = c;
  149. }
  150. }), React.createElement(Affix, _extends({}, props, {
  151. offsetTop: effectiveOffsetTop,
  152. viewportOffsetTop: viewportOffsetTop,
  153. offsetBottom: offsetBottom,
  154. affixStyle: affixStyle,
  155. bottomStyle: bottomStyle
  156. }), children));
  157. };
  158. return AutoAffix;
  159. }(React.Component);
  160. AutoAffix.displayName = displayName;
  161. AutoAffix.propTypes = propTypes;
  162. AutoAffix.defaultProps = defaultProps;
  163. export default AutoAffix;