Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish side navigation styling #155

Merged
merged 7 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/components/BreadcrumbContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export const BreadcrumbContext = React.createContext([]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] You can make this the default export since this is the "main" thing exported from this file.

69 changes: 43 additions & 26 deletions src/components/Sidebar.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,65 @@
import React from 'react';
import React, { useState, useContext } from 'react';
import PropTypes from 'prop-types';
import { Link } from 'gatsby';
import cx from 'classnames';
import { BreadcrumbContext } from './BreadcrumbContext';

import { link } from '../types';
import styles from './Sidebar.module.scss';

// recursively create navigation
const renderNav = (page, index) => (
<li key={index}>
{page.url ? (
<Link to={page.url} className={cx({ [styles.isActive]: page.active })}>
{page.displayName}
</Link>
const renderNav = (pages, depthLevel = 0) => {
return pages.map((page, index) => {
const crumbs = useContext(BreadcrumbContext).flatMap((x) => x.displayName);
const [isDisplay, setIsDisplay] = useState(
crumbs.length === depthLevel || crumbs.includes(page.displayName)
);
const isCurrentPage = crumbs[crumbs.length - 1] === page.displayName;

const display = page.url ? (
<Link to={page.url}>{page.displayName}</Link>
) : (
<div>{page.displayName}</div>
)}
{page.children && <ul>{page.children.map(renderNav)}</ul>}
</li>
);
<div
role="button"
onClick={() => setIsDisplay(!isDisplay)}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, if you do have a section that is collapsed and can be expanded because it doesn't include a url, will this collapse other sections or leave them open?

onKeyPress={() => setIsDisplay(!isDisplay)}
tabIndex={0}
>
{page.displayName}
</div>
);
let subNav;

const Sidebar = ({ className, pages, isOpen, toggle }) => (
if (page.children) {
subNav = renderNav(page.children, depthLevel + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nit] Since this subnav variable isn't used elsewhere, you can move this inline:

<ul {/* ... */}>
  {page.children && renderNav(page.children, depthLevel + 1)}
<ul>

Totally your call on what you find more readable.

}
return (
<li
className={cx(styles[`navDepth${depthLevel}`], {
[styles.isCurrentPage]: isCurrentPage,
})}
key={index}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the URLs should all be unique, I think it would be better to use that url as the key here. This will be more efficient for React since it won't need to destroy/recreate the DOM nodes if the links re-order themselves (like if a section is collapsed, or we add filtering to the nav).

>
{display}
<ul className={cx(styles.nestedNav, { [styles.isDisplay]: isDisplay })}>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are no children for the page, does this ul render with empty content? Should this ul render at all if there are no links to render inside of it?

{subNav}
</ul>
</li>
);
});
};

const Sidebar = ({ className, pages, isOpen }) => (
<aside className={cx(styles.sidebar, className, { [styles.isOpen]: isOpen })}>
<Link to="/" className={styles.logo} />
<div className={styles.top}>
<h3>Pages</h3>
<button
aria-expanded={isOpen}
aria-label="Main Menu Toggle"
type="button"
onClick={() => toggle()}
>
{isOpen ? 'close' : 'open'}
</button>
</div>
<nav role="navigation" aria-label="Sidebar">
<ul>{pages.map(renderNav)}</ul>
<ul className={styles.listNav}>{renderNav(pages)}</ul>
</nav>
</aside>
);

Sidebar.propTypes = {
className: PropTypes.string,
toggle: PropTypes.func.isRequired,
pages: PropTypes.arrayOf(link).isRequired,
isOpen: PropTypes.bool,
};
Expand Down
60 changes: 51 additions & 9 deletions src/components/Sidebar.module.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.sidebar {
font-size: 0.875rem;

ul {
margin: 0;
padding-left: 1rem;
Expand All @@ -9,15 +11,6 @@
text-decoration: none;
color: var(--color-black);
display: inline-block;
padding: 0.2rem 0;

&:hover {
text-decoration: underline;
}

&.isActive {
font-weight: bold;
}
}

h3 {
Expand Down Expand Up @@ -67,3 +60,52 @@
margin-right: 1rem;
background-image: url('../images/developers-logo.svg');
}

.nestedNav {
display: none;
}

.isDisplay {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to call this class name expanded? It took me a bit to realize the isDisplay was for expanding a section of the nav.

display: block;
}

.listNav {
ul {
padding-left: 1rem;
}
}

.navDepth0 {
color: var(--color-black);
font-weight: bold;
div {
margin: 1rem 0;
}
li {
margin: 1rem 0;
}
}

.navDepth1 {
font-weight: normal;
ul {
padding-left: 0.2rem;
}
}

.navDepth2 {
font-weight: bold;
text-transform: uppercase;
color: var(--color-neutrals-600);
font-size: 14px;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[style] Prefer rems instead of px for font sizes. Can you change this to 0.875rem?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that the .sidebar class already includes this font size. Is there a need for this declaration here?

}

.navDepth3 {
font-weight: normal;
text-transform: initial;
color: var(--color-black);
}

.isCurrentPage {
font-weight: bold;
}
Loading