From 2d39cbf97ca02710804f4308dc56c42425070d35 Mon Sep 17 00:00:00 2001 From: Vince Picone Date: Tue, 25 Jun 2019 09:31:24 -0500 Subject: [PATCH] fix: update lefnav icon logic (#196) * fix: update icon logic. closes #195 * update prop name --- .../components/LeftNav/ResourceLinks.js | 5 +- .../components/LeftNav/LeftNav.module.scss | 17 ++++++ .../src/components/LeftNav/ResourceLinks.js | 59 ++++++++++++++----- 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/packages/example/src/gatsby-theme-carbon/components/LeftNav/ResourceLinks.js b/packages/example/src/gatsby-theme-carbon/components/LeftNav/ResourceLinks.js index f1c69d4e6..dd0806943 100644 --- a/packages/example/src/gatsby-theme-carbon/components/LeftNav/ResourceLinks.js +++ b/packages/example/src/gatsby-theme-carbon/components/LeftNav/ResourceLinks.js @@ -2,10 +2,11 @@ import React from 'react'; import ResourceLinks from 'gatsby-theme-carbon/src/components/LeftNav/ResourceLinks'; const links = [ - { title: 'Github', href: 'https://github.com' }, + { title: 'Resources', href: '/resources' }, { title: 'Storybook', href: 'https://react.carbondesignsystem.com' }, ]; -const CustomResources = () => ; +// shouldOpenNewTabs: true if outbound links should open in a new tab +const CustomResources = () => ; export default CustomResources; diff --git a/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNav.module.scss b/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNav.module.scss index 240f1e61b..b5e1ea389 100644 --- a/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNav.module.scss +++ b/packages/gatsby-theme-carbon/src/components/LeftNav/LeftNav.module.scss @@ -7,3 +7,20 @@ .current-item .current-item-text { color: #171717; } + +.resource-link { + &:first-of-type { + margin-top: $spacing-05; + } +} +// Moves icons to the right +.outboundLink { + flex-direction: row-reverse; + justify-content: space-between; +} + +// Icons +.outboundLink div { + display: inline-block !important; + flex: 0; +} diff --git a/packages/gatsby-theme-carbon/src/components/LeftNav/ResourceLinks.js b/packages/gatsby-theme-carbon/src/components/LeftNav/ResourceLinks.js index 9db74be8e..7ade83422 100644 --- a/packages/gatsby-theme-carbon/src/components/LeftNav/ResourceLinks.js +++ b/packages/gatsby-theme-carbon/src/components/LeftNav/ResourceLinks.js @@ -2,24 +2,53 @@ import React from 'react'; import { SideNavLink } from 'carbon-components-react/lib/components/UIShell'; import { Link } from 'gatsby'; import LaunchIcon from '@carbon/icons-react/es/launch/16'; +import cx from 'classnames'; +import PropTypes from 'prop-types'; -const LeftNavResourceLinks = ({ links }) => - links ? ( +import { resourceLink, outboundLink } from './LeftNav.module.scss'; + +const LeftNavResourceLinks = ({ links, shouldOpenNewTabs }) => { + if (!links) return null; + + const shouldOpenNewTabsProps = { + ...(shouldOpenNewTabs && { rel: 'noopener noreferrer', target: '_blank' }), + }; + + return ( <>
- {links.map((link, i) => ( - } - href={link.href} - className="bx--side-nav--website-link" - element={!link.href.includes('http') ? Link : 'a'} - > - {link.title} - - ))} + {links.map(({ title, href, ...rest }, i) => { + const outbound = !/^\/(?!\/)/.test(href); + return ( + } + // eslint-disable-next-line jsx-a11y/aria-proptypes + aria-current="" + to={href} + href={href} + className={cx(resourceLink, { [outboundLink]: outbound })} + element={outbound ? 'a' : Link} + {...shouldOpenNewTabsProps} + > + {title} + + ); + })} - ) : null; + ); +}; + +LeftNavResourceLinks.propTypes = { + links: PropTypes.arrayOf( + PropTypes.objectOf({ + title: PropTypes.string, + href: PropTypes.string, + }) + ), + // true if outbound links should open in a new tab + shouldOpenNewTabs: PropTypes.bool, +}; export default LeftNavResourceLinks;