Skip to content

Commit

Permalink
Update design of secondary navigation component (#3102)
Browse files Browse the repository at this point in the history
- Aligns with updated navigation design in the NHS header (no padding
either side)
- Uses same method of highlighting current item, using `<strong>`
element as a fallback
- Items appear linearised on smaller screens
  • Loading branch information
paulrobertlloyd authored Feb 27, 2025
2 parents 1ae83f7 + f810971 commit 9785b01
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 73 deletions.
112 changes: 46 additions & 66 deletions app/assets/stylesheets/_secondary-navigation.scss
Original file line number Diff line number Diff line change
@@ -1,88 +1,68 @@
// Adapted from https://github.com/x-govuk/govuk-prototype-components/blob/main/x-govuk/components/secondary-navigation/_secondary-navigation.scss
.app-secondary-navigation {
@include govuk-font(19);
}

.app-secondary-navigation--sticky {
background-color: $color_nhsuk-grey-5;
position: sticky;
top: 0;
z-index: 99;
}
@include nhsuk-font(19);
@include nhsuk-responsive-margin(5, "bottom");

.app-secondary-navigation__link {
@include govuk-link-common;
@include govuk-link-style-no-visited-state;
@include govuk-link-style-no-underline;
margin-left: #{$nhsuk-gutter-half * -1};
margin-right: #{$nhsuk-gutter-half * -1};

// Extend the touch area of the link to the list
&::after {
bottom: 0;
content: "";
left: 0;
position: absolute;
right: 0;
top: 0;
@include mq($from: tablet) {
margin-left: auto;
margin-right: auto;
}
}

.app-secondary-navigation__list {
@include govuk-clearfix;

// The list uses box-shadow rather than a border to set a 1px
// grey line at the bottom, so that border from the current
// item appears on top of the grey line.
box-shadow: inset 0 -1px 0 $govuk-border-colour;
list-style: none;
margin: 0;
padding: 0;
width: 100%;
}
.app-secondary-navigation__link {
@include nhsuk-link-style-default;
@include nhsuk-link-style-no-visited-state;

.app-secondary-navigation__list-item {
box-sizing: border-box;
display: block;
float: left;
margin-right: govuk-spacing(4);
padding-bottom: govuk-spacing(2);
padding-top: govuk-spacing(2);
position: relative;
padding: nhsuk-spacing(2) $nhsuk-gutter-half;
text-decoration: none;

// More generous padding beneath items on wider screens
@include govuk-media-query($from: tablet) {
padding-bottom: govuk-spacing(3);
@include mq($from: tablet) {
padding: nhsuk-spacing(3) 2px;
}
}

// The last item of the list doesn’t need any spacing to its right.
// Removing this prevents the item from wrapping to the next line
// unnecessarily.
.app-secondary-navigation__list-item:last-child {
margin-right: 0;
}
&[aria-current] {
box-shadow: inset $nhsuk-border-width 0 $color_nhsuk-blue;
color: $nhsuk-text-color;
text-decoration: none;

@include mq($from: tablet) {
box-shadow: inset 0 ($nhsuk-border-width * -1) $color_nhsuk-blue;
}
}

.app-secondary-navigation__list-item--current {
border-bottom: $govuk-border-width solid $govuk-brand-colour;
padding-bottom: govuk-spacing(1);
&:focus {
box-shadow: inset $nhsuk-focus-width 0 $nhsuk-focus-text-color;

// More generous padding beneath items on wider screens
@include govuk-media-query($from: tablet) {
padding-bottom: govuk-spacing(2);
@include mq($from: tablet) {
box-shadow: inset 0 ($nhsuk-focus-width * -1) $nhsuk-focus-text-color;
}
}
}

.app-secondary-navigation__list-item--current
.app-secondary-navigation__link:link,
.app-secondary-navigation__list-item--current
.app-secondary-navigation__link:visited {
color: $govuk-text-colour;
.app-secondary-navigation__current {
font-weight: inherit;
}

.app-secondary-navigation__list-item {
.app-secondary-navigation__list {
// The list uses box-shadow rather than a border to set a 1px grey line at the
// bottom, so that the current item appears on top of the grey line.
box-shadow: inset 0 -1px 0 $nhsuk-border-color;
display: flex;
flex-flow: column;
list-style: none;
margin: 0;
padding: 0;
width: 100%;

@include mq($from: tablet) {
flex-flow: row wrap;
gap: nhsuk-spacing(2) nhsuk-spacing(4);
}
}

.app-secondary-navigation__link {
padding-left: nhsuk-spacing(3);
padding-right: nhsuk-spacing(3);
.app-secondary-navigation__list-item {
margin-bottom: 0;
}
22 changes: 16 additions & 6 deletions app/components/app_secondary_navigation_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
<nav <%= tag.attributes @attributes %>>
<ul class="app-secondary-navigation__list">
<% items.each do |item| %>
<li class="app-secondary-navigation__list-item
<%= "app-secondary-navigation__list-item--current" if item.selected %>">
<%= link_to item.href,
class: "app-secondary-navigation__link",
data: { turbo: true, turbo_action: "replace" } do %>
<%= item %>
<li class="app-secondary-navigation__list-item">
<% if item.selected %>
<%= link_to item.href,
class: "app-secondary-navigation__link",
aria: { current: true },
data: { turbo: true, turbo_action: "replace" } do %>
<strong class="app-secondary-navigation__current">
<%= item %>
</strong>
<% end %>
<% else %>
<%= link_to item.href,
class: "app-secondary-navigation__link",
data: { turbo: true, turbo_action: "replace" } do %>
<%= item %>
<% end %>
<% end %>
</li>
<% end %>
Expand Down
2 changes: 1 addition & 1 deletion spec/components/app_secondary_navigation_component_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

it { should have_css("ul.app-secondary-navigation__list") }
it { should have_css("li.app-secondary-navigation__list-item") }
it { should have_css("li.app-secondary-navigation__list-item--current") }
it { should have_css("strong.app-secondary-navigation__current") }

it { should have_link("Example 1", href: "https://example.com") }
it { should have_link("Example 2", href: "https://example.com") }
Expand Down

0 comments on commit 9785b01

Please sign in to comment.