-
Notifications
You must be signed in to change notification settings - Fork 4.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
Try: Use route detection to set active state of links #24985
Try: Use route detection to set active state of links #24985
Conversation
I'm hesitant to go down this path because it transforms this component from something presentational to a full solution. I see the benefit to offloading this logic from the consuming app, but it comes at the cost of diversity of use cases. Are there advantages that I'm not considering? |
I'm pretty sure I agree as well. This PR was just meant to be an experiment to test various use cases. I'd rather leave the component open-ended, but on the slight chance that we could cover all use cases in the component, it would be great to internalize this logic.
I think we can skip using the full-blown router and just detect history changes and allow path matching. This would allow the consumer to continue using their own router and this component to watch for changes. But unfortunately I can't seem to resolve the dependency issue I mentioned in Slack so I'm unclear on how well this works across different routers. One piece of this I'm concerned with is having multiple extensions who each utilize different routers. Currently we can expose a method to update the navigation active state, but this is one more method that third parties will need to subscribe to. Also in this case, if two plugins overlap on which item should be active, how do we determine the source of truth? |
9004526
to
8ae98de
Compare
@psealock I updated this a bit and pulled out the dependencies in favor of a more simple history detection script. Pros of this approach:
Cons of this approach:
I started this as an experiment, but I'm starting to lean towards this approach for the pros mentioned above. I'd love to hear any use cases you feel this doesn't work for. My initial thoughts were that there may be cases where location is not the determining factor for status, however, I think these may be edge cases and most navigation items will require a location by nature (even if that's just a hash change). |
Is this PR still relevant? I saw very promising work in #27124 where @Addison-Stavlo and @noahtallen work on similar logic. Do you plan to continue working on this feature? |
@gziolo This logic has been moved to https://github.com/woocommerce/woocommerce-admin/blob/main/client/navigation/utils.js for the time being. It may be something we eventually look at merging back into GB, but it still needs refining. Thanks for the link to #27124! We're discussing a similar idea around route/context detection, although we'd need to keep the solution much more open-ended and extensible for 3PDs registering nav items. Closing this PR for now. |
Description
This is an attempt to set the active state based on the current route. It works by watching history changes and updating the active link based on routes that match.
If we go this route, there's probably some follow-ups needed:
isActive
on an item.Testing
npm run storybook:dev
.http://localhost:50240/iframe.html?id=components-navigation--default&viewMode=story
(to avoid the constraints of the iframe in storybook).history.pushState({fdsfds: 'bfdssla'}, 'New Title', '#item-1');
and note the active item is updated.