/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import React, {useCallback, useState, useEffect} from 'react'; import clsx from 'clsx'; import SearchBar from '@theme/SearchBar'; import Toggle from '@theme/Toggle'; import useThemeContext from '@theme/hooks/useThemeContext'; import {useThemeConfig} from '@docusaurus/theme-common'; import useHideableNavbar from '@theme/hooks/useHideableNavbar'; import useLockBodyScroll from '@theme/hooks/useLockBodyScroll'; import useWindowSize, {windowSizes} from '@theme/hooks/useWindowSize'; import NavbarItem from '@theme/NavbarItem'; import Logo from '@theme/Logo'; import IconMenu from '@theme/IconMenu'; import styles from './styles.module.css'; // retrocompatible with v1 const DefaultNavItemPosition = 'right'; // If split links by left/right // if position is unspecified, fallback to right (as v1) function splitNavItemsByPosition(items) { const leftItems = items.filter( (item) => (item.position ?? DefaultNavItemPosition) === 'left', ); const rightItems = items.filter( (item) => (item.position ?? DefaultNavItemPosition) === 'right', ); return { leftItems, rightItems, }; } function Navbar() { const { navbar: {items, hideOnScroll, style}, colorMode: {disableSwitch: disableColorModeSwitch}, } = useThemeConfig(); const [sidebarShown, setSidebarShown] = useState(false); const {isDarkTheme, setLightTheme, setDarkTheme} = useThemeContext(); const {navbarRef, isNavbarVisible} = useHideableNavbar(hideOnScroll); useLockBodyScroll(sidebarShown); const showSidebar = useCallback(() => { setSidebarShown(true); }, [setSidebarShown]); const hideSidebar = useCallback(() => { setSidebarShown(false); }, [setSidebarShown]); const onToggleChange = useCallback( (e) => (e.target.checked ? setDarkTheme() : setLightTheme()), [setLightTheme, setDarkTheme], ); const windowSize = useWindowSize(); useEffect(() => { if (windowSize === windowSizes.desktop) { setSidebarShown(false); } }, [windowSize]); const hasSearchNavbarItem = items.some((item) => item.type === 'search'); const {leftItems, rightItems} = splitNavItemsByPosition(items); const algoliaSearch = rightItems.shift() return ( ); } export default Navbar;