Skip to content

Commit

Permalink
Determine active page by considering parent too
Browse files Browse the repository at this point in the history
Currently the logic that works out which page to highlight as ‘active’
in the top navigation is quite naïve (only compares URLs directly).

This commit makes it a bit smarter so that a page can define its
`parent`, then the navigation will highlight the matching ‘parent’
navigation item.

Adapted from: alphagov/govuk-developer-docs@7e5ac51
  • Loading branch information
quis committed Apr 25, 2018
1 parent 492e621 commit 7c82eba
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
5 changes: 5 additions & 0 deletions lib/govuk_tech_docs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
require 'govuk_tech_docs/redirects'
require 'govuk_tech_docs/table_of_contents/helpers'
require 'govuk_tech_docs/contribution_banner'
require 'govuk_tech_docs/helpers'
require 'govuk_tech_docs/meta_tags'
require 'govuk_tech_docs/page_review'
require 'govuk_tech_docs/pages'
Expand Down Expand Up @@ -70,6 +71,10 @@ def current_page_review
def format_date(date)
date.strftime('%-e %B %Y')
end

def active_page
@active_page ||= NavigationHelpers.new(current_page)
end
end

context.page '/*.xml', layout: false
Expand Down
17 changes: 17 additions & 0 deletions lib/govuk_tech_docs/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module GovukTechDocs
class NavigationHelpers

def initialize(current_page)
@current_page = current_page
end

def from_path?(page_path)
("/" + @current_page.path) == page_path || @current_page.data.parent == page_path
end

private

attr_reader :config, :current_page

end
end
6 changes: 3 additions & 3 deletions lib/source/layouts/_header.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@

<nav id="navigation" class="header__navigation js-nav" aria-label="Top Level Navigation" aria-hidden="true">
<ul>
<% config[:tech_docs][:header_links].each do |title, url| %>
<li<% if url == current_page.url %> class="active"<% end %>>
<a href="<%= url %>">
<% config[:tech_docs][:header_links].each do |title, path| %>
<li<% if active_page.from_path?(path) %> class="active"<% end %>>
<a href="<%= path %>">
<%= title %>
</a>
</li>
Expand Down

0 comments on commit 7c82eba

Please sign in to comment.