Skip to content

Commit 53a7e11

Browse files
Add current item indicator for header navigation (#1067)
* Add current item indicator for header navigation Add support for indicating the current section, using either `current: true` (if the user is currently on this exact page) or `active: true` (if the user is in that section but not necessarily that exact page). When displayed in the regular view within the blue header, the active links have a light-grey border at the bottom. When displayed in the expanded menu view (eg on mobile), they have a blue border to their left. * Remove custom class * Fix focus style on expanded menu item * Changed current item indicator in white nav to blue * Bump mobile current item indicator to 8px * Set current item in example header-org-white variant * Update backstop images * Fix * Bump mobile nav current idem indicator to 6px * Remove duplicate entries from changelog merge issues * Add entry to changelog * Update backstop images * Remove excess whitespace * Fix: focus link in white header should always have black border --------- Co-authored-by: Paul Robert Lloyd <me+git@paulrobertlloyd.com>
1 parent 46854f9 commit 53a7e11

16 files changed

+91
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# NHS.UK frontend Changelog
2-
2+
33
## Unreleased
44

5+
:new: **New features**
6+
7+
- The primary navigation in the header now lets you show which item is the current section or page. ([PR 1067](https://github.com/nhsuk/nhsuk-frontend/pull/1067))
8+
59
:wrench: **Fixes**
610

711
- Fix appearance of disabled warning buttons ([Issue 1034]([https://github.com/nhsuk/nhsuk-service-manual-community-backlog/issues/1034]))

app/components/header/header-navigation.njk

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,8 @@
88
{{ header({
99
"primaryLinks": [
1010
{
11-
"url" : "https://www.nhs.uk/conditions",
12-
"label" : "Health A-Z",
13-
"classes": "app-header__navigation-item--current",
14-
"attributes": {
15-
"aria-current": "true"
16-
}
11+
href: "https://www.nhs.uk/conditions",
12+
text: "Health A-Z"
1713
},
1814
{
1915
href: 'https://www.nhs.uk/live-well/',
@@ -25,7 +21,8 @@
2521
},
2622
{
2723
href: 'https://www.nhs.uk/conditions/social-care-and-support/',
28-
text: 'Care and support'
24+
text: 'Care and support',
25+
active: true
2926
},
3027
{
3128
href: 'https://www.nhs.uk/pregnancy/',

app/components/header/header-org-white-nav.njk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
{
2121
href: '#',
22-
text: 'Wards and departments'
22+
text: 'Wards and departments',
23+
active: true
2324
},
2425
{
2526
href: '#',

app/components/header/header-org-white.njk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
{
2121
href: '#',
22-
text: 'Wards and departments'
22+
text: 'Wards and departments',
23+
active: true
2324
},
2425
{
2526
href: '#',

app/components/header/header-service-name-with-nav.njk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
{
2121
href: '#',
22-
text: 'Design system'
22+
text: 'Design system',
23+
current: true
2324
},
2425
{
2526
'url' : '#',

app/components/header/index.njk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"search": true,
1010
"primaryLinks": [
1111
{
12+
1213
href: "https://www.nhs.uk/conditions",
1314
text: "Health A-Z"
1415
},

packages/components/header/_header-white.scss

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,28 @@
8181

8282
.nhsuk-header__navigation-link {
8383
@include nhsuk-link-style-no-visited-state;
84+
85+
// Visual indicator for current navigation item has blue line on
86+
// bottom edge instead of grey in the regular Header nav
87+
&[aria-current="page"],
88+
&[aria-current="true"] {
89+
box-shadow: inset 0 ($nhsuk-focus-width * -1) $color_nhsuk-blue;
90+
}
91+
92+
&:focus {
93+
box-shadow: inset 0 ($nhsuk-focus-width * -1) $color_nhsuk-black;
94+
}
95+
}
96+
97+
// Drop down navigation
98+
.nhsuk-header__drop-down {
99+
.nhsuk-header__navigation-link {
100+
// When in expanded menu, a current item link should revert to having a
101+
// no box-shadow, with a box-shadow added to containing list item instead.
102+
&[aria-current="page"],
103+
&[aria-current="true"] {
104+
box-shadow: none;
105+
}
106+
}
84107
}
85108
}

packages/components/header/_header.scss

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@
220220
margin-bottom: 0;
221221
}
222222

223+
// This is a <strong> element used as a fallback mechanism for visually
224+
// indicating current page in scenarios where CSS isn’t available.
225+
// We don’t actually want it to be bold normally, so inherit parent font-weight.
226+
.nhsuk-header__navigation-item-active-fallback {
227+
font-weight: inherit;
228+
}
229+
223230
.nhsuk-header__navigation-link {
224231
@include nhsuk-font(16);
225232
@include nhsuk-link-style-header;
@@ -228,6 +235,14 @@
228235
padding: nhsuk-spacing(3) 2px;
229236
white-space: nowrap;
230237

238+
// Visual indicator for current navigation item has grey line on bottom edge,
239+
// and no underline on the link.
240+
&[aria-current="page"],
241+
&[aria-current="true"] {
242+
text-decoration: none;
243+
box-shadow: inset 0 ($nhsuk-focus-width * -1) $nhsuk-border-color;
244+
}
245+
231246
&:focus {
232247
box-shadow: inset 0 ($nhsuk-focus-width * -1) $nhsuk-focus-text-color;
233248
}
@@ -277,11 +292,34 @@
277292
.nhsuk-header__navigation-link {
278293
@include govuk-width-container;
279294
@include nhsuk-link-style-no-visited-state;
295+
padding: 12px 0;
296+
297+
// When in expanded menu, a current item link should revert to having a
298+
// no box-shadow, with a box-shadow added to containing list item instead.
299+
&[aria-current="page"],
300+
&[aria-current="true"] {
301+
box-shadow: none;
302+
}
303+
304+
// But when focused the black line at the bottom should return,
305+
// again using box-shadow.
306+
&:focus {
307+
box-shadow: inset 0 ($nhsuk-focus-width * -1) $nhsuk-focus-text-color;
308+
}
280309
}
281310

282311
.nhsuk-header__navigation-item {
283312
border-top: 1px solid $color_nhsuk-grey-5;
284313
}
314+
315+
// Current item within expended mobile menu gets blue line on left edge.
316+
//
317+
// The blue line uses an inset box-shadow instead of a border, as there’s
318+
// already a grey border on the top, and using 2 different colour borders
319+
// creates an awkward diagonal line where they meet.
320+
.nhsuk-header__navigation-item--current {
321+
box-shadow: inset 6px 0 $color_nhsuk-blue;
322+
}
285323
}
286324

287325
.nhsuk-header__drop-down--hidden {

packages/components/header/template.njk

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,20 @@
7070
<nav class="nhsuk-navigation" id="header-navigation" role="navigation" aria-label="Primary navigation">
7171
<ul class="nhsuk-header__navigation-list {%- if params.primaryLinks.length < 4 %} nhsuk-header__navigation-list--left-aligned{% endif %}">
7272
{%- for item in params.primaryLinks %}
73-
<li class="nhsuk-header__navigation-item {%- if item.classes %} {{ item.classes }}{% endif %}" {{- nhsukAttributes(item.attributes) }}>
74-
<a class="nhsuk-header__navigation-link" href="{{ item.href if item.href else item.url }}">{{ item.text if item.text else item.label }}</a>
73+
{% set linkInnerContent %}
74+
{# Wrap active links in strong element so users who override colours
75+
or styles still have some indicator of the current nav item. #}
76+
{% if item.active or item.current %}
77+
<strong class="nhsuk-header__navigation-item-active-fallback">{{- item.text -}}</strong>
78+
{% else %}
79+
{{- item.text -}}
80+
{% endif %}
81+
{% endset %}
82+
83+
<li class="nhsuk-header__navigation-item {%- if item.active or item.current %} nhsuk-header__navigation-item--current{% endif %} {%- if item.classes %} {{ item.classes }}{% endif %}" {{- nhsukAttributes(item.attributes) }}>
84+
<a class="nhsuk-header__navigation-link" href="{{item.url}}" {%- if item.active or item.current %} aria-current="{{ 'page' if item.current else 'true' }}"{% endif %}>
85+
{{ linkInnerContent | safe }}
86+
</a>
7587
</li>
7688
{%- endfor %}
7789
<li class="nhsuk-mobile-menu-container">
Loading

0 commit comments

Comments
 (0)