StickyStretchableScroller.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import React, { useEffect, useCallback } from 'react';
  2. import PropTypes from 'prop-types';
  3. import loggerFactory from '@alias/logger';
  4. import { debounce } from 'throttle-debounce';
  5. import StickyEvents from 'sticky-events';
  6. import NavigationContainer from '../services/NavigationContainer';
  7. import { withUnstatedContainers } from './UnstatedUtils';
  8. const logger = loggerFactory('growi:cli:StickyStretchableScroller');
  9. /**
  10. * USAGE:
  11. *
  12. const calcViewHeight = useCallback(() => {
  13. const containerElem = document.querySelector('#sticky-elem');
  14. const containerTop = containerElem.getBoundingClientRect().top;
  15. // stretch to the bottom of window
  16. return window.innerHeight - containerTop;
  17. });
  18. return (
  19. <StickyStretchableScroller
  20. contentsElemSelector="#long-contents-elem"
  21. stickyElemSelector="#sticky-elem"
  22. calcViewHeightFunc={calcViewHeight}
  23. >
  24. <div id="scroll-elem">
  25. ...
  26. </div>
  27. </StickyStretchableScroller>
  28. );
  29. or
  30. return (
  31. <StickyStretchableScroller
  32. scrollTargetId="scroll-elem"
  33. contentsElemSelector="#long-contents-elem"
  34. stickyElemSelector="#sticky-elem"
  35. calcViewHeightFunc={calcViewHeight}
  36. />
  37. );
  38. */
  39. const StickyStretchableScroller = (props) => {
  40. let { scrollTargetSelector } = props;
  41. const {
  42. navigationContainer,
  43. children, contentsElemSelector, stickyElemSelector,
  44. calcViewHeightFunc, calcContentsHeightFunc,
  45. } = props;
  46. if (scrollTargetSelector == null && children == null) {
  47. throw new Error('Either of scrollTargetSelector or children is required');
  48. }
  49. if (scrollTargetSelector == null) {
  50. scrollTargetSelector = `#${children.props.id}`;
  51. }
  52. /**
  53. * Reset scrollbar
  54. */
  55. const resetScrollbar = useCallback(() => {
  56. const contentsElem = document.querySelector(contentsElemSelector);
  57. if (contentsElem == null) {
  58. return;
  59. }
  60. const viewHeight = calcViewHeightFunc != null
  61. ? calcViewHeightFunc()
  62. : 'auto';
  63. const contentsHeight = calcContentsHeightFunc != null
  64. ? calcContentsHeightFunc(contentsElem)
  65. : contentsElem.getBoundingClientRect().height;
  66. logger.debug(`[${scrollTargetSelector}] viewHeight`, viewHeight);
  67. logger.debug(`[${scrollTargetSelector}] contentsHeight`, contentsHeight);
  68. const isScrollEnabled = viewHeight === 'auto' || (viewHeight < contentsHeight);
  69. $(scrollTargetSelector).slimScroll({
  70. color: '#666',
  71. railColor: '#999',
  72. railVisible: true,
  73. position: 'right',
  74. height: isScrollEnabled ? viewHeight : contentsHeight,
  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. resetScrollbar();
  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 by isScrollTop
  114. useEffect(() => {
  115. if (navigationContainer.state.isScrollTop) {
  116. resetScrollbar();
  117. }
  118. }, [navigationContainer.state.isScrollTop, resetScrollbar]);
  119. // setup effect by update props
  120. useEffect(() => {
  121. resetScrollbarDebounced();
  122. }, [resetScrollbarDebounced]);
  123. return (
  124. <>
  125. { children }
  126. </>
  127. );
  128. };
  129. StickyStretchableScroller.propTypes = {
  130. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  131. contentsElemSelector: PropTypes.string.isRequired,
  132. children: PropTypes.node,
  133. scrollTargetSelector: PropTypes.string,
  134. stickyElemSelector: PropTypes.string,
  135. calcViewHeightFunc: PropTypes.func,
  136. calcContentsHeightFunc: PropTypes.func,
  137. };
  138. export default withUnstatedContainers(StickyStretchableScroller, [NavigationContainer]);