Skip to content

Commit

Permalink
chore: Add auto collapsing to component library subnavs
Browse files Browse the repository at this point in the history
  • Loading branch information
LizBaker committed Jul 16, 2020
1 parent 93059ea commit 2c9a6a1
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/components/NavigationItems.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Fragment, useState, useContext, useEffect } from 'react';
import usePrevious from '../hooks/usePrevious';
import PropTypes from 'prop-types';
import FeatherIcon from './FeatherIcon';
import NewRelicIcon from './NewRelicIcon';
Expand Down Expand Up @@ -85,6 +86,7 @@ const NavIcon = ({ page }) => {

const NavItem = ({ page, depthLevel, searchTerm, filteredPageNames }) => {
const crumbs = useContext(BreadcrumbContext).flatMap((x) => x.displayName);
const prevCrumbs = usePrevious(crumbs) ?? [];
const isHomePage = crumbs.length === 0 && depthLevel === 0;
const [toggleIsExpanded, setToggleIsExpanded] = useState(false);
const [overviewIsExpanded, setOverviewIsExpanded] = useState(
Expand All @@ -96,7 +98,14 @@ const NavItem = ({ page, depthLevel, searchTerm, filteredPageNames }) => {
if (page.displayName !== 'Component library') {
setOverviewIsExpanded(isHomePage || crumbs.includes(page.displayName));
}
}, [isHomePage, page.displayName, crumbs]);
if (
prevCrumbs[prevCrumbs.length - 1] !== crumbs[crumbs.length - 1] &&
!crumbs.includes(page.displayName)
) {
setToggleIsExpanded(false);
setOverviewIsExpanded(false);
}
}, [isHomePage, page.displayName, prevCrumbs, crumbs]);

const isCurrentPage = crumbs[crumbs.length - 1] === page.displayName;
const isBreadCrumb = crumbs.includes(page.displayName);
Expand Down

0 comments on commit 2c9a6a1

Please sign in to comment.