/** * Copyright (c) 2017-present, Facebook, Inc. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ import React, {useState, useCallback} from 'react'; import classnames from 'classnames'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import useBaseUrl from '@docusaurus/useBaseUrl'; import useLockBodyScroll from '@theme/hooks/useLockBodyScroll'; import Link from '@docusaurus/Link'; import isInternalUrl from '@docusaurus/utils'; import styles from './styles.module.css'; const MOBILE_TOGGLE_SIZE = 24; function DocSidebarItem({item, onItemClick, collapsible}) { const {items, href, label, type} = item; const [collapsed, setCollapsed] = useState(item.collapsed); const [prevCollapsedProp, setPreviousCollapsedProp] = useState(null); // If the collapsing state from props changed, probably a navigation event // occurred. Overwrite the component's collapsed state with the props' // collapsed value. if (item.collapsed !== prevCollapsedProp) { setPreviousCollapsedProp(item.collapsed); setCollapsed(item.collapsed); } const handleItemClick = useCallback(e => { e.preventDefault(); setCollapsed(state => !state); }); switch (type) { case 'category': return ( items.length > 0 && (
  • {label}
  • ) ); case 'link': default: return (
  • {label}
  • ); } } // Calculate the category collapsing state when a page navigation occurs. // We want to automatically expand the categories which contains the current page. function mutateSidebarCollapsingState(item, path) { const {items, href, type} = item; switch (type) { case 'category': { const anyChildItemsActive = items .map(childItem => mutateSidebarCollapsingState(childItem, path)) .filter(val => val).length > 0; // eslint-disable-next-line no-param-reassign item.collapsed = !anyChildItemsActive; return anyChildItemsActive; } case 'link': default: return href === path; } } function DocSidebar(props) { const [showResponsiveSidebar, setShowResponsiveSidebar] = useState(false); const { siteConfig: {themeConfig: {navbar: {title, logo = {}} = {}}} = {}, } = useDocusaurusContext(); const logoUrl = useBaseUrl(logo.src); const { docsSidebars, path, sidebar: currentSidebar, sidebarCollapsible, } = props; useLockBodyScroll(showResponsiveSidebar); if (!currentSidebar) { return null; } const sidebarData = docsSidebars[currentSidebar]; if (!sidebarData) { throw new Error( `Cannot find the sidebar "${currentSidebar}" in the sidebar config!`, ); } if (sidebarCollapsible) { sidebarData.forEach(sidebarItem => mutateSidebarCollapsingState(sidebarItem, path), ); } return (
    {logo != null && {logo.alt}} {title != null && {title}}
    ); } export default DocSidebar;