Dashboard sipadu mbip
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Affix.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = void 0;
  4. var _classnames = _interopRequireDefault(require("classnames"));
  5. var _height = _interopRequireDefault(require("dom-helpers/query/height"));
  6. var _offset = _interopRequireDefault(require("dom-helpers/query/offset"));
  7. var _listen = _interopRequireDefault(require("dom-helpers/events/listen"));
  8. var _offsetParent = _interopRequireDefault(require("dom-helpers/query/offsetParent"));
  9. var _scrollTop = _interopRequireDefault(require("dom-helpers/query/scrollTop"));
  10. var _requestAnimationFrame = _interopRequireDefault(require("dom-helpers/util/requestAnimationFrame"));
  11. var _propTypes = _interopRequireDefault(require("prop-types"));
  12. var _react = _interopRequireDefault(require("react"));
  13. var _reactDom = _interopRequireDefault(require("react-dom"));
  14. var _getDocumentHeight = _interopRequireDefault(require("./utils/getDocumentHeight"));
  15. var _ownerDocument = _interopRequireDefault(require("./utils/ownerDocument"));
  16. var _ownerWindow = _interopRequireDefault(require("./utils/ownerWindow"));
  17. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18. 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); }
  19. function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
  20. function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
  21. /**
  22. * The `<Affix/>` component toggles `position: fixed;` on and off, emulating
  23. * the effect found with `position: sticky;`.
  24. */
  25. var Affix =
  26. /*#__PURE__*/
  27. function (_React$Component) {
  28. _inheritsLoose(Affix, _React$Component);
  29. function Affix(props, context) {
  30. var _this;
  31. _this = _React$Component.call(this, props, context) || this;
  32. _this.onWindowScroll = function () {
  33. _this.onUpdate();
  34. };
  35. _this.onDocumentClick = function () {
  36. (0, _requestAnimationFrame.default)(function () {
  37. return _this.onUpdate();
  38. });
  39. };
  40. _this.onUpdate = function () {
  41. if (!_this._isMounted) {
  42. return;
  43. }
  44. var _this$props = _this.props,
  45. offsetTop = _this$props.offsetTop,
  46. viewportOffsetTop = _this$props.viewportOffsetTop;
  47. var scrollTop = (0, _scrollTop.default)((0, _ownerWindow.default)(_assertThisInitialized(_assertThisInitialized(_this))));
  48. var positionTopMin = scrollTop + (viewportOffsetTop || 0);
  49. if (positionTopMin <= offsetTop) {
  50. _this.updateState('top', null, null);
  51. return;
  52. }
  53. if (positionTopMin > _this.getPositionTopMax()) {
  54. if (_this.state.affixed === 'bottom') {
  55. _this.updateStateAtBottom();
  56. } else {
  57. // Setting position away from `fixed` can change the offset parent of
  58. // the affix, so we can't calculate the correct position until after
  59. // we've updated its position.
  60. _this.setState({
  61. affixed: 'bottom',
  62. position: 'absolute',
  63. top: null
  64. }, function () {
  65. if (!_this._isMounted) {
  66. return;
  67. }
  68. _this.updateStateAtBottom();
  69. });
  70. }
  71. return;
  72. }
  73. _this.updateState('affix', 'fixed', viewportOffsetTop);
  74. };
  75. _this.getPositionTopMax = function () {
  76. var documentHeight = (0, _getDocumentHeight.default)((0, _ownerDocument.default)(_assertThisInitialized(_assertThisInitialized(_this))));
  77. var height = (0, _height.default)(_reactDom.default.findDOMNode(_assertThisInitialized(_assertThisInitialized(_this))));
  78. return documentHeight - height - _this.props.offsetBottom;
  79. };
  80. _this.updateState = function (affixed, position, top) {
  81. if (affixed === _this.state.affixed && position === _this.state.position && top === _this.state.top) {
  82. return;
  83. }
  84. var upperName = affixed === 'affix' ? '' : affixed.charAt(0).toUpperCase() + affixed.substr(1);
  85. if (_this.props['onAffix' + upperName]) {
  86. _this.props['onAffix' + upperName]();
  87. }
  88. _this.setState({
  89. affixed: affixed,
  90. position: position,
  91. top: top
  92. }, function () {
  93. if (_this.props['onAffixed' + upperName]) {
  94. _this.props['onAffixed' + upperName]();
  95. }
  96. });
  97. };
  98. _this.updateStateAtBottom = function () {
  99. var positionTopMax = _this.getPositionTopMax();
  100. var offsetParent = (0, _offsetParent.default)(_reactDom.default.findDOMNode(_assertThisInitialized(_assertThisInitialized(_this))));
  101. var parentTop = (0, _offset.default)(offsetParent).top;
  102. _this.updateState('bottom', 'absolute', positionTopMax - parentTop);
  103. };
  104. _this.state = {
  105. affixed: 'top',
  106. position: null,
  107. top: null
  108. };
  109. return _this;
  110. }
  111. var _proto = Affix.prototype;
  112. _proto.componentDidMount = function componentDidMount() {
  113. var _this2 = this;
  114. this._isMounted = true;
  115. this.removeScrollListener = (0, _listen.default)((0, _ownerWindow.default)(this), 'scroll', function () {
  116. return _this2.onWindowScroll();
  117. });
  118. this.removeClickListener = (0, _listen.default)((0, _ownerDocument.default)(this), 'click', function () {
  119. return _this2.onDocumentClick();
  120. });
  121. this.onUpdate();
  122. };
  123. _proto.componentDidUpdate = function componentDidUpdate(prevProps) {
  124. if (prevProps !== this.props) {
  125. this.onUpdate();
  126. }
  127. };
  128. _proto.componentWillUnmount = function componentWillUnmount() {
  129. this._isMounted = false;
  130. if (this.removeClickListener) this.removeClickListener();
  131. if (this.removeScrollListener) this.removeScrollListener();
  132. };
  133. _proto.render = function render() {
  134. var child = _react.default.Children.only(this.props.children);
  135. var _child$props = child.props,
  136. className = _child$props.className,
  137. style = _child$props.style;
  138. var _this$state = this.state,
  139. affixed = _this$state.affixed,
  140. position = _this$state.position,
  141. top = _this$state.top;
  142. var positionStyle = {
  143. position: position,
  144. top: top
  145. };
  146. var affixClassName;
  147. var affixStyle;
  148. if (affixed === 'top') {
  149. affixClassName = this.props.topClassName;
  150. affixStyle = this.props.topStyle;
  151. } else if (affixed === 'bottom') {
  152. affixClassName = this.props.bottomClassName;
  153. affixStyle = this.props.bottomStyle;
  154. } else {
  155. affixClassName = this.props.affixClassName;
  156. affixStyle = this.props.affixStyle;
  157. }
  158. return _react.default.cloneElement(child, {
  159. className: (0, _classnames.default)(affixClassName, className),
  160. style: _extends({}, positionStyle, affixStyle, style)
  161. });
  162. };
  163. return Affix;
  164. }(_react.default.Component);
  165. Affix.propTypes = {
  166. /**
  167. * Pixels to offset from top of screen when calculating position
  168. */
  169. offsetTop: _propTypes.default.number,
  170. /**
  171. * When affixed, pixels to offset from top of viewport
  172. */
  173. viewportOffsetTop: _propTypes.default.number,
  174. /**
  175. * Pixels to offset from bottom of screen when calculating position
  176. */
  177. offsetBottom: _propTypes.default.number,
  178. /**
  179. * CSS class or classes to apply when at top
  180. */
  181. topClassName: _propTypes.default.string,
  182. /**
  183. * Style to apply when at top
  184. */
  185. topStyle: _propTypes.default.object,
  186. /**
  187. * CSS class or classes to apply when affixed
  188. */
  189. affixClassName: _propTypes.default.string,
  190. /**
  191. * Style to apply when affixed
  192. */
  193. affixStyle: _propTypes.default.object,
  194. /**
  195. * CSS class or classes to apply when at bottom
  196. */
  197. bottomClassName: _propTypes.default.string,
  198. /**
  199. * Style to apply when at bottom
  200. */
  201. bottomStyle: _propTypes.default.object,
  202. /**
  203. * Callback fired right before the `affixStyle` and `affixClassName` props are rendered
  204. */
  205. onAffix: _propTypes.default.func,
  206. /**
  207. * Callback fired after the component `affixStyle` and `affixClassName` props have been rendered
  208. */
  209. onAffixed: _propTypes.default.func,
  210. /**
  211. * Callback fired right before the `topStyle` and `topClassName` props are rendered
  212. */
  213. onAffixTop: _propTypes.default.func,
  214. /**
  215. * Callback fired after the component `topStyle` and `topClassName` props have been rendered
  216. */
  217. onAffixedTop: _propTypes.default.func,
  218. /**
  219. * Callback fired right before the `bottomStyle` and `bottomClassName` props are rendered
  220. */
  221. onAffixBottom: _propTypes.default.func,
  222. /**
  223. * Callback fired after the component `bottomStyle` and `bottomClassName` props have been rendered
  224. */
  225. onAffixedBottom: _propTypes.default.func
  226. };
  227. Affix.defaultProps = {
  228. offsetTop: 0,
  229. viewportOffsetTop: null,
  230. offsetBottom: 0
  231. };
  232. var _default = Affix;
  233. exports.default = _default;
  234. module.exports = exports.default;