-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Add option to gatsby-link for matching active when on child routes #7208
Comments
👍 as well. Until then, the following polyfill might help someone: const isPartiallyActive = ({ isPartiallyCurrent }) =>
isPartiallyCurrent ? { className: "item active" } : null; <div
class="item"
activeClassName={link === "/" ? "active" : undefined}
getProps={link === "/" ? undefined : isPartiallyActive}
/> This checks whether the link links to the root level (and thus should not be have the More info is in the reach router docs. See https://gitlab.com/libresat/libresat/tree/master/packages/site for an example (blog posts). |
@KyleAMathews would you accept a PR for this feature? |
|
@ryanflorence any thoughts here? |
@KyleAMathews If @seaneking is willing to provide a PR for this feature, I think that's great. Couldn't If you're worried about changing the default behavior for everyone, maybe we could add a global option to module.exports = {
siteMetadata: {
...
},
plugins: [
...
],
settings: {
gatsby-link: {
greedyActive: true,
}
}
} |
Hi there, if at all possible would you have an example using this? I'm having problems implementing this functionality. Thanks! |
@wllkle This is how I am using it. import React from 'react'
import { Link } from 'gatsby'
const partlyActive = className => ({ isPartiallyCurrent }) => ({
className: className + (isPartiallyCurrent ? ` active` : ``),
})
const PartlyActiveLink = ({ className, ...rest }) => (
<Link getProps={partlyActive(className)} {...rest} />
) In my case, the actual styles are then coming from a import styled from 'styled-components'
const NavLink = styled(PartlyActiveLink)`
color: ${props => props.theme.lightBlue};
transition: ${props => props.theme.shortTrans};
cursor: pointer;
&.active {
color: ${props => props.theme.darkYellow};
}
:hover {
color: ${props => props.theme.lightGreen};
}
` |
Hiya! This issue has gone quiet. Spooky quiet. 👻 We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here. If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open! Thanks for being a part of the Gatsby community! 💪💜 |
Think we should keep this open, until it's clarified either way whether from the team whether this should feature be included in gatsby-link or not (I vote yes, FWIW). Tbh I think this should be the default behaviour, with an opt-out flag instead. But that would be a breaking change so fair enough if it's added as an opt-in feature instead. |
I've opened a PR, in case someone actually implementing it was all that was needed 😛 (#12495) |
Summary
Would be great to mirror reach router's
isCurrent
andisPartiallyCurrent
options togatsby-link
sactiveStyle
andactiveClassname
props. Basically I'd want to be able to designate a Link as active when any child routes are also active, without having to bypass gatbsy-link and useisPartiallyCurrent
directly.Basic example
Hesitant to suggest just tacking on another prop as lazy DX, but that would probably make the most sense. Something like this:
Would default to false, so non-breaking change that doesn't really add much cognitive overhead for people.
Motivation
Common use-case is top-level nav for any nested content (eg: a blog), where you'd often want to designate the nav item active (eg: 'Blog') when on a child path (eg:
/blog/some-post
).If this isn't added, would at least be worth documenting how to achieve by accessing react router APIs directly in gatsby-link docs, as I imagine it's a common pattern.
The text was updated successfully, but these errors were encountered: