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

feat: ability to add divider to side nav #885

Merged
merged 9 commits into from
Jun 26, 2020
19 changes: 19 additions & 0 deletions packages/example/src/pages/guides/navigation/sidebar.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ Some important things to note here:
- You can make a `Page/index.mdx` file if you’d prefer to have assets in a folder.
The path would still just look like `/Page`

## Add divider
xylish7 marked this conversation as resolved.
Show resolved Hide resolved

You can add divider(s) between the items of your nav item list by adding `hasDivider: true` in `src/data/nav-items.yaml`. Note the divider is only for top-level nav items and can only be used with the left sidebar navigation style. It cannot be used in conjunction with the [header navigation style](/guides/configuration#navigation-style).

```yaml
- title: Menu
pages:
- title: Page 1
path: /menu/Page-1
- title: Page 2
path: /menu/Page-2
hasDivider: true
- title: Single Page
pages:
- path: /single-page
```

In the example above, a divider will appear between **Menu** and **Single Page**.

## Customizing

The nav item list can be customized using Gatsby theme [shadowing](../shadowing).
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-theme-carbon/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ exports.createSchemaCustomization = ({ actions, schema }) => {
type NavItemsYaml implements Node {
title: String!
pages: [NavItemsYamlPage]!
hasDivider: Boolean
}`,
schema.buildObjectType({
name: 'MediumFeed',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ const LeftNav = (props) => {
>
<SideNavItems className="sidenav-list">
{navItems.map((item, i) => (
<LeftNavItem items={item.pages} category={item.title} key={i} />
<LeftNavItem
items={item.pages}
category={item.title}
key={i}
hasDivider={item.hasDivider}
/>
))}
<LeftNavResourceLinks />
</SideNavItems>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
color: $ui-05;
}

.divider {
margin: $spacing-03 $spacing-05;
border-color: var(--cds-ui-03, #e0e0e0);
sstrubberg marked this conversation as resolved.
Show resolved Hide resolved
border-style: solid;
border-bottom: none;
}

.resource-link {
&:first-of-type {
margin-top: $spacing-05;
Expand Down
56 changes: 32 additions & 24 deletions packages/gatsby-theme-carbon/src/components/LeftNav/LeftNavItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import {
SideNavMenuItem,
} from 'carbon-components-react';

import styles, { currentItem } from './LeftNav.module.scss';
import styles from './LeftNav.module.scss';

import NavContext from '../../util/context/NavContext';
import usePathprefix from '../../util/hooks/usePathprefix';

const LeftNavItem = (props) => {
const { items, category } = props;
const { items, category, hasDivider } = props;
const { toggleNavState } = useContext(NavContext);
const closeLeftNav = () => {
toggleNavState('leftNavIsOpen', 'close');
Expand All @@ -34,31 +34,39 @@ const LeftNavItem = (props) => {

if (items.length === 1) {
return (
<SideNavLink
onClick={closeLeftNav}
icon={<span>dummy icon</span>}
element={Link}
className={cx({ [currentItem]: isActive })}
isActive={isActive}
to={`${items[0].path}`}
>
{category}
</SideNavLink>
<>
<SideNavLink
onClick={closeLeftNav}
icon={<span>dummy icon</span>}
element={Link}
className={cx({
[styles.currentItem]: isActive,
})}
isActive={isActive}
to={`${items[0].path}`}
>
{category}
</SideNavLink>
{hasDivider && <hr className={styles.divider} />}
</>
);
}
return (
<SideNavMenu
icon={<span>dummy icon</span>}
isActive={isActive} // TODO similar categories
defaultExpanded={isActive}
title={category}
>
<SubNavItems
onClick={closeLeftNav}
items={items}
pathname={pathname}
/>
</SideNavMenu>
<>
<SideNavMenu
icon={<span>dummy icon</span>}
isActive={isActive} // TODO similar categories
defaultExpanded={isActive}
title={category}
>
<SubNavItems
onClick={closeLeftNav}
items={items}
pathname={pathname}
/>
</SideNavMenu>
{hasDivider && <hr className={styles.divider} />}
</>
);
}}
</Location>
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby-theme-carbon/src/util/NavItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function useNavItems() {
title
path
}
hasDivider
}
}
}
Expand Down