|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | +import React, { useState, useEffect, useRef } from 'react'; |
| 8 | +import clsx from 'clsx'; |
| 9 | +import { ThemeClassNames } from '@docusaurus/theme-common'; |
| 10 | +import { |
| 11 | + useAnnouncementBar, |
| 12 | + useScrollPosition, |
| 13 | +} from '@docusaurus/theme-common/internal'; |
| 14 | +import { translate } from '@docusaurus/Translate'; |
| 15 | +import DocSidebarItems from '@theme/DocSidebarItems'; |
| 16 | +import styles from './styles.module.css'; |
| 17 | + |
| 18 | +function useShowAnnouncementBar() { |
| 19 | + const { isActive } = useAnnouncementBar(); |
| 20 | + const [showAnnouncementBar, setShowAnnouncementBar] = useState(isActive); |
| 21 | + useScrollPosition( |
| 22 | + ({ scrollY }) => { |
| 23 | + if (isActive) { |
| 24 | + setShowAnnouncementBar(scrollY === 0); |
| 25 | + } |
| 26 | + }, |
| 27 | + [isActive], |
| 28 | + ); |
| 29 | + return isActive && showAnnouncementBar; |
| 30 | +} |
| 31 | + |
| 32 | +export default function DocSidebarDesktopContent({ path, sidebar, className }) { |
| 33 | + console.log('DocSidebarDesktopContent mounted', { path }); |
| 34 | + const showAnnouncementBar = useShowAnnouncementBar(); |
| 35 | + const menuRef = useRef(null); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + // Function to handle active item scrolling and duplicate cleanup |
| 39 | + const handleActiveItem = () => { |
| 40 | + if (!menuRef.current) return; |
| 41 | + |
| 42 | + // Find all active links |
| 43 | + // Docusaurus uses 'menu__link--active' for the active item |
| 44 | + const activeLinks = menuRef.current.querySelectorAll('.menu__link--active'); |
| 45 | + |
| 46 | + if (activeLinks.length > 0) { |
| 47 | + // If there are duplicates, keep only the first one active |
| 48 | + // We check href to ensure we don't un-highlight distinct active items (like parent categories) |
| 49 | + const seenHrefs = new Set(); |
| 50 | + |
| 51 | + activeLinks.forEach((link) => { |
| 52 | + const href = link.getAttribute('href'); |
| 53 | + if (seenHrefs.has(href)) { |
| 54 | + link.classList.remove('menu__link--active'); |
| 55 | + link.removeAttribute('aria-current'); |
| 56 | + } else { |
| 57 | + seenHrefs.add(href); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + const activeItem = activeLinks[0]; |
| 62 | + |
| 63 | + // Check if the item is already visible in the viewport |
| 64 | + const rect = activeItem.getBoundingClientRect(); |
| 65 | + const isVisible = ( |
| 66 | + rect.top >= 0 && |
| 67 | + rect.left >= 0 && |
| 68 | + rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && |
| 69 | + rect.right <= (window.innerWidth || document.documentElement.clientWidth) |
| 70 | + ); |
| 71 | + |
| 72 | + // Only scroll if not fully visible |
| 73 | + if (!isVisible) { |
| 74 | + activeItem.scrollIntoView({ |
| 75 | + behavior: 'auto', |
| 76 | + block: 'center', |
| 77 | + inline: 'nearest' |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + }; |
| 82 | + |
| 83 | + // Initial check |
| 84 | + handleActiveItem(); |
| 85 | + |
| 86 | + // Use MutationObserver to detect changes in the sidebar (e.g. expansion, loading) |
| 87 | + const observer = new MutationObserver((mutations) => { |
| 88 | + handleActiveItem(); |
| 89 | + }); |
| 90 | + |
| 91 | + if (menuRef.current) { |
| 92 | + observer.observe(menuRef.current, { |
| 93 | + childList: true, |
| 94 | + subtree: true, |
| 95 | + attributes: true, |
| 96 | + attributeFilter: ['class'] |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + return () => { |
| 101 | + observer.disconnect(); |
| 102 | + }; |
| 103 | + }, [path, sidebar]); // Re-run when path or sidebar changes |
| 104 | + |
| 105 | + return ( |
| 106 | + <nav |
| 107 | + ref={menuRef} |
| 108 | + aria-label={translate({ |
| 109 | + id: 'theme.docs.sidebar.navAriaLabel', |
| 110 | + message: 'Docs sidebar', |
| 111 | + description: 'The ARIA label for the sidebar navigation', |
| 112 | + })} |
| 113 | + className={clsx( |
| 114 | + 'menu', |
| 115 | + styles.menu, |
| 116 | + showAnnouncementBar && styles.menuWithAnnouncementBar, |
| 117 | + className, |
| 118 | + )}> |
| 119 | + <ul className={clsx(ThemeClassNames.docs.docSidebarMenu, 'menu__list')}> |
| 120 | + <DocSidebarItems items={sidebar} activePath={path} level={1} /> |
| 121 | + </ul> |
| 122 | + </nav> |
| 123 | + ); |
| 124 | +} |
0 commit comments