فهرست منبع

Merge pull request #5047 from weseek/imprv/subnavigation

imprv: Subnavigation behavior
Yuki Takei 4 سال پیش
والد
کامیت
b9a4a8bbea
2فایلهای تغییر یافته به همراه32 افزوده شده و 12 حذف شده
  1. 31 11
      packages/app/src/components/Navbar/GrowiSubNavigationSwitcher.jsx
  2. 1 1
      packages/app/src/styles/_sidebar.scss

+ 31 - 11
packages/app/src/components/Navbar/GrowiSubNavigationSwitcher.jsx

@@ -1,9 +1,12 @@
-import React, { useState, useEffect, useCallback } from 'react';
-// import PropTypes from 'prop-types';
+import React, {
+  useMemo, useState, useRef, useEffect, useCallback,
+} from 'react';
 
 
 import StickyEvents from 'sticky-events';
 import StickyEvents from 'sticky-events';
 import { debounce } from 'throttle-debounce';
 import { debounce } from 'throttle-debounce';
+
 import loggerFactory from '~/utils/logger';
 import loggerFactory from '~/utils/logger';
+import { useSidebarCollapsed } from '~/stores/ui';
 
 
 import GrowiSubNavigation from './GrowiSubNavigation';
 import GrowiSubNavigation from './GrowiSubNavigation';
 
 
@@ -21,19 +24,25 @@ const logger = loggerFactory('growi:cli:GrowiSubNavigationSticky');
  */
  */
 const GrowiSubNavigationSwitcher = (props) => {
 const GrowiSubNavigationSwitcher = (props) => {
 
 
+  const { data: isSidebarCollapsed } = useSidebarCollapsed();
+
   const [isVisible, setVisible] = useState(false);
   const [isVisible, setVisible] = useState(false);
+  const [width, setWidth] = useState(null);
+
+  const fixedContainerRef = useRef();
+  const stickyEvents = useMemo(() => new StickyEvents({ stickySelector: '#grw-subnav-sticky-trigger' }), []);
 
 
   const resetWidth = useCallback(() => {
   const resetWidth = useCallback(() => {
-    const elem = document.getElementById('grw-subnav-fixed-container');
+    const instance = fixedContainerRef.current;
 
 
-    if (elem == null || elem.parentNode == null) {
+    if (instance == null || instance.parentNode == null) {
       return;
       return;
     }
     }
 
 
     // get parent width
     // get parent width
-    const { clientWidth: width } = elem.parentNode;
+    const { clientWidth } = instance.parentNode;
     // update style
     // update style
-    elem.style.width = `${width}px`;
+    setWidth(clientWidth);
   }, []);
   }, []);
 
 
   // setup effect by resizing event
   // setup effect by resizing event
@@ -57,7 +66,6 @@ const GrowiSubNavigationSwitcher = (props) => {
   useEffect(() => {
   useEffect(() => {
     // sticky
     // sticky
     // See: https://github.com/ryanwalters/sticky-events
     // See: https://github.com/ryanwalters/sticky-events
-    const stickyEvents = new StickyEvents({ stickySelector: '#grw-subnav-sticky-trigger' });
     const { stickySelector } = stickyEvents;
     const { stickySelector } = stickyEvents;
     const elem = document.querySelector(stickySelector);
     const elem = document.querySelector(stickySelector);
     elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
     elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
@@ -66,16 +74,28 @@ const GrowiSubNavigationSwitcher = (props) => {
     return () => {
     return () => {
       elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
       elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
     };
     };
-  }, [stickyChangeHandler]);
+  }, [stickyChangeHandler, stickyEvents]);
+
+  // update width when sidebar collapsing changed
+  useEffect(() => {
+    if (isSidebarCollapsed != null) {
+      setTimeout(resetWidth, 300);
+    }
+  }, [isSidebarCollapsed, resetWidth]);
 
 
-  // update width
+  // initialize
   useEffect(() => {
   useEffect(() => {
     resetWidth();
     resetWidth();
-  });
+    setTimeout(() => {
+      stickyEvents.state.values((sticky) => {
+        setVisible(sticky.isSticky);
+      });
+    }, 100);
+  }, [resetWidth, stickyEvents]);
 
 
   return (
   return (
     <div className={`grw-subnav-switcher ${isVisible ? '' : 'grw-subnav-switcher-hidden'}`}>
     <div className={`grw-subnav-switcher ${isVisible ? '' : 'grw-subnav-switcher-hidden'}`}>
-      <div id="grw-subnav-fixed-container" className="grw-subnav-fixed-container position-fixed">
+      <div id="grw-subnav-fixed-container" className="grw-subnav-fixed-container position-fixed" ref={fixedContainerRef} style={{ width }}>
         <GrowiSubNavigation isCompactMode />
         <GrowiSubNavigation isCompactMode />
       </div>
       </div>
     </div>
     </div>

+ 1 - 1
packages/app/src/styles/_sidebar.scss

@@ -104,7 +104,7 @@
         width: 240px;
         width: 240px;
         height: 100%;
         height: 100%;
         &:not(.dragging) {
         &:not(.dragging) {
-          transition: width 300ms cubic-bezier(0.2, 0, 0, 1) 0s;
+          transition: width 200ms cubic-bezier(0.2, 0, 0, 1) 0s;
         }
         }
         will-change: width;
         will-change: width;
         .grw-contextual-navigation-child {
         .grw-contextual-navigation-child {