StickyStretchableScroller.jsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import React, { useEffect, useCallback } from 'react';
  2. import PropTypes from 'prop-types';
  3. import { debounce } from 'throttle-debounce';
  4. import StickyEvents from 'sticky-events';
  5. import loggerFactory from '~/utils/logger';
  6. const logger = loggerFactory('growi:cli:StickyStretchableScroller');
  7. /**
  8. * USAGE:
  9. *
  10. const calcViewHeight = useCallback(() => {
  11. const containerElem = document.querySelector('#sticky-elem');
  12. const containerTop = containerElem.getBoundingClientRect().top;
  13. // stretch to the bottom of window
  14. return window.innerHeight - containerTop;
  15. });
  16. return (
  17. <StickyStretchableScroller
  18. contentsElemSelector="#long-contents-elem"
  19. stickyElemSelector="#sticky-elem"
  20. calcViewHeightFunc={calcViewHeight}
  21. >
  22. <div id="scroll-elem">
  23. ...
  24. </div>
  25. </StickyStretchableScroller>
  26. );
  27. or
  28. return (
  29. <StickyStretchableScroller
  30. scrollTargetId="scroll-elem"
  31. contentsElemSelector="#long-contents-elem"
  32. stickyElemSelector="#sticky-elem"
  33. calcViewHeightFunc={calcViewHeight}
  34. />
  35. );
  36. */
  37. const StickyStretchableScroller = (props) => {
  38. let { scrollTargetSelector } = props;
  39. const {
  40. children, contentsElemSelector, stickyElemSelector,
  41. calcViewHeightFunc, calcContentsHeightFunc,
  42. resetKey,
  43. } = props;
  44. if (scrollTargetSelector == null && children == null) {
  45. throw new Error('Either of scrollTargetSelector or children is required');
  46. }
  47. if (scrollTargetSelector == null) {
  48. scrollTargetSelector = `#${children.props.id}`;
  49. }
  50. /**
  51. * Reset scrollbar
  52. */
  53. const resetScrollbar = useCallback(() => {
  54. const contentsElem = document.querySelector(contentsElemSelector);
  55. if (contentsElem == null) {
  56. return;
  57. }
  58. const viewHeight = calcViewHeightFunc != null
  59. ? calcViewHeightFunc()
  60. : 'auto';
  61. const contentsHeight = calcContentsHeightFunc != null
  62. ? calcContentsHeightFunc(contentsElem)
  63. : contentsElem.getBoundingClientRect().height;
  64. logger.debug(`[${scrollTargetSelector}] viewHeight`, viewHeight);
  65. logger.debug(`[${scrollTargetSelector}] contentsHeight`, contentsHeight);
  66. const isScrollEnabled = viewHeight === 'auto' || (viewHeight < contentsHeight);
  67. $(scrollTargetSelector).slimScroll({
  68. color: '#666',
  69. railColor: '#999',
  70. railVisible: true,
  71. position: 'right',
  72. height: isScrollEnabled ? viewHeight : contentsHeight,
  73. wheelStep: 10,
  74. allowPageScroll: true,
  75. });
  76. // destroy
  77. if (!isScrollEnabled) {
  78. $(scrollTargetSelector).slimScroll({ destroy: true });
  79. }
  80. }, [contentsElemSelector, calcViewHeightFunc, calcContentsHeightFunc, scrollTargetSelector]);
  81. const resetScrollbarDebounced = debounce(100, resetScrollbar);
  82. const stickyChangeHandler = useCallback((event) => {
  83. logger.debug('StickyEvents.CHANGE detected');
  84. setTimeout(resetScrollbar, 100);
  85. }, [resetScrollbar]);
  86. // setup effect by sticky event
  87. useEffect(() => {
  88. if (stickyElemSelector == null) {
  89. return;
  90. }
  91. // sticky
  92. // See: https://github.com/ryanwalters/sticky-events
  93. const stickyEvents = new StickyEvents({ stickySelector: stickyElemSelector });
  94. const { stickySelector } = stickyEvents;
  95. const elem = document.querySelector(stickySelector);
  96. elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  97. // return clean up handler
  98. return () => {
  99. elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  100. };
  101. }, [stickyElemSelector, stickyChangeHandler]);
  102. // setup effect by resizing event
  103. useEffect(() => {
  104. const resizeHandler = (event) => {
  105. resetScrollbarDebounced();
  106. };
  107. window.addEventListener('resize', resizeHandler);
  108. // return clean up handler
  109. return () => {
  110. window.removeEventListener('resize', resizeHandler);
  111. };
  112. }, [resetScrollbarDebounced]);
  113. // setup effect on init
  114. useEffect(() => {
  115. if (resetKey != null) {
  116. resetScrollbarDebounced();
  117. }
  118. }, [resetKey, resetScrollbarDebounced]);
  119. return (
  120. <>
  121. { children }
  122. </>
  123. );
  124. };
  125. StickyStretchableScroller.propTypes = {
  126. contentsElemSelector: PropTypes.string.isRequired,
  127. children: PropTypes.node,
  128. scrollTargetSelector: PropTypes.string,
  129. stickyElemSelector: PropTypes.string,
  130. resetKey: PropTypes.any,
  131. calcViewHeightFunc: PropTypes.func,
  132. calcContentsHeightFunc: PropTypes.func,
  133. };
  134. export default StickyStretchableScroller;