Navtastic
Navtastic is way to create and display complex navigation menus for websites. It allows for runtime
configurations of menus, depending on context.
- Keep menu content and rendering logic separate
- Automatically highlight the current page
- Different output structures supported (Simple, Bootstrap4, Bulma, Foundation6)
- HTML structure only, bring your own CSS.
- Easily create your own renderer to fit your style.
The current renderers only support vertical menus. Horizontal (navbar) menus are planned.
Table of Contents
Installation
Add it to your Gemfile:
gem 'navtastic'
Run the following command to install it:
bundle install
Example
Define a menu somewhere:
Navtastic.define :main_menu do |menu|
menu.item "Home", '/'
menu.item "Posts", '/posts'
menu.item "About", '/about'
end
Render it in your partials:
<%= Navtastic.render :main_menu, current_url %>
Using the default renderer, assuming that the current url starts with /posts
, will result in:
<ul>
<li>
<a href="/">Home</a>
</li>
<li class="current">
<a href="/posts">Posts</a>
</li>
<li>
<a href="/about">About</a>
</li>
</ul>
Rails
When using rails, a render_menu
helper is added that automatically sets the current_url.
Also, put defined menus in app/menus
to take advantage of hot reload.
Documentation
Every item can have a submenu. They can be nested as deeply as you want.
Navtastic.define :main_menu do |menu|
menu.item "Home", '/' do |submenu|
submenu.item "Posts", '/posts'
submenu.item "About", '/about'
end
menu.item "Settings" do |submenu|
submenu.item "General", '/settings'
submenu.item "Profile", '/settings/profile'
end
end
By default, submenus will be rendered inside the <li>
tag of the parent item.
<ul>
<li>
<a href="/">Parent</a>
<ul>
<li>
<a href="/child">Child</a>
</li>
</ul>
</li>
</ul>
Current item
The current active item is decided by the current_url
parameter when rendering a menu. It is the
item with the longest url that starts with the current_url.
For example, if there is a menu containing these urls:
If the current_url is /posts/featured/2017
, the /posts/featured
item will be highlighted. If the
current_url is /posts/123
, then /posts
is highlighted.
The root url /
will always match, if no other items match the current url. If there is no item
with /
as url in the menu and no other urls match, nothing will be highlighted.
Runtime parameters
You can pass runtime parameters when defining a menu. For example, passing the current user and
change the menu accordingly.
Navtastic.define :main_menu do |menu, params|
menu.item "Home", "/"
if params[:current_user]
menu.item "Profile", "/users/#{params[:current_user].id}"
menu.item "Logout", "/logout"
else
menu.item "Login", "/login"
end
end
Navtastic.render :main_menu, current_url, current_user: User.current
Global Configuration
Some global settings that Navtastic uses can be configured. Make sure the configuration happens
before defining any menu (e.g. when using rails, add it to config/initializers/navtastic.rb
).
These are the defaults values:
Navtastic.configure do |config|
config.base_url = nil
config.renderer = :simple
config.renderer_options = {}
config.reload_renderer = false
end
Each individual menu can also be configured in some way:
Navtastic.define :user_menu do |menu, params|
menu.config.base_url = '/users/' + params[:user_id]
menu.item "Profile", '/profile'
end
Item Configuration
Every item can have options passed to it:
Navtastic.define :menu do |menu|
menu.item "Home", "/", class: 'highlight'
menu.item "Posts", "/posts", content_class: 'important'
menu.item "Somewhere", '/', root: true
menu.item "Settings", '/settings', base_url: true do |submenu|
end
end
Renderers
The following renders are currently available:
- Simple adds a
current
class to the basic list structure - Bootstrap4 is used with the Bootstrap 4 framework.
- Bulma is used with the Bulma.io framework.
- Foundation6 is used with the Foundation 6 framework.
Some renderers also support extra options. You can set them globally in the configuration or pass
them at runtime when rendering a menu:
Navtastic.configure do |config|
config.renderer_options = { option: value }
end
# Runtime options
<%= Navtastic.render :menu, current_url, renderer: { option: value } %>
Globally defined options and options given at runtime are merged together, with those at runtime
taking precedence.
Bulma Configuration
Foundation Configuration
-
active_class (default: nil
)
CSS class to use for active items. Active items are items that have a child that is the current
item. Useful to highlight the current item in a drilldown menu.
-
style (default: nil
)
Set to :drilldown
to generate a drilldown
menu. Make sure you include the
required javascript files.
left: default style, right: drilldown style
Development
- Checkout Repository:
git clone https://github.com/aramvisser/navtastic.git
- Install Bundler:
gem install bundler
- Run
bundle install
inside the project root to install the gem dependencies - Run test cases:
bundle exec rake
For testing visual changes, a test server is provided by running bundle exec rake demo
. It's
accessibly on http://localhost:9090.