Skip to content
This repository has been archived by the owner on Dec 15, 2018. It is now read-only.

Correct usage of nested Links, to keep active state of parent #253

Open
mkascel opened this issue Dec 20, 2017 · 2 comments
Open

Correct usage of nested Links, to keep active state of parent #253

mkascel opened this issue Dec 20, 2017 · 2 comments

Comments

@mkascel
Copy link

mkascel commented Dec 20, 2017

I have a nav/subnav structure set up where I'm using an app-wide navigation with Links and on one page, I have a sub-nav of other Links. The problem is that as I navigate through the submenu, the "parent's" (app-wide) active state gets lost, because I'm expecting it to act in a greedy way when determining whether it's link is active. What is the correct way to achieve what I'm trying to do below?

Parent link in app-wide nav
<Link href="/home/me" activeProps={{ className: 'active' }}>{'Home'}</Link>

Sublinks in page-nav

<Link href="/home/me" activeProps={{ className: 'active' }}>{'Me'}</Link>
<Link href="/home/me/about" activeProps={{ className: 'active' }}>{'About'}</Link>
<Link href="/home/me/other" activeProps={{ className: 'active' }}>{'Other'}</Link>

The routes I have defined for this are:

  '/home/:homeId': {
    '/:sectionName': {},
  },

The "me" parts of the path above will eventually be dynamic.

Thanks in advance!

@mkascel
Copy link
Author

mkascel commented Dec 21, 2017

Looking at the Link source, it doesn't seem this is actually supported currently; there's just a simple equality comparison between the current browser pathname and the Link instance's defined path to determine whether something is active or not.

To support this it looks like it would be necessary to inspect result.parent as well, is this a feature you'd be interested in adding? What would the best API for that be, a boolean passed to Link or something else?

@mkascel
Copy link
Author

mkascel commented Dec 21, 2017

Perhaps it could be a prop similar to Fragment to unify the API a bit, activeForRoute?

For anyone else who comes across this, for now I am managing this manually by connecting this particular Link instance to the redux store as mentioned here and managing className="active" based on a String.prototype.includes check

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant