question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add helper for automatically highlighting the active item in a menu

See original GitHub issue

Automatically highlighting the active part in e.g. a menu hierarchy can already be done manually by making the menu or individual links implement AfterNavigationObserver and then manually toggle a css classname or an attribute.

In the general case, menu items can be represented by any kind of component and there can also be arbitrary logic for when a section is considered to be active. In practice, the menu items are usually RouterLink components and the hierarchy is either defined based on the URL structure or sharing a common RouterLayout (though in most cases the topmost layout is shared among the entire application).

We could add helper API to RouterLink for the common cases and ensure the documentation also contains an example of how this is done manually.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

3reactions
Legiothcommented, Nov 14, 2017

Some ideas on what the API in RouterLink could look like:

// Low level flexible API based on BiPredicate<RouterLink, AfterNavigationEvent>
someLink.setHighlightCondition((link, event) -> event.getLocation().getPath().startsWith("foo"));

// Helper for creating conditions
someLink.setHighlightCondition(HighlightConditions.sameLocation()); // This could be used by default?
someLink.setHighlightCondition(HighlightConditions.locationPrefix("foo"));
someLink.setHighlightCondition(HighlightConditions.locationPrefix()); // Uses the URL of this link as the prefix
someLink.setHighlightCondition(HighlightConditions.hasLayout(SomeLayout.class));

// Configuring how the highlight is represented in the DOM
someLink.setHighlightClassName("link-active"); // default is null
someLink.setHighlightAttribute("link-active"); // default is "active"

Edit: Renamed APIs to use highlight instead of active

1reaction
Legiothcommented, Feb 9, 2018

Anything that is currently attached to the component tree will receive navigation events if they implement the appropriate interface.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add helper for automatically highlighting the active item in a menu
Automatically highlighting the active part in e.g. a menu hierarchy can already be done manually by making the menu or individual links implement ......
Read more >
How To Style And Highlight The Active Menu Item In Divi
In this tutorial I will show you how to style and highlight the active menu item in your Divi navigation menu.
Read more >
How to keep the active menu item highlighted when visiting ...
The selected/clicked menu gets highlighted for a second, and then automatically first menu gets highlighted. So as soon as the whole page ...
Read more >
Menus - Android Developers
An optional, invisible container for <item> elements. It allows you to categorize menu items so they share properties such as active state and ......
Read more >
Need help keeping active Bootstrap dropdown menu highlighted
The easiest way to solve this is on your Account page, have a small javascript/jquery program that will add the necessary class to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found