Dashboard sipadu mbip
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Affix.js 8.0KB

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