StickyStretchableScroller.jsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import React, { useEffect } from 'react';
  2. import PropTypes from 'prop-types';
  3. import loggerFactory from '@alias/logger';
  4. import { debounce } from 'throttle-debounce';
  5. const logger = loggerFactory('growi:cli:StickyStretchableScroller');
  6. const StickyStretchableScroller = (props) => {
  7. const {
  8. children, contentsElemSelector, calcViewHeightFunc, calcContentsHeightFunc,
  9. } = props;
  10. const id = children.props.id;
  11. /**
  12. * Reset scrollbar
  13. */
  14. const resetScrollbar = () => {
  15. const contentsElem = document.querySelector(contentsElemSelector);
  16. if (contentsElem == null) {
  17. return;
  18. }
  19. const viewHeight = calcViewHeightFunc != null
  20. ? calcViewHeightFunc()
  21. : 'auto';
  22. const contentsHeight = calcContentsHeightFunc != null
  23. ? calcContentsHeightFunc(contentsElem)
  24. : contentsElem.getBoundingClientRect().height;
  25. logger.debug('viewHeight', viewHeight);
  26. logger.debug('contentsHeight', contentsHeight);
  27. if (viewHeight < contentsHeight) {
  28. $(`#${id}`).slimScroll({
  29. railVisible: true,
  30. position: 'right',
  31. height: viewHeight,
  32. });
  33. }
  34. else {
  35. $(`#${id}`).slimScroll({ destroy: true });
  36. }
  37. };
  38. const resetScrollbarDebounced = debounce(100, resetScrollbar);
  39. // didMount
  40. useEffect(() => {
  41. });
  42. // didMount, didUpdate
  43. useEffect(() => {
  44. resetScrollbarDebounced();
  45. });
  46. return (
  47. <>
  48. { children }
  49. </>
  50. );
  51. };
  52. StickyStretchableScroller.propTypes = {
  53. children: PropTypes.node.isRequired,
  54. contentsElemSelector: PropTypes.string.isRequired,
  55. calcViewHeightFunc: PropTypes.func,
  56. calcContentsHeightFunc: PropTypes.func,
  57. };
  58. export default StickyStretchableScroller;