Skip to content

Commit dd79f2a

Browse files
committed
Add right-aligned option to end slot and language switcher example
1 parent e0adf23 commit dd79f2a

5 files changed

Lines changed: 103 additions & 1 deletion

File tree

packages/govuk-frontend/src/govuk/components/language-switcher/_mixin.scss

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
@use "sass:math";
2+
13
@use "../../base";
24

35
/// @access private
@@ -32,4 +34,31 @@
3234
@include base.govuk-link-common;
3335
@include base.govuk-link-style-default;
3436
}
37+
38+
// When used inside service navigation, align the language switcher
39+
.govuk-service-navigation .govuk-language-switcher {
40+
margin-top: base.govuk-spacing(2);
41+
margin-bottom: base.govuk-spacing(2);
42+
43+
@media #{base.govuk-from-breakpoint(tablet)} {
44+
display: inline;
45+
margin-top: 0;
46+
margin-bottom: 0;
47+
}
48+
}
49+
50+
.govuk-service-navigation .govuk-language-switcher__list {
51+
@media #{base.govuk-from-breakpoint(tablet)} {
52+
padding: base.govuk-spacing(3) 0;
53+
54+
// Match the service navigation row height at the switcher's 16px size.
55+
line-height: math.div(29, 16);
56+
}
57+
}
58+
59+
.govuk-service-navigation .govuk-language-switcher__list-item {
60+
@media #{base.govuk-from-breakpoint(tablet)} {
61+
margin-bottom: 0;
62+
}
63+
}
3564
}

packages/govuk-frontend/src/govuk/components/service-navigation/_mixin.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@
116116
flex-grow: 1;
117117
}
118118

119+
.govuk-service-navigation__end {
120+
width: 100%;
121+
}
122+
123+
@media #{base.govuk-from-breakpoint(tablet)} {
124+
.govuk-service-navigation__end {
125+
width: auto;
126+
}
127+
}
128+
119129
//
120130
// Navigation list specific code
121131
//

packages/govuk-frontend/src/govuk/components/service-navigation/service-navigation.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ params:
8585
type: string
8686
required: false
8787
description: HTML injected at the end of the service header container.
88+
- name: endRightAligned
89+
type: boolean
90+
required: false
91+
description: If `true`, renders the `end` slot inline in the service navigation row and right aligns it at tablet and above. Defaults to `false`.
8892
- name: navigationStart
8993
type: string
9094
required: false
@@ -402,6 +406,31 @@ examples:
402406
</li>
403407
</ul>
404408
</nav>
409+
- name: with right-aligned language switcher
410+
description: HTML rendered by the language switcher component can be injected into the `end` slot of the service header.
411+
options:
412+
serviceName: Service name
413+
navigation:
414+
- href: '#/1'
415+
text: Navigation item 1
416+
active: true
417+
- href: '#/2'
418+
text: Navigation item 2
419+
- href: '#/3'
420+
text: Navigation item 3
421+
slots:
422+
end: |-
423+
<nav class="govuk-language-switcher" aria-label="Language switcher">
424+
<ul class="govuk-language-switcher__list">
425+
<li class="govuk-language-switcher__list-item">
426+
<span class="govuk-language-switcher__text" lang="en" aria-current="true">English</span>
427+
</li>
428+
<li class="govuk-language-switcher__list-item">
429+
<a class="govuk-language-switcher__link" href="#/cy" lang="cy" hreflang="cy" rel="alternate">Cymraeg</a>
430+
</li>
431+
</ul>
432+
</nav>
433+
endRightAligned: true
405434
- name: with slotted content
406435
hidden: true
407436
options:
@@ -410,3 +439,12 @@ examples:
410439
end: '<div>[end]</div>'
411440
navigationStart: '<li>[navigation start]</li>'
412441
navigationEnd: '<li>[navigation end]</li>'
442+
- name: with right-aligned end slot
443+
options:
444+
serviceName: Service name
445+
navigation:
446+
- href: '#/1'
447+
text: Navigation item 1
448+
slots:
449+
end: '<div>[end]</div>'
450+
endRightAligned: true

packages/govuk-frontend/src/govuk/components/service-navigation/template.njk

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
{%- set menuButtonText = params.menuButtonText | default("Menu", true) -%}
44
{%- set navigationId = params.navigationId | default("navigation", true) %}
5+
{%- set endSlotRightAligned = params.slots.endRightAligned | default(false) %}
56

67
{%- set commonAttributes %}
78
class="govuk-service-navigation {%- if params.classes %} {{ params.classes }}{% endif %}"
@@ -84,10 +85,17 @@ data-module="govuk-service-navigation"
8485
</ul>
8586
</nav>
8687
{% endif %}
88+
89+
{# Slot: end (right aligned) #}
90+
{% if params.slots.end and endSlotRightAligned %}
91+
<div class="govuk-service-navigation__end">
92+
{{ params.slots.end | safe }}
93+
</div>
94+
{% endif %}
8795
</div>
8896

8997
{# Slot: end #}
90-
{%- if params.slots.end %}{{ params.slots.end | safe }}{% endif -%}
98+
{%- if params.slots.end and not endSlotRightAligned %}{{ params.slots.end | safe }}{% endif -%}
9199

92100
</div>
93101
{% endset -%}

packages/govuk-frontend/src/govuk/components/service-navigation/template.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,23 @@ describe('Service Navigation', () => {
488488
expect($slottedElement.prop('outerHTML')).toBe('<div>[end]</div>')
489489
})
490490

491+
it('inserts HTML from the `end` slot inline when `endRightAligned` is enabled', () => {
492+
const $ = render(
493+
'service-navigation',
494+
examples['with right-aligned end slot']
495+
)
496+
497+
const $endSlotInContainer = $('.govuk-service-navigation__end')
498+
const $endSlotContent = $endSlotInContainer.find('> :first-child')
499+
const $slottedElementAtRoot = $('.govuk-width-container > :last-child')
500+
501+
expect($endSlotInContainer).toHaveLength(1)
502+
expect($endSlotContent.prop('outerHTML')).toBe('<div>[end]</div>')
503+
expect(
504+
$slottedElementAtRoot.hasClass('govuk-service-navigation__container')
505+
).toBeTruthy()
506+
})
507+
491508
it('renders the component as a <section> if `start` or `end` slots are used', () => {
492509
const $ = render('service-navigation', examples['with slotted content'])
493510
const tagName = $('.govuk-service-navigation').get(0).tagName

0 commit comments

Comments
 (0)