No commit activity in last 3 years
No release in over 3 years
This gem provides an easy way to change navbar links based on controller and action in ruby on rails. Usecase: Can be used to activate certain link based on controller.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
~> 10.0

Runtime

 Project Readme

Navbar-Highlighter

Navbar highlighter provides an easier way to activate link based on controller and action used by rails.

Usecase: You want to activate certain links when user is hits static controller's index action. you just need to add data-controller='controller_name' and data-action='action_name' in order to add specified class to it when user hits this controller's action.

Usage

Add navbar_highlighter to Gemfile and bundle install

gem 'navbar_highlighter'

Add the following to your app/assets/javascripts/application.js:

//= require navbar_highlighter

Now, Initialize navbar highlighter using following code, this can be done in a separate js file. for example: navbar_highlighter_init.js

If you are doing so, don't forget to add it to asset.rb like so: Rails.application.config.assets.precompile += %w( navbar_highlighter_init.js )

(function(){
  var data = {
    // navbar you want to use.
    navBar: $('[data-navbar=highlight]'),

    // navbar links class selector
    linkSelectorClass: 'primary-link',

    // name of the class you want to add to activate a link
    classNameToAdd: 'active-link'
  },

  linkHighlighter = new NavbarHighlighter(data);
  linkHighlighter.init();

})();

Add<%= navbar_highlighter_tags %> to any layout that you are using.


Add data-controller='controller-name' and data-action='action-name' to the links you want to add class based on controller and action.

Add data-navbar='highlight' to navbar just like below snippet:

<nav data-navbar="highlight">
  <ul class="primary-nav-list">
    <li class="primary-menu">
      <a href="/" class="primary-link" data-controller="sessions" data-action="index">
        Home
      </a>
    </li>

    <li class="primary-menu">
      <a href="/users" class="primary-link" data-controller="users" data-action="index" data-parent='sessions'>
      <!-- Adding data-parent='controller_name' will add active-link class to parent link i.e Home link in this case. -->
      Users
      </a>
    </li>
    <ul>
</nav>

Note:

As user hits sessions_controller index action Home link will be active, and when user clicks Users link Users link will be get active-link class.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/abhikanojia/navbar_highlighter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the NavbarHighlighter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.