Skip to content

Commit

Permalink
feat: Use group key on sidenav instead of subnav to populate group he…
Browse files Browse the repository at this point in the history
…ader
  • Loading branch information
jerelmiller committed Jun 17, 2020
1 parent d33e287 commit 22a9254
Show file tree
Hide file tree
Showing 3 changed files with 452 additions and 417 deletions.
108 changes: 62 additions & 46 deletions src/components/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useContext } from 'react';
import React, { Fragment, useState, useContext } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'gatsby';
import cx from 'classnames';
Expand All @@ -11,53 +11,69 @@ import styles from './Sidebar.module.scss';

// recursively create navigation
const renderNav = (pages, depthLevel = 0) => {
return pages.map((page, index) => {
const crumbs = useContext(BreadcrumbContext).flatMap((x) => x.displayName);
const [isExpanded, setIsExpanded] = useState(
crumbs.length === depthLevel || crumbs.includes(page.displayName)
);
const isCurrentPage = crumbs[crumbs.length - 1] === page.displayName;
const crumbs = useContext(BreadcrumbContext).map((x) => x.displayName);

return (
<li
className={cx(styles[`navDepth${depthLevel}`], {
[styles.isCurrentPage]: isCurrentPage,
})}
key={index}
>
{page.url ? (
<Link className={styles.navItem} to={page.url}>
{page.displayName}
{isCurrentPage && (
<FeatherIcon
className={styles.currentPageIndicator}
name="chevron-right"
/>
)}
</Link>
) : (
<div
className={styles.navItem}
role="button"
onClick={() => setIsExpanded((isExpanded) => !isExpanded)}
onKeyPress={() => setIsExpanded((isExpanded) => !isExpanded)}
tabIndex={0}
>
{page.displayName}
</div>
)}
{page.children && (
<ul
className={cx(styles.nestedNav, {
[styles.isExpanded]: isExpanded,
})}
const groupedPages = pages.reduce((groups, page) => {
const { group = '' } = page;

return {
...groups,
[group]: [...(groups[group] || []), page],
};
}, {});

return Object.entries(groupedPages).map(([group, pages]) => (
<Fragment key={group}>
{group && (
<li className={cx(styles.navLink, styles.groupName)}>{group}</li>
)}
{pages.map((page) => {
const [isExpanded, setIsExpanded] = useState(
crumbs.includes(page.displayName)
);
const isCurrentPage = crumbs[crumbs.length - 1] === page.displayName;

return (
<li
key={page.displayName}
data-depth={depthLevel}
className={cx({ [styles.isCurrentPage]: isCurrentPage })}
>
{renderNav(page.children, depthLevel + 1)}
</ul>
)}
</li>
);
});
{page.url ? (
<Link className={styles.navLink} to={page.url}>
{page.displayName}
{isCurrentPage && (
<FeatherIcon
className={styles.currentPageIndicator}
name="chevron-right"
/>
)}
</Link>
) : (
<div
role="button"
className={styles.navLink}
onClick={() => setIsExpanded((isExpanded) => !isExpanded)}
onKeyPress={() => setIsExpanded((isExpanded) => !isExpanded)}
tabIndex={0}
>
{page.displayName}
</div>
)}
{page.children && (
<ul
className={cx(styles.nestedNav, {
[styles.isExpanded]: isExpanded,
})}
>
{renderNav(page.children, depthLevel + 1)}
</ul>
)}
</li>
);
})}
</Fragment>
));
};

const Sidebar = ({ className, pages, isOpen }) => (
Expand Down
31 changes: 12 additions & 19 deletions src/components/Sidebar.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -54,39 +54,32 @@
padding-left: 0;
}

.navItem {
color: var(--color-black);
.navLink {
color: var(--color-neutrals-900);
margin-bottom: 1rem;
display: flex;
align-items: center;
justify-content: space-between;
text-decoration: none;

[data-depth='0'] > & {
font-weight: bold;
}
}

.currentPageIndicator {
stroke-width: 4;
}

.navDepth0 {
color: var(--color-black);
font-weight: bold;
}

.navDepth1 {
font-weight: normal;
}

.navDepth2 {
.groupName {
color: var(--color-neutrals-600);
font-weight: bold;
font-size: 0.75rem;
text-transform: uppercase;
color: var(--color-neutrals-600);
font-size: 14px;
}

.navDepth3 {
font-weight: normal;
text-transform: initial;
color: var(--color-black);
&:not(:first-child) {
margin-top: 2rem;
}
}

.isCurrentPage {
Expand Down
Loading

0 comments on commit 22a9254

Please sign in to comment.