StickyStretchableScroller.jsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. $(scrollTargetSelector).slimScroll({
  69. color: '#666',
  70. railColor: '#999',
  71. railVisible: true,
  72. position: 'right',
  73. height: viewHeight,
  74. });
  75. if (contentsHeight < viewHeight) {
  76. $(scrollTargetSelector).slimScroll({ destroy: true });
  77. }
  78. }, [contentsElemSelector, calcViewHeightFunc, calcContentsHeightFunc, scrollTargetSelector]);
  79. const resetScrollbarDebounced = debounce(100, resetScrollbar);
  80. const stickyChangeHandler = useCallback((event) => {
  81. logger.debug('StickyEvents.CHANGE detected');
  82. resetScrollbar();
  83. }, [resetScrollbar]);
  84. // setup effect by sticky event
  85. useEffect(() => {
  86. if (stickyElemSelector == null) {
  87. return;
  88. }
  89. // sticky
  90. // See: https://github.com/ryanwalters/sticky-events
  91. const stickyEvents = new StickyEvents({ stickySelector: stickyElemSelector });
  92. const { stickySelector } = stickyEvents;
  93. const elem = document.querySelector(stickySelector);
  94. elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  95. // return clean up handler
  96. return () => {
  97. elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  98. };
  99. }, [stickyElemSelector, stickyChangeHandler]);
  100. // setup effect by resizing event
  101. useEffect(() => {
  102. const resizeHandler = (event) => {
  103. resetScrollbarDebounced();
  104. };
  105. window.addEventListener('resize', resizeHandler);
  106. // return clean up handler
  107. return () => {
  108. window.removeEventListener('resize', resizeHandler);
  109. };
  110. }, [resetScrollbarDebounced]);
  111. // setup effect by isScrollTop
  112. useEffect(() => {
  113. if (navigationContainer.state.isScrollTop) {
  114. resetScrollbar();
  115. }
  116. }, [navigationContainer.state.isScrollTop, resetScrollbar]);
  117. // setup effect by update props
  118. useEffect(() => {
  119. resetScrollbarDebounced();
  120. }, [resetScrollbarDebounced]);
  121. return (
  122. <>
  123. { children }
  124. </>
  125. );
  126. };
  127. StickyStretchableScroller.propTypes = {
  128. navigationContainer: PropTypes.instanceOf(NavigationContainer).isRequired,
  129. contentsElemSelector: PropTypes.string.isRequired,
  130. children: PropTypes.node,
  131. scrollTargetSelector: PropTypes.string,
  132. stickyElemSelector: PropTypes.string,
  133. calcViewHeightFunc: PropTypes.func,
  134. calcContentsHeightFunc: PropTypes.func,
  135. };
  136. export default withUnstatedContainers(StickyStretchableScroller, [NavigationContainer]);