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(gatsby-link): adds support for partiallyActive=true to Link #12495

Merged
merged 4 commits into from
Mar 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion packages/gatsby-link/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
},
"dependencies": {
"@babel/runtime": "^7.0.0",
"@reach/router": "^1.1.1",
"@types/reach__router": "^1.0.0",
"prop-types": "^15.6.1"
},
Expand All @@ -26,6 +25,7 @@
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"@reach/router": "^1.1.1",
"gatsby": "^2.0.0",
"react": "^16.4.2",
"react-dom": "^16.4.2"
Expand Down
12 changes: 12 additions & 0 deletions packages/gatsby-link/src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ exports[`<Link /> matches basic snapshot 1`] = `
</a>
</div>
`;

exports[`<Link /> matches partially active snapshot 1`] = `
<div>
<a
class="link"
href="/active/nested"
style="color: black;"
>
link
</a>
</div>
`;
7 changes: 7 additions & 0 deletions packages/gatsby-link/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ describe(`<Link />`, () => {
expect(container).toMatchSnapshot()
})

it(`matches partially active snapshot`, () => {
const { container } = setup({
linkProps: { to: `/active/nested`, partiallyActive: true },
})
expect(container).toMatchSnapshot()
})

it(`does not fail to initialize without --prefix-paths`, () => {
expect(() => {
getInstance({})
Expand Down
6 changes: 4 additions & 2 deletions packages/gatsby-link/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function normalizePath(path) {
const NavLinkPropTypes = {
activeClassName: PropTypes.string,
activeStyle: PropTypes.object,
partiallyActive: PropTypes.bool,
}

// Set up IntersectionObserver
Expand Down Expand Up @@ -83,8 +84,8 @@ class GatsbyLink extends React.Component {
}
}

defaultGetProps = ({ isCurrent }) => {
if (isCurrent) {
defaultGetProps = ({ isPartiallyCurrent, isCurrent }) => {
if (this.props.partiallyActive ? isPartiallyCurrent : isCurrent) {
return {
className: [this.props.className, this.props.activeClassName]
.filter(Boolean)
Expand All @@ -105,6 +106,7 @@ class GatsbyLink extends React.Component {
activeClassName: $activeClassName,
activeStyle: $activeStyle,
innerRef: $innerRef,
partiallyActive,
state,
replace,
/* eslint-enable no-unused-vars */
Expand Down