|
|
@@ -2,12 +2,12 @@ import React, {
|
|
|
useState, useRef, useEffect, useCallback,
|
|
|
} from 'react';
|
|
|
|
|
|
-import StickyEvents from 'sticky-events';
|
|
|
import { debounce } from 'throttle-debounce';
|
|
|
|
|
|
import { useSWRxCurrentPage } from '~/stores/page';
|
|
|
import { useSidebarCollapsed } from '~/stores/ui';
|
|
|
import loggerFactory from '~/utils/logger';
|
|
|
+import { useSticky } from '~/utils/use-sticky-utils';
|
|
|
|
|
|
import GrowiContextualSubNavigation from './GrowiContextualSubNavigation';
|
|
|
|
|
|
@@ -32,7 +32,6 @@ export const GrowiSubNavigationSwitcher = (props: GrowiSubNavigationSwitcherProp
|
|
|
const { data: currentPage } = useSWRxCurrentPage();
|
|
|
const { data: isSidebarCollapsed } = useSidebarCollapsed();
|
|
|
|
|
|
- const [isVisible, setIsVisible] = useState<boolean>(false);
|
|
|
const [width, setWidth] = useState<number>(0);
|
|
|
|
|
|
// use more specific type HTMLDivElement for avoid assertion error.
|
|
|
@@ -40,6 +39,9 @@ export const GrowiSubNavigationSwitcher = (props: GrowiSubNavigationSwitcherProp
|
|
|
const fixedContainerRef = useRef<HTMLDivElement>(null);
|
|
|
const clientWidth = fixedContainerRef.current?.parentElement?.clientWidth;
|
|
|
|
|
|
+ // Get sticky status
|
|
|
+ const isSticky = useSticky('#grw-subnav-sticky-trigger');
|
|
|
+
|
|
|
// Do not use clientWidth as useCallback deps, resizing events will not work in production builds.
|
|
|
const initWidth = useCallback(() => {
|
|
|
if (fixedContainerRef.current != null && fixedContainerRef.current.parentElement != null) {
|
|
|
@@ -49,25 +51,6 @@ export const GrowiSubNavigationSwitcher = (props: GrowiSubNavigationSwitcherProp
|
|
|
}
|
|
|
}, []);
|
|
|
|
|
|
- const stickyChangeHandler = useCallback((event) => {
|
|
|
- logger.debug('StickyEvents.CHANGE detected');
|
|
|
- setIsVisible(event.detail.isSticky);
|
|
|
- }, []);
|
|
|
-
|
|
|
- // setup effect by sticky-events
|
|
|
- useEffect(() => {
|
|
|
- // sticky-events
|
|
|
- // See: https://github.com/ryanwalters/sticky-events
|
|
|
- const { stickySelector } = new StickyEvents({ stickySelector: '#grw-subnav-sticky-trigger' });
|
|
|
- const elem = document.querySelector(stickySelector);
|
|
|
- elem.addEventListener(StickyEvents.CHANGE, stickyChangeHandler);
|
|
|
-
|
|
|
- // return clean up handler
|
|
|
- return () => {
|
|
|
- elem.removeEventListener(StickyEvents.CHANGE, stickyChangeHandler);
|
|
|
- };
|
|
|
- }, [stickyChangeHandler]);
|
|
|
-
|
|
|
// setup effect by resizing event
|
|
|
useEffect(() => {
|
|
|
const resizeHandler = debounce(100, initWidth);
|
|
|
@@ -100,7 +83,7 @@ export const GrowiSubNavigationSwitcher = (props: GrowiSubNavigationSwitcherProp
|
|
|
}
|
|
|
|
|
|
return (
|
|
|
- <div className={`${styles['grw-subnav-switcher']} ${isVisible ? '' : 'grw-subnav-switcher-hidden'}`}>
|
|
|
+ <div className={`${styles['grw-subnav-switcher']} ${isSticky ? '' : 'grw-subnav-switcher-hidden'}`}>
|
|
|
<div
|
|
|
id="grw-subnav-fixed-container"
|
|
|
className={`grw-subnav-fixed-container ${styles['grw-subnav-fixed-container']} position-fixed grw-subnav-append-shadow-container`}
|