GrowiSubNavigationSwitcher.jsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import React, {
  2. useMemo, useState, useRef, useEffect, useCallback,
  3. } from 'react';
  4. import PropTypes from 'prop-types';
  5. import StickyEvents from 'sticky-events';
  6. import { debounce } from 'throttle-debounce';
  7. import { useSidebarCollapsed } from '~/stores/ui';
  8. import loggerFactory from '~/utils/logger';
  9. import GrowiContextualSubNavigation from './GrowiContextualSubNavigation';
  10. import styles from './GrowiSubNavigationSwitcher.module.scss';
  11. const logger = loggerFactory('growi:cli:GrowiSubNavigationSticky');
  12. /**
  13. * Subnavigation
  14. *
  15. * needs:
  16. * #grw-subnav-fixed-container element
  17. * #grw-subnav-sticky-trigger element
  18. *
  19. * @param {object} props
  20. */
  21. const GrowiSubNavigationSwitcher = (props) => {
  22. const { data: isSidebarCollapsed } = useSidebarCollapsed();
  23. const [isVisible, setVisible] = useState(false);
  24. const [width, setWidth] = useState(null);
  25. const fixedContainerRef = useRef();
  26. /*
  27. * Comment out to prevent err >>> TypeError: Cannot read properties of null (reading 'bottom')
  28. * The above err occurs when moving to admin page after rendering normal pages.
  29. * This is because id "grw-subnav-sticky-trigger" does not exist on admin pages.
  30. */
  31. // const stickyEvents = useMemo(() => new StickyEvents({ stickySelector: '#grw-subnav-sticky-trigger' }), []);
  32. const initWidth = useCallback(() => {
  33. const instance = fixedContainerRef.current;
  34. if (instance == null || instance.parentNode == null) {
  35. return;
  36. }
  37. // get parent width
  38. const { clientWidth } = instance.parentNode;
  39. // update style
  40. setWidth(clientWidth);
  41. }, []);
  42. // const initVisible = useCallback(() => {
  43. // const elements = stickyEvents.stickyElements;
  44. // for (const elem of elements) {
  45. // const bool = stickyEvents.isSticking(elem);
  46. // if (bool) {
  47. // setVisible(bool);
  48. // break;
  49. // }
  50. // }
  51. // }, [stickyEvents]);
  52. // setup effect by resizing event
  53. useEffect(() => {
  54. const resizeHandler = debounce(100, initWidth);
  55. window.addEventListener('resize', resizeHandler);
  56. // return clean up handler
  57. return () => {
  58. window.removeEventListener('resize', resizeHandler);
  59. };
  60. }, [initWidth]);
  61. const stickyChangeHandler = useCallback((event) => {
  62. logger.debug('StickyEvents.CHANGE detected');
  63. setVisible(event.detail.isSticky);
  64. }, []);
  65. // // setup effect by sticky event
  66. // useEffect(() => {
  67. // // sticky
  68. // // See: https://github.com/ryanwalters/sticky-events
  69. // const { stickySelector } = stickyEvents;
  70. // const elem = document.querySelector(stickySelector);
  71. // elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  72. // // return clean up handler
  73. // return () => {
  74. // elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
  75. // };
  76. // }, [stickyChangeHandler, stickyEvents]);
  77. // update width when sidebar collapsing changed
  78. useEffect(() => {
  79. if (isSidebarCollapsed != null) {
  80. setTimeout(initWidth, 300);
  81. }
  82. }, [isSidebarCollapsed, initWidth]);
  83. // // initialize
  84. // useEffect(() => {
  85. // initWidth();
  86. // // check sticky state several times
  87. // setTimeout(initVisible, 100);
  88. // setTimeout(initVisible, 300);
  89. // setTimeout(initVisible, 2000);
  90. // }, [initWidth, initVisible]);
  91. // ${styles['grw-subnav-switcher']}
  92. return (
  93. <div className={`${styles['grw-subnav-switcher']} ${isVisible ? '' : 'grw-subnav-switcher-hidden'}`}>
  94. <div
  95. id="grw-subnav-fixed-container"
  96. className={`grw-subnav-fixed-container ${styles['grw-subnav-fixed-container']} position-fixed grw-subnav-append-shadow-container`}
  97. ref={fixedContainerRef}
  98. style={{ width }}
  99. >
  100. <GrowiContextualSubNavigation isCompactMode isLinkSharingDisabled />
  101. </div>
  102. </div>
  103. );
  104. };
  105. GrowiSubNavigationSwitcher.propTypes = {
  106. isLinkSharingDisabled: PropTypes.bool,
  107. };
  108. export default GrowiSubNavigationSwitcher;